png支持单色位图

This commit is contained in:
luoliangyi 2023-07-04 17:26:29 +08:00
parent 497f248c43
commit ee6dc3ee51
1 changed files with 47 additions and 27 deletions

View File

@ -147,6 +147,7 @@ HGResult HGAPI HGImgFmt_LoadPngImage(const HGChar* fileName, HGPngLoadInfo* info
if (NULL != image) if (NULL != image)
{ {
png_set_scale_16(png_ptr); png_set_scale_16(png_ptr);
if (1 != info_ptr->bit_depth)
png_set_expand(png_ptr); png_set_expand(png_ptr);
png_set_interlace_handling(png_ptr); png_set_interlace_handling(png_ptr);
@ -182,6 +183,9 @@ HGResult HGAPI HGImgFmt_LoadPngImage(const HGChar* fileName, HGPngLoadInfo* info
} }
else if (PNG_COLOR_TYPE_GRAY == info_ptr->color_type) else if (PNG_COLOR_TYPE_GRAY == info_ptr->color_type)
{ {
if (1 == info_ptr->bit_depth)
imgType = HGBASE_IMGTYPE_BINARY;
else
imgType = HGBASE_IMGTYPE_GRAY; imgType = HGBASE_IMGTYPE_GRAY;
} }
} }
@ -210,6 +214,25 @@ HGResult HGAPI HGImgFmt_LoadPngImage(const HGChar* fileName, HGPngLoadInfo* info
HGBase_GetImageInfo(image2, &imgInfo); HGBase_GetImageInfo(image2, &imgInfo);
if (PNG_COLOR_TYPE_GRAY == info_ptr->color_type) if (PNG_COLOR_TYPE_GRAY == info_ptr->color_type)
{
if (1 == info_ptr->bit_depth)
{
HGImageInfo imgInfo;
imgInfo.width = info_ptr->width;
imgInfo.height = info_ptr->height;
imgInfo.type = HGBASE_IMGTYPE_BINARY;
imgInfo.widthStep = info_ptr->rowbytes;
imgInfo.origin = HGBASE_IMGORIGIN_TOP;
HGImage imgTmp = NULL;
HGBase_CreateImageWithData(buffer, &imgInfo, &imgTmp);
if (NULL != imgTmp)
{
HGBase_CopyImage(imgTmp, image2);
HGBase_DestroyImage(imgTmp);
}
}
else
{ {
//#pragma omp parallel for //#pragma omp parallel for
for (png_int_32 i = 0; i < (png_int_32)info_ptr->height; i++) for (png_int_32 i = 0; i < (png_int_32)info_ptr->height; i++)
@ -227,6 +250,7 @@ HGResult HGAPI HGImgFmt_LoadPngImage(const HGChar* fileName, HGPngLoadInfo* info
pDestEx += 4; pDestEx += 4;
} }
} }
}
} }
else if (PNG_COLOR_TYPE_RGB == info_ptr->color_type) else if (PNG_COLOR_TYPE_RGB == info_ptr->color_type)
{ {
@ -378,14 +402,6 @@ HGResult HGAPI HGImgFmt_SavePngImage(HGImage image, const HGPngSaveInfo* info, c
longjmp(png_jmpbuf(png_ptr), (int)ret); longjmp(png_jmpbuf(png_ptr), (int)ret);
} }
} }
else if (HGBASE_IMGTYPE_BINARY == imgInfo.type)
{
HGResult ret = HGBase_CloneImage(image, HGBASE_IMGTYPE_GRAY, HGBASE_IMGORIGIN_TOP, &image2);
if (HGBASE_ERR_OK != ret)
{
longjmp(png_jmpbuf(png_ptr), (int)ret);
}
}
else else
{ {
HGResult ret = HGBase_CloneImage(image, imgInfo.type, HGBASE_IMGORIGIN_TOP, &image2); HGResult ret = HGBase_CloneImage(image, imgInfo.type, HGBASE_IMGORIGIN_TOP, &image2);
@ -406,7 +422,7 @@ HGResult HGAPI HGImgFmt_SavePngImage(HGImage image, const HGPngSaveInfo* info, c
HGBase_GetImageData(image2, &data); HGBase_GetImageData(image2, &data);
int color_type = -1; int color_type = -1;
if (HGBASE_IMGTYPE_GRAY == type) if (HGBASE_IMGTYPE_BINARY == type || HGBASE_IMGTYPE_GRAY == type)
color_type = PNG_COLOR_TYPE_GRAY; color_type = PNG_COLOR_TYPE_GRAY;
else if (HGBASE_IMGTYPE_RGB == type) else if (HGBASE_IMGTYPE_RGB == type)
color_type = PNG_COLOR_TYPE_RGB; color_type = PNG_COLOR_TYPE_RGB;
@ -414,7 +430,11 @@ HGResult HGAPI HGImgFmt_SavePngImage(HGImage image, const HGPngSaveInfo* info, c
color_type = PNG_COLOR_TYPE_RGB_ALPHA; color_type = PNG_COLOR_TYPE_RGB_ALPHA;
assert(-1 != color_type); assert(-1 != color_type);
png_set_IHDR(png_ptr, info_ptr, width, height, 8, color_type, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); int bpp = 8;
if (HGBASE_IMGTYPE_BINARY == type)
bpp = 1;
png_set_IHDR(png_ptr, info_ptr, width, height, bpp, color_type, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
if (NULL != info) if (NULL != info)
{ {