From 04ee8dbe7925ee068f811e4e30a645684cfc9c09 Mon Sep 17 00:00:00 2001 From: Peter Johnson Date: Fri, 27 Jul 2018 21:50:28 -0700 Subject: [PATCH] UsbCamera: In JPEG mode, get size from image instead of mode (#1222) Some cameras will accept a different resolution as the mode but then provide JPEG images of a different size. Trust the JPEG image size if available. This also validates the JPEG image is actually JPEG. --- cscore/src/main/native/cpp/UsbCameraImpl.cpp | 22 ++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/cscore/src/main/native/cpp/UsbCameraImpl.cpp b/cscore/src/main/native/cpp/UsbCameraImpl.cpp index 500ab78068..8e5ba5d7cb 100644 --- a/cscore/src/main/native/cpp/UsbCameraImpl.cpp +++ b/cscore/src/main/native/cpp/UsbCameraImpl.cpp @@ -33,6 +33,7 @@ #include #include "Handle.h" +#include "JpegUtil.h" #include "Log.h" #include "Notifier.h" #include "Telemetry.h" @@ -400,12 +401,21 @@ void UsbCameraImpl::CameraThreadMain() { continue; } - PutFrame(static_cast(m_mode.pixelFormat), - m_mode.width, m_mode.height, - wpi::StringRef( - static_cast(m_buffers[buf.index].m_data), - static_cast(buf.bytesused)), - wpi::Now()); // TODO: time + wpi::StringRef image{ + static_cast(m_buffers[buf.index].m_data), + static_cast(buf.bytesused)}; + int width = m_mode.width; + int height = m_mode.height; + bool good = true; + if (m_mode.pixelFormat == VideoMode::kMJPEG && + !GetJpegSize(image, &width, &height)) { + SWARNING("invalid JPEG image received from camera"); + good = false; + } + if (good) { + PutFrame(static_cast(m_mode.pixelFormat), + width, height, image, wpi::Now()); // TODO: time + } } // Requeue buffer