diff --git a/frameworks/innerkitsimpl/converter/include/image_format_convert_utils.h b/frameworks/innerkitsimpl/converter/include/image_format_convert_utils.h index 2f0723fca12ae66af749987c655201049eff8ae6..d13fdc0de94e03d5262ab66da2b4bd116887cf23 100644 --- a/frameworks/innerkitsimpl/converter/include/image_format_convert_utils.h +++ b/frameworks/innerkitsimpl/converter/include/image_format_convert_utils.h @@ -145,6 +145,12 @@ public: [[maybe_unused]]ColorSpace colorSpace); static bool NV12ToRGB(const uint8_t *srcBuffer, const YUVDataInfo &yDInfo, DestConvertInfo &destInfo, [[maybe_unused]]ColorSpace colorSpace); + static bool AlignBufferCore(const ImageInfo& srcImageInfo, const uint8_t* buffer, + SrcConvertParam& srcParam, std::unique_ptr& copySrcBuffer); + static bool AlignSrcBuffer(const YUVDataInfo& yDInfo, PixelFormat srcFormat, const uint8_t* srcBuffer, + SrcConvertParam& srcParam, std::unique_ptr& copySrcBuffer); + static bool AlignSrcBuffer(const RGBDataInfo &rgbInfo, PixelFormat srcFormat, const uint8_t* srcBuffer, + SrcConvertParam& srcParam, std::unique_ptr& copySrcBuffer); }; } // namespace Media } // namespace OHOS diff --git a/frameworks/innerkitsimpl/converter/src/image_format_convert_utils.cpp b/frameworks/innerkitsimpl/converter/src/image_format_convert_utils.cpp index b76d9b35321c5b388bba74148a6d7dd907290e62..a86d6b658b72f527df9e9a8d116d18be6e986eee 100644 --- a/frameworks/innerkitsimpl/converter/src/image_format_convert_utils.cpp +++ b/frameworks/innerkitsimpl/converter/src/image_format_convert_utils.cpp @@ -404,6 +404,12 @@ static bool RGBToYuvP010(const uint8_t *srcBuffer, const RGBDataInfo &rgbInfo, P srcParam.format = srcFormat; srcParam.buffer = srcBuffer; + std::unique_ptr copySrcBuffer; + if (!ImageFormatConvertUtils::AlignSrcBuffer(rgbInfo, srcFormat, srcBuffer, srcParam, copySrcBuffer)) { + IMAGE_LOGE("Failed to prepare aligned src buffer for YuvToRGB"); + return false; + } + DestConvertParam destParam = {rgbInfo.width, rgbInfo.height}; destParam.format = dstFormat; @@ -1132,8 +1138,9 @@ static bool YuvToYuvParam(const YUVDataInfo &yDInfo, SrcConvertParam &srcParam, return true; } -static bool AlignBufferCore(const ImageInfo& srcImageInfo, const uint8_t* buffer, - SrcConvertParam& srcParam, std::unique_ptr& copySrcBuffer) +bool ImageFormatConvertUtils::AlignBufferCore(const ImageInfo& srcImageInfo, const uint8_t* buffer, + SrcConvertParam& srcParam, + std::unique_ptr& copySrcBuffer) { if (srcImageInfo.size.width <= 0 || srcImageInfo.size.height <= 0) { IMAGE_LOGE("Invalid src width(%{public}d) or height(%{public}d)", @@ -1170,14 +1177,26 @@ static bool AlignBufferCore(const ImageInfo& srcImageInfo, const uint8_t* buffer return true; } -static bool AlignSrcBuffer(const YUVDataInfo& yDInfo, PixelFormat srcFormat, const uint8_t* srcBuffer, - SrcConvertParam& srcParam, std::unique_ptr& copySrcBuffer) +bool ImageFormatConvertUtils::AlignSrcBuffer(const YUVDataInfo& yDInfo, PixelFormat srcFormat, + const uint8_t* srcBuffer, SrcConvertParam& srcParam, + std::unique_ptr& copySrcBuffer) { ImageInfo srcImageInfo = { .size = {static_cast(yDInfo.yWidth), static_cast(yDInfo.yHeight)}, .pixelFormat = srcFormat }; - return AlignBufferCore(srcImageInfo, srcBuffer, srcParam, copySrcBuffer); + return ImageFormatConvertUtils::AlignBufferCore(srcImageInfo, srcBuffer, srcParam, copySrcBuffer); +} + +bool ImageFormatConvertUtils::AlignSrcBuffer(const RGBDataInfo &rgbInfo, PixelFormat srcFormat, + const uint8_t* srcBuffer, SrcConvertParam& srcParam, + std::unique_ptr& copySrcBuffer) +{ + ImageInfo srcImageInfo = { + .size = {rgbInfo.width, rgbInfo.height}, + .pixelFormat = srcFormat + }; + return ImageFormatConvertUtils::AlignBufferCore(srcImageInfo, srcBuffer, srcParam, copySrcBuffer); } static bool YuvToYuv(const uint8_t *srcBuffer, const YUVDataInfo &yDInfo, PixelFormat srcFormat, @@ -1217,7 +1236,7 @@ static bool YuvToRGB(const uint8_t *srcBuffer, const YUVDataInfo &yDInfo, PixelF srcParam.buffer = srcBuffer; std::unique_ptr copySrcBuffer; - if (!AlignSrcBuffer(yDInfo, srcFormat, srcBuffer, srcParam, copySrcBuffer)) { + if (!ImageFormatConvertUtils::AlignSrcBuffer(yDInfo, srcFormat, srcBuffer, srcParam, copySrcBuffer)) { IMAGE_LOGE("Failed to prepare aligned src buffer for YuvToRGB"); return false; } @@ -1243,28 +1262,15 @@ static bool RGBToYuv(const uint8_t *srcBuffer, const RGBDataInfo &rgbInfo, Pixel destInfo.bufferSize == 0) { return false; } - int32_t copyWidth = rgbInfo.width; - int32_t copyHeight = rgbInfo.height; - SrcConvertParam srcParam; + SrcConvertParam srcParam = {rgbInfo.width, rgbInfo.height}; + srcParam.format = srcFormat; srcParam.buffer = srcBuffer; + std::unique_ptr copySrcBuffer; - if (rgbInfo.width % EVEN_ALIGNMENT != 0 || rgbInfo.height % EVEN_ALIGNMENT != 0) { - if (!ImageUtils::GetAlignedNumber(copyWidth, EVEN_ALIGNMENT) || - !ImageUtils::GetAlignedNumber(copyHeight, EVEN_ALIGNMENT)) { - return false; - } - int32_t copySrcLen = copyWidth * copyHeight * ImageUtils::GetPixelBytes(srcFormat); - int32_t rgbInfoLen = rgbInfo.width * rgbInfo.height * ImageUtils::GetPixelBytes(srcFormat); - copySrcBuffer = std::make_unique(copySrcLen); - if (copySrcBuffer == nullptr || EOK != memcpy_s(copySrcBuffer.get(), rgbInfoLen, srcBuffer, rgbInfoLen)) { - IMAGE_LOGE("alloc memory or memcpy_s failed!"); - return false; - } - srcParam.buffer = copySrcBuffer.get(); + if (!ImageFormatConvertUtils::AlignSrcBuffer(rgbInfo, srcFormat, srcBuffer, srcParam, copySrcBuffer)) { + IMAGE_LOGE("Failed to prepare aligned src buffer for YuvToRGB"); + return false; } - srcParam.width = static_cast(copyWidth); - srcParam.height = static_cast(copyHeight); - srcParam.format = srcFormat; DestConvertParam destParam = {destInfo.width, destInfo.height}; destParam.format = destFormat;