From aaef365e6c51dcf98334e9f7a70e5fc4e3281f91 Mon Sep 17 00:00:00 2001 From: Ian Chen Date: Mon, 11 Nov 2024 01:09:21 -0800 Subject: [PATCH 1/2] Check camera resolution (#480) Signed-off-by: Ian Chen --- src/BoundingBoxCameraSensor.cc | 7 +++++++ src/CameraSensor.cc | 7 +++++++ src/DepthCameraSensor.cc | 11 +++++++++-- src/RgbdCameraSensor.cc | 11 +++++++++-- src/SegmentationCameraSensor.cc | 7 +++++++ src/ThermalCameraSensor.cc | 11 +++++++++-- src/WideAngleCameraSensor.cc | 7 +++++++ 7 files changed, 55 insertions(+), 6 deletions(-) diff --git a/src/BoundingBoxCameraSensor.cc b/src/BoundingBoxCameraSensor.cc index 7729945b..b66209d7 100644 --- a/src/BoundingBoxCameraSensor.cc +++ b/src/BoundingBoxCameraSensor.cc @@ -293,6 +293,13 @@ bool BoundingBoxCameraSensor::CreateCamera() auto width = sdfCamera->ImageWidth(); auto height = sdfCamera->ImageHeight(); + if (width == 0u || height == 0u) + { + gzerr << "Unable to create a bounding box camera sensor with 0 width or " + << "height. " << std::endl; + return false; + } + // Set Camera Properties this->dataPtr->rgbCamera->SetImageFormat(rendering::PF_R8G8B8); this->dataPtr->rgbCamera->SetImageWidth(width); diff --git a/src/CameraSensor.cc b/src/CameraSensor.cc index dca7bf9a..a74c88c4 100644 --- a/src/CameraSensor.cc +++ b/src/CameraSensor.cc @@ -144,6 +144,13 @@ bool CameraSensor::CreateCamera() unsigned int width = cameraSdf->ImageWidth(); unsigned int height = cameraSdf->ImageHeight(); + if (width == 0u || height == 0u) + { + gzerr << "Unable to create a camera sensor with 0 width or height." + << std::endl; + return false; + } + this->dataPtr->camera = this->Scene()->CreateCamera(this->Name()); this->dataPtr->camera->SetImageWidth(width); this->dataPtr->camera->SetImageHeight(height); diff --git a/src/DepthCameraSensor.cc b/src/DepthCameraSensor.cc index bd936a62..9623ba0c 100644 --- a/src/DepthCameraSensor.cc +++ b/src/DepthCameraSensor.cc @@ -337,8 +337,15 @@ bool DepthCameraSensor::CreateCamera() return false; } - int width = cameraSdf->ImageWidth(); - int height = cameraSdf->ImageHeight(); + unsigned int width = cameraSdf->ImageWidth(); + unsigned int height = cameraSdf->ImageHeight(); + + if (width == 0u || height == 0u) + { + gzerr << "Unable to create a depth camera sensor with 0 width or height." + << std::endl; + return false; + } double far = cameraSdf->FarClip(); double near = cameraSdf->NearClip(); diff --git a/src/RgbdCameraSensor.cc b/src/RgbdCameraSensor.cc index f78a74a2..251ef7b7 100644 --- a/src/RgbdCameraSensor.cc +++ b/src/RgbdCameraSensor.cc @@ -278,8 +278,15 @@ bool RgbdCameraSensor::CreateCameras() return false; } - int width = cameraSdf->ImageWidth(); - int height = cameraSdf->ImageHeight(); + unsigned int width = cameraSdf->ImageWidth(); + unsigned int height = cameraSdf->ImageHeight(); + + if (width == 0u || height == 0u) + { + gzerr << "Unable to create an RGBD camera sensor with 0 width or height." + << std::endl; + return false; + } this->dataPtr->depthCamera = this->Scene()->CreateDepthCamera(this->Name()); diff --git a/src/SegmentationCameraSensor.cc b/src/SegmentationCameraSensor.cc index 92b5a983..b11af526 100644 --- a/src/SegmentationCameraSensor.cc +++ b/src/SegmentationCameraSensor.cc @@ -353,6 +353,13 @@ bool SegmentationCameraSensor::CreateCamera() auto width = sdfCamera->ImageWidth(); auto height = sdfCamera->ImageHeight(); + if (width == 0u || height == 0u) + { + gzerr << "Unable to create a segmentation camera sensor with 0 width or " + << "height." << std::endl; + return false; + } + math::Angle angle = sdfCamera->HorizontalFov(); if (angle < 0.01 || angle > GZ_PI*2) { diff --git a/src/ThermalCameraSensor.cc b/src/ThermalCameraSensor.cc index d0aec077..1ab20d5f 100644 --- a/src/ThermalCameraSensor.cc +++ b/src/ThermalCameraSensor.cc @@ -255,8 +255,15 @@ bool ThermalCameraSensor::CreateCamera() return false; } - int width = cameraSdf->ImageWidth(); - int height = cameraSdf->ImageHeight(); + unsigned int width = cameraSdf->ImageWidth(); + unsigned int height = cameraSdf->ImageHeight(); + + if (width == 0u || height == 0u) + { + gzerr << "Unable to create a thermal camera sensor with 0 width or height." + << std::endl; + return false; + } sdf::PixelFormatType pixelFormat = cameraSdf->PixelFormat(); diff --git a/src/WideAngleCameraSensor.cc b/src/WideAngleCameraSensor.cc index 94b935d6..a851287d 100644 --- a/src/WideAngleCameraSensor.cc +++ b/src/WideAngleCameraSensor.cc @@ -228,6 +228,13 @@ bool WideAngleCameraSensor::CreateCamera() unsigned int width = cameraSdf->ImageWidth(); unsigned int height = cameraSdf->ImageHeight(); + if (width == 0u || height == 0u) + { + gzerr << "Unable to create a wide angle camera sensor with 0 width or " + << "height." << std::endl; + return false; + } + this->dataPtr->camera = this->Scene()->CreateWideAngleCamera(this->Name()); if (!this->dataPtr->camera) From cd283aebf3f7847089b897d741ae1f7bf83cbb35 Mon Sep 17 00:00:00 2001 From: Ian Chen Date: Fri, 10 Jan 2025 07:24:07 -0800 Subject: [PATCH 2/2] Add support for L8 and L16 image formats in WideAngleCameraSensor (#494) Signed-off-by: Ian Chen --- src/WideAngleCameraSensor.cc | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/WideAngleCameraSensor.cc b/src/WideAngleCameraSensor.cc index a851287d..832df8c2 100644 --- a/src/WideAngleCameraSensor.cc +++ b/src/WideAngleCameraSensor.cc @@ -333,6 +333,12 @@ bool WideAngleCameraSensor::CreateCamera() case sdf::PixelFormatType::RGB_INT8: this->dataPtr->camera->SetImageFormat(gz::rendering::PF_R8G8B8); break; + case sdf::PixelFormatType::L_INT8: + this->dataPtr->camera->SetImageFormat(gz::rendering::PF_L8); + break; + case sdf::PixelFormatType::L_INT16: + this->dataPtr->camera->SetImageFormat(gz::rendering::PF_L16); + break; default: gzerr << "Unsupported pixel format [" << static_cast(pixelFormat) << "]\n"; @@ -367,7 +373,9 @@ void WideAngleCameraSensor::OnNewWideAngleFrame( { std::lock_guard lock(this->dataPtr->mutex); - unsigned int len = _width * _height * _channels; + unsigned int bytesPerChannel = rendering::PixelUtil::BytesPerChannel( + this->dataPtr->camera->ImageFormat()); + unsigned int len = _width * _height * _channels * bytesPerChannel; unsigned int bufferSize = len * sizeof(unsigned char); if (!this->dataPtr->imageBuffer) @@ -467,6 +475,12 @@ bool WideAngleCameraSensor::Update( format = gz::common::Image::RGB_INT8; msgsPixelFormat = msgs::PixelFormatType::RGB_INT8; break; + case gz::rendering::PF_L8: + msgsPixelFormat = msgs::PixelFormatType::L_INT8; + break; + case gz::rendering::PF_L16: + msgsPixelFormat = msgs::PixelFormatType::L_INT16; + break; default: gzerr << "Unsupported pixel format [" << this->dataPtr->camera->ImageFormat() << "]\n";