From 404932d97c6af14d22a0257c37307053ae16088e Mon Sep 17 00:00:00 2001 From: yaozhupeng Date: Fri, 26 Apr 2024 12:56:57 +0800 Subject: [PATCH] add pixelmap ndk Signed-off-by: yaozhupeng --- .../common/ndk/include/pixelmap_native_impl.h | 1 + .../common/pixelmap_ndk/pixelmap_native.cpp | 63 ++++++++++++++++++- .../pixelmap_ndk/pixelmap_native_impl.cpp | 6 ++ .../native/include/image/pixelmap_native.h | 49 +++++++++++++++ 4 files changed, 118 insertions(+), 1 deletion(-) diff --git a/frameworks/kits/js/common/ndk/include/pixelmap_native_impl.h b/frameworks/kits/js/common/ndk/include/pixelmap_native_impl.h index 1233ab2f7..3399f7fac 100644 --- a/frameworks/kits/js/common/ndk/include/pixelmap_native_impl.h +++ b/frameworks/kits/js/common/ndk/include/pixelmap_native_impl.h @@ -25,6 +25,7 @@ struct OH_PixelmapNative { public: OH_PixelmapNative(std::shared_ptr pixelMap); OH_PixelmapNative(const uint32_t *colors, uint32_t colorLength, const OHOS::Media::InitializationOptions &opts); + OH_PixelmapNative(const OHOS::Media::InitializationOptions &opts); OH_PixelmapNative(OH_PixelmapNative *OH_PixelmapNative, const OHOS::Media::InitializationOptions &opts); ~OH_PixelmapNative(); diff --git a/frameworks/kits/js/common/pixelmap_ndk/pixelmap_native.cpp b/frameworks/kits/js/common/pixelmap_ndk/pixelmap_native.cpp index 9162ff84a..0e3fb29b0 100644 --- a/frameworks/kits/js/common/pixelmap_ndk/pixelmap_native.cpp +++ b/frameworks/kits/js/common/pixelmap_ndk/pixelmap_native.cpp @@ -45,6 +45,7 @@ static constexpr int32_t IMAGE_BASE_23 = 23; struct OH_Pixelmap_InitializationOptions { uint32_t width; uint32_t height; + PIXEL_FORMAT srcPixelFormat = PIXEL_FORMAT::PIXEL_FORMAT_BGRA_8888; PIXEL_FORMAT pixelFormat = PIXEL_FORMAT::PIXEL_FORMAT_UNKNOWN; uint32_t editable = false; PIXELMAP_ALPHA_TYPE alphaType = PIXELMAP_ALPHA_TYPE::PIXELMAP_ALPHA_TYPE_UNKNOWN; @@ -69,7 +70,7 @@ static PIXEL_FORMAT ParsePixelForamt(int32_t val) static PIXELMAP_ALPHA_TYPE ParseAlphaType(int32_t val) { - if (val <= static_cast(PIXELMAP_ALPHA_TYPE::PIXELMAP_ALPHA_TYPE_PREMULTIPLIED)) { + if (val <= static_cast(PIXELMAP_ALPHA_TYPE::PIXELMAP_ALPHA_TYPE_UNPREMULTIPLIED)) { return PIXELMAP_ALPHA_TYPE(val); } @@ -189,6 +190,28 @@ Image_ErrorCode OH_PixelmapInitializationOptions_SetPixelFormat(OH_Pixelmap_Init return IMAGE_SUCCESS; } +MIDK_EXPORT +Image_ErrorCode OH_PixelmapInitializationOptions_GetSrcPixelFormat(OH_Pixelmap_InitializationOptions *ops, + int32_t *srcpixelFormat) +{ + if (ops == nullptr || srcpixelFormat == nullptr) { + return IMAGE_BAD_PARAMETER; + } + *srcpixelFormat = static_cast(ops->srcPixelFormat); + return IMAGE_SUCCESS; +} + +MIDK_EXPORT +Image_ErrorCode OH_PixelmapInitializationOptions_SetSrcPixelFormat(OH_Pixelmap_InitializationOptions *ops, + int32_t srcpixelFormat) +{ + if (ops == nullptr) { + return IMAGE_BAD_PARAMETER; + } + ops->srcPixelFormat = ParsePixelForamt(srcpixelFormat); + return IMAGE_SUCCESS; +} + MIDK_EXPORT Image_ErrorCode OH_PixelmapInitializationOptions_GetAlphaType(OH_Pixelmap_InitializationOptions *ops, int32_t *alphaType) @@ -301,6 +324,7 @@ Image_ErrorCode OH_PixelmapNative_CreatePixelmap(uint8_t *data, size_t dataLengt InitializationOptions info; info.editable = true; info.alphaType = static_cast(options->alphaType); + info.srcPixelFormat = static_cast(options->srcPixelFormat); info.pixelFormat = static_cast(options->pixelFormat); info.size.height = options->height; info.size.width = options->width; @@ -316,6 +340,32 @@ Image_ErrorCode OH_PixelmapNative_CreatePixelmap(uint8_t *data, size_t dataLengt return IMAGE_SUCCESS; } +MIDK_EXPORT +Image_ErrorCode OH_PixelmapNative_CreateEmptyPixelmap( + OH_Pixelmap_InitializationOptions *options, OH_PixelmapNative **pixelmap) +{ + if (options == nullptr) { + return IMAGE_BAD_PARAMETER; + } + InitializationOptions info; + info.editable = true; + info.alphaType = static_cast(options->alphaType); + info.srcPixelFormat = static_cast(options->srcPixelFormat); + info.pixelFormat = static_cast(options->pixelFormat); + info.size.height = options->height; + info.size.width = options->width; + + auto pixelmap2 = new OH_PixelmapNative(info); + if (pixelmap2 == nullptr || pixelmap2->GetInnerPixelmap() == nullptr) { + if (pixelmap2) { + delete pixelmap2; + } + return IMAGE_BAD_PARAMETER; + } + *pixelmap = pixelmap2; + return IMAGE_SUCCESS; +} + MIDK_EXPORT Image_ErrorCode OH_PixelmapNative_ReadPixels(OH_PixelmapNative *pixelmap, uint8_t *destination, size_t *bufferSize) { @@ -425,6 +475,17 @@ Image_ErrorCode OH_PixelmapNative_Release(OH_PixelmapNative *pixelmap) return IMAGE_SUCCESS; } +MIDK_EXPORT +Image_ErrorCode OH_PixelmapNative_ConvertAlphaFormat(OH_PixelmapNative* srcpixelmap, + OH_PixelmapNative* dstpixelmap, const bool isPremul) +{ + if (srcpixelmap == nullptr || dstpixelmap == nullptr) { + return IMAGE_BAD_PARAMETER; + } + srcpixelmap->GetInnerPixelmap()->ConvertAlphaFormat(*(dstpixelmap->GetInnerPixelmap()), isPremul); + return IMAGE_SUCCESS; +} + #ifdef __cplusplus }; #endif \ No newline at end of file diff --git a/frameworks/kits/js/common/pixelmap_ndk/pixelmap_native_impl.cpp b/frameworks/kits/js/common/pixelmap_ndk/pixelmap_native_impl.cpp index 4f93dc56e..421b5ecd4 100644 --- a/frameworks/kits/js/common/pixelmap_ndk/pixelmap_native_impl.cpp +++ b/frameworks/kits/js/common/pixelmap_ndk/pixelmap_native_impl.cpp @@ -36,6 +36,12 @@ OH_PixelmapNative::OH_PixelmapNative(const uint32_t *colors, uint32_t colorLengt pixelmap_ = std::move(tmpPixelmap); } +OH_PixelmapNative::OH_PixelmapNative(const InitializationOptions &opts) +{ + auto tmpPixelmap = PixelMap::Create(opts); + pixelmap_ = std::move(tmpPixelmap); +} + OH_PixelmapNative::OH_PixelmapNative(OH_PixelmapNative *OH_PixelmapNative, const InitializationOptions &opts) { if (OH_PixelmapNative == nullptr) { diff --git a/interfaces/kits/native/include/image/pixelmap_native.h b/interfaces/kits/native/include/image/pixelmap_native.h index c54cc57d1..93b8e2c8d 100644 --- a/interfaces/kits/native/include/image/pixelmap_native.h +++ b/interfaces/kits/native/include/image/pixelmap_native.h @@ -67,6 +67,10 @@ typedef enum { * Premultiplied format */ PIXELMAP_ALPHA_TYPE_PREMULTIPLIED = 2, + /* + * UnPremultiplied format + */ + PIXELMAP_ALPHA_TYPE_UNPREMULTIPLIED = 3, }PIXELMAP_ALPHA_TYPE; typedef enum { @@ -191,6 +195,28 @@ Image_ErrorCode OH_PixelmapInitializationOptions_GetPixelFormat(OH_Pixelmap_Init Image_ErrorCode OH_PixelmapInitializationOptions_SetPixelFormat(OH_Pixelmap_InitializationOptions *options, int32_t pixelFormat); +/** + * @brief Get pixelFormat number for InitializationOtions struct. + * + * @param options The InitializationOtions pointer will be operated. + * @param srcpixelFormat the number of image srcpixelFormat. + * @return Returns {@link Image_ErrorCode} + * @since 12 + */ +Image_ErrorCode OH_PixelmapInitializationOptions_GetSrcPixelFormat(OH_Pixelmap_InitializationOptions *options, + int32_t *srcpixelFormat); + +/** + * @brief Set pixelFormat number for InitializationOtions struct. + * + * @param options The InitializationOtions pointer will be operated. + * @param srcpixelFormat the number of image srcpixelFormat. + * @return Returns {@link Image_ErrorCode} + * @since 12 + */ +Image_ErrorCode OH_PixelmapInitializationOptions_SetSrcPixelFormat(OH_Pixelmap_InitializationOptions *options, + int32_t srcpixelFormat); + /** * @brief Get alphaType number for InitializationOtions struct. * @@ -438,6 +464,29 @@ Image_ErrorCode OH_PixelmapNative_Crop(OH_PixelmapNative *pixelmap, Image_Region */ Image_ErrorCode OH_PixelmapNative_Release(OH_PixelmapNative *pixelmap); +/** + * @brief Converting images to alpha format + * + * @param srcpixelmap The source pixel map pointer will be operated. + * @param dstpixelmap The destination pixel map pointer will be operated. + * @param isPremul Whether it is pre-multiplied, true for prediction, false for non-pre-multiplied. + * @return Returns {@link Image_ErrorCode} + * @since 12 + */ +Image_ErrorCode OH_PixelmapNative_ConvertAlphaFormat(OH_PixelmapNative* srcpixelmap, + OH_PixelmapNative* dstpixelmap, const bool isPremul); + +/** + * @brief Create a empty PixelMap object. + * + * @param options IPixel properties, including the alpha type, size, pixel format, and editable. + * @param pixelmap Pixelmap pointer for created. + * @return Returns {@link Image_ErrorCode} + * @since 12 + */ +Image_ErrorCode OH_PixelmapNative_CreateEmptyPixelmap(OH_Pixelmap_InitializationOptions *options, + OH_PixelmapNative **pixelmap); + #ifdef __cplusplus }; #endif -- Gitee