code_app/modules/imgfmt/HGPng.cpp

483 lines
12 KiB
C++
Raw Normal View History

2022-05-03 10:25:52 +00:00
#include "HGPng.h"
#include "../base/HGInc.h"
2022-11-23 03:17:13 +00:00
#include "../base/HGInfo.h"
2022-05-03 10:25:52 +00:00
#include "png.h"
#include "pngstruct.h"
#include "pnginfo.h"
#include "pngpriv.h"
HGResult HGAPI HGImgFmt_CheckPngFile(const HGChar* fileName, HGBool* isPng)
{
if (NULL == fileName || NULL == isPng)
{
return HGBASE_ERR_INVALIDARG;
}
HGPngLoadInfo info;
HGResult ret = HGImgFmt_LoadPngImage(fileName, &info, 0, 0, NULL);
2022-11-26 02:42:06 +00:00
if (HGBASE_ERR_OK != ret)
{
return ret;
}
*isPng = HGTRUE;
return ret;
2022-05-03 10:25:52 +00:00
}
HGResult HGAPI HGImgFmt_LoadPngImage(const HGChar* fileName, HGPngLoadInfo* info,
HGUInt imgType, HGUInt imgOrigin, HGImage* image)
{
if (NULL == fileName)
{
return HGBASE_ERR_INVALIDARG;
}
if (NULL == image)
{
if (0 != imgType || 0 != imgOrigin)
{
return HGBASE_ERR_INVALIDARG;
}
}
else
{
if (0 != imgType && HGBASE_IMGTYPE_BINARY != imgType && HGBASE_IMGTYPE_GRAY != imgType
&& HGBASE_IMGTYPE_BGR != imgType && HGBASE_IMGTYPE_RGB != imgType
&& HGBASE_IMGTYPE_BGRA != imgType && HGBASE_IMGTYPE_RGBA != imgType)
2022-05-03 10:25:52 +00:00
{
return HGBASE_ERR_INVALIDARG;
}
if (0 != imgOrigin && HGBASE_IMGORIGIN_TOP != imgOrigin && HGBASE_IMGORIGIN_BOTTOM != imgOrigin)
2022-05-03 10:25:52 +00:00
{
return HGBASE_ERR_INVALIDARG;
}
}
#if defined(HG_CMP_MSC)
if (0 != _access(fileName, 0))
#else
if (0 != access(fileName, 0))
#endif
{
return HGBASE_ERR_FILENOTEXIST;
}
2022-05-03 10:25:52 +00:00
FILE* file = fopen(fileName, "rb");
if (NULL == file)
{
return HGBASE_ERR_ACCESSDENIED;
}
png_byte buf[8] = { 0 };
if (fread(buf, 1, 8, file) != 8)
{
fclose(file);
file = NULL;
return HGBASE_ERR_FAIL;
}
if (0 != png_sig_cmp(buf, 0, 8))
{
fclose(file);
file = NULL;
return HGBASE_ERR_FILEERROR;
2022-05-03 10:25:52 +00:00
}
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (NULL == png_ptr)
{
fclose(file);
file = NULL;
2022-11-26 02:42:06 +00:00
return HGIMGFMT_ERR_FAIL;
2022-05-03 10:25:52 +00:00
}
png_infop info_ptr = png_create_info_struct(png_ptr);
if (NULL == info_ptr)
{
png_destroy_read_struct(&png_ptr, NULL, NULL);
fclose(file);
file = NULL;
2022-11-26 02:42:06 +00:00
return HGIMGFMT_ERR_FAIL;
2022-05-03 10:25:52 +00:00
}
uint8_t* buffer = NULL;
uint8_t** rowPointers = NULL;
HGImage image2 = NULL;
2022-05-03 10:25:52 +00:00
int jmpResult = setjmp(png_jmpbuf(png_ptr));
if (0 != jmpResult)
{
HGBase_DestroyImage(image2);
image2 = NULL;
2022-05-03 10:25:52 +00:00
free(rowPointers);
rowPointers = NULL;
free(buffer);
buffer = NULL;
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
fclose(file);
file = NULL;
return (HGResult)jmpResult;
}
#if defined(HG_CMP_MSC)
_fseeki64(file, 0, SEEK_SET);
#else
fseeko64(file, 0, SEEK_SET);
#endif
png_init_io(png_ptr, file);
png_read_info(png_ptr, info_ptr);
if (NULL != info)
{
info->width = info_ptr->width;
info->height = info_ptr->height;
info->bitDepth = info_ptr->bit_depth;
info->colorType = info_ptr->color_type;
info->channels = info_ptr->channels;
info->filterType = info_ptr->filter_type;
info->InterlaceType = info_ptr->interlace_type;
info->compressionType = info_ptr->compression_type;
info->pixelDepth = info_ptr->pixel_depth;
info->physUnitType = info_ptr->phys_unit_type;
info->xPixelsPerUnit = info_ptr->x_pixels_per_unit;
info->yPixelsPerUnit = info_ptr->y_pixels_per_unit;
}
if (NULL != image)
{
png_set_scale_16(png_ptr);
2023-07-04 09:26:29 +00:00
if (1 != info_ptr->bit_depth)
png_set_expand(png_ptr);
2022-05-03 10:25:52 +00:00
png_set_interlace_handling(png_ptr);
png_read_update_info(png_ptr, info_ptr);
buffer = (uint8_t*)malloc((uintptr_t)info_ptr->rowbytes * (uintptr_t)info_ptr->height);
if (NULL == buffer)
{
2022-11-26 02:42:06 +00:00
longjmp(png_jmpbuf(png_ptr), (int)HGBASE_ERR_OUTOFMEMORY);
2022-05-03 10:25:52 +00:00
}
rowPointers = (uint8_t**)malloc(info_ptr->height * sizeof(png_bytep));
if (NULL == rowPointers)
{
2022-11-26 02:42:06 +00:00
longjmp(png_jmpbuf(png_ptr), (int)HGBASE_ERR_OUTOFMEMORY);
2022-05-03 10:25:52 +00:00
}
//#pragma omp parallel for
for (png_int_32 i = 0; i < (png_int_32)info_ptr->height; ++i)
{
rowPointers[i] = buffer + (uintptr_t)i * (uintptr_t)info_ptr->rowbytes;
}
png_read_image(png_ptr, rowPointers);
png_read_end(png_ptr, info_ptr);
if (0 == imgType)
{
imgType = HGBASE_IMGTYPE_RGB;
2022-05-03 10:25:52 +00:00
if (PNG_COLOR_TYPE_GRAY_ALPHA == info_ptr->color_type || PNG_COLOR_TYPE_RGB_ALPHA == info_ptr->color_type)
{
imgType = HGBASE_IMGTYPE_RGBA;
}
else if (PNG_COLOR_TYPE_GRAY == info_ptr->color_type)
{
2023-07-04 09:26:29 +00:00
if (1 == info_ptr->bit_depth)
imgType = HGBASE_IMGTYPE_BINARY;
else
imgType = HGBASE_IMGTYPE_GRAY;
2022-05-03 10:25:52 +00:00
}
}
if (imgOrigin == 0)
{
imgOrigin = HGBASE_IMGORIGIN_TOP;
}
2022-11-26 02:42:06 +00:00
HGResult ret = HGBase_CreateImage(info_ptr->width, info_ptr->height, HGBASE_IMGTYPE_RGBA, HGBASE_IMGORIGIN_TOP, &image2);
if (HGBASE_ERR_OK != ret)
2022-05-03 10:25:52 +00:00
{
2022-11-26 02:42:06 +00:00
longjmp(png_jmpbuf(png_ptr), (int)ret);
2022-05-03 10:25:52 +00:00
}
if (PNG_RESOLUTION_METER == info_ptr->phys_unit_type)
2022-05-03 10:25:52 +00:00
{
uint32_t xDpi = (uint32_t)((double)info_ptr->x_pixels_per_unit / 39.3700787 + 0.5);
uint32_t yDpi = (uint32_t)((double)info_ptr->y_pixels_per_unit / 39.3700787 + 0.5);
HGBase_SetImageDpi(image2, xDpi, yDpi);
}
uint8_t* data;
HGBase_GetImageData(image2, &data);
HGImageInfo imgInfo;
HGBase_GetImageInfo(image2, &imgInfo);
if (PNG_COLOR_TYPE_GRAY == info_ptr->color_type)
2022-05-03 10:25:52 +00:00
{
2023-07-04 09:26:29 +00:00
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
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;
while (pEx < pExEnd)
{
uint8_t v = *pEx;
*((uint32_t*)pDestEx) = (v & 0x000000FF) | ((v << 8) & 0x0000FF00) | ((v << 16) & 0x00FF0000) | 0xFF000000;
++pEx;
pDestEx += 4;
}
}
}
2022-05-03 10:25:52 +00:00
}
else if (PNG_COLOR_TYPE_RGB == info_ptr->color_type)
2022-05-03 10:25:52 +00:00
{
//#pragma omp parallel for
for (png_int_32 i = 0; i < (png_int_32)info_ptr->height; i++)
2022-05-03 10:25:52 +00:00
{
uint8_t* pEx = rowPointers[i];
uint8_t* pExEnd = pEx + info_ptr->width * 3;
uint8_t* pDestEx = data + (HGSize)i * (HGSize)imgInfo.widthStep;
2022-05-03 10:25:52 +00:00
while (pEx < pExEnd)
2022-05-03 10:25:52 +00:00
{
uint8_t r = pEx[0];
uint8_t g = pEx[1];
uint8_t b = pEx[2];
*((uint32_t*)pDestEx) = (r & 0x000000FF) | ((g << 8) & 0x0000FF00) | ((b << 16) & 0x00FF0000) | 0xFF000000;
pEx += 3;
pDestEx += 4;
2022-05-03 10:25:52 +00:00
}
}
}
else if (PNG_COLOR_TYPE_GRAY_ALPHA == info_ptr->color_type)
2022-05-03 10:25:52 +00:00
{
//#pragma omp parallel for
for (png_int_32 i = 0; i < (png_int_32)info_ptr->height; i++)
2022-05-03 10:25:52 +00:00
{
uint8_t* pEx = rowPointers[i];
uint8_t* pExEnd = pEx + info_ptr->width * 2;
uint8_t* pDestEx = data + (HGSize)i * (HGSize)imgInfo.widthStep;
2022-05-03 10:25:52 +00:00
while (pEx < pExEnd)
2022-05-03 10:25:52 +00:00
{
uint8_t v = pEx[0];
uint8_t alpha = pEx[1];
*((uint32_t*)pDestEx) = (v & 0x000000FF) | ((v << 8) & 0x0000FF00) | ((v << 16) & 0x00FF0000) | ((alpha << 24) & 0xFF000000);
pEx += 2;
pDestEx += 4;
2022-05-03 10:25:52 +00:00
}
}
}
else
{
assert(PNG_COLOR_TYPE_RGB_ALPHA == info_ptr->color_type);
2022-05-03 10:25:52 +00:00
//#pragma omp parallel for
for (png_int_32 i = 0; i < (png_int_32)info_ptr->height; i++)
2022-05-03 10:25:52 +00:00
{
uint8_t* pEx = rowPointers[i];
uint8_t* pDestEx = data + (HGSize)i * (HGSize)imgInfo.widthStep;
memcpy(pDestEx, pEx, info_ptr->width * 4);
2022-05-03 10:25:52 +00:00
}
}
2022-11-26 02:42:06 +00:00
ret = HGBase_CloneImage(image2, imgType, imgOrigin, image);
if (HGBASE_ERR_OK != ret)
{
2022-11-26 02:42:06 +00:00
longjmp(png_jmpbuf(png_ptr), (int)ret);
}
2022-05-03 10:25:52 +00:00
}
HGBase_DestroyImage(image2);
image2 = NULL;
free(rowPointers);
rowPointers = NULL;
free(buffer);
buffer = NULL;
2022-05-03 10:25:52 +00:00
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
fclose(file);
file = NULL;
return HGBASE_ERR_OK;
}
HGResult HGAPI HGImgFmt_SavePngImage(HGImage image, const HGPngSaveInfo* info, const HGChar* fileName)
{
if (NULL == image || NULL == fileName)
{
return HGBASE_ERR_INVALIDARG;
}
if (NULL != info)
{
// 判断合法性
}
FILE* file = fopen(fileName, "wb");
if (NULL == file)
{
HGBase_WriteInfo(HGBASE_INFOTYPE_ERROR, "HGImgFmt_SavePngImage: fopen fail, %s errno=%d", fileName, errno);
2022-05-03 10:25:52 +00:00
return HGBASE_ERR_ACCESSDENIED;
}
png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (NULL == png_ptr)
{
fclose(file);
file = NULL;
2022-11-26 02:42:06 +00:00
return HGIMGFMT_ERR_FAIL;
2022-05-03 10:25:52 +00:00
}
png_infop info_ptr = png_create_info_struct(png_ptr);
if (NULL == info_ptr)
{
png_destroy_write_struct(&png_ptr, NULL);
fclose(file);
file = NULL;
2022-11-26 02:42:06 +00:00
return HGIMGFMT_ERR_FAIL;
2022-05-03 10:25:52 +00:00
}
uint8_t** rowPointers = NULL;
HGImage image2 = NULL;
HGImageRoi roi;
HGBase_GetImageROI(image, &roi);
2022-05-03 10:25:52 +00:00
int jmpResult = setjmp(png_jmpbuf(png_ptr));
if (0 != jmpResult)
{
HGBase_SetImageROI(image, &roi);
HGBase_DestroyImage(image2);
image2 = NULL;
2022-05-03 10:25:52 +00:00
free(rowPointers);
rowPointers = NULL;
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(file);
file = NULL;
return (HGResult)jmpResult;
}
png_init_io(png_ptr, file);
HGBase_ResetImageROI(image);
HGImageInfo imgInfo;
HGBase_GetImageInfo(image, &imgInfo);
if (HGBASE_IMGTYPE_BGR == imgInfo.type)
{
2022-11-26 02:42:06 +00:00
HGResult ret = HGBase_CloneImage(image, HGBASE_IMGTYPE_RGB, HGBASE_IMGORIGIN_TOP, &image2);
if (HGBASE_ERR_OK != ret)
{
2022-11-26 02:42:06 +00:00
longjmp(png_jmpbuf(png_ptr), (int)ret);
}
}
else if (HGBASE_IMGTYPE_BGRA == imgInfo.type)
{
2022-11-26 02:42:06 +00:00
HGResult ret = HGBase_CloneImage(image, HGBASE_IMGTYPE_RGBA, HGBASE_IMGORIGIN_TOP, &image2);
if (HGBASE_ERR_OK != ret)
{
2022-11-26 02:42:06 +00:00
longjmp(png_jmpbuf(png_ptr), (int)ret);
}
}
else
{
2022-11-26 02:42:06 +00:00
HGResult ret = HGBase_CloneImage(image, imgInfo.type, HGBASE_IMGORIGIN_TOP, &image2);
if (HGBASE_ERR_OK != ret)
{
2022-11-26 02:42:06 +00:00
longjmp(png_jmpbuf(png_ptr), (int)ret);
}
}
HGBase_GetImageInfo(image2, &imgInfo);
uint32_t width = imgInfo.width;
uint32_t height = imgInfo.height;
uint32_t widthStep = imgInfo.widthStep;
uint32_t type = imgInfo.type;
uint32_t origin = imgInfo.origin;
2022-05-03 10:25:52 +00:00
uint8_t* data;
HGBase_GetImageData(image2, &data);
2022-05-03 10:25:52 +00:00
int color_type = -1;
2023-07-04 09:26:29 +00:00
if (HGBASE_IMGTYPE_BINARY == type || HGBASE_IMGTYPE_GRAY == type)
2022-05-03 10:25:52 +00:00
color_type = PNG_COLOR_TYPE_GRAY;
else if (HGBASE_IMGTYPE_RGB == type)
color_type = PNG_COLOR_TYPE_RGB;
else if (HGBASE_IMGTYPE_RGBA == type)
color_type = PNG_COLOR_TYPE_RGB_ALPHA;
assert(-1 != color_type);
2023-07-04 09:26:29 +00:00
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);
2022-05-03 10:25:52 +00:00
if (NULL != info)
{
info_ptr->phys_unit_type = info->physUnitType;
info_ptr->x_pixels_per_unit = info->xPixelsPerUnit;
info_ptr->y_pixels_per_unit = info->yPixelsPerUnit;
info_ptr->valid |= PNG_INFO_pHYs;
}
else
{
HGUInt xDpi, yDpi;
HGBase_GetImageDpi(image2, &xDpi, &yDpi);
info_ptr->phys_unit_type = PNG_RESOLUTION_METER;
info_ptr->x_pixels_per_unit = (uint32_t)((double)xDpi * 39.3700787 + 0.5);
info_ptr->y_pixels_per_unit = (uint32_t)((double)yDpi * 39.3700787 + 0.5);
info_ptr->valid |= PNG_INFO_pHYs;
}
2022-05-03 10:25:52 +00:00
png_write_info(png_ptr, info_ptr);
rowPointers = (uint8_t**)malloc(height * sizeof(png_bytep));
if (NULL == rowPointers)
{
2022-11-26 02:42:06 +00:00
longjmp(png_jmpbuf(png_ptr), (int)HGBASE_ERR_OUTOFMEMORY);
2022-05-03 10:25:52 +00:00
}
//#pragma omp parallel for
for (int32_t i = 0; i < (int32_t)height; ++i)
{
rowPointers[i] = data + (HGSize)i * (HGSize)widthStep;
2022-05-03 10:25:52 +00:00
}
png_write_image(png_ptr, rowPointers);
png_write_end(png_ptr, info_ptr);
HGBase_SetImageROI(image, &roi);
HGBase_DestroyImage(image2);
image2 = NULL;
2022-05-03 10:25:52 +00:00
free(rowPointers);
rowPointers = NULL;
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(file);
file = NULL;
return HGBASE_ERR_OK;
2023-07-04 09:26:29 +00:00
}