diff --git a/frameworks/kits/js/common/image_packer_napi.cpp b/frameworks/kits/js/common/image_packer_napi.cpp index 4bff9e800..3272270c8 100644 --- a/frameworks/kits/js/common/image_packer_napi.cpp +++ b/frameworks/kits/js/common/image_packer_napi.cpp @@ -108,7 +108,6 @@ struct ImagePackerAsyncContext { int64_t packedSize = 0; int fd = INVALID_FD; ImagePackerError error; - bool needReturnErrorCode = true; }; struct PackingOption { @@ -205,6 +204,16 @@ static void CommonCallbackRoutine(napi_env env, ImagePackerAsyncContext* &connec connect = nullptr; } +static void BuildMsgOnError(ImagePackerAsyncContext* ctx, bool assertion, const std::string msg) +{ + if (ctx == nullptr || assertion) { + return; + } + IMAGE_LOGE("%{public}s", msg.c_str()); + ctx->error.hasErrorCode = false; + ctx->error.msg = msg; +} + static void BuildMsgOnError(ImagePackerAsyncContext* ctx, bool assertion, const std::string msg, int32_t errorCode) { @@ -212,7 +221,7 @@ static void BuildMsgOnError(ImagePackerAsyncContext* ctx, bool assertion, return; } IMAGE_LOGE("%{public}s", msg.c_str()); - ctx->error.hasErrorCode = ctx->needReturnErrorCode; + ctx->error.hasErrorCode = true; ctx->error.errorCode = errorCode; ctx->error.msg = msg; } @@ -263,7 +272,7 @@ bool SetPicture(ImagePackerAsyncContext *context) IMAGE_LOGD("ImagePacker set picture"); if (context->rPicture == nullptr) { BuildMsgOnError(context, context->rImageSource == nullptr, - "Picture is nullptr", COMMON_ERR_INVALID_PARAMETER); + "Picture is nullptr", ERR_IMAGE_INVALID_PARAMETER); return false; } context->rImagePacker->AddPicture(*(context->rPicture)); @@ -275,7 +284,7 @@ bool SetArrayPixel(ImagePackerAsyncContext *context) { IMAGE_LOGD("ImagePacker set pixelmap array"); if (!context->rPixelMaps) { - BuildMsgOnError(context, !context->rPixelMaps, "Pixelmap is nullptr", COMMON_ERR_INVALID_PARAMETER); + BuildMsgOnError(context, !context->rPixelMaps, "Pixelmap is nullptr", ERR_IMAGE_INVALID_PARAMETER); return false; } for (auto &pixelMap : *context->rPixelMaps.get()) { @@ -292,8 +301,7 @@ STATIC_EXEC_FUNC(Packing) context->resultBuffer = std::make_unique( (context->resultBufferSize <= 0) ? getDefaultBufferSize(context) : context->resultBufferSize); if (context->resultBuffer == nullptr) { - BuildMsgOnError(context, context->resultBuffer == nullptr, "ImagePacker buffer alloc error", - ERR_IMAGE_ENCODE_FAILED); + BuildMsgOnError(context, context->resultBuffer == nullptr, "ImagePacker buffer alloc error"); return; } context->rImagePacker->StartPacking(context->resultBuffer.get(), @@ -301,16 +309,14 @@ STATIC_EXEC_FUNC(Packing) if (context->packType == TYPE_IMAGE_SOURCE) { IMAGE_LOGI("ImagePacker set image source"); if (context->rImageSource == nullptr) { - BuildMsgOnError(context, context->rImageSource == nullptr, "ImageSource is nullptr", - COMMON_ERR_INVALID_PARAMETER); + BuildMsgOnError(context, context->rImageSource == nullptr, "ImageSource is nullptr"); return; } context->rImagePacker->AddImage(*(context->rImageSource)); } else if (context->packType == TYPE_PIXEL_MAP) { IMAGE_LOGD("ImagePacker set pixelmap"); if (context->rPixelMap == nullptr) { - BuildMsgOnError(context, context->rImageSource == nullptr, "Pixelmap is nullptr", - COMMON_ERR_INVALID_PARAMETER); + BuildMsgOnError(context, context->rImageSource == nullptr, "Pixelmap is nullptr"); return; } context->rImagePacker->AddImage(*(context->rPixelMap)); @@ -325,20 +331,14 @@ STATIC_EXEC_FUNC(Packing) return; } } - auto packRes = context->rImagePacker->FinalizePacking(packedSize); + context->rImagePacker->FinalizePacking(packedSize); IMAGE_LOGD("packedSize=%{public}" PRId64, packedSize); - if (packRes == SUCCESS) { + if (packedSize > 0 && (packedSize < context->resultBufferSize)) { context->packedSize = packedSize; context->status = SUCCESS; - } else if (packedSize == context->resultBufferSize) { - context->status = ERROR; - BuildMsgOnError(context, packRes == SUCCESS, "output buffer is not enough", ERR_IMAGE_TOO_LARGE); - IMAGE_LOGE("output buffer is not enough."); } else { context->status = ERROR; - BuildMsgOnError(context, packRes == SUCCESS, "packing failed", - packRes == ERR_IMAGE_INVALID_PARAMETER ? COMMON_ERR_INVALID_PARAMETER : packRes); - IMAGE_LOGE("packing failed, packRes is %{public}u", packRes); + IMAGE_LOGE("Packing failed, packedSize outside size."); } } @@ -414,7 +414,6 @@ napi_value ImagePackerNapi::Init(napi_env env, napi_value exports) { napi_property_descriptor props[] = { DECLARE_NAPI_FUNCTION("packing", Packing), - DECLARE_NAPI_FUNCTION("packToData", PackToData), DECLARE_NAPI_FUNCTION("packToFile", PackToFile), DECLARE_NAPI_FUNCTION("packingFromPixelMap", Packing), DECLARE_NAPI_FUNCTION("release", Release), @@ -767,38 +766,37 @@ static void ParserPackingArguments(napi_env env, { int32_t refCount = 1; if (argc < PARAM1 || argc > PARAM3) { - BuildMsgOnError(context, (argc < PARAM1 || argc > PARAM3), "Arguments Count error", - COMMON_ERR_INVALID_PARAMETER); + BuildMsgOnError(context, (argc < PARAM1 || argc > PARAM3), "Arguments Count error"); } context->packType = ParserPackingArgumentType(env, argv[PARAM0]); if (context->packType == TYPE_IMAGE_SOURCE) { context->rImageSource = GetImageSourceFromNapi(env, argv[PARAM0]); - BuildMsgOnError(context, context->rImageSource != nullptr, "ImageSource mismatch", - COMMON_ERR_INVALID_PARAMETER); + BuildMsgOnError(context, context->rImageSource != nullptr, "ImageSource mismatch"); } else if (context->packType == TYPE_PIXEL_MAP) { context->rPixelMap = PixelMapNapi::GetPixelMap(env, argv[PARAM0]); - BuildMsgOnError(context, context->rPixelMap != nullptr, "PixelMap mismatch", COMMON_ERR_INVALID_PARAMETER); + BuildMsgOnError(context, context->rPixelMap != nullptr, "PixelMap mismatch"); #if !defined(IOS_PLATFORM) && !defined(ANDROID_PLATFORM) } else if (context->packType == TYPE_PICTURE) { context->rPicture = PictureNapi::GetPicture(env, argv[PARAM0]); - BuildMsgOnError(context, context->rPicture != nullptr, "Picture mismatch", COMMON_ERR_INVALID_PARAMETER); + BuildMsgOnError(context, context->rPicture != nullptr, "Picture mismatch"); #endif } else if (context->packType == TYPE_ARRAY) { context->rPixelMaps = PixelMapNapi::GetPixelMaps(env, argv[PARAM0]); - BuildMsgOnError(context, context->rPixelMaps != nullptr, "PixelMap mismatch", COMMON_ERR_INVALID_PARAMETER); + BuildMsgOnError(context, context->rPixelMaps != nullptr, + "PixelMap mismatch", ERR_IMAGE_INVALID_PARAMETER); } if (argc > PARAM1 && ImageNapiUtils::getType(env, argv[PARAM1]) == napi_object) { if (context->packType == TYPE_ARRAY) { BuildMsgOnError(context, parsePackOptionOfLoop(env, argv[PARAM1], &(context->packOption)), - "PackOptions mismatch", COMMON_ERR_INVALID_PARAMETER); + "PackOptions mismatch", ERR_IMAGE_INVALID_PARAMETER); context->resultBufferSize = parseBufferSize(env, argv[PARAM1]); BuildMsgOnError(context, parsePackOptionOfdisposalTypes(env, argv[PARAM1], &(context->packOption)), - "PackOptions mismatch", COMMON_ERR_INVALID_PARAMETER); + "PackOptions mismatch", ERR_IMAGE_INVALID_PARAMETER); } else { - BuildMsgOnError(context, parsePackOptions(env, argv[PARAM1], &(context->packOption)), - "PackOptions mismatch", COMMON_ERR_INVALID_PARAMETER); + BuildMsgOnError(context, + parsePackOptions(env, argv[PARAM1], &(context->packOption)), "PackOptions mismatch"); context->resultBufferSize = parseBufferSize(env, argv[PARAM1], context); } } @@ -807,7 +805,7 @@ static void ParserPackingArguments(napi_env env, } } -napi_value ImagePackerNapi::Packing(napi_env env, napi_callback_info info, bool needReturnError) +napi_value ImagePackerNapi::Packing(napi_env env, napi_callback_info info) { ImageTrace imageTrace("ImagePackerNapi::Packing"); napi_status status; @@ -824,7 +822,6 @@ napi_value ImagePackerNapi::Packing(napi_env env, napi_callback_info info, bool std::unique_ptr asyncContext = std::make_unique(); status = napi_unwrap(env, thisVar, reinterpret_cast(&asyncContext->constructor_)); NAPI_ASSERT(env, IMG_IS_READY(status, asyncContext->constructor_), "fail to unwrap constructor_"); - asyncContext->needReturnErrorCode = needReturnError; asyncContext->rImagePacker = asyncContext->constructor_->nativeImgPck; ParserPackingArguments(env, argv, argc, asyncContext.get()); @@ -847,16 +844,6 @@ napi_value ImagePackerNapi::Packing(napi_env env, napi_callback_info info, bool return result; } -napi_value ImagePackerNapi::Packing(napi_env env, napi_callback_info info) -{ - return Packing(env, info, false); -} - -napi_value ImagePackerNapi::PackToData(napi_env env, napi_callback_info info) -{ - return Packing(env, info, true); -} - napi_value ImagePackerNapi::GetSupportedFormats(napi_env env, napi_callback_info info) { napi_value result = nullptr; @@ -945,43 +932,45 @@ static void ParserPackToFileArguments(napi_env env, { int32_t refCount = 1; if (argc < PARAM1 || argc > PARAM4) { - BuildMsgOnError(context, (argc < PARAM1 || argc > PARAM4), "Arguments Count error", - COMMON_ERR_INVALID_PARAMETER); + BuildMsgOnError(context, (argc < PARAM1 || argc > PARAM4), + "Arguments Count error", ERR_IMAGE_INVALID_PARAMETER); } context->packType = ParserPackingArgumentType(env, argv[PARAM0]); if (context->packType == TYPE_IMAGE_SOURCE) { context->rImageSource = GetImageSourceFromNapi(env, argv[PARAM0]); - BuildMsgOnError(context, context->rImageSource != nullptr, "ImageSource mismatch", - COMMON_ERR_INVALID_PARAMETER); + BuildMsgOnError(context, context->rImageSource != nullptr, + "ImageSource mismatch", ERR_IMAGE_INVALID_PARAMETER); } else if (context->packType == TYPE_PIXEL_MAP) { context->rPixelMap = PixelMapNapi::GetPixelMap(env, argv[PARAM0]); - BuildMsgOnError(context, context->rPixelMap != nullptr, "PixelMap mismatch", COMMON_ERR_INVALID_PARAMETER); + BuildMsgOnError(context, context->rPixelMap != nullptr, + "PixelMap mismatch", ERR_IMAGE_INVALID_PARAMETER); #if !defined(IOS_PLATFORM) && !defined(ANDROID_PLATFORM) } else if (context->packType == TYPE_PICTURE) { context->rPicture = PictureNapi::GetPicture(env, argv[PARAM0]); BuildMsgOnError(context, context->rPicture != nullptr, - "Picture mismatch", COMMON_ERR_INVALID_PARAMETER); + "Picture mismatch", ERR_IMAGE_INVALID_PARAMETER); #endif } else if (context->packType == TYPE_ARRAY) { context->rPixelMaps = PixelMapNapi::GetPixelMaps(env, argv[PARAM0]); BuildMsgOnError(context, context->rPixelMaps != nullptr, - "PixelMap mismatch", COMMON_ERR_INVALID_PARAMETER); + "PixelMap mismatch", ERR_IMAGE_INVALID_PARAMETER); } if (argc > PARAM1 && ImageNapiUtils::getType(env, argv[PARAM1]) == napi_number) { BuildMsgOnError(context, (napi_get_value_int32(env, argv[PARAM1], &(context->fd)) == napi_ok && - context->fd > INVALID_FD), "fd mismatch", COMMON_ERR_INVALID_PARAMETER); + context->fd > INVALID_FD), "fd mismatch", ERR_IMAGE_INVALID_PARAMETER); } if (argc > PARAM2 && ImageNapiUtils::getType(env, argv[PARAM2]) == napi_object) { if (context->packType == TYPE_ARRAY) { BuildMsgOnError(context, parsePackOptionOfLoop(env, argv[PARAM2], &(context->packOption)), - "PackOptions mismatch", COMMON_ERR_INVALID_PARAMETER); + "PackOptions mismatch", ERR_IMAGE_INVALID_PARAMETER); BuildMsgOnError(context, parsePackOptionOfdisposalTypes(env, argv[PARAM2], &(context->packOption)), - "PackOptions mismatch", COMMON_ERR_INVALID_PARAMETER); + "PackOptions mismatch", ERR_IMAGE_INVALID_PARAMETER); } else { - BuildMsgOnError(context, parsePackOptions(env, argv[PARAM2], &(context->packOption)), - "PackOptions mismatch", COMMON_ERR_INVALID_PARAMETER); + BuildMsgOnError(context, + parsePackOptions(env, argv[PARAM2], &(context->packOption)), + "PackOptions mismatch", ERR_IMAGE_INVALID_PARAMETER); } } if (argc > PARAM3 && ImageNapiUtils::getType(env, argv[PARAM3]) == napi_function) { @@ -994,27 +983,28 @@ STATIC_EXEC_FUNC(PackToFile) int64_t packedSize = 0; auto context = static_cast(data); if (context->fd <= INVALID_FD) { - BuildMsgOnError(context, context->fd <= INVALID_FD, "ImagePacker invalid fd", COMMON_ERR_INVALID_PARAMETER); + BuildMsgOnError(context, context->fd <= INVALID_FD, + "ImagePacker invalid fd", ERR_IMAGE_INVALID_PARAMETER); return; } auto startRes = context->rImagePacker->StartPacking(context->fd, context->packOption); if (startRes != SUCCESS) { context->status = ERROR; - BuildMsgOnError(context, startRes == SUCCESS, "Start packing failed", - startRes == ERR_IMAGE_INVALID_PARAMETER ? COMMON_ERR_INVALID_PARAMETER : startRes); + BuildMsgOnError(context, startRes == SUCCESS, "Start packing failed", startRes); return; } - IMAGE_LOGD("ImagePacker packType is %{public}d", context->packType); if (context->packType == TYPE_IMAGE_SOURCE) { + IMAGE_LOGD("ImagePacker set image source"); if (!context->rImageSource) { - BuildMsgOnError(context, !context->rImageSource, "ImageSource is nullptr", COMMON_ERR_INVALID_PARAMETER); + BuildMsgOnError(context, !context->rImageSource, "ImageSource is nullptr", ERR_IMAGE_INVALID_PARAMETER); return; } context->rImagePacker->AddImage(*(context->rImageSource)); } else if (context->packType == TYPE_PIXEL_MAP) { + IMAGE_LOGD("ImagePacker set pixelmap"); if (!context->rPixelMap) { - BuildMsgOnError(context, !context->rImageSource, "Pixelmap is nullptr", COMMON_ERR_INVALID_PARAMETER); + BuildMsgOnError(context, !context->rImageSource, "Pixelmap is nullptr", ERR_IMAGE_INVALID_PARAMETER); return; } context->rImagePacker->AddImage(*(context->rPixelMap)); @@ -1036,8 +1026,7 @@ STATIC_EXEC_FUNC(PackToFile) context->status = SUCCESS; } else { context->status = ERROR; - BuildMsgOnError(context, packRes == SUCCESS, "PackedSize outside size", - packRes == ERR_IMAGE_INVALID_PARAMETER ? COMMON_ERR_INVALID_PARAMETER : packRes); + BuildMsgOnError(context, packRes == SUCCESS, "PackedSize outside size", packRes); IMAGE_LOGE("Packing failed, packedSize outside size."); } } diff --git a/interfaces/kits/js/common/include/image_packer_napi.h b/interfaces/kits/js/common/include/image_packer_napi.h index d3c32dad4..43b2dc465 100644 --- a/interfaces/kits/js/common/include/image_packer_napi.h +++ b/interfaces/kits/js/common/include/image_packer_napi.h @@ -50,8 +50,6 @@ private: static napi_value Constructor(napi_env env, napi_callback_info info); static void Destructor(napi_env env, void *nativeObject, void *finalize); static napi_value Packing(napi_env env, napi_callback_info info); - static napi_value PackToData(napi_env env, napi_callback_info info); - static napi_value Packing(napi_env env, napi_callback_info info, bool needReturnError); static napi_value PackingFromPixelMap(napi_env env, napi_callback_info info); static napi_value PackToFile(napi_env env, napi_callback_info info); static napi_value Release(napi_env env, napi_callback_info info);