Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions highgui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ option(WITH_CVI "build with cvi" OFF)
option(WITH_AW "build with aw" OFF)
option(WITH_RK "build with rk" OFF)
option(WITH_RPI "build with rpi" OFF)
option(WITH_CIX "build with cix" OFF)

set(highgui_srcs
${CMAKE_CURRENT_LIST_DIR}/src/exif.cpp
Expand Down Expand Up @@ -61,6 +62,13 @@ if(WITH_RPI)
message(STATUS "highgui rpi enabled")
endif()

if(WITH_CIX)
list(APPEND highgui_srcs
${CMAKE_CURRENT_LIST_DIR}/src/jpeg_encoder_v4l_cix.cpp)
add_definitions(-DCV_WITH_CIX=1)
message(STATUS "highgui cix enabled")
endif()

file(GLOB highgui_ext_hdrs
"${CMAKE_CURRENT_LIST_DIR}/include/opencv2/*.hpp"
"${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/*.hpp"
Expand Down
86 changes: 86 additions & 0 deletions highgui/src/highgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@
#if CV_WITH_RPI
#include "jpeg_encoder_v4l_rpi.h"
#endif
#if CV_WITH_CIX
#include "jpeg_encoder_v4l_cix.h"
#endif
#if defined __linux__ && !__ANDROID__
#include "display_fb.h"
#endif
Expand Down Expand Up @@ -454,6 +457,56 @@ bool imwrite(const String& filename, InputArray _img, const std::vector<int>& pa
}
}
}
// fallback to stb_image_write
}
#endif
#if CV_WITH_CIX
if (jpeg_encoder_v4l_cix::supported(img.cols, img.rows, c))
{
// anything to bgr
if (!img.isContinuous())
{
img = img.clone();
}

int quality = 95;
for (size_t i = 0; i < params.size(); i += 2)
{
if (params[i] == IMWRITE_JPEG_QUALITY)
{
quality = params[i + 1];
break;
}
}

// cache jpeg_encoder_v4l_cix context
static int old_w = 0;
Copy link

Copilot AI Apr 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The static caching variables used in the CV_WITH_CIX block of imwrite may not be thread-safe. Consider using a mutex or other synchronization mechanism to protect access.

Copilot uses AI. Check for mistakes.
static int old_h = 0;
static int old_ch = 0;
static int old_quality = 0;
static jpeg_encoder_v4l_cix e;
if (img.cols == old_w && img.rows == old_h && c == old_ch && quality == old_quality)
{
int ret = e.encode(img.data, filename.c_str());
if (ret == 0)
return true;
Comment on lines +491 to +492
Copy link

Copilot AI Apr 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The caching branch in the CV_WITH_CIX block of imwrite does not call deinit() after a successful encode, which might leave resources allocated longer than intended. Ensure that proper resource cleanup is performed if required by the encoder's design.

Suggested change
if (ret == 0)
return true;
if (ret == 0)
{
e.deinit(); // Ensure resources are cleaned up
return true;
}

Copilot uses AI. Check for mistakes.
}
else
{
int ret = e.init(img.cols, img.rows, c, quality);
if (ret == 0)
{
ret = e.encode(img.data, filename.c_str());
if (ret == 0)
{
old_w = img.cols;
old_h = img.rows;
old_ch = c;
old_quality = quality;
return true;
}
}
}

// fallback to stb_image_write
}
Expand Down Expand Up @@ -808,6 +861,39 @@ bool imencode(const String& ext, InputArray _img, std::vector<uchar>& buf, const
return true;
}
}
// fallback to stb_image_write
}
#endif
#if CV_WITH_CIX
if (jpeg_encoder_v4l_cix::supported(img.cols, img.rows, c))
{
// anything to bgr
if (!img.isContinuous())
{
img = img.clone();
}

int quality = 95;
for (size_t i = 0; i < params.size(); i += 2)
{
if (params[i] == IMWRITE_JPEG_QUALITY)
{
quality = params[i + 1];
break;
}
}

jpeg_encoder_v4l_cix e;
int ret = e.init(img.cols, img.rows, c, quality);
if (ret == 0)
{
ret = e.encode(img.data, buf);
Copy link

Copilot AI Apr 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the imencode function's CV_WITH_CIX block, if the encode call fails, deinit() is not invoked, potentially causing resource leaks. Consider calling deinit() on failure as well to ensure proper cleanup.

Copilot uses AI. Check for mistakes.
if (ret == 0)
{
e.deinit();
return true;
}
}

// fallback to stb_image_write
}
Expand Down
Loading