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,7 +147,8 @@ HGResult HGAPI HGImgFmt_LoadPngImage(const HGChar* fileName, HGPngLoadInfo* info
if (NULL != image)
{
png_set_scale_16(png_ptr);
png_set_expand(png_ptr);
if (1 != info_ptr->bit_depth)
png_set_expand(png_ptr);
png_set_interlace_handling(png_ptr);
png_read_update_info(png_ptr, info_ptr);
@ -182,7 +183,10 @@ HGResult HGAPI HGImgFmt_LoadPngImage(const HGChar* fileName, HGPngLoadInfo* info
}
else if (PNG_COLOR_TYPE_GRAY == info_ptr->color_type)
{
imgType = HGBASE_IMGTYPE_GRAY;
if (1 == info_ptr->bit_depth)
imgType = HGBASE_IMGTYPE_BINARY;
else
imgType = HGBASE_IMGTYPE_GRAY;
}
}
@ -211,22 +215,42 @@ HGResult HGAPI HGImgFmt_LoadPngImage(const HGChar* fileName, HGPngLoadInfo* info
if (PNG_COLOR_TYPE_GRAY == info_ptr->color_type)
{
//#pragma omp parallel for
for (png_int_32 i = 0; i < (png_int_32)info_ptr->height; i++)
{
uint8_t* pEx = rowPointers[i];
uint8_t* pExEnd = pEx + info_ptr->width;
uint8_t* pDestEx = data + (HGSize)i * (HGSize)imgInfo.widthStep;
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;
while (pEx < pExEnd)
{
uint8_t v = *pEx;
*((uint32_t*)pDestEx) = (v & 0x000000FF) | ((v << 8) & 0x0000FF00) | ((v << 16) & 0x00FF0000) | 0xFF000000;
HGImage imgTmp = NULL;
HGBase_CreateImageWithData(buffer, &imgInfo, &imgTmp);
if (NULL != imgTmp)
{
HGBase_CopyImage(imgTmp, image2);
HGBase_DestroyImage(imgTmp);
}
}
else
{
//#pragma omp parallel for
for (png_int_32 i = 0; i < (png_int_32)info_ptr->height; i++)
{
uint8_t* pEx = rowPointers[i];
uint8_t* pExEnd = pEx + info_ptr->width;
uint8_t* pDestEx = data + (HGSize)i * (HGSize)imgInfo.widthStep;
++pEx;
pDestEx += 4;
}
}
while (pEx < pExEnd)
{
uint8_t v = *pEx;
*((uint32_t*)pDestEx) = (v & 0x000000FF) | ((v << 8) & 0x0000FF00) | ((v << 16) & 0x00FF0000) | 0xFF000000;
++pEx;
pDestEx += 4;
}
}
}
}
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);
}
}
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
{
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);
int color_type = -1;
if (HGBASE_IMGTYPE_GRAY == type)
if (HGBASE_IMGTYPE_BINARY == type || HGBASE_IMGTYPE_GRAY == type)
color_type = PNG_COLOR_TYPE_GRAY;
else if (HGBASE_IMGTYPE_RGB == type)
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;
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)
{
@ -459,4 +479,4 @@ HGResult HGAPI HGImgFmt_SavePngImage(HGImage image, const HGPngSaveInfo* info, c
fclose(file);
file = NULL;
return HGBASE_ERR_OK;
}
}