Skip to content

Commit 722ff95

Browse files
authored
raspberry pi hw jpg encoder (nihui#185)
1 parent d0ccea6 commit 722ff95

File tree

4 files changed

+1471
-0
lines changed

4 files changed

+1471
-0
lines changed

highgui/CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ ocv_add_module(highgui opencv_imgproc)
44
option(WITH_CVI "build with cvi" OFF)
55
option(WITH_AW "build with aw" OFF)
66
option(WITH_RK "build with rk" OFF)
7+
option(WITH_RPI "build with rpi" OFF)
78

89
set(highgui_srcs
910
${CMAKE_CURRENT_LIST_DIR}/src/exif.cpp
@@ -53,6 +54,13 @@ if(WITH_RK)
5354
message(STATUS "highgui rk enabled")
5455
endif()
5556

57+
if(WITH_RPI)
58+
list(APPEND highgui_srcs
59+
${CMAKE_CURRENT_LIST_DIR}/src/jpeg_encoder_v4l_rpi.cpp)
60+
add_definitions(-DCV_WITH_RPI=1)
61+
message(STATUS "highgui rpi enabled")
62+
endif()
63+
5664
file(GLOB highgui_ext_hdrs
5765
"${CMAKE_CURRENT_LIST_DIR}/include/opencv2/*.hpp"
5866
"${CMAKE_CURRENT_LIST_DIR}/include/opencv2/${name}/*.hpp"

highgui/src/highgui.cpp

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@
5151
#if CV_WITH_RK
5252
#include "jpeg_encoder_rk_mpp.h"
5353
#endif
54+
#if CV_WITH_RPI
55+
#include "jpeg_encoder_v4l_rpi.h"
56+
#endif
5457
#if defined __linux__ && !__ANDROID__
5558
#include "display_fb.h"
5659
#endif
@@ -404,6 +407,57 @@ bool imwrite(const String& filename, InputArray _img, const std::vector<int>& pa
404407
// fallback to stb_image_write
405408
}
406409
#endif
410+
#if CV_WITH_RPI
411+
if (jpeg_encoder_v4l_rpi::supported(img.cols, img.rows, c))
412+
{
413+
// anything to bgr
414+
if (!img.isContinuous())
415+
{
416+
img = img.clone();
417+
}
418+
419+
int quality = 95;
420+
for (size_t i = 0; i < params.size(); i += 2)
421+
{
422+
if (params[i] == IMWRITE_JPEG_QUALITY)
423+
{
424+
quality = params[i + 1];
425+
break;
426+
}
427+
}
428+
429+
// cache jpeg_encoder_v4l_rpi context
430+
static int old_w = 0;
431+
static int old_h = 0;
432+
static int old_ch = 0;
433+
static int old_quality = 0;
434+
static jpeg_encoder_v4l_rpi e;
435+
if (img.cols == old_w && img.rows == old_h && c == old_ch && quality == old_quality)
436+
{
437+
int ret = e.encode(img.data, filename.c_str());
438+
if (ret == 0)
439+
return true;
440+
}
441+
else
442+
{
443+
int ret = e.init(img.cols, img.rows, c, quality);
444+
if (ret == 0)
445+
{
446+
ret = e.encode(img.data, filename.c_str());
447+
if (ret == 0)
448+
{
449+
old_w = img.cols;
450+
old_h = img.rows;
451+
old_ch = c;
452+
old_quality = quality;
453+
return true;
454+
}
455+
}
456+
}
457+
458+
// fallback to stb_image_write
459+
}
460+
#endif
407461
}
408462

409463
// bgr to rgb
@@ -724,6 +778,40 @@ bool imencode(const String& ext, InputArray _img, std::vector<uchar>& buf, const
724778
// fallback to stb_image_write
725779
}
726780
#endif
781+
#if CV_WITH_RPI
782+
if (jpeg_encoder_v4l_rpi::supported(img.cols, img.rows, c))
783+
{
784+
// anything to bgr
785+
if (!img.isContinuous())
786+
{
787+
img = img.clone();
788+
}
789+
790+
int quality = 95;
791+
for (size_t i = 0; i < params.size(); i += 2)
792+
{
793+
if (params[i] == IMWRITE_JPEG_QUALITY)
794+
{
795+
quality = params[i + 1];
796+
break;
797+
}
798+
}
799+
800+
jpeg_encoder_v4l_rpi e;
801+
int ret = e.init(img.cols, img.rows, c, quality);
802+
if (ret == 0)
803+
{
804+
ret = e.encode(img.data, buf);
805+
if (ret == 0)
806+
{
807+
e.deinit();
808+
return true;
809+
}
810+
}
811+
812+
// fallback to stb_image_write
813+
}
814+
#endif
727815
}
728816

729817
// bgr to rgb

0 commit comments

Comments
 (0)