mirror of
https://github.com/orbbec/OrbbecSDK_ROS2.git
synced 2026-10-10 14:39:49 +08:00
fix: enhance JPEG validation and add corresponding tests
This commit is contained in:
@@ -946,17 +946,28 @@ std::string parseUsbPort(const std::string &line) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool isValidJPEG(const std::shared_ptr<ob::ColorFrame> &frame) {
|
bool isValidJPEG(const std::shared_ptr<ob::ColorFrame> &frame) {
|
||||||
if (frame->getDataSize() < 2) { // Checking both start and end markers, so minimal size is 4
|
if (!frame) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const auto data_size = frame->getDataSize();
|
||||||
const auto *data = static_cast<const uint8_t *>(frame->getData());
|
const auto *data = static_cast<const uint8_t *>(frame->getData());
|
||||||
|
if (data == nullptr || data_size < 4) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
// Check for JPEG start marker
|
// Check for JPEG start marker
|
||||||
if (data[0] != 0xFF || data[1] != 0xD8) {
|
if (data[0] != 0xFF || data[1] != 0xD8) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
|
||||||
|
auto jpeg_size = data_size;
|
||||||
|
while (jpeg_size > 2 && data[jpeg_size - 1] == 0x00) {
|
||||||
|
--jpeg_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check for JPEG end marker after trimming zero padding.
|
||||||
|
return jpeg_size >= 4 && data[jpeg_size - 2] == 0xFF && data[jpeg_size - 1] == 0xD9;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string metaDataTypeToString(const OBFrameMetadataType &meta_data_type) {
|
std::string metaDataTypeToString(const OBFrameMetadataType &meta_data_type) {
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
#include <gtest/gtest.h>
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
|
#include <cstring>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "orbbec_camera/utils.h"
|
#include "orbbec_camera/utils.h"
|
||||||
@@ -32,6 +33,14 @@ OBCameraDistortion makeDistortion(OBCameraDistortionModel model) {
|
|||||||
return distortion;
|
return distortion;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::shared_ptr<ob::ColorFrame> makeMjpegFrame(const std::vector<uint8_t>& data) {
|
||||||
|
auto frame = ob::FrameFactory::createFrame(OB_FRAME_COLOR, OB_FORMAT_MJPG,
|
||||||
|
static_cast<uint32_t>(data.size()));
|
||||||
|
auto color_frame = frame->as<ob::ColorFrame>();
|
||||||
|
std::memcpy(color_frame->getData(), data.data(), data.size());
|
||||||
|
return color_frame;
|
||||||
|
}
|
||||||
|
|
||||||
TEST(CameraInfoDistortionTest, ConvertsBrownConradyToPlumbBob) {
|
TEST(CameraInfoDistortionTest, ConvertsBrownConradyToPlumbBob) {
|
||||||
const auto intrinsic = makeIntrinsic();
|
const auto intrinsic = makeIntrinsic();
|
||||||
const auto distortion = makeDistortion(OB_DISTORTION_BROWN_CONRADY);
|
const auto distortion = makeDistortion(OB_DISTORTION_BROWN_CONRADY);
|
||||||
@@ -69,5 +78,17 @@ TEST(CameraInfoDistortionTest, ConvertsKannalaBrandtToEquidistant) {
|
|||||||
std::vector<double>({distortion.k1, distortion.k2, distortion.k3, distortion.k4}));
|
std::vector<double>({distortion.k1, distortion.k2, distortion.k3, distortion.k4}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST(JpegValidationTest, AcceptsEoiBeforeZeroPadding) {
|
||||||
|
const auto frame = makeMjpegFrame({0xFF, 0xD8, 0x01, 0x02, 0xFF, 0xD9, 0x00, 0x00});
|
||||||
|
|
||||||
|
EXPECT_TRUE(isValidJPEG(frame));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(JpegValidationTest, RejectsMjpegWithoutEoi) {
|
||||||
|
const auto frame = makeMjpegFrame({0xFF, 0xD8, 0x01, 0x02, 0x00, 0x00});
|
||||||
|
|
||||||
|
EXPECT_FALSE(isValidJPEG(frame));
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
} // namespace orbbec_camera
|
} // namespace orbbec_camera
|
||||||
|
|||||||
Reference in New Issue
Block a user