Skip to content

Commit

Permalink
rkmpp jpg encoder (#72)
Browse files Browse the repository at this point in the history
* rkmpp jpeg encoder for luckfox pico
  • Loading branch information
nihui authored Nov 5, 2023
1 parent de886e8 commit 19c00b1
Show file tree
Hide file tree
Showing 4 changed files with 1,261 additions and 14 deletions.
1 change: 1 addition & 0 deletions highgui/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ ocv_add_module(highgui opencv_imgproc)
set(highgui_srcs
${CMAKE_CURRENT_LIST_DIR}/src/exif.cpp
${CMAKE_CURRENT_LIST_DIR}/src/highgui.cpp
${CMAKE_CURRENT_LIST_DIR}/src/jpeg_encoder_rk_mpp.cpp
)

file(GLOB highgui_ext_hdrs
Expand Down
123 changes: 109 additions & 14 deletions highgui/src/highgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"

#if defined __linux__
#include "jpeg_encoder_rk_mpp.h"
#endif

namespace cv {
//
// 1 2 3 4 5 6 7 8
Expand Down Expand Up @@ -198,7 +202,6 @@ bool imwrite(const String& filename, InputArray _img, const std::vector<int>& pa
String ext = _ext;
Mat img = _img.getMat();

// bgr to rgb
int c = 0;
if (img.type() == CV_8UC1)
{
Expand All @@ -207,23 +210,69 @@ bool imwrite(const String& filename, InputArray _img, const std::vector<int>& pa
else if (img.type() == CV_8UC3)
{
c = 3;
Mat img2;
cvtColor(img, img2, COLOR_BGR2RGB);
img = img2;
}
else if (img.type() == CV_8UC4)
{
c = 4;
Mat img2;
cvtColor(img, img2, COLOR_BGRA2RGBA);
img = img2;
}
else
{
// unexpected image channels
return false;
}

#if defined __linux__
if (ext == ".jpg" || ext == ".jpeg" || ext == ".JPG" || ext == ".JPEG")
{
if (jpeg_encoder_rk_mpp::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_rk_mpp e;
int ret = e.init(img.cols, img.rows, c, quality);
if (ret == 0)
{
ret = e.encode(img.data, filename.c_str());
if (ret == 0)
{
e.deinit();
return true;
}
}

// fallback to stb_image_write
}
}
#endif

// bgr to rgb
if (c == 3)
{
Mat img2;
cvtColor(img, img2, COLOR_BGR2RGB);
img = img2;
}
if (c == 4)
{
Mat img2;
cvtColor(img, img2, COLOR_BGRA2RGBA);
img = img2;
}

if (!img.isContinuous())
{
img = img.clone();
Expand Down Expand Up @@ -369,7 +418,6 @@ bool imencode(const String& ext, InputArray _img, std::vector<uchar>& buf, const
{
Mat img = _img.getMat();

// bgr to rgb
int c = 0;
if (img.type() == CV_8UC1)
{
Expand All @@ -378,23 +426,69 @@ bool imencode(const String& ext, InputArray _img, std::vector<uchar>& buf, const
else if (img.type() == CV_8UC3)
{
c = 3;
Mat img2;
cvtColor(img, img2, COLOR_BGR2RGB);
img = img2;
}
else if (img.type() == CV_8UC4)
{
c = 4;
Mat img2;
cvtColor(img, img2, COLOR_BGRA2RGBA);
img = img2;
}
else
{
// unexpected image channels
return false;
}

#if defined __linux__
if (ext == ".jpg" || ext == ".jpeg" || ext == ".JPG" || ext == ".JPEG")
{
if (jpeg_encoder_rk_mpp::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_rk_mpp e;
int ret = e.init(img.cols, img.rows, c, quality);
if (ret == 0)
{
ret = e.encode(img.data, buf);
if (ret == 0)
{
e.deinit();
return true;
}
}

// fallback to stb_image_write
}
}
#endif

// bgr to rgb
if (c == 3)
{
Mat img2;
cvtColor(img, img2, COLOR_BGR2RGB);
img = img2;
}
if (c == 4)
{
Mat img2;
cvtColor(img, img2, COLOR_BGRA2RGBA);
img = img2;
}

if (!img.isContinuous())
{
img = img.clone();
Expand All @@ -413,6 +507,7 @@ bool imencode(const String& ext, InputArray _img, std::vector<uchar>& buf, const
break;
}
}

success = stbi_write_jpg_to_func(imencode_write_func, (void*)&buf, img.cols, img.rows, c, img.data, quality);
}
else if (ext == ".png" || ext == ".PNG")
Expand Down
Loading

0 comments on commit 19c00b1

Please sign in to comment.