From f9f6209309335433ab9e5ea85525fc3ba6a64b98 Mon Sep 17 00:00:00 2001 From: luoliangyi <87842688@qq.com> Date: Fri, 24 May 2024 12:53:52 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3TGA=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E4=BF=9D=E5=AD=98=E5=A4=B1=E8=B4=A5=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build2/qt/HGImgFmt/HGImgFmt.pro | 1 + modules/base/HGImage.cpp | 12 +++++++ modules/imgfmt/HGImgFmt.cpp | 62 ++++++++++++++++----------------- 3 files changed, 43 insertions(+), 32 deletions(-) diff --git a/build2/qt/HGImgFmt/HGImgFmt.pro b/build2/qt/HGImgFmt/HGImgFmt.pro index 44c6e1aa..4a0bcbb3 100644 --- a/build2/qt/HGImgFmt/HGImgFmt.pro +++ b/build2/qt/HGImgFmt/HGImgFmt.pro @@ -221,6 +221,7 @@ SOURCES += \ ../../../modules/imgfmt/HGPng.cpp \ ../../../modules/imgfmt/HGPnm.cpp \ ../../../modules/imgfmt/HGTiff.cpp \ + ../../../modules/imgfmt/dllmain.cpp \ ../../../utility/HGString.cpp HEADERS += \ diff --git a/modules/base/HGImage.cpp b/modules/base/HGImage.cpp index c4f6e59a..dac19329 100644 --- a/modules/base/HGImage.cpp +++ b/modules/base/HGImage.cpp @@ -3089,6 +3089,7 @@ static HGResult CopyImageWithBinary(HGImageImpl *srcImageImpl, HGImageImpl *dest pDestEx[(destImageImpl->m_left + j) * 4] = (0 == value) ? 0 : 255; pDestEx[(destImageImpl->m_left + j) * 4 + 1] = (0 == value) ? 0 : 255; pDestEx[(destImageImpl->m_left + j) * 4 + 2] = (0 == value) ? 0 : 255; + pDestEx[(destImageImpl->m_left + j) * 4 + 3] = 255; } } } @@ -3283,6 +3284,8 @@ HGResult HGAPI HGBase_CopyImage(HGImage image, HGImage destImage) while (pEx < pExEnd) { pDestEx[0] = pDestEx[1] = pDestEx[2] = *pEx; + if (HGBASE_IMGTYPE_RGBA == destType || HGBASE_IMGTYPE_BGRA == destType) + pDestEx[3] = 255; ++pEx; pDestEx += destChannels; } @@ -3348,6 +3351,8 @@ HGResult HGAPI HGBase_CopyImage(HGImage image, HGImage destImage) pDestEx[0] = pEx[0]; pDestEx[1] = pEx[1]; pDestEx[2] = pEx[2]; + if (HGBASE_IMGTYPE_RGBA == destType || HGBASE_IMGTYPE_BGRA == destType) + pDestEx[3] = 255; pEx += channels; pDestEx += destChannels; @@ -3377,6 +3382,13 @@ HGResult HGAPI HGBase_CopyImage(HGImage image, HGImage destImage) pDestEx[2] = pEx[0]; pDestEx[1] = pEx[1]; pDestEx[0] = pEx[2]; + if (HGBASE_IMGTYPE_RGBA == destType || HGBASE_IMGTYPE_BGRA == destType) + { + if (HGBASE_IMGTYPE_RGBA == type || HGBASE_IMGTYPE_BGRA == type) + pDestEx[3] = pEx[3]; + else + pDestEx[3] = 255; + } pEx += channels; pDestEx += destChannels; diff --git a/modules/imgfmt/HGImgFmt.cpp b/modules/imgfmt/HGImgFmt.cpp index 92818bf2..68f2de18 100644 --- a/modules/imgfmt/HGImgFmt.cpp +++ b/modules/imgfmt/HGImgFmt.cpp @@ -21,12 +21,7 @@ static HGResult FBITMAPToHGImage(FIBITMAP *dib, HGUInt imgType, HGUInt imgOrigin assert(NULL != dib); assert(NULL != image); - BYTE *data = FreeImage_GetBits(dib); - HGUInt width = FreeImage_GetWidth(dib); - HGUInt height = FreeImage_GetHeight(dib); HGUInt bpp = FreeImage_GetBPP(dib); - HGUInt pitch = FreeImage_GetPitch(dib); - if (0 == imgType) { imgType = HGBASE_IMGTYPE_RGB; @@ -44,10 +39,10 @@ static HGResult FBITMAPToHGImage(FIBITMAP *dib, HGUInt imgType, HGUInt imgOrigin } HGImageInfo imgInfo; - imgInfo.width = width; - imgInfo.height = height; + imgInfo.width = FreeImage_GetWidth(dib); + imgInfo.height = FreeImage_GetHeight(dib); imgInfo.type = 0; - imgInfo.widthStep = pitch; + imgInfo.widthStep = FreeImage_GetPitch(dib); imgInfo.origin = HGBASE_IMGORIGIN_BOTTOM; if (1 == bpp) @@ -61,7 +56,7 @@ static HGResult FBITMAPToHGImage(FIBITMAP *dib, HGUInt imgType, HGUInt imgOrigin if (0 != imgInfo.type) { - HGResult ret = HGBase_CreateImageFromData(data, &imgInfo, NULL, imgType, imgOrigin, image); + HGResult ret = HGBase_CreateImageFromData(FreeImage_GetBits(dib), &imgInfo, NULL, imgType, imgOrigin, image); if (HGBASE_ERR_OK == ret) { HGBase_SetImageDpi(*image, (HGUInt)((double)FreeImage_GetDotsPerMeterX(dib) / 39.3700787 + 0.5), @@ -77,17 +72,14 @@ static HGResult FBITMAPToHGImage(FIBITMAP *dib, HGUInt imgType, HGUInt imgOrigin return HGBASE_ERR_FAIL; } - data = FreeImage_GetBits(dib2); - width = FreeImage_GetWidth(dib2); - height = FreeImage_GetHeight(dib2); - pitch = FreeImage_GetPitch(dib2); + HGImageInfo imgInfo2; + imgInfo2.width = FreeImage_GetWidth(dib2); + imgInfo2.height = FreeImage_GetHeight(dib2); + imgInfo2.type = HGBASE_IMGTYPE_BGRA; + imgInfo2.widthStep = FreeImage_GetPitch(dib2); + imgInfo2.origin = HGBASE_IMGORIGIN_BOTTOM; - imgInfo.width = width; - imgInfo.height = height; - imgInfo.type = HGBASE_IMGTYPE_BGRA; - imgInfo.widthStep = pitch; - imgInfo.origin = HGBASE_IMGORIGIN_BOTTOM; - HGResult ret = HGBase_CreateImageFromData(data, &imgInfo, NULL, imgType, imgOrigin, image); + HGResult ret = HGBase_CreateImageFromData(FreeImage_GetBits(dib2), &imgInfo2, NULL, imgType, imgOrigin, image); if (HGBASE_ERR_OK == ret) { HGBase_SetImageDpi(*image, (HGUInt)((double)FreeImage_GetDotsPerMeterX(dib2) / 39.3700787 + 0.5), @@ -98,19 +90,29 @@ static HGResult FBITMAPToHGImage(FIBITMAP *dib, HGUInt imgType, HGUInt imgOrigin return ret; } -static HGResult HGImageToFIBITMAP(HGImage image, FIBITMAP **dib) +static HGResult HGImageToFIBITMAP(HGImage image, FREE_IMAGE_FORMAT fif, FIBITMAP **dib) { assert(NULL != image); assert(NULL != dib); - HGImage image2 = NULL; - + HGImage image2 = image; HGImageInfo imgInfo; - HGBase_GetImageInfo(image, &imgInfo); - if (HGBASE_IMGTYPE_RGB == imgInfo.type || HGBASE_IMGTYPE_RGBA == imgInfo.type) + HGBase_GetImageInfo(image2, &imgInfo); + + if ((HGBASE_IMGTYPE_RGB == imgInfo.type) || (HGBASE_IMGTYPE_RGBA == imgInfo.type) + || (HGBASE_IMGTYPE_BINARY == imgInfo.type && FIF_TARGA == fif)) { - HGResult ret = HGBase_CloneImage(image, (HGBASE_IMGTYPE_RGB == imgInfo.type ? HGBASE_IMGTYPE_BGR : HGBASE_IMGTYPE_BGRA), - imgInfo.origin, &image2); + HGUInt type = HGBASE_IMGTYPE_BGR; + if (HGBASE_IMGTYPE_RGBA == imgInfo.type) + type = HGBASE_IMGTYPE_BGRA; + else if (HGBASE_IMGTYPE_BINARY == imgInfo.type && FIF_TARGA == fif) + type = HGBASE_IMGTYPE_GRAY; + + HGImageRoi imageRoi; + HGBase_GetImageROI(image, &imageRoi); + HGBase_ResetImageROI(image); + HGResult ret = HGBase_CloneImage(image, type, imgInfo.origin, &image2); + HGBase_SetImageROI(image, &imageRoi); if (HGBASE_ERR_OK != ret) { return ret; @@ -118,10 +120,6 @@ static HGResult HGImageToFIBITMAP(HGImage image, FIBITMAP **dib) HGBase_GetImageInfo(image2, &imgInfo); } - else - { - image2 = image; - } HGByte *data = NULL; HGBase_GetImageData(image2, &data); @@ -138,7 +136,7 @@ static HGResult HGImageToFIBITMAP(HGImage image, FIBITMAP **dib) else assert(HGBASE_IMGTYPE_BINARY == imgInfo.type); - *dib = FreeImage_ConvertFromRawBits(data, imgInfo.width, imgInfo.height, imgInfo.widthStep, + *dib = FreeImage_ConvertFromRawBitsEx(TRUE, data, FIT_BITMAP, imgInfo.width, imgInfo.height, imgInfo.widthStep, bpp, 0, 0, 0, (HGBASE_IMGORIGIN_TOP == imgInfo.origin)); if (NULL != *dib) { @@ -253,7 +251,7 @@ static HGResult SaveFIImage(HGImage image, const HGImgFmtSaveInfo* info, const H } FIBITMAP *dib = NULL; - HGResult ret = HGImageToFIBITMAP(image, &dib); + HGResult ret = HGImageToFIBITMAP(image, fif, &dib); if (HGBASE_ERR_OK != ret) { return ret;