Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
nihui committed Nov 4, 2023
1 parent 4fe5627 commit 3f4b234
Show file tree
Hide file tree
Showing 3 changed files with 175 additions and 87 deletions.
118 changes: 44 additions & 74 deletions highgui/src/highgui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,31 +200,28 @@ bool imwrite(const String& filename, InputArray _img, const std::vector<int>& pa
String ext = _ext;
Mat img = _img.getMat();

int c = 0;
if (img.type() == CV_8UC1)
{
c = 1;
}
else if (img.type() == CV_8UC3)
{
c = 3;
}
else if (img.type() == CV_8UC4)
{
c = 4;
}
else
{
// unexpected image channels
return false;
}

if (jpeg_encoder_rk_mpp::supported() && (ext == ".jpg" || ext == ".jpeg" || ext == ".JPG" || ext == ".JPEG"))
{
// anything to bgr
if (img.type() == CV_8UC1)
{
Mat img2;
cvtColor(img, img2, COLOR_GRAY2BGR);
img = img2;
}
else if (img.type() == CV_8UC3)
{
// bgr
}
else if (img.type() == CV_8UC4)
{
Mat img2;
cvtColor(img, img2, COLOR_BGRA2BGR);
img = img2;
}
else
{
// unexpected image channels
return false;
}

if (!img.isContinuous())
{
img = img.clone();
Expand All @@ -241,7 +238,7 @@ bool imwrite(const String& filename, InputArray _img, const std::vector<int>& pa
}

jpeg_encoder_rk_mpp e;
int ret = e.init(img.cols, img.rows, quality);
int ret = e.init(img.cols, img.rows, c, quality);
if (ret == 0)
{
ret = e.encode(img.data, filename.c_str());
Expand All @@ -256,30 +253,18 @@ bool imwrite(const String& filename, InputArray _img, const std::vector<int>& pa
}

// bgr to rgb
int c = 0;
if (img.type() == CV_8UC1)
{
c = 1;
}
else if (img.type() == CV_8UC3)
if (c == 3)
{
c = 3;
Mat img2;
cvtColor(img, img2, COLOR_BGR2RGB);
img = img2;
}
else if (img.type() == CV_8UC4)
if (c == 4)
{
c = 4;
Mat img2;
cvtColor(img, img2, COLOR_BGRA2RGBA);
img = img2;
}
else
{
// unexpected image channels
return false;
}

if (!img.isContinuous())
{
Expand Down Expand Up @@ -426,31 +411,28 @@ bool imencode(const String& ext, InputArray _img, std::vector<uchar>& buf, const
{
Mat img = _img.getMat();

int c = 0;
if (img.type() == CV_8UC1)
{
c = 1;
}
else if (img.type() == CV_8UC3)
{
c = 3;
}
else if (img.type() == CV_8UC4)
{
c = 4;
}
else
{
// unexpected image channels
return false;
}

if (jpeg_encoder_rk_mpp::supported() && (ext == ".jpg" || ext == ".jpeg" || ext == ".JPG" || ext == ".JPEG"))
{
// anything to bgr
if (img.type() == CV_8UC1)
{
Mat img2;
cvtColor(img, img2, COLOR_GRAY2BGR);
img = img2;
}
else if (img.type() == CV_8UC3)
{
// bgr
}
else if (img.type() == CV_8UC4)
{
Mat img2;
cvtColor(img, img2, COLOR_BGRA2BGR);
img = img2;
}
else
{
// unexpected image channels
return false;
}

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

jpeg_encoder_rk_mpp e;
int ret = e.init(img.cols, img.rows, quality);
int ret = e.init(img.cols, img.rows, c, quality);
if (ret == 0)
{
ret = e.encode(img.data, buf);
Expand All @@ -482,30 +464,18 @@ bool imencode(const String& ext, InputArray _img, std::vector<uchar>& buf, const
}

// bgr to rgb
int c = 0;
if (img.type() == CV_8UC1)
{
c = 1;
}
else if (img.type() == CV_8UC3)
if (c == 3)
{
c = 3;
Mat img2;
cvtColor(img, img2, COLOR_BGR2RGB);
img = img2;
}
else if (img.type() == CV_8UC4)
if (c == 4)
{
c = 4;
Mat img2;
cvtColor(img, img2, COLOR_BGRA2RGBA);
img = img2;
}
else
{
// unexpected image channels
return false;
}

if (!img.isContinuous())
{
Expand Down
Loading

0 comments on commit 3f4b234

Please sign in to comment.