LEADTOOLS Support
Imaging
Imaging SDK Questions
How can I convert GrayScale bitmap data to origin bitmap BBP?
#1
Posted
:
Monday, July 23, 2018 4:51:58 AM(UTC)
Groups: Registered
Posts: 7
Thanks: 2 times
Hi.
I Convert loaded 1BPP or 8BPP bitmap to 32-bit grayscale bitmap to get 4-channels image.
And save 32-bit grayscale bitmap to 1BPP or 8BPP bitmap.
(Implementing LBitmapBase to/from OpenCV cv::Mat Function)
After 1BPP bitmap grayscaling, converting LBitmapBase to 4-channels data(cv::Mat) is success.
And it converting 4-channels data to LBitmapBase again success.
But save it to 1BPP was failed.
I think that was caused by saving 32-bit data to 1BPP image.
But I want to keep origin format.
(1BPP file load -> 4-channels data -> Image processing -> 4-channels data -> 1BPP file save)
Is there way to save 4-channels data to 1BPP image?
My code is below:
Code:
L_INT ret;
LFile r_file;
FILEINFO r_fileinfo;
memset(&r_fileinfo, 0, sizeof(FILEINFO));
r_file.SetFileName(string_to_TCHAR(input_file_path.string()));
ret = r_file.GetInfo(&r_fileinfo, sizeof(FILEINFO));
if (ret == SUCCESS)
{
for (int page_idx = 1; page_idx <= r_fileinfo.TotalPages; page_idx++)
{
LBitmapBase bitmap;
ret = bitmap.Load(string_to_TCHAR(input_file_path.string()), 0, ORDER_BGRORGRAY, page_idx, NULL);
if (ret == SUCCESS)
{
L_INT bbp = bitmap.GetBitsPerPixel();
if (r_fileinfo.BitsPerPixel == 1 || r_fileinfo.BitsPerPixel == 8)
{
bitmap.GrayScale(32); // For retrive data to BGRA type
}
// LBitmapBase to cv::Mat
cv::Mat src(bitmap.GetHeight(), bitmap.GetWidth(), CV_8UC4);
for (int y = 0; y < bitmap.GetHeight(); y++)
{
for (int x = 0; x < bitmap.GetWidth(); x++)
{
L_UCHAR pixel_data[4];
ret = bitmap.GetPixelData(pixel_data, y, x, sizeof(pixel_data));
if (ret == SUCCESS)
{
cv::Vec4b& pixel = src.at<cv::Vec4b>(cv::Point(x, y));
pixel[0] = pixel_data[0];
pixel[1] = pixel_data[1];
pixel[2] = pixel_data[2];
pixel[3] = 255;
}
}
}
std::cout << "Mat load done..." << std::endl;
// Image Processing....
// LBitmapBase from cv::Mat
for (int y = 0; y < src.rows; y++)
{
for (int x = 0; x < src.cols; x++)
{
L_UCHAR pixel_data[4];
cv::Vec4b& pixel = src.at<cv::Vec4b>(cv::Point(x, y));
pixel_data[0] = pixel[0];
pixel_data[1] = pixel[1];
pixel_data[2] = pixel[2];
pixel_data[3] = 255;
ret = bitmap.PutPixelData(pixel_data, y, x, sizeof(pixel_data));
}
}
std::cout << "Mat save done..." << std::endl;
// ??? How can I save 4-channels data to 1BPP bitmap ???
ret = bitmap.Save(string_to_TCHAR(output_file_path.string()), r_fileinfo.Format, 0, get_quailty_factor(r_fileinfo.Format), page_idx, 0);
std::cout << "Save ret : " << ret << std::endl;
}
}
}
#2
Posted
:
Monday, July 23, 2018 4:32:09 PM(UTC)
Groups: Registered, Tech Support, Administrators
Posts: 54
Thanks: 2 times
Was thanked: 10 time(s) in 10 post(s)
Hello,
On line 68, when you save the bitmap back to a file, the third argument controls the BPP of the new file. See
https://www.leadtools.com/help/leadtools/v20/main/clib/lbitmapbase-save.html. Currently you have it set to 0 which uses the closest available bpp setting to the image you're saving in the particular format, but you can set it explicitly to 1 if you know that file format can support it.
Code:ret = bitmap.Save(string_to_TCHAR(output_file_path.string()), r_fileinfo.Format, 1,
get_quailty_factor(r_fileinfo.Format), page_idx, 0);
Thanks,
Edited by user Tuesday, July 24, 2018 8:50:58 AM(UTC)
| Reason: Not specified
Josh Clark
Developer Support Engineer
LEAD Technologies, Inc.
1 user thanked Josh Clark for this useful post.
#3
Posted
:
Monday, July 23, 2018 7:23:51 PM(UTC)
Groups: Registered
Posts: 7
Thanks: 2 times
Dear Josh Clark.
Hi.
I know third argument is BPP.
But when save image using third argument 1(1BPP), it was failed(Output image has white pixels only)
Please review my code, and attached test image.
Input image :
Output image(third argument is 1) :
Thanks.
#4
Posted
:
Tuesday, July 24, 2018 12:10:32 AM(UTC)
Groups: Registered
Posts: 7
Thanks: 2 times
I solved this issue myself.
I using ColorRes method instead of GrayScale.
L_INT bpp = bitmap.GetBitsPerPixel();
if (bpp != 32)
{
bitmap.ColorRes(32, CRF_BYTEORDERBGR);
}
And before save, change the BPP using ColorRes again.
if (bpp != 32)
{
bitmap.ColorRes(bpp);
}
And save the bitmap.
ret = bitmap.Save(string_to_TCHAR(output_file_path.string()), r_fileinfo.Format, 0, get_quailty_factor(r_fileinfo.Format), page_idx, 0);
Thanks.
LEADTOOLS Support
Imaging
Imaging SDK Questions
How can I convert GrayScale bitmap data to origin bitmap BBP?
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.