diff --git a/examples/ultrahdr_app.cpp b/examples/ultrahdr_app.cpp index d279a988..3497a8fc 100644 --- a/examples/ultrahdr_app.cpp +++ b/examples/ultrahdr_app.cpp @@ -111,7 +111,7 @@ class Profiler { void timerStop() { QueryPerformanceCounter(&mEndingTime); } - int64_t elapsedTime() { + double elapsedTime() { LARGE_INTEGER frequency; LARGE_INTEGER elapsedMicroseconds; QueryPerformanceFrequency(&frequency); @@ -151,10 +151,15 @@ class Profiler { return false; \ } -static bool loadFile(const char* filename, void*& result, int length) { +static bool loadFile(const char* filename, void*& result, std::streamoff length) { + if (length <= 0) { + std::cerr << "requested to read invalid length : " << length + << " bytes from file : " << filename << std::endl; + return false; + } std::ifstream ifd(filename, std::ios::binary | std::ios::ate); if (ifd.good()) { - int size = ifd.tellg(); + auto size = ifd.tellg(); if (size < length) { std::cerr << "requested to read " << length << " bytes from file : " << filename << ", file contains only " << size << " bytes" << std::endl; @@ -178,23 +183,23 @@ static bool loadFile(const char* filename, uhdr_raw_image_t* handle) { std::ifstream ifd(filename, std::ios::binary); if (ifd.good()) { if (handle->fmt == UHDR_IMG_FMT_24bppYCbCrP010) { - const int bpp = 2; - READ_BYTES(ifd, handle->planes[UHDR_PLANE_Y], handle->w * handle->h * bpp) - READ_BYTES(ifd, handle->planes[UHDR_PLANE_UV], (handle->w / 2) * (handle->h / 2) * bpp * 2) + const size_t bpp = 2; + READ_BYTES(ifd, handle->planes[UHDR_PLANE_Y], bpp * handle->w * handle->h) + READ_BYTES(ifd, handle->planes[UHDR_PLANE_UV], bpp * (handle->w / 2) * (handle->h / 2) * 2) return true; } else if (handle->fmt == UHDR_IMG_FMT_32bppRGBA1010102 || handle->fmt == UHDR_IMG_FMT_32bppRGBA8888) { - const int bpp = 4; - READ_BYTES(ifd, handle->planes[UHDR_PLANE_PACKED], handle->w * handle->h * bpp) + const size_t bpp = 4; + READ_BYTES(ifd, handle->planes[UHDR_PLANE_PACKED], bpp * handle->w * handle->h) return true; } else if (handle->fmt == UHDR_IMG_FMT_64bppRGBAHalfFloat) { - const int bpp = 8; - READ_BYTES(ifd, handle->planes[UHDR_PLANE_PACKED], handle->w * handle->h * bpp) + const size_t bpp = 8; + READ_BYTES(ifd, handle->planes[UHDR_PLANE_PACKED], bpp * handle->w * handle->h) return true; } else if (handle->fmt == UHDR_IMG_FMT_12bppYCbCr420) { - READ_BYTES(ifd, handle->planes[UHDR_PLANE_Y], handle->w * handle->h) - READ_BYTES(ifd, handle->planes[UHDR_PLANE_U], (handle->w / 2) * (handle->h / 2)) - READ_BYTES(ifd, handle->planes[UHDR_PLANE_V], (handle->w / 2) * (handle->h / 2)) + READ_BYTES(ifd, handle->planes[UHDR_PLANE_Y], (size_t)handle->w * handle->h) + READ_BYTES(ifd, handle->planes[UHDR_PLANE_U], (size_t)(handle->w / 2) * (handle->h / 2)) + READ_BYTES(ifd, handle->planes[UHDR_PLANE_V], (size_t)(handle->w / 2) * (handle->h / 2)) return true; } return false; @@ -203,7 +208,7 @@ static bool loadFile(const char* filename, uhdr_raw_image_t* handle) { return false; } -static bool writeFile(const char* filename, void*& result, int length) { +static bool writeFile(const char* filename, void*& result, size_t length) { std::ofstream ofd(filename, std::ios::binary); if (ofd.is_open()) { ofd.write(static_cast(result), length); @@ -219,7 +224,7 @@ static bool writeFile(const char* filename, uhdr_raw_image_t* img) { if (img->fmt == UHDR_IMG_FMT_32bppRGBA8888 || img->fmt == UHDR_IMG_FMT_64bppRGBAHalfFloat || img->fmt == UHDR_IMG_FMT_32bppRGBA1010102) { char* data = static_cast(img->planes[UHDR_PLANE_PACKED]); - int bpp = img->fmt == UHDR_IMG_FMT_64bppRGBAHalfFloat ? 8 : 4; + const size_t bpp = img->fmt == UHDR_IMG_FMT_64bppRGBAHalfFloat ? 8 : 4; const size_t stride = img->stride[UHDR_PLANE_PACKED] * bpp; const size_t length = img->w * bpp; for (unsigned i = 0; i < img->h; i++, data += stride) { @@ -229,7 +234,7 @@ static bool writeFile(const char* filename, uhdr_raw_image_t* img) { } else if ((int)img->fmt == UHDR_IMG_FMT_24bppYCbCr444 || (int)img->fmt == UHDR_IMG_FMT_48bppYCbCr444) { char* data = static_cast(img->planes[UHDR_PLANE_Y]); - int bpp = (int)img->fmt == UHDR_IMG_FMT_48bppYCbCr444 ? 2 : 1; + const size_t bpp = (int)img->fmt == UHDR_IMG_FMT_48bppYCbCr444 ? 2 : 1; size_t stride = img->stride[UHDR_PLANE_Y] * bpp; size_t length = img->w * bpp; for (unsigned i = 0; i < img->h; i++, data += stride) { @@ -255,19 +260,21 @@ static bool writeFile(const char* filename, uhdr_raw_image_t* img) { class UltraHdrAppInput { public: - UltraHdrAppInput( - const char* hdrIntentRawFile, const char* sdrIntentRawFile, - const char* sdrIntentCompressedFile, const char* gainmapCompressedFile, - const char* gainmapMetadataCfgFile, const char* exifFile, const char* outputFile, - size_t width, size_t height, uhdr_img_fmt_t hdrCf = UHDR_IMG_FMT_32bppRGBA1010102, - uhdr_img_fmt_t sdrCf = UHDR_IMG_FMT_32bppRGBA8888, - uhdr_color_gamut_t hdrCg = UHDR_CG_DISPLAY_P3, uhdr_color_gamut_t sdrCg = UHDR_CG_BT_709, - uhdr_color_transfer_t hdrTf = UHDR_CT_HLG, int quality = 95, - uhdr_color_transfer_t oTf = UHDR_CT_HLG, uhdr_img_fmt_t oFmt = UHDR_IMG_FMT_32bppRGBA1010102, - bool isHdrCrFull = false, int gainmapScaleFactor = 1, int gainmapQuality = 95, - bool enableMultiChannelGainMap = true, float gamma = 1.0f, bool enableGLES = false, - uhdr_enc_preset_t encPreset = UHDR_USAGE_BEST_QUALITY, float minContentBoost = FLT_MIN, - float maxContentBoost = FLT_MAX, float targetDispPeakBrightness = -1.0f) + UltraHdrAppInput(const char* hdrIntentRawFile, const char* sdrIntentRawFile, + const char* sdrIntentCompressedFile, const char* gainmapCompressedFile, + const char* gainmapMetadataCfgFile, const char* exifFile, const char* outputFile, + int width, int height, uhdr_img_fmt_t hdrCf = UHDR_IMG_FMT_32bppRGBA1010102, + uhdr_img_fmt_t sdrCf = UHDR_IMG_FMT_32bppRGBA8888, + uhdr_color_gamut_t hdrCg = UHDR_CG_DISPLAY_P3, + uhdr_color_gamut_t sdrCg = UHDR_CG_BT_709, + uhdr_color_transfer_t hdrTf = UHDR_CT_HLG, int quality = 95, + uhdr_color_transfer_t oTf = UHDR_CT_HLG, + uhdr_img_fmt_t oFmt = UHDR_IMG_FMT_32bppRGBA1010102, bool isHdrCrFull = false, + int gainmapScaleFactor = 1, int gainmapQuality = 95, + bool enableMultiChannelGainMap = true, float gamma = 1.0f, + bool enableGLES = false, uhdr_enc_preset_t encPreset = UHDR_USAGE_BEST_QUALITY, + float minContentBoost = FLT_MIN, float maxContentBoost = FLT_MAX, + float targetDispPeakBrightness = -1.0f) : mHdrIntentRawFile(hdrIntentRawFile), mSdrIntentRawFile(sdrIntentRawFile), mSdrIntentCompressedFile(sdrIntentCompressedFile), @@ -319,7 +326,7 @@ class UltraHdrAppInput { mQuality(95), mOTf(oTf), mOfmt(oFmt), - mFullRange(UHDR_CR_UNSPECIFIED), + mFullRange(false), mMapDimensionScaleFactor(1), mMapCompressQuality(95), mUseMultiChannelGainMap(true), @@ -408,7 +415,7 @@ class UltraHdrAppInput { const uhdr_color_transfer_t mOTf; const uhdr_img_fmt_t mOfmt; const bool mFullRange; - const size_t mMapDimensionScaleFactor; + const int mMapDimensionScaleFactor; const int mMapCompressQuality; const bool mUseMultiChannelGainMap; const float mGamma; @@ -435,8 +442,8 @@ class UltraHdrAppInput { }; bool UltraHdrAppInput::fillP010ImageHandle() { - const int bpp = 2; - int p010Size = mWidth * mHeight * bpp * 1.5; + const size_t bpp = 2; + size_t p010Size = bpp * mWidth * mHeight * 3 / 2; mRawP010Image.fmt = UHDR_IMG_FMT_24bppYCbCrP010; mRawP010Image.cg = mHdrCg; mRawP010Image.ct = mHdrTf; @@ -444,8 +451,8 @@ bool UltraHdrAppInput::fillP010ImageHandle() { mRawP010Image.range = mFullRange ? UHDR_CR_FULL_RANGE : UHDR_CR_LIMITED_RANGE; mRawP010Image.w = mWidth; mRawP010Image.h = mHeight; - mRawP010Image.planes[UHDR_PLANE_Y] = malloc(mWidth * mHeight * bpp); - mRawP010Image.planes[UHDR_PLANE_UV] = malloc((mWidth / 2) * (mHeight / 2) * bpp * 2); + mRawP010Image.planes[UHDR_PLANE_Y] = malloc(bpp * mWidth * mHeight); + mRawP010Image.planes[UHDR_PLANE_UV] = malloc(bpp * (mWidth / 2) * (mHeight / 2) * 2); mRawP010Image.planes[UHDR_PLANE_V] = nullptr; mRawP010Image.stride[UHDR_PLANE_Y] = mWidth; mRawP010Image.stride[UHDR_PLANE_UV] = mWidth; @@ -454,16 +461,16 @@ bool UltraHdrAppInput::fillP010ImageHandle() { } bool UltraHdrAppInput::fillYuv420ImageHandle() { - int yuv420Size = mWidth * mHeight * 1.5; + size_t yuv420Size = (size_t)mWidth * mHeight * 3 / 2; mRawYuv420Image.fmt = UHDR_IMG_FMT_12bppYCbCr420; mRawYuv420Image.cg = mSdrCg; mRawYuv420Image.ct = UHDR_CT_SRGB; mRawYuv420Image.range = UHDR_CR_FULL_RANGE; mRawYuv420Image.w = mWidth; mRawYuv420Image.h = mHeight; - mRawYuv420Image.planes[UHDR_PLANE_Y] = malloc(mWidth * mHeight); - mRawYuv420Image.planes[UHDR_PLANE_U] = malloc((mWidth / 2) * (mHeight / 2)); - mRawYuv420Image.planes[UHDR_PLANE_V] = malloc((mWidth / 2) * (mHeight / 2)); + mRawYuv420Image.planes[UHDR_PLANE_Y] = malloc((size_t)mWidth * mHeight); + mRawYuv420Image.planes[UHDR_PLANE_U] = malloc((size_t)(mWidth / 2) * (mHeight / 2)); + mRawYuv420Image.planes[UHDR_PLANE_V] = malloc((size_t)(mWidth / 2) * (mHeight / 2)); mRawYuv420Image.stride[UHDR_PLANE_Y] = mWidth; mRawYuv420Image.stride[UHDR_PLANE_U] = mWidth / 2; mRawYuv420Image.stride[UHDR_PLANE_V] = mWidth / 2; @@ -471,14 +478,14 @@ bool UltraHdrAppInput::fillYuv420ImageHandle() { } bool UltraHdrAppInput::fillRGBA1010102ImageHandle() { - const int bpp = 4; + const size_t bpp = 4; mRawRgba1010102Image.fmt = UHDR_IMG_FMT_32bppRGBA1010102; mRawRgba1010102Image.cg = mHdrCg; mRawRgba1010102Image.ct = mHdrTf; mRawRgba1010102Image.range = UHDR_CR_FULL_RANGE; mRawRgba1010102Image.w = mWidth; mRawRgba1010102Image.h = mHeight; - mRawRgba1010102Image.planes[UHDR_PLANE_PACKED] = malloc(mWidth * mHeight * bpp); + mRawRgba1010102Image.planes[UHDR_PLANE_PACKED] = malloc(bpp * mWidth * mHeight); mRawRgba1010102Image.planes[UHDR_PLANE_UV] = nullptr; mRawRgba1010102Image.planes[UHDR_PLANE_V] = nullptr; mRawRgba1010102Image.stride[UHDR_PLANE_PACKED] = mWidth; @@ -488,14 +495,14 @@ bool UltraHdrAppInput::fillRGBA1010102ImageHandle() { } bool UltraHdrAppInput::fillRGBAF16ImageHandle() { - const int bpp = 8; + const size_t bpp = 8; mRawRgbaF16Image.fmt = UHDR_IMG_FMT_64bppRGBAHalfFloat; mRawRgbaF16Image.cg = mHdrCg; mRawRgbaF16Image.ct = mHdrTf; mRawRgbaF16Image.range = UHDR_CR_FULL_RANGE; mRawRgbaF16Image.w = mWidth; mRawRgbaF16Image.h = mHeight; - mRawRgbaF16Image.planes[UHDR_PLANE_PACKED] = malloc(mWidth * mHeight * bpp); + mRawRgbaF16Image.planes[UHDR_PLANE_PACKED] = malloc(bpp * mWidth * mHeight); mRawRgbaF16Image.planes[UHDR_PLANE_UV] = nullptr; mRawRgbaF16Image.planes[UHDR_PLANE_V] = nullptr; mRawRgbaF16Image.stride[UHDR_PLANE_PACKED] = mWidth; @@ -505,14 +512,14 @@ bool UltraHdrAppInput::fillRGBAF16ImageHandle() { } bool UltraHdrAppInput::fillRGBA8888ImageHandle() { - const int bpp = 4; + const size_t bpp = 4; mRawRgba8888Image.fmt = UHDR_IMG_FMT_32bppRGBA8888; mRawRgba8888Image.cg = mSdrCg; mRawRgba8888Image.ct = UHDR_CT_SRGB; mRawRgba8888Image.range = UHDR_CR_FULL_RANGE; mRawRgba8888Image.w = mWidth; mRawRgba8888Image.h = mHeight; - mRawRgba8888Image.planes[UHDR_PLANE_PACKED] = malloc(mWidth * mHeight * bpp); + mRawRgba8888Image.planes[UHDR_PLANE_PACKED] = malloc(bpp * mWidth * mHeight); mRawRgba8888Image.planes[UHDR_PLANE_U] = nullptr; mRawRgba8888Image.planes[UHDR_PLANE_V] = nullptr; mRawRgba8888Image.stride[UHDR_PLANE_Y] = mWidth; @@ -524,7 +531,7 @@ bool UltraHdrAppInput::fillRGBA8888ImageHandle() { bool UltraHdrAppInput::fillSdrCompressedImageHandle() { std::ifstream ifd(mSdrIntentCompressedFile, std::ios::binary | std::ios::ate); if (ifd.good()) { - int size = ifd.tellg(); + auto size = ifd.tellg(); mSdrIntentCompressedImage.capacity = size; mSdrIntentCompressedImage.data_sz = size; mSdrIntentCompressedImage.data = nullptr; @@ -540,7 +547,7 @@ bool UltraHdrAppInput::fillSdrCompressedImageHandle() { bool UltraHdrAppInput::fillGainMapCompressedImageHandle() { std::ifstream ifd(mGainMapCompressedFile, std::ios::binary | std::ios::ate); if (ifd.good()) { - int size = ifd.tellg(); + auto size = ifd.tellg(); mGainMapCompressedImage.capacity = size; mGainMapCompressedImage.data_sz = size; mGainMapCompressedImage.data = nullptr; @@ -592,7 +599,7 @@ bool UltraHdrAppInput::fillGainMapMetadataDescriptor() { bool UltraHdrAppInput::fillExifMemoryBlock() { std::ifstream ifd(mExifFile, std::ios::binary | std::ios::ate); if (ifd.good()) { - int size = ifd.tellg(); + auto size = ifd.tellg(); ifd.close(); return loadFile(mExifFile, mExifBlock.data, size); } @@ -618,7 +625,7 @@ bool UltraHdrAppInput::writeGainMapMetadataToFile(uhdr_gainmap_metadata_t* metad bool UltraHdrAppInput::fillUhdrImageHandle() { std::ifstream ifd(mUhdrFile, std::ios::binary | std::ios::ate); if (ifd.good()) { - int size = ifd.tellg(); + auto size = ifd.tellg(); mUhdrImage.capacity = size; mUhdrImage.data_sz = size; mUhdrImage.data = nullptr; @@ -831,8 +838,8 @@ bool UltraHdrAppInput::decode() { mDecodedUhdrRgbImage.range = output->range; mDecodedUhdrRgbImage.w = output->w; mDecodedUhdrRgbImage.h = output->h; - int bpp = (output->fmt == UHDR_IMG_FMT_64bppRGBAHalfFloat) ? 8 : 4; - mDecodedUhdrRgbImage.planes[UHDR_PLANE_PACKED] = malloc(output->w * output->h * bpp); + size_t bpp = (output->fmt == UHDR_IMG_FMT_64bppRGBAHalfFloat) ? 8 : 4; + mDecodedUhdrRgbImage.planes[UHDR_PLANE_PACKED] = malloc(bpp * output->w * output->h); char* inData = static_cast(output->planes[UHDR_PLANE_PACKED]); char* outData = static_cast(mDecodedUhdrRgbImage.planes[UHDR_PLANE_PACKED]); const size_t inStride = output->stride[UHDR_PLANE_PACKED] * bpp; @@ -861,13 +868,14 @@ bool UltraHdrAppInput::convertP010ToRGBImage() { << std::endl; } + size_t bpp = 4; mRawRgba1010102Image.fmt = UHDR_IMG_FMT_32bppRGBA1010102; mRawRgba1010102Image.cg = mRawP010Image.cg; mRawRgba1010102Image.ct = mRawP010Image.ct; mRawRgba1010102Image.range = UHDR_CR_FULL_RANGE; mRawRgba1010102Image.w = mRawP010Image.w; mRawRgba1010102Image.h = mRawP010Image.h; - mRawRgba1010102Image.planes[UHDR_PLANE_PACKED] = malloc(mRawP010Image.w * mRawP010Image.h * 4); + mRawRgba1010102Image.planes[UHDR_PLANE_PACKED] = malloc(bpp * mRawP010Image.w * mRawP010Image.h); mRawRgba1010102Image.planes[UHDR_PLANE_U] = nullptr; mRawRgba1010102Image.planes[UHDR_PLANE_V] = nullptr; mRawRgba1010102Image.stride[UHDR_PLANE_PACKED] = mWidth; @@ -927,13 +935,14 @@ bool UltraHdrAppInput::convertP010ToRGBImage() { } bool UltraHdrAppInput::convertYuv420ToRGBImage() { + size_t bpp = 4; mRawRgba8888Image.fmt = UHDR_IMG_FMT_32bppRGBA8888; mRawRgba8888Image.cg = mRawYuv420Image.cg; mRawRgba8888Image.ct = mRawYuv420Image.ct; mRawRgba8888Image.range = UHDR_CR_FULL_RANGE; mRawRgba8888Image.w = mRawYuv420Image.w; mRawRgba8888Image.h = mRawYuv420Image.h; - mRawRgba8888Image.planes[UHDR_PLANE_PACKED] = malloc(mRawYuv420Image.w * mRawYuv420Image.h * 4); + mRawRgba8888Image.planes[UHDR_PLANE_PACKED] = malloc(bpp * mRawYuv420Image.w * mRawYuv420Image.h); mRawRgba8888Image.planes[UHDR_PLANE_U] = nullptr; mRawRgba8888Image.planes[UHDR_PLANE_V] = nullptr; mRawRgba8888Image.stride[UHDR_PLANE_PACKED] = mWidth; @@ -1000,11 +1009,11 @@ bool UltraHdrAppInput::convertRgba8888ToYUV444Image() { mDecodedUhdrYuv444Image.w = mDecodedUhdrRgbImage.w; mDecodedUhdrYuv444Image.h = mDecodedUhdrRgbImage.h; mDecodedUhdrYuv444Image.planes[UHDR_PLANE_Y] = - malloc(mDecodedUhdrRgbImage.w * mDecodedUhdrRgbImage.h); + malloc((size_t)mDecodedUhdrRgbImage.w * mDecodedUhdrRgbImage.h); mDecodedUhdrYuv444Image.planes[UHDR_PLANE_U] = - malloc(mDecodedUhdrRgbImage.w * mDecodedUhdrRgbImage.h); + malloc((size_t)mDecodedUhdrRgbImage.w * mDecodedUhdrRgbImage.h); mDecodedUhdrYuv444Image.planes[UHDR_PLANE_V] = - malloc(mDecodedUhdrRgbImage.w * mDecodedUhdrRgbImage.h); + malloc((size_t)mDecodedUhdrRgbImage.w * mDecodedUhdrRgbImage.h); mDecodedUhdrYuv444Image.stride[UHDR_PLANE_Y] = mWidth; mDecodedUhdrYuv444Image.stride[UHDR_PLANE_U] = mWidth; mDecodedUhdrYuv444Image.stride[UHDR_PLANE_V] = mWidth; @@ -1075,6 +1084,7 @@ bool UltraHdrAppInput::convertRgba1010102ToYUV444Image() { << " using BT2020Matrix" << std::endl; } + size_t bpp = 2; mDecodedUhdrYuv444Image.fmt = static_cast(UHDR_IMG_FMT_48bppYCbCr444); mDecodedUhdrYuv444Image.cg = mDecodedUhdrRgbImage.cg; mDecodedUhdrYuv444Image.ct = mDecodedUhdrRgbImage.ct; @@ -1082,11 +1092,11 @@ bool UltraHdrAppInput::convertRgba1010102ToYUV444Image() { mDecodedUhdrYuv444Image.w = mDecodedUhdrRgbImage.w; mDecodedUhdrYuv444Image.h = mDecodedUhdrRgbImage.h; mDecodedUhdrYuv444Image.planes[UHDR_PLANE_Y] = - malloc(mDecodedUhdrRgbImage.w * mDecodedUhdrRgbImage.h * 2); + malloc(bpp * mDecodedUhdrRgbImage.w * mDecodedUhdrRgbImage.h); mDecodedUhdrYuv444Image.planes[UHDR_PLANE_U] = - malloc(mDecodedUhdrRgbImage.w * mDecodedUhdrRgbImage.h * 2); + malloc(bpp * mDecodedUhdrRgbImage.w * mDecodedUhdrRgbImage.h); mDecodedUhdrYuv444Image.planes[UHDR_PLANE_V] = - malloc(mDecodedUhdrRgbImage.w * mDecodedUhdrRgbImage.h * 2); + malloc(bpp * mDecodedUhdrRgbImage.w * mDecodedUhdrRgbImage.h); mDecodedUhdrYuv444Image.stride[UHDR_PLANE_Y] = mWidth; mDecodedUhdrYuv444Image.stride[UHDR_PLANE_U] = mWidth; mDecodedUhdrYuv444Image.stride[UHDR_PLANE_V] = mWidth; @@ -1164,7 +1174,7 @@ void UltraHdrAppInput::computeRGBHdrPSNR() { << std::endl; } uint64_t rSqError = 0, gSqError = 0, bSqError = 0; - for (size_t i = 0; i < mDecodedUhdrRgbImage.w * mDecodedUhdrRgbImage.h; i++) { + for (size_t i = 0; i < (size_t)mDecodedUhdrRgbImage.w * mDecodedUhdrRgbImage.h; i++) { int rSrc = *rgbDataSrc & 0x3ff; int rDst = *rgbDataDst & 0x3ff; rSqError += (rSrc - rDst) * (rSrc - rDst); @@ -1180,13 +1190,14 @@ void UltraHdrAppInput::computeRGBHdrPSNR() { rgbDataSrc++; rgbDataDst++; } - double meanSquareError = (double)rSqError / (mDecodedUhdrRgbImage.w * mDecodedUhdrRgbImage.h); + double meanSquareError = + (double)rSqError / ((size_t)mDecodedUhdrRgbImage.w * mDecodedUhdrRgbImage.h); mPsnr[0] = meanSquareError ? 10 * log10((double)1023 * 1023 / meanSquareError) : 100; - meanSquareError = (double)gSqError / (mDecodedUhdrRgbImage.w * mDecodedUhdrRgbImage.h); + meanSquareError = (double)gSqError / ((size_t)mDecodedUhdrRgbImage.w * mDecodedUhdrRgbImage.h); mPsnr[1] = meanSquareError ? 10 * log10((double)1023 * 1023 / meanSquareError) : 100; - meanSquareError = (double)bSqError / (mDecodedUhdrRgbImage.w * mDecodedUhdrRgbImage.h); + meanSquareError = (double)bSqError / ((size_t)mDecodedUhdrRgbImage.w * mDecodedUhdrRgbImage.h); mPsnr[2] = meanSquareError ? 10 * log10((double)1023 * 1023 / meanSquareError) : 100; std::cout << "psnr rgb: \t" << mPsnr[0] << " \t " << mPsnr[1] << " \t " << mPsnr[2] << std::endl; @@ -1205,7 +1216,7 @@ void UltraHdrAppInput::computeRGBSdrPSNR() { } uint64_t rSqError = 0, gSqError = 0, bSqError = 0; - for (size_t i = 0; i < mDecodedUhdrRgbImage.w * mDecodedUhdrRgbImage.h; i++) { + for (size_t i = 0; i < (size_t)mDecodedUhdrRgbImage.w * mDecodedUhdrRgbImage.h; i++) { int rSrc = *rgbDataSrc & 0xff; int rDst = *rgbDataDst & 0xff; rSqError += (rSrc - rDst) * (rSrc - rDst); @@ -1221,13 +1232,14 @@ void UltraHdrAppInput::computeRGBSdrPSNR() { rgbDataSrc++; rgbDataDst++; } - double meanSquareError = (double)rSqError / (mDecodedUhdrRgbImage.w * mDecodedUhdrRgbImage.h); + double meanSquareError = + (double)rSqError / ((size_t)mDecodedUhdrRgbImage.w * mDecodedUhdrRgbImage.h); mPsnr[0] = meanSquareError ? 10 * log10((double)255 * 255 / meanSquareError) : 100; - meanSquareError = (double)gSqError / (mDecodedUhdrRgbImage.w * mDecodedUhdrRgbImage.h); + meanSquareError = (double)gSqError / ((size_t)mDecodedUhdrRgbImage.w * mDecodedUhdrRgbImage.h); mPsnr[1] = meanSquareError ? 10 * log10((double)255 * 255 / meanSquareError) : 100; - meanSquareError = (double)bSqError / (mDecodedUhdrRgbImage.w * mDecodedUhdrRgbImage.h); + meanSquareError = (double)bSqError / ((size_t)mDecodedUhdrRgbImage.w * mDecodedUhdrRgbImage.h); mPsnr[2] = meanSquareError ? 10 * log10((double)255 * 255 / meanSquareError) : 100; std::cout << "psnr rgb: \t" << mPsnr[0] << " \t " << mPsnr[1] << " \t " << mPsnr[2] << std::endl; @@ -1299,13 +1311,15 @@ void UltraHdrAppInput::computeYUVHdrPSNR() { } double meanSquareError = - (double)ySqError / (mDecodedUhdrYuv444Image.w * mDecodedUhdrYuv444Image.h); + (double)ySqError / ((size_t)mDecodedUhdrYuv444Image.w * mDecodedUhdrYuv444Image.h); mPsnr[0] = meanSquareError ? 10 * log10((double)1023 * 1023 / meanSquareError) : 100; - meanSquareError = (double)uSqError / (mDecodedUhdrYuv444Image.w * mDecodedUhdrYuv444Image.h / 4); + meanSquareError = + (double)uSqError / ((size_t)mDecodedUhdrYuv444Image.w * mDecodedUhdrYuv444Image.h / 4); mPsnr[1] = meanSquareError ? 10 * log10((double)1023 * 1023 / meanSquareError) : 100; - meanSquareError = (double)vSqError / (mDecodedUhdrYuv444Image.w * mDecodedUhdrYuv444Image.h / 4); + meanSquareError = + (double)vSqError / ((size_t)mDecodedUhdrYuv444Image.w * mDecodedUhdrYuv444Image.h / 4); mPsnr[2] = meanSquareError ? 10 * log10((double)1023 * 1023 / meanSquareError) : 100; std::cout << "psnr yuv: \t" << mPsnr[0] << " \t " << mPsnr[1] << " \t " << mPsnr[2] << std::endl; @@ -1352,13 +1366,15 @@ void UltraHdrAppInput::computeYUVSdrPSNR() { } } double meanSquareError = - (double)ySqError / (mDecodedUhdrYuv444Image.w * mDecodedUhdrYuv444Image.h); + (double)ySqError / ((size_t)mDecodedUhdrYuv444Image.w * mDecodedUhdrYuv444Image.h); mPsnr[0] = meanSquareError ? 10 * log10((double)255 * 255 / meanSquareError) : 100; - meanSquareError = (double)uSqError / (mDecodedUhdrYuv444Image.w * mDecodedUhdrYuv444Image.h / 4); + meanSquareError = + (double)uSqError / ((size_t)mDecodedUhdrYuv444Image.w * mDecodedUhdrYuv444Image.h / 4); mPsnr[1] = meanSquareError ? 10 * log10((double)255 * 255 / meanSquareError) : 100; - meanSquareError = (double)vSqError / (mDecodedUhdrYuv444Image.w * mDecodedUhdrYuv444Image.h / 4); + meanSquareError = + (double)vSqError / ((size_t)mDecodedUhdrYuv444Image.w * mDecodedUhdrYuv444Image.h / 4); mPsnr[2] = meanSquareError ? 10 * log10((double)255 * 255 / meanSquareError) : 100; std::cout << "psnr yuv: \t" << mPsnr[0] << " \t " << mPsnr[1] << " \t " << mPsnr[2] << std::endl; diff --git a/fuzzer/ultrahdr_dec_fuzzer.cpp b/fuzzer/ultrahdr_dec_fuzzer.cpp index 30e6f541..dba543b9 100644 --- a/fuzzer/ultrahdr_dec_fuzzer.cpp +++ b/fuzzer/ultrahdr_dec_fuzzer.cpp @@ -55,8 +55,8 @@ void UltraHdrDecFuzzer::process() { int bottom = mFdp.ConsumeIntegral(); auto applyResize = mFdp.ConsumeBool(); - int resizeWidth = mFdp.ConsumeIntegralInRange(-32, kMaxWidth + 128); - int resizeHeight = mFdp.ConsumeIntegralInRange(-32, kMaxHeight + 128); + int resizeWidth = mFdp.ConsumeIntegralInRange(-32, kMaxWidth + 128); + int resizeHeight = mFdp.ConsumeIntegralInRange(-32, kMaxHeight + 128); auto buffer = mFdp.ConsumeRemainingBytes(); diff --git a/fuzzer/ultrahdr_enc_fuzzer.cpp b/fuzzer/ultrahdr_enc_fuzzer.cpp index a7e71e93..dbcef152 100644 --- a/fuzzer/ultrahdr_enc_fuzzer.cpp +++ b/fuzzer/ultrahdr_enc_fuzzer.cpp @@ -142,19 +142,19 @@ void UltraHdrEncFuzzer::process() { // raw buffer config bool hasHdrStride = mFdp.ConsumeBool(); - int yHdrStride = mFdp.ConsumeIntegralInRange(width, width + 128); + size_t yHdrStride = mFdp.ConsumeIntegralInRange(width, width + 128); if (!hasHdrStride) yHdrStride = width; bool isHdrUVContiguous = mFdp.ConsumeBool(); bool hasHdrUVStride = mFdp.ConsumeBool(); - int uvHdrStride = mFdp.ConsumeIntegralInRange(width, width + 128); + size_t uvHdrStride = mFdp.ConsumeIntegralInRange(width, width + 128); if (!hasHdrUVStride) uvHdrStride = width; bool hasSdrStride = mFdp.ConsumeBool(); - int ySdrStride = mFdp.ConsumeIntegralInRange(width, width + 128); + size_t ySdrStride = mFdp.ConsumeIntegralInRange(width, width + 128); if (!hasSdrStride) ySdrStride = width; bool isSdrUVContiguous = mFdp.ConsumeBool(); bool hasSdrUVStride = mFdp.ConsumeBool(); - int uvSdrStride = mFdp.ConsumeIntegralInRange(width / 2, width / 2 + 128); + size_t uvSdrStride = mFdp.ConsumeIntegralInRange(width / 2, width / 2 + 128); if (!hasSdrUVStride) uvSdrStride = width / 2; // editing effects @@ -172,8 +172,8 @@ void UltraHdrEncFuzzer::process() { int bottom = mFdp.ConsumeIntegral(); auto applyResize = mFdp.ConsumeBool(); - int resizeWidth = mFdp.ConsumeIntegralInRange(-32, kMaxWidth + 128); - int resizeHeight = mFdp.ConsumeIntegralInRange(-32, kMaxHeight + 128); + int resizeWidth = mFdp.ConsumeIntegralInRange(-32, kMaxWidth + 128); + int resizeHeight = mFdp.ConsumeIntegralInRange(-32, kMaxHeight + 128); // exif char greeting[] = "Exif says hello world"; diff --git a/fuzzer/ultrahdr_legacy_fuzzer.cpp b/fuzzer/ultrahdr_legacy_fuzzer.cpp index 3523cdf0..0849d72b 100644 --- a/fuzzer/ultrahdr_legacy_fuzzer.cpp +++ b/fuzzer/ultrahdr_legacy_fuzzer.cpp @@ -134,19 +134,19 @@ void UltraHdrEncFuzzer::process() { // raw buffer config bool hasP010Stride = mFdp.ConsumeBool(); - int yP010Stride = mFdp.ConsumeIntegralInRange(width, width + 128); + size_t yP010Stride = mFdp.ConsumeIntegralInRange(width, width + 128); if (!hasP010Stride) yP010Stride = width; bool isP010UVContiguous = mFdp.ConsumeBool(); bool hasP010UVStride = mFdp.ConsumeBool(); - int uvP010Stride = mFdp.ConsumeIntegralInRange(width, width + 128); + size_t uvP010Stride = mFdp.ConsumeIntegralInRange(width, width + 128); if (!hasP010UVStride) uvP010Stride = width; bool hasYuv420Stride = mFdp.ConsumeBool(); - int yYuv420Stride = mFdp.ConsumeIntegralInRange(width, width + 128); + size_t yYuv420Stride = mFdp.ConsumeIntegralInRange(width, width + 128); if (!hasYuv420Stride) yYuv420Stride = width; bool isYuv420UVContiguous = mFdp.ConsumeBool(); bool hasYuv420UVStride = mFdp.ConsumeBool(); - int uvYuv420Stride = mFdp.ConsumeIntegralInRange(width / 2, width / 2 + 128); + size_t uvYuv420Stride = mFdp.ConsumeIntegralInRange(width / 2, width / 2 + 128); if (!hasYuv420UVStride) uvYuv420Stride = width / 2; // display boost diff --git a/java/jni/ultrahdr-jni.cpp b/java/jni/ultrahdr-jni.cpp index f70a4569..0fa8f4b7 100644 --- a/java/jni/ultrahdr-jni.cpp +++ b/java/jni/ultrahdr-jni.cpp @@ -388,6 +388,8 @@ Java_com_google_media_codecs_ultrahdr_UltraHDREncoder_getOutputNative(JNIEnv *en "no output returned, may be call to uhdr_encode() was not made or encountered " "error during encoding process.", nullptr) + RET_VAL_IF_TRUE(enc_output->data_sz >= INT32_MAX, "java/lang/OutOfMemoryError", + "encoded output size exceeds integer max", nullptr) jbyteArray output = env->NewByteArray(enc_output->data_sz); RET_VAL_IF_TRUE(output == nullptr, "java/io/IOException", "failed to allocate storage for output", nullptr) diff --git a/lib/include/ultrahdr/icc.h b/lib/include/ultrahdr/icc.h index 23e5a2bb..9f71e308 100644 --- a/lib/include/ultrahdr/icc.h +++ b/lib/include/ultrahdr/icc.h @@ -180,7 +180,7 @@ static inline Fixed float_round_to_fixed(float x) { } static inline uint16_t float_round_to_unorm16(float x) { - x = x * 65535.f + 0.5; + x = x * 65535.f + 0.5f; if (x > 65535) return 65535; if (x < 0) return 0; return static_cast(x); diff --git a/lib/include/ultrahdr/jpegdecoderhelper.h b/lib/include/ultrahdr/jpegdecoderhelper.h index 19f58354..5abbb0db 100644 --- a/lib/include/ultrahdr/jpegdecoderhelper.h +++ b/lib/include/ultrahdr/jpegdecoderhelper.h @@ -63,7 +63,7 @@ class JpegDecoderHelper { * * \return uhdr_error_info_t #UHDR_CODEC_OK if operation succeeds, uhdr_codec_err_t otherwise. */ - uhdr_error_info_t decompressImage(const void* image, int length, + uhdr_error_info_t decompressImage(const void* image, size_t length, decode_mode_t mode = DECODE_TO_YCBCR_CS); /*!\brief This function parses the bitstream that is passed to it and makes image information @@ -75,7 +75,7 @@ class JpegDecoderHelper { * * \return uhdr_error_info_t #UHDR_CODEC_OK if operation succeeds, uhdr_codec_err_t otherwise. */ - uhdr_error_info_t parseImage(const void* image, int length) { + uhdr_error_info_t parseImage(const void* image, size_t length) { return decompressImage(image, length, PARSE_STREAM); } @@ -99,13 +99,13 @@ class JpegDecoderHelper { * and it returned true. */ /*!\brief returns image width */ - size_t getDecompressedImageWidth() { return mPlaneWidth[0]; } + unsigned int getDecompressedImageWidth() { return mPlaneWidth[0]; } /*!\brief returns image height */ - size_t getDecompressedImageHeight() { return mPlaneHeight[0]; } + unsigned int getDecompressedImageHeight() { return mPlaneHeight[0]; } /*!\brief returns number of components in image */ - size_t getNumComponentsInImage() { return mNumComponents; } + unsigned int getNumComponentsInImage() { return mNumComponents; } /*!\brief returns pointer to xmp block present in input image */ void* getXMPPtr() { return mXMPBuffer.data(); } @@ -135,13 +135,13 @@ class JpegDecoderHelper { * via parseImage()/decompressImage() call. Note this does not include jpeg marker (0xffe1) and * the next 2 bytes indicating the size of the payload. If exif block is not present in the image * passed, then it returns -1. */ - int getEXIFPos() { return mExifPayLoadOffset; } + long getEXIFPos() { return mExifPayLoadOffset; } private: // max number of components supported static constexpr int kMaxNumComponents = 3; - uhdr_error_info_t decode(const void* image, int length, decode_mode_t mode); + uhdr_error_info_t decode(const void* image, size_t length, decode_mode_t mode); uhdr_error_info_t decode(jpeg_decompress_struct* cinfo, uint8_t* dest); uhdr_error_info_t decodeToCSYCbCr(jpeg_decompress_struct* cinfo, uint8_t* dest); uhdr_error_info_t decodeToCSRGB(jpeg_decompress_struct* cinfo, uint8_t* dest); @@ -157,14 +157,14 @@ class JpegDecoderHelper { // image attributes uhdr_img_fmt_t mOutFormat; - size_t mNumComponents; - size_t mPlaneWidth[kMaxNumComponents]; - size_t mPlaneHeight[kMaxNumComponents]; - size_t mPlaneHStride[kMaxNumComponents]; - size_t mPlaneVStride[kMaxNumComponents]; - - int mExifPayLoadOffset; // Position of EXIF package, default value is -1 which means no EXIF - // package appears. + unsigned int mNumComponents; + unsigned int mPlaneWidth[kMaxNumComponents]; + unsigned int mPlaneHeight[kMaxNumComponents]; + unsigned int mPlaneHStride[kMaxNumComponents]; + unsigned int mPlaneVStride[kMaxNumComponents]; + + long mExifPayLoadOffset; // Position of EXIF package, default value is -1 which means no EXIF + // package appears. }; } /* namespace ultrahdr */ diff --git a/lib/include/ultrahdr/jpegencoderhelper.h b/lib/include/ultrahdr/jpegencoderhelper.h index 1335671c..e0b106fc 100644 --- a/lib/include/ultrahdr/jpegencoderhelper.h +++ b/lib/include/ultrahdr/jpegencoderhelper.h @@ -61,7 +61,7 @@ class JpegEncoderHelper { * \return uhdr_error_info_t #UHDR_CODEC_OK if operation succeeds, uhdr_codec_err_t otherwise. */ uhdr_error_info_t compressImage(const uhdr_raw_image_t* img, const int qfactor, - const void* iccBuffer, const unsigned int iccSize); + const void* iccBuffer, const size_t iccSize); /*!\brief This function encodes the raw image that is passed to it and stores the results * internally. The result is accessible via getter functions. @@ -77,10 +77,9 @@ class JpegEncoderHelper { * * \return uhdr_error_info_t #UHDR_CODEC_OK if operation succeeds, uhdr_codec_err_t otherwise. */ - uhdr_error_info_t compressImage(const uint8_t* planes[3], const size_t strides[3], + uhdr_error_info_t compressImage(const uint8_t* planes[3], const unsigned int strides[3], const int width, const int height, const uhdr_img_fmt_t format, - const int qfactor, const void* iccBuffer, - const unsigned int iccSize); + const int qfactor, const void* iccBuffer, const size_t iccSize); /*! Below public methods are only effective if a call to compressImage() is made and it returned * true. */ @@ -102,20 +101,20 @@ class JpegEncoderHelper { // max number of components supported static constexpr int kMaxNumComponents = 3; - uhdr_error_info_t encode(const uint8_t* planes[3], const size_t strides[3], const int width, + uhdr_error_info_t encode(const uint8_t* planes[3], const unsigned int strides[3], const int width, const int height, const uhdr_img_fmt_t format, const int qfactor, - const void* iccBuffer, const unsigned int iccSize); + const void* iccBuffer, const size_t iccSize); uhdr_error_info_t compressYCbCr(jpeg_compress_struct* cinfo, const uint8_t* planes[3], - const size_t strides[3]); + const unsigned int strides[3]); destination_mgr_impl mDestMgr; // object for managing output // temporary storage std::unique_ptr mPlanesMCURow[kMaxNumComponents]; - size_t mPlaneWidth[kMaxNumComponents]; - size_t mPlaneHeight[kMaxNumComponents]; + unsigned int mPlaneWidth[kMaxNumComponents]; + unsigned int mPlaneHeight[kMaxNumComponents]; }; } /* namespace ultrahdr */ diff --git a/lib/include/ultrahdr/jpegr.h b/lib/include/ultrahdr/jpegr.h index 466b3897..2b7bbaaa 100644 --- a/lib/include/ultrahdr/jpegr.h +++ b/lib/include/ultrahdr/jpegr.h @@ -30,8 +30,8 @@ namespace ultrahdr { // Default configurations // gainmap image downscale factor -static const size_t kMapDimensionScaleFactorDefault = 1; -static const size_t kMapDimensionScaleFactorAndroidDefault = 4; +static const int kMapDimensionScaleFactorDefault = 1; +static const int kMapDimensionScaleFactorAndroidDefault = 4; // JPEG compress quality (0 ~ 100) for base image static const int kBaseCompressQualityDefault = 95; @@ -63,17 +63,17 @@ struct jpeg_info_struct { std::vector exifData = std::vector(0); std::vector xmpData = std::vector(0); std::vector isoData = std::vector(0); - size_t width; - size_t height; - size_t numComponents; + unsigned int width; + unsigned int height; + unsigned int numComponents; }; /* * Holds information of jpegr image */ struct jpegr_info_struct { - size_t width; // copy of primary image width (for easier access) - size_t height; // copy of primary image height (for easier access) + unsigned int width; // copy of primary image width (for easier access) + unsigned int height; // copy of primary image height (for easier access) jpeg_info_struct* primaryImgInfo = nullptr; jpeg_info_struct* gainmapImgInfo = nullptr; }; @@ -84,7 +84,7 @@ typedef struct jpegr_info_struct* jr_info_ptr; class JpegR { public: JpegR(void* uhdrGLESCtxt = nullptr, - size_t mapDimensionScaleFactor = kMapDimensionScaleFactorAndroidDefault, + int mapDimensionScaleFactor = kMapDimensionScaleFactorAndroidDefault, int mapCompressQuality = kMapCompressQualityAndroidDefault, bool useMultiChannelGainMap = kUseMultiChannelGainMapAndroidDefault, float gamma = kGainMapGammaDefault, @@ -260,7 +260,7 @@ class JpegR { * * \return none */ - void setMapDimensionScaleFactor(size_t mapDimensionScaleFactor) { + void setMapDimensionScaleFactor(int mapDimensionScaleFactor) { this->mMapDimensionScaleFactor = mapDimensionScaleFactor; } @@ -269,7 +269,7 @@ class JpegR { * * \return mapDimensionScaleFactor */ - size_t getMapDimensionScaleFactor() { return this->mMapDimensionScaleFactor; } + int getMapDimensionScaleFactor() { return this->mMapDimensionScaleFactor; } /*!\brief set gain map compression quality factor * NOTE: Applicable only in encoding scenario @@ -415,8 +415,9 @@ class JpegR { * * \return uhdr_error_info_t #UHDR_CODEC_OK if operation succeeds, uhdr_codec_err_t otherwise. */ - uhdr_error_info_t parseGainMapMetadata(uint8_t* iso_data, int iso_size, uint8_t* xmp_data, - int xmp_size, uhdr_gainmap_metadata_ext_t* uhdr_metadata); + uhdr_error_info_t parseGainMapMetadata(uint8_t* iso_data, size_t iso_size, uint8_t* xmp_data, + size_t xmp_size, + uhdr_gainmap_metadata_ext_t* uhdr_metadata); /*!\brief This method is used to tone map a hdr image * @@ -514,7 +515,8 @@ class JpegR { * \return uhdr_error_info_t #UHDR_CODEC_OK if operation succeeds, uhdr_codec_err_t otherwise. */ uhdr_error_info_t parseJpegInfo(uhdr_compressed_image_t* jpeg_image, j_info_ptr image_info, - size_t* img_width = nullptr, size_t* img_height = nullptr); + unsigned int* img_width = nullptr, + unsigned int* img_height = nullptr); /*!\brief This method takes compressed sdr intent, compressed gainmap coefficient, gainmap * metadata and creates a ultrahdr image. This is done by first generating XMP packet from gainmap @@ -597,7 +599,7 @@ class JpegR { // Configurations void* mUhdrGLESCtxt; // opengl es context - size_t mMapDimensionScaleFactor; // gain map scale factor + int mMapDimensionScaleFactor; // gain map scale factor int mMapCompressQuality; // gain map quality factor bool mUseMultiChannelGainMap; // enable multichannel gain map float mGamma; // gain map gamma parameter diff --git a/lib/include/ultrahdr/jpegrutils.h b/lib/include/ultrahdr/jpegrutils.h index 2ddcb740..6e52b12b 100644 --- a/lib/include/ultrahdr/jpegrutils.h +++ b/lib/include/ultrahdr/jpegrutils.h @@ -39,20 +39,20 @@ static inline uint16_t EndianSwap16(uint16_t value) { class DataStruct { private: void* data; - int writePos; - int length; + size_t writePos; + size_t length; public: DataStruct(int s); ~DataStruct(); void* getData(); - int getLength(); - int getBytesWritten(); + size_t getLength(); + size_t getBytesWritten(); bool write8(uint8_t value); bool write16(uint16_t value); bool write32(uint32_t value); - bool write(const void* src, int size); + bool write(const void* src, size_t size); }; /* @@ -64,8 +64,8 @@ class DataStruct { * @param position cursor in desitination where the data is to be written. * @return success or error code. */ -uhdr_error_info_t Write(uhdr_compressed_image_t* destination, const void* source, int length, - int& position); +uhdr_error_info_t Write(uhdr_compressed_image_t* destination, const void* source, size_t length, + size_t& position); /* * Parses XMP packet and fills metadata with data from XMP @@ -75,7 +75,7 @@ uhdr_error_info_t Write(uhdr_compressed_image_t* destination, const void* source * @param metadata place to store HDR metadata values * @return success or error code. */ -uhdr_error_info_t getMetadataFromXMP(uint8_t* xmp_data, int xmp_size, +uhdr_error_info_t getMetadataFromXMP(uint8_t* xmp_data, size_t xmp_size, uhdr_gainmap_metadata_ext_t* metadata); /* @@ -118,7 +118,7 @@ uhdr_error_info_t getMetadataFromXMP(uint8_t* xmp_data, int xmp_size, * @param secondary_image_length length of secondary image * @return XMP metadata in type of string */ -std::string generateXmpForPrimaryImage(int secondary_image_length, +std::string generateXmpForPrimaryImage(size_t secondary_image_length, uhdr_gainmap_metadata_ext_t& metadata); /* diff --git a/lib/include/ultrahdr/multipictureformat.h b/lib/include/ultrahdr/multipictureformat.h index 434b2bab..42b44009 100644 --- a/lib/include/ultrahdr/multipictureformat.h +++ b/lib/include/ultrahdr/multipictureformat.h @@ -69,8 +69,8 @@ constexpr uint32_t kMPEntryAttributeFormatJpeg = 0x0000000; constexpr uint32_t kMPEntryAttributeTypePrimary = 0x030000; size_t calculateMpfSize(); -std::shared_ptr generateMpf(int primary_image_size, int primary_image_offset, - int secondary_image_size, int secondary_image_offset); +std::shared_ptr generateMpf(size_t primary_image_size, size_t primary_image_offset, + size_t secondary_image_size, size_t secondary_image_offset); } // namespace ultrahdr diff --git a/lib/include/ultrahdr/ultrahdr.h b/lib/include/ultrahdr/ultrahdr.h index 941850f5..24a43acc 100644 --- a/lib/include/ultrahdr/ultrahdr.h +++ b/lib/include/ultrahdr/ultrahdr.h @@ -127,9 +127,9 @@ struct jpegr_uncompressed_struct { // Pointer to the data location. void* data; // Width of the gain map or the luma plane of the image in pixels. - size_t width; + unsigned int width; // Height of the gain map or the luma plane of the image in pixels. - size_t height; + unsigned int height; // Color gamut. ultrahdr_color_gamut colorGamut; @@ -140,7 +140,7 @@ struct jpegr_uncompressed_struct { // Stride of Y plane in number of pixels. 0 indicates the member is uninitialized. If // non-zero this value must be larger than or equal to luma width. If stride is // uninitialized then it is assumed to be equal to luma width. - size_t luma_stride = 0; + unsigned int luma_stride = 0; // Stride of UV plane in number of pixels. // 1. If this handle points to P010 image then this value must be larger than // or equal to luma width. @@ -148,7 +148,7 @@ struct jpegr_uncompressed_struct { // or equal to (luma width / 2). // NOTE: if chroma_data is nullptr, chroma_stride is irrelevant. Just as the way, // chroma_data is derived from luma ptr, chroma stride is derived from luma stride. - size_t chroma_stride = 0; + unsigned int chroma_stride = 0; // Pixel format. uhdr_img_fmt_t pixelFormat = UHDR_IMG_FMT_UNSPECIFIED; // Color range. @@ -162,9 +162,9 @@ struct jpegr_compressed_struct { // Pointer to the data location. void* data; // Used data length in bytes. - int length; + size_t length; // Maximum available data length in bytes. - int maxLength; + size_t maxLength; // Color gamut. ultrahdr_color_gamut colorGamut; }; diff --git a/lib/src/gainmapmath.cpp b/lib/src/gainmapmath.cpp index 1820c30d..5666b63c 100644 --- a/lib/src/gainmapmath.cpp +++ b/lib/src/gainmapmath.cpp @@ -974,8 +974,8 @@ float sampleMap(uhdr_raw_image_t* map, size_t map_scale_factor, size_t x, size_t // TODO: If map_scale_factor is guaranteed to be an integer power of 2, then optimize the // following by using & (map_scale_factor - 1) - int offset_x = x % map_scale_factor; - int offset_y = y % map_scale_factor; + size_t offset_x = x % map_scale_factor; + size_t offset_y = y % map_scale_factor; float* weights = weightTables.mWeights; if (x_lower == x_upper && y_lower == y_upper) @@ -1098,8 +1098,8 @@ Color sampleMap3Channel(uhdr_raw_image_t* map, size_t map_scale_factor, size_t x // TODO: If map_scale_factor is guaranteed to be an integer power of 2, then optimize the // following by using & (map_scale_factor - 1) - int offset_x = x % map_scale_factor; - int offset_y = y % map_scale_factor; + size_t offset_x = x % map_scale_factor; + size_t offset_y = y % map_scale_factor; float* weights = weightTables.mWeights; if (x_lower == x_upper && y_lower == y_upper) @@ -1472,8 +1472,8 @@ std::unique_ptr convert_raw_input_to_ycbcr(uhdr_raw_image_ pixel[0].u = (pixel[0].u + pixel[1].u + pixel[2].u + pixel[3].u) / 4; pixel[0].v = (pixel[0].v + pixel[1].v + pixel[2].v + pixel[3].v) / 4; - pixel[0].u = pixel[0].u * 255.0f + 0.5 + 128.0f; - pixel[0].v = pixel[0].v * 255.0f + 0.5 + 128.0f; + pixel[0].u = pixel[0].u * 255.0f + 0.5f + 128.0f; + pixel[0].v = pixel[0].v * 255.0f + 0.5f + 128.0f; pixel[0].u = CLIP3(pixel[0].u, 0.0f, 255.0f); pixel[0].v = CLIP3(pixel[0].v, 0.0f, 255.0f); @@ -1507,8 +1507,8 @@ std::unique_ptr convert_raw_input_to_ycbcr(uhdr_raw_image_ pixel.y = CLIP3(pixel.y, 0.0f, 255.0f); yData[dst->stride[UHDR_PLANE_Y] * i + j] = uint8_t(pixel.y); - pixel.u = pixel.u * 255.0f + 0.5 + 128.0f; - pixel.v = pixel.v * 255.0f + 0.5 + 128.0f; + pixel.u = pixel.u * 255.0f + 0.5f + 128.0f; + pixel.v = pixel.v * 255.0f + 0.5f + 128.0f; pixel.u = CLIP3(pixel.u, 0.0f, 255.0f); pixel.v = CLIP3(pixel.v, 0.0f, 255.0f); @@ -1617,7 +1617,7 @@ uhdr_error_info_t copy_raw_image(uhdr_raw_image_t* src, uhdr_raw_image_t* dst) { dst->range = src->range; if (dst->fmt == src->fmt) { if (src->fmt == UHDR_IMG_FMT_24bppYCbCrP010) { - int bpp = 2; + size_t bpp = 2; uint8_t* y_dst = static_cast(dst->planes[UHDR_PLANE_Y]); uint8_t* y_src = static_cast(src->planes[UHDR_PLANE_Y]); uint8_t* uv_dst = static_cast(dst->planes[UHDR_PLANE_UV]); @@ -1665,7 +1665,7 @@ uhdr_error_info_t copy_raw_image(uhdr_raw_image_t* src, uhdr_raw_image_t* dst) { src->fmt == UHDR_IMG_FMT_32bppRGBA1010102 || src->fmt == UHDR_IMG_FMT_24bppRGB888) { uint8_t* plane_dst = static_cast(dst->planes[UHDR_PLANE_PACKED]); uint8_t* plane_src = static_cast(src->planes[UHDR_PLANE_PACKED]); - int bpp = 1; + size_t bpp = 1; if (src->fmt == UHDR_IMG_FMT_32bppRGBA1010102 || src->fmt == UHDR_IMG_FMT_32bppRGBA8888) bpp = 4; @@ -1693,7 +1693,7 @@ uhdr_error_info_t copy_raw_image(uhdr_raw_image_t* src, uhdr_raw_image_t* dst) { pixel_dst += 1; } plane_dst += dst->stride[UHDR_PLANE_PACKED]; - plane_src += 3 * src->stride[UHDR_PLANE_PACKED]; + plane_src += (size_t)3 * src->stride[UHDR_PLANE_PACKED]; } return g_no_error; } diff --git a/lib/src/jpegdecoderhelper.cpp b/lib/src/jpegdecoderhelper.cpp index 7b107a13..76c4c788 100644 --- a/lib/src/jpegdecoderhelper.cpp +++ b/lib/src/jpegdecoderhelper.cpp @@ -65,7 +65,7 @@ const int kMaxHeight = UHDR_MAX_DIMENSION; /*!\brief module for managing input */ struct jpeg_source_mgr_impl : jpeg_source_mgr { - jpeg_source_mgr_impl(const uint8_t* ptr, int len); + jpeg_source_mgr_impl(const uint8_t* ptr, size_t len); ~jpeg_source_mgr_impl() = default; const uint8_t* mBufferPtr; @@ -101,7 +101,7 @@ static void jpegr_skip_input_data(j_decompress_ptr cinfo, long num_bytes) { static void jpegr_term_source(j_decompress_ptr /*cinfo*/) {} -jpeg_source_mgr_impl::jpeg_source_mgr_impl(const uint8_t* ptr, int len) +jpeg_source_mgr_impl::jpeg_source_mgr_impl(const uint8_t* ptr, size_t len) : mBufferPtr(ptr), mBufferLength(len) { init_source = jpegr_init_source; fill_input_buffer = jpegr_fill_input_buffer; @@ -126,8 +126,8 @@ static void jpeg_extract_marker_payload(const j_decompress_ptr cinfo, const uint const uint8_t* marker_fourcc_code, const uint32_t fourcc_length, std::vector& destination, - int& markerPayloadOffsetRelativeToSourceBuffer) { - size_t pos = 2; /* position after reading SOI marker (0xffd8) */ + long& markerPayloadOffsetRelativeToSourceBuffer) { + unsigned int pos = 2; /* position after reading SOI marker (0xffd8) */ markerPayloadOffsetRelativeToSourceBuffer = -1; for (jpeg_marker_struct* marker = cinfo->marker_list; marker; marker = marker->next) { @@ -172,7 +172,7 @@ static uhdr_img_fmt_t getOutputSamplingFormat(const j_decompress_ptr cinfo) { return UHDR_IMG_FMT_UNSPECIFIED; } -uhdr_error_info_t JpegDecoderHelper::decompressImage(const void* image, int length, +uhdr_error_info_t JpegDecoderHelper::decompressImage(const void* image, size_t length, decode_mode_t mode) { if (image == nullptr) { uhdr_error_info_t status; @@ -185,7 +185,7 @@ uhdr_error_info_t JpegDecoderHelper::decompressImage(const void* image, int leng uhdr_error_info_t status; status.error_code = UHDR_CODEC_INVALID_PARAM; status.has_detail = 1; - snprintf(status.detail, sizeof status.detail, "received bad compressed image size %d", length); + snprintf(status.detail, sizeof status.detail, "received bad compressed image size %zd", length); return status; } @@ -209,7 +209,7 @@ uhdr_error_info_t JpegDecoderHelper::decompressImage(const void* image, int leng return decode(image, length, mode); } -uhdr_error_info_t JpegDecoderHelper::decode(const void* image, int length, decode_mode_t mode) { +uhdr_error_info_t JpegDecoderHelper::decode(const void* image, size_t length, decode_mode_t mode) { jpeg_source_mgr_impl mgr(static_cast(image), length); jpeg_decompress_struct cinfo; jpeg_error_mgr_impl myerr; @@ -234,7 +234,7 @@ uhdr_error_info_t JpegDecoderHelper::decode(const void* image, int length, decod jpeg_destroy_decompress(&cinfo); return status; } - int payloadOffset = -1; + long payloadOffset = -1; jpeg_extract_marker_payload(&cinfo, kAPP1Marker, kXmpNameSpace, sizeof kXmpNameSpace / sizeof kXmpNameSpace[0], mXMPBuffer, payloadOffset); @@ -373,10 +373,10 @@ uhdr_error_info_t JpegDecoderHelper::decode(const void* image, int length, decod mPlaneVStride[i] = 0; } #ifdef JCS_ALPHA_EXTENSIONS - mResultBuffer.resize(mPlaneHStride[0] * mPlaneVStride[0] * 4); + mResultBuffer.resize((size_t)mPlaneHStride[0] * mPlaneVStride[0] * 4); cinfo.out_color_space = JCS_EXT_RGBA; #else - mResultBuffer.resize(mPlaneHStride[0] * mPlaneVStride[0] * 3); + mResultBuffer.resize((size_t)mPlaneHStride[0] * mPlaneVStride[0] * 3); cinfo.out_color_space = JCS_RGB; #endif } else if (DECODE_TO_YCBCR_CS == mode) { @@ -389,11 +389,11 @@ uhdr_error_info_t JpegDecoderHelper::decode(const void* image, int length, decod jpeg_destroy_decompress(&cinfo); return status; } - int size = 0; + size_t size = 0; for (int i = 0; i < cinfo.num_components; i++) { mPlaneHStride[i] = ALIGNM(mPlaneWidth[i], cinfo.max_h_samp_factor); mPlaneVStride[i] = ALIGNM(mPlaneHeight[i], cinfo.max_v_samp_factor); - size += mPlaneHStride[i] * mPlaneVStride[i]; + size += (size_t)mPlaneHStride[i] * mPlaneVStride[i]; } mResultBuffer.resize(size); cinfo.out_color_space = cinfo.jpeg_color_space; @@ -463,9 +463,9 @@ uhdr_error_info_t JpegDecoderHelper::decodeToCSRGB(jpeg_decompress_struct* cinfo return status; } #ifdef JCS_ALPHA_EXTENSIONS - out += mPlaneHStride[0] * 4; + out += (size_t)mPlaneHStride[0] * 4; #else - out += mPlaneHStride[0] * 3; + out += (size_t)mPlaneHStride[0] * 3; #endif } return g_no_error; @@ -508,7 +508,7 @@ uhdr_error_info_t JpegDecoderHelper::decodeToCSYCbCr(jpeg_decompress_struct* cin JDIMENSION scanline = mcu_scanline_start[i] + j; if (scanline < mPlaneVStride[i]) { - mcuRows[i][j] = planes[i] + scanline * mPlaneHStride[i]; + mcuRows[i][j] = planes[i] + (size_t)scanline * mPlaneHStride[i]; } else { mcuRows[i][j] = mPlanesMCURow[i].get(); } @@ -553,7 +553,7 @@ uhdr_raw_image_t JpegDecoderHelper::getDecompressedImage() { for (int i = 0; i < 3; i++) { img.planes[i] = data; img.stride[i] = mPlaneHStride[i]; - data += mPlaneHStride[i] * mPlaneVStride[i]; + data += (size_t)mPlaneHStride[i] * mPlaneVStride[i]; } return img; diff --git a/lib/src/jpegencoderhelper.cpp b/lib/src/jpegencoderhelper.cpp index 53b8b366..2bbec622 100644 --- a/lib/src/jpegencoderhelper.cpp +++ b/lib/src/jpegencoderhelper.cpp @@ -105,21 +105,20 @@ static void outputErrorMessage(j_common_ptr cinfo) { } uhdr_error_info_t JpegEncoderHelper::compressImage(const uhdr_raw_image_t* img, const int qfactor, - const void* iccBuffer, - const unsigned int iccSize) { + const void* iccBuffer, const size_t iccSize) { const uint8_t* planes[3]{reinterpret_cast(img->planes[UHDR_PLANE_Y]), reinterpret_cast(img->planes[UHDR_PLANE_U]), reinterpret_cast(img->planes[UHDR_PLANE_V])}; - const size_t strides[3]{img->stride[UHDR_PLANE_Y], img->stride[UHDR_PLANE_U], - img->stride[UHDR_PLANE_V]}; + const unsigned int strides[3]{img->stride[UHDR_PLANE_Y], img->stride[UHDR_PLANE_U], + img->stride[UHDR_PLANE_V]}; return compressImage(planes, strides, img->w, img->h, img->fmt, qfactor, iccBuffer, iccSize); } uhdr_error_info_t JpegEncoderHelper::compressImage(const uint8_t* planes[3], - const size_t strides[3], const int width, + const unsigned int strides[3], const int width, const int height, const uhdr_img_fmt_t format, const int qfactor, const void* iccBuffer, - const unsigned int iccSize) { + const size_t iccSize) { return encode(planes, strides, width, height, format, qfactor, iccBuffer, iccSize); } @@ -135,10 +134,10 @@ uhdr_compressed_image_t JpegEncoderHelper::getCompressedImage() { return img; } -uhdr_error_info_t JpegEncoderHelper::encode(const uint8_t* planes[3], const size_t strides[3], +uhdr_error_info_t JpegEncoderHelper::encode(const uint8_t* planes[3], const unsigned int strides[3], const int width, const int height, const uhdr_img_fmt_t format, const int qfactor, - const void* iccBuffer, const unsigned int iccSize) { + const void* iccBuffer, const size_t iccSize) { jpeg_compress_struct cinfo; jpeg_error_mgr_impl myerr; uhdr_error_info_t status = g_no_error; @@ -252,7 +251,7 @@ uhdr_error_info_t JpegEncoderHelper::encode(const uint8_t* planes[3], const size uhdr_error_info_t JpegEncoderHelper::compressYCbCr(jpeg_compress_struct* cinfo, const uint8_t* planes[3], - const size_t strides[3]) { + const unsigned int strides[3]) { JSAMPROW mcuRows[kMaxNumComponents][2 * DCTSIZE]; JSAMPROW mcuRowsTmp[kMaxNumComponents][2 * DCTSIZE]; size_t alignedPlaneWidth[kMaxNumComponents]{}; @@ -292,7 +291,7 @@ uhdr_error_info_t JpegEncoderHelper::compressYCbCr(jpeg_compress_struct* cinfo, JDIMENSION scanline = mcu_scanline_start[i] + j; if (scanline < mPlaneHeight[i]) { - mcuRows[i][j] = const_cast(planes[i] + scanline * strides[i]); + mcuRows[i][j] = const_cast(planes[i] + (size_t)scanline * strides[i]); if (strides[i] < alignedPlaneWidth[i]) { memcpy(mcuRowsTmp[i][j], mcuRows[i][j], mPlaneWidth[i]); } diff --git a/lib/src/jpegr.cpp b/lib/src/jpegr.cpp index f1203c84..548c2fce 100644 --- a/lib/src/jpegr.cpp +++ b/lib/src/jpegr.cpp @@ -63,19 +63,19 @@ static_assert(kWriteXmpMetadata || kWriteIso21496_1Metadata, class JobQueue { public: - bool dequeueJob(size_t& rowStart, size_t& rowEnd); - void enqueueJob(size_t rowStart, size_t rowEnd); + bool dequeueJob(unsigned int& rowStart, unsigned int& rowEnd); + void enqueueJob(unsigned int rowStart, unsigned int rowEnd); void markQueueForEnd(); void reset(); private: bool mQueuedAllJobs = false; - std::deque> mJobs; + std::deque> mJobs; std::mutex mMutex; std::condition_variable mCv; }; -bool JobQueue::dequeueJob(size_t& rowStart, size_t& rowEnd) { +bool JobQueue::dequeueJob(unsigned int& rowStart, unsigned int& rowEnd) { std::unique_lock lock{mMutex}; while (true) { if (mJobs.empty()) { @@ -95,7 +95,7 @@ bool JobQueue::dequeueJob(size_t& rowStart, size_t& rowEnd) { return false; } -void JobQueue::enqueueJob(size_t rowStart, size_t rowEnd) { +void JobQueue::enqueueJob(unsigned int rowStart, unsigned int rowEnd) { std::unique_lock lock{mMutex}; mJobs.push_back(std::make_tuple(rowStart, rowEnd)); lock.unlock(); @@ -126,26 +126,9 @@ class AlogMessageWriter : public MessageWriter { } }; -int GetCPUCoreCount() { - int cpuCoreCount = 1; - -#if defined(_WIN32) - SYSTEM_INFO system_info; - ZeroMemory(&system_info, sizeof(system_info)); - GetSystemInfo(&system_info); - cpuCoreCount = (size_t)system_info.dwNumberOfProcessors; -#elif defined(_SC_NPROCESSORS_ONLN) - cpuCoreCount = sysconf(_SC_NPROCESSORS_ONLN); -#elif defined(_SC_NPROCESSORS_CONF) - cpuCoreCount = sysconf(_SC_NPROCESSORS_CONF); -#else -#error platform-specific implementation for GetCPUCoreCount() missing. -#endif - if (cpuCoreCount <= 0) cpuCoreCount = 1; - return cpuCoreCount; -} +unsigned int GetCPUCoreCount() { return (std::max)(1u, std::thread::hardware_concurrency()); } -JpegR::JpegR(void* uhdrGLESCtxt, size_t mapDimensionScaleFactor, int mapCompressQuality, +JpegR::JpegR(void* uhdrGLESCtxt, int mapDimensionScaleFactor, int mapCompressQuality, bool useMultiChannelGainMap, float gamma, uhdr_enc_preset_t preset, float minContentBoost, float maxContentBoost, float targetDispPeakBrightness) { mUhdrGLESCtxt = uhdrGLESCtxt; @@ -662,17 +645,17 @@ uhdr_error_info_t JpegR::generateGainMap(uhdr_raw_image_t* sdr_intent, uhdr_raw_ sdrYuvToRgbFn = p3YuvToRgb; } - size_t image_width = sdr_intent->w; - size_t image_height = sdr_intent->h; - size_t map_width = image_width / mMapDimensionScaleFactor; - size_t map_height = image_height / mMapDimensionScaleFactor; + unsigned int image_width = sdr_intent->w; + unsigned int image_height = sdr_intent->h; + unsigned int map_width = image_width / mMapDimensionScaleFactor; + unsigned int map_height = image_height / mMapDimensionScaleFactor; if (map_width == 0 || map_height == 0) { int scaleFactor = (std::min)(image_width, image_height); scaleFactor = (scaleFactor >= DCTSIZE) ? (scaleFactor / DCTSIZE) : 1; ALOGW( "configured gainmap scale factor is resulting in gainmap width and/or height to be zero, " - "image width %d, image height %d, scale factor %d. Modifying gainmap scale factor to %d ", - (int)image_width, (int)image_height, (int)mMapDimensionScaleFactor, scaleFactor); + "image width %u, image height %u, scale factor %d. Modifying gainmap scale factor to %d ", + image_width, image_height, mMapDimensionScaleFactor, scaleFactor); setMapDimensionScaleFactor(scaleFactor); map_width = image_width / mMapDimensionScaleFactor; map_height = image_height / mMapDimensionScaleFactor; @@ -702,16 +685,16 @@ uhdr_error_info_t JpegR::generateGainMap(uhdr_raw_image_t* sdr_intent, uhdr_raw_ float log2MinBoost = log2(gainmap_metadata->min_content_boost); float log2MaxBoost = log2(gainmap_metadata->max_content_boost); - const int threads = (std::min)(GetCPUCoreCount(), 4); + const int threads = (std::min)(GetCPUCoreCount(), 4u); const int jobSizeInRows = 1; - size_t rowStep = threads == 1 ? map_height : jobSizeInRows; + unsigned int rowStep = threads == 1 ? map_height : jobSizeInRows; JobQueue jobQueue; std::function generateMap = [this, sdr_intent, hdr_intent, gainmap_metadata, dest, hdrInvOetf, hdrLuminanceFn, hdrOotfFn, hdrGamutConversionFn, luminanceFn, sdrYuvToRgbFn, hdrYuvToRgbFn, sdr_sample_pixel_fn, hdr_sample_pixel_fn, hdr_white_nits, log2MinBoost, log2MaxBoost, use_luminance, &jobQueue]() -> void { - size_t rowStart, rowEnd; + unsigned int rowStart, rowEnd; const bool isHdrIntentRgb = isPixelFormatRgb(hdr_intent->fmt); const bool isSdrIntentRgb = isPixelFormatRgb(sdr_intent->fmt); const float hdrSampleToNitsFactor = @@ -787,8 +770,8 @@ uhdr_error_info_t JpegR::generateGainMap(uhdr_raw_image_t* sdr_intent, uhdr_raw_ workers.push_back(std::thread(generateMap)); } - for (size_t rowStart = 0; rowStart < map_height;) { - size_t rowEnd = (std::min)(rowStart + rowStep, map_height); + for (unsigned int rowStart = 0; rowStart < map_height;) { + unsigned int rowEnd = (std::min)(rowStart + rowStep, map_height); jobQueue.enqueueJob(rowStart, rowEnd); rowStart = rowEnd; } @@ -801,23 +784,23 @@ uhdr_error_info_t JpegR::generateGainMap(uhdr_raw_image_t* sdr_intent, uhdr_raw_ [this, sdr_intent, hdr_intent, gainmap_metadata, dest, map_width, map_height, hdrInvOetf, hdrLuminanceFn, hdrOotfFn, hdrGamutConversionFn, luminanceFn, sdrYuvToRgbFn, hdrYuvToRgbFn, sdr_sample_pixel_fn, hdr_sample_pixel_fn, hdr_white_nits, use_luminance]() -> void { - uhdr_memory_block_t gainmap_mem(map_width * map_height * sizeof(float) * + uhdr_memory_block_t gainmap_mem((size_t)map_width * map_height * sizeof(float) * (mUseMultiChannelGainMap ? 3 : 1)); float* gainmap_data = reinterpret_cast(gainmap_mem.m_buffer.get()); float gainmap_min[3] = {127.0f, 127.0f, 127.0f}; float gainmap_max[3] = {-128.0f, -128.0f, -128.0f}; std::mutex gainmap_minmax; - const int threads = (std::min)(GetCPUCoreCount(), 4); + const int threads = (std::min)(GetCPUCoreCount(), 4u); const int jobSizeInRows = 1; - size_t rowStep = threads == 1 ? map_height : jobSizeInRows; + unsigned int rowStep = threads == 1 ? map_height : jobSizeInRows; JobQueue jobQueue; std::function generateMap = [this, sdr_intent, hdr_intent, gainmap_data, map_width, hdrInvOetf, hdrLuminanceFn, hdrOotfFn, hdrGamutConversionFn, luminanceFn, sdrYuvToRgbFn, hdrYuvToRgbFn, sdr_sample_pixel_fn, hdr_sample_pixel_fn, hdr_white_nits, use_luminance, &gainmap_min, &gainmap_max, &gainmap_minmax, &jobQueue]() -> void { - size_t rowStart, rowEnd; + unsigned int rowStart, rowEnd; const bool isHdrIntentRgb = isPixelFormatRgb(hdr_intent->fmt); const bool isSdrIntentRgb = isPixelFormatRgb(sdr_intent->fmt); const float hdrSampleToNitsFactor = @@ -903,8 +886,8 @@ uhdr_error_info_t JpegR::generateGainMap(uhdr_raw_image_t* sdr_intent, uhdr_raw_ workers.push_back(std::thread(generateMap)); } - for (size_t rowStart = 0; rowStart < map_height;) { - size_t rowEnd = (std::min)(rowStart + rowStep, map_height); + for (unsigned int rowStart = 0; rowStart < map_height;) { + unsigned int rowEnd = (std::min)(rowStart + rowStep, map_height); jobQueue.enqueueJob(rowStart, rowEnd); rowStart = rowEnd; } @@ -936,7 +919,7 @@ uhdr_error_info_t JpegR::generateGainMap(uhdr_raw_image_t* sdr_intent, uhdr_raw_ std::function encodeMap = [this, gainmap_data, map_width, dest, min_content_boost_log2, max_content_boost_log2, &jobQueue]() -> void { - size_t rowStart, rowEnd; + unsigned int rowStart, rowEnd; while (jobQueue.dequeueJob(rowStart, rowEnd)) { if (mUseMultiChannelGainMap) { @@ -968,8 +951,8 @@ uhdr_error_info_t JpegR::generateGainMap(uhdr_raw_image_t* sdr_intent, uhdr_raw_ for (int th = 0; th < threads - 1; th++) { workers.push_back(std::thread(encodeMap)); } - for (size_t rowStart = 0; rowStart < map_height;) { - size_t rowEnd = (std::min)(rowStart + rowStep, map_height); + for (unsigned int rowStart = 0; rowStart < map_height;) { + unsigned int rowEnd = (std::min)(rowStart + rowStep, map_height); jobQueue.enqueueJob(rowStart, rowEnd); rowStart = rowEnd; } @@ -1049,8 +1032,8 @@ uhdr_error_info_t JpegR::appendGainMap(uhdr_compressed_image_t* sdr_intent_compr uhdr_mem_block_t* pExif, void* pIcc, size_t icc_size, uhdr_gainmap_metadata_ext_t* metadata, uhdr_compressed_image_t* dest) { - const int xmpNameSpaceLength = kXmpNameSpace.size() + 1; // need to count the null terminator - const int isoNameSpaceLength = kIsoNameSpace.size() + 1; // need to count the null terminator + const size_t xmpNameSpaceLength = kXmpNameSpace.size() + 1; // need to count the null terminator + const size_t isoNameSpaceLength = kIsoNameSpace.size() + 1; // need to count the null terminator ///////////////////////////////////////////////////////////////////////////////////////////////// // calculate secondary image length first, because the length will be written into the primary // @@ -1061,7 +1044,7 @@ uhdr_error_info_t JpegR::appendGainMap(uhdr_compressed_image_t* sdr_intent_compr // xmp_secondary_length = 2 bytes representing the length of the package + // + xmpNameSpaceLength = 29 bytes length // + length of xmp packet = xmp_secondary.size() - const int xmp_secondary_length = 2 + xmpNameSpaceLength + xmp_secondary.size(); + const size_t xmp_secondary_length = 2 + xmpNameSpaceLength + xmp_secondary.size(); // ISO uhdr_gainmap_metadata_frac iso_secondary_metadata; std::vector iso_secondary_data; @@ -1074,9 +1057,9 @@ uhdr_error_info_t JpegR::appendGainMap(uhdr_compressed_image_t* sdr_intent_compr // iso_secondary_length = 2 bytes representing the length of the package + // + isoNameSpaceLength = 28 bytes length // + length of iso metadata packet = iso_secondary_data.size() - const int iso_secondary_length = 2 + isoNameSpaceLength + iso_secondary_data.size(); + const size_t iso_secondary_length = 2 + isoNameSpaceLength + iso_secondary_data.size(); - int secondary_image_size = 2 /* 2 bytes length of APP1 sign */ + gainmap_compressed->data_sz; + size_t secondary_image_size = 2 /* 2 bytes length of APP1 sign */ + gainmap_compressed->data_sz; if (kWriteXmpMetadata) { secondary_image_size += xmp_secondary_length; } @@ -1123,7 +1106,7 @@ uhdr_error_info_t JpegR::appendGainMap(uhdr_compressed_image_t* sdr_intent_compr uhdr_compressed_image_t* final_primary_jpg_image_ptr = new_jpg_image.data_sz == 0 ? sdr_intent_compressed : &new_jpg_image; - int pos = 0; + size_t pos = 0; // Begin primary image // Write SOI UHDR_ERR_CHECK(Write(dest, &photos_editing_formats::image_io::JpegMarker::kStart, 1, pos)); @@ -1131,7 +1114,7 @@ uhdr_error_info_t JpegR::appendGainMap(uhdr_compressed_image_t* sdr_intent_compr // Write EXIF if (pExif != nullptr) { - const int length = 2 + pExif->data_sz; + const size_t length = 2 + pExif->data_sz; const uint8_t lengthH = ((length >> 8) & 0xff); const uint8_t lengthL = (length & 0xff); UHDR_ERR_CHECK(Write(dest, &photos_editing_formats::image_io::JpegMarker::kStart, 1, pos)); @@ -1144,7 +1127,7 @@ uhdr_error_info_t JpegR::appendGainMap(uhdr_compressed_image_t* sdr_intent_compr // Prepare and write XMP if (kWriteXmpMetadata) { const string xmp_primary = generateXmpForPrimaryImage(secondary_image_size, *metadata); - const int length = 2 + xmpNameSpaceLength + xmp_primary.size(); + const size_t length = 2 + xmpNameSpaceLength + xmp_primary.size(); const uint8_t lengthH = ((length >> 8) & 0xff); const uint8_t lengthL = (length & 0xff); UHDR_ERR_CHECK(Write(dest, &photos_editing_formats::image_io::JpegMarker::kStart, 1, pos)); @@ -1157,7 +1140,7 @@ uhdr_error_info_t JpegR::appendGainMap(uhdr_compressed_image_t* sdr_intent_compr // Write ICC if (pIcc != nullptr && icc_size > 0) { - const int length = icc_size + 2; + const size_t length = icc_size + 2; const uint8_t lengthH = ((length >> 8) & 0xff); const uint8_t lengthL = (length & 0xff); UHDR_ERR_CHECK(Write(dest, &photos_editing_formats::image_io::JpegMarker::kStart, 1, pos)); @@ -1169,7 +1152,7 @@ uhdr_error_info_t JpegR::appendGainMap(uhdr_compressed_image_t* sdr_intent_compr // Prepare and write ISO 21496-1 metadata if (kWriteIso21496_1Metadata) { - const int length = 2 + isoNameSpaceLength + 4; + const size_t length = 2 + isoNameSpaceLength + 4; uint8_t zero = 0; const uint8_t lengthH = ((length >> 8) & 0xff); const uint8_t lengthL = (length & 0xff); @@ -1186,15 +1169,15 @@ uhdr_error_info_t JpegR::appendGainMap(uhdr_compressed_image_t* sdr_intent_compr // Prepare and write MPF { - const int length = 2 + calculateMpfSize(); + const size_t length = 2 + calculateMpfSize(); const uint8_t lengthH = ((length >> 8) & 0xff); const uint8_t lengthL = (length & 0xff); - int primary_image_size = pos + length + final_primary_jpg_image_ptr->data_sz; + size_t primary_image_size = pos + length + final_primary_jpg_image_ptr->data_sz; // between APP2 + package size + signature // ff e2 00 58 4d 50 46 00 // 2 + 2 + 4 = 8 (bytes) // and ff d8 sign of the secondary image - int secondary_image_offset = primary_image_size - pos - 8; + size_t secondary_image_offset = primary_image_size - pos - 8; std::shared_ptr mpf = generateMpf(primary_image_size, 0, /* primary_image_offset */ secondary_image_size, secondary_image_offset); UHDR_ERR_CHECK(Write(dest, &photos_editing_formats::image_io::JpegMarker::kStart, 1, pos)); @@ -1216,7 +1199,7 @@ uhdr_error_info_t JpegR::appendGainMap(uhdr_compressed_image_t* sdr_intent_compr // Prepare and write XMP if (kWriteXmpMetadata) { - const int length = xmp_secondary_length; + const size_t length = xmp_secondary_length; const uint8_t lengthH = ((length >> 8) & 0xff); const uint8_t lengthL = (length & 0xff); UHDR_ERR_CHECK(Write(dest, &photos_editing_formats::image_io::JpegMarker::kStart, 1, pos)); @@ -1229,7 +1212,7 @@ uhdr_error_info_t JpegR::appendGainMap(uhdr_compressed_image_t* sdr_intent_compr // Prepare and write ISO 21496-1 metadata if (kWriteIso21496_1Metadata) { - const int length = iso_secondary_length; + const size_t length = iso_secondary_length; const uint8_t lengthH = ((length >> 8) & 0xff); const uint8_t lengthL = (length & 0xff); UHDR_ERR_CHECK(Write(dest, &photos_editing_formats::image_io::JpegMarker::kStart, 1, pos)); @@ -1266,22 +1249,22 @@ uhdr_error_info_t JpegR::getJPEGRInfo(uhdr_compressed_image_t* uhdr_compressed_i return g_no_error; } -uhdr_error_info_t JpegR::parseGainMapMetadata(uint8_t* iso_data, int iso_size, uint8_t* xmp_data, - int xmp_size, +uhdr_error_info_t JpegR::parseGainMapMetadata(uint8_t* iso_data, size_t iso_size, uint8_t* xmp_data, + size_t xmp_size, uhdr_gainmap_metadata_ext_t* uhdr_metadata) { if (iso_size > 0) { - if (iso_size < (int)kIsoNameSpace.size() + 1) { + if (iso_size < kIsoNameSpace.size() + 1) { uhdr_error_info_t status; status.error_code = UHDR_CODEC_ERROR; status.has_detail = 1; snprintf(status.detail, sizeof status.detail, - "iso block size needs to be atleast %d but got %d", (int)kIsoNameSpace.size() + 1, + "iso block size needs to be atleast %zd but got %zd", kIsoNameSpace.size() + 1, iso_size); return status; } uhdr_gainmap_metadata_frac decodedMetadata; std::vector iso_vec; - for (int i = (int)kIsoNameSpace.size() + 1; i < iso_size; i++) { + for (size_t i = kIsoNameSpace.size() + 1; i < iso_size; i++) { iso_vec.push_back(iso_data[i]); } @@ -1497,8 +1480,8 @@ uhdr_error_info_t JpegR::applyGainMap(uhdr_raw_image_t* sdr_intent, uhdr_raw_ima gainmap_metadata, gainmap_weight, #endif map_scale_factor, get_pixel_fn, sdrYuvToRgbFn]() -> void { - size_t width = sdr_intent->w; - size_t rowStart, rowEnd; + unsigned int width = sdr_intent->w; + unsigned int rowStart, rowEnd; while (jobQueue.dequeueJob(rowStart, rowEnd)) { for (size_t y = rowStart; y < rowEnd; ++y) { @@ -1588,14 +1571,14 @@ uhdr_error_info_t JpegR::applyGainMap(uhdr_raw_image_t* sdr_intent, uhdr_raw_ima } }; - const int threads = (std::min)(GetCPUCoreCount(), 4); + const int threads = (std::min)(GetCPUCoreCount(), 4u); std::vector workers; for (int th = 0; th < threads - 1; th++) { workers.push_back(std::thread(applyRecMap)); } - const int rowStep = threads == 1 ? sdr_intent->h : map_scale_factor_rnd; - for (size_t rowStart = 0; rowStart < sdr_intent->h;) { - int rowEnd = (std::min)(rowStart + rowStep, (size_t)sdr_intent->h); + const unsigned int rowStep = threads == 1 ? sdr_intent->h : map_scale_factor_rnd; + for (unsigned int rowStart = 0; rowStart < sdr_intent->h;) { + unsigned int rowEnd = (std::min)(rowStart + rowStep, sdr_intent->h); jobQueue.enqueueJob(rowStart, rowEnd); rowStart = rowEnd; } @@ -1675,10 +1658,10 @@ uhdr_error_info_t JpegR::extractPrimaryImageAndGainMap(uhdr_compressed_image_t* } uhdr_error_info_t JpegR::parseJpegInfo(uhdr_compressed_image_t* jpeg_image, j_info_ptr image_info, - size_t* img_width, size_t* img_height) { + unsigned int* img_width, unsigned int* img_height) { JpegDecoderHelper jpeg_dec_obj; UHDR_ERR_CHECK(jpeg_dec_obj.parseImage(jpeg_image->data, jpeg_image->data_sz)) - size_t imgWidth, imgHeight, numComponents; + unsigned int imgWidth, imgHeight, numComponents; imgWidth = jpeg_dec_obj.getDecompressedImageWidth(); imgHeight = jpeg_dec_obj.getDecompressedImageHeight(); numComponents = jpeg_dec_obj.getNumComponentsInImage(); @@ -1895,18 +1878,18 @@ uhdr_error_info_t JpegR::toneMap(uhdr_raw_image_t* hdr_intent, uhdr_raw_image_t* ColorTransformFn hdrGamutConversionFn = getGamutConversionFn(sdr_intent->cg, hdr_intent->cg); - size_t height = hdr_intent->h; - const int threads = (std::min)(GetCPUCoreCount(), 4); + unsigned int height = hdr_intent->h; + const int threads = (std::min)(GetCPUCoreCount(), 4u); // for 420 subsampling, process 2 rows at once const int jobSizeInRows = hdr_intent->fmt == UHDR_IMG_FMT_24bppYCbCrP010 ? 2 : 1; - size_t rowStep = threads == 1 ? height : jobSizeInRows; + unsigned int rowStep = threads == 1 ? height : jobSizeInRows; JobQueue jobQueue; std::function toneMapInternal; toneMapInternal = [hdr_intent, sdr_intent, hdrInvOetf, hdrGamutConversionFn, hdrYuvToRgbFn, hdr_white_nits, get_pixel_fn, put_pixel_fn, hdrLuminanceFn, hdrOotfFn, &jobQueue]() -> void { - size_t rowStart, rowEnd; + unsigned int rowStart, rowEnd; const int hfactor = hdr_intent->fmt == UHDR_IMG_FMT_24bppYCbCrP010 ? 2 : 1; const int vfactor = hdr_intent->fmt == UHDR_IMG_FMT_24bppYCbCrP010 ? 2 : 1; const bool isHdrIntentRgb = isPixelFormatRgb(hdr_intent->fmt); @@ -1984,8 +1967,8 @@ uhdr_error_info_t JpegR::toneMap(uhdr_raw_image_t* hdr_intent, uhdr_raw_image_t* workers.push_back(std::thread(toneMapInternal)); } - for (size_t rowStart = 0; rowStart < height;) { - size_t rowEnd = (std::min)(rowStart + rowStep, height); + for (unsigned int rowStart = 0; rowStart < height;) { + unsigned int rowEnd = (std::min)(rowStart + rowStep, height); jobQueue.enqueueJob(rowStart, rowEnd); rowStart = rowEnd; } @@ -2005,17 +1988,17 @@ status_t JpegR::areInputArgumentsValid(jr_uncompressed_ptr p010_image_ptr, return ERROR_JPEGR_BAD_PTR; } if (p010_image_ptr->width % 2 != 0 || p010_image_ptr->height % 2 != 0) { - ALOGE("Image dimensions cannot be odd, image dimensions %zux%zu", p010_image_ptr->width, + ALOGE("Image dimensions cannot be odd, image dimensions %ux%u", p010_image_ptr->width, p010_image_ptr->height); return ERROR_JPEGR_UNSUPPORTED_WIDTH_HEIGHT; } if ((int)p010_image_ptr->width < kMinWidth || (int)p010_image_ptr->height < kMinHeight) { - ALOGE("Image dimensions cannot be less than %dx%d, image dimensions %zux%zu", kMinWidth, + ALOGE("Image dimensions cannot be less than %dx%d, image dimensions %ux%u", kMinWidth, kMinHeight, p010_image_ptr->width, p010_image_ptr->height); return ERROR_JPEGR_UNSUPPORTED_WIDTH_HEIGHT; } if ((int)p010_image_ptr->width > kMaxWidth || (int)p010_image_ptr->height > kMaxHeight) { - ALOGE("Image dimensions cannot be larger than %dx%d, image dimensions %zux%zu", kMaxWidth, + ALOGE("Image dimensions cannot be larger than %dx%d, image dimensions %ux%u", kMaxWidth, kMaxHeight, p010_image_ptr->width, p010_image_ptr->height); return ERROR_JPEGR_UNSUPPORTED_WIDTH_HEIGHT; } @@ -2025,13 +2008,13 @@ status_t JpegR::areInputArgumentsValid(jr_uncompressed_ptr p010_image_ptr, return ERROR_JPEGR_INVALID_COLORGAMUT; } if (p010_image_ptr->luma_stride != 0 && p010_image_ptr->luma_stride < p010_image_ptr->width) { - ALOGE("Luma stride must not be smaller than width, stride=%zu, width=%zu", + ALOGE("Luma stride must not be smaller than width, stride=%u, width=%u", p010_image_ptr->luma_stride, p010_image_ptr->width); return ERROR_JPEGR_INVALID_STRIDE; } if (p010_image_ptr->chroma_data != nullptr && p010_image_ptr->chroma_stride < p010_image_ptr->width) { - ALOGE("Chroma stride must not be smaller than width, stride=%zu, width=%zu", + ALOGE("Chroma stride must not be smaller than width, stride=%u, width=%u", p010_image_ptr->chroma_stride, p010_image_ptr->width); return ERROR_JPEGR_INVALID_STRIDE; } @@ -2044,7 +2027,7 @@ status_t JpegR::areInputArgumentsValid(jr_uncompressed_ptr p010_image_ptr, return ERROR_JPEGR_INVALID_TRANS_FUNC; } if (mMapDimensionScaleFactor <= 0 || mMapDimensionScaleFactor > 128) { - ALOGE("gainmap scale factor is ecpected to be in range (0, 128], received %zu", + ALOGE("gainmap scale factor is ecpected to be in range (0, 128], received %d", mMapDimensionScaleFactor); return ERROR_JPEGR_UNSUPPORTED_MAP_SCALE_FACTOR; } @@ -2084,19 +2067,19 @@ status_t JpegR::areInputArgumentsValid(jr_uncompressed_ptr p010_image_ptr, } if (yuv420_image_ptr->luma_stride != 0 && yuv420_image_ptr->luma_stride < yuv420_image_ptr->width) { - ALOGE("Luma stride must not be smaller than width, stride=%zu, width=%zu", + ALOGE("Luma stride must not be smaller than width, stride=%u, width=%u", yuv420_image_ptr->luma_stride, yuv420_image_ptr->width); return ERROR_JPEGR_INVALID_STRIDE; } if (yuv420_image_ptr->chroma_data != nullptr && yuv420_image_ptr->chroma_stride < yuv420_image_ptr->width / 2) { - ALOGE("Chroma stride must not be smaller than (width / 2), stride=%zu, width=%zu", + ALOGE("Chroma stride must not be smaller than (width / 2), stride=%u, width=%u", yuv420_image_ptr->chroma_stride, yuv420_image_ptr->width); return ERROR_JPEGR_INVALID_STRIDE; } if (p010_image_ptr->width != yuv420_image_ptr->width || p010_image_ptr->height != yuv420_image_ptr->height) { - ALOGE("Image resolutions mismatch: P010: %zux%zu, YUV420: %zux%zu", p010_image_ptr->width, + ALOGE("Image resolutions mismatch: P010: %ux%u, YUV420: %ux%u", p010_image_ptr->width, p010_image_ptr->height, yuv420_image_ptr->width, yuv420_image_ptr->height); return ERROR_JPEGR_RESOLUTION_MISMATCH; } @@ -2175,7 +2158,7 @@ status_t JpegR::encodeJPEGR(jr_uncompressed_ptr p010_image_ptr, ultrahdr_transfe if (p010_image.luma_stride == 0) p010_image.luma_stride = p010_image.width; if (!p010_image.chroma_data) { uint16_t* data = reinterpret_cast(p010_image.data); - p010_image.chroma_data = data + p010_image.luma_stride * p010_image.height; + p010_image.chroma_data = data + (size_t)p010_image.luma_stride * p010_image.height; p010_image.chroma_stride = p010_image.luma_stride; } @@ -2236,7 +2219,7 @@ status_t JpegR::encodeJPEGR(jr_uncompressed_ptr p010_image_ptr, if (p010_image.luma_stride == 0) p010_image.luma_stride = p010_image.width; if (!p010_image.chroma_data) { uint16_t* data = reinterpret_cast(p010_image.data); - p010_image.chroma_data = data + p010_image.luma_stride * p010_image.height; + p010_image.chroma_data = data + (size_t)p010_image.luma_stride * p010_image.height; p010_image.chroma_stride = p010_image.luma_stride; } uhdr_raw_image_t hdr_intent; @@ -2257,7 +2240,7 @@ status_t JpegR::encodeJPEGR(jr_uncompressed_ptr p010_image_ptr, if (yuv420_image.luma_stride == 0) yuv420_image.luma_stride = yuv420_image.width; if (!yuv420_image.chroma_data) { uint8_t* data = reinterpret_cast(yuv420_image.data); - yuv420_image.chroma_data = data + yuv420_image.luma_stride * yuv420_image.height; + yuv420_image.chroma_data = data + (size_t)yuv420_image.luma_stride * yuv420_image.height; yuv420_image.chroma_stride = yuv420_image.luma_stride >> 1; } uhdr_raw_image_t sdrRawImg; @@ -2322,7 +2305,7 @@ status_t JpegR::encodeJPEGR(jr_uncompressed_ptr p010_image_ptr, if (p010_image.luma_stride == 0) p010_image.luma_stride = p010_image.width; if (!p010_image.chroma_data) { uint16_t* data = reinterpret_cast(p010_image.data); - p010_image.chroma_data = data + p010_image.luma_stride * p010_image.height; + p010_image.chroma_data = data + (size_t)p010_image.luma_stride * p010_image.height; p010_image.chroma_stride = p010_image.luma_stride; } uhdr_raw_image_t hdr_intent; @@ -2343,7 +2326,7 @@ status_t JpegR::encodeJPEGR(jr_uncompressed_ptr p010_image_ptr, if (yuv420_image.luma_stride == 0) yuv420_image.luma_stride = yuv420_image.width; if (!yuv420_image.chroma_data) { uint8_t* data = reinterpret_cast(yuv420_image.data); - yuv420_image.chroma_data = data + yuv420_image.luma_stride * p010_image.height; + yuv420_image.chroma_data = data + (size_t)yuv420_image.luma_stride * p010_image.height; yuv420_image.chroma_stride = yuv420_image.luma_stride >> 1; } uhdr_raw_image_t sdrRawImg; @@ -2404,7 +2387,7 @@ status_t JpegR::encodeJPEGR(jr_uncompressed_ptr p010_image_ptr, if (p010_image.luma_stride == 0) p010_image.luma_stride = p010_image.width; if (!p010_image.chroma_data) { uint16_t* data = reinterpret_cast(p010_image.data); - p010_image.chroma_data = data + p010_image.luma_stride * p010_image.height; + p010_image.chroma_data = data + (size_t)p010_image.luma_stride * p010_image.height; p010_image.chroma_stride = p010_image.luma_stride; } uhdr_raw_image_t hdr_intent; diff --git a/lib/src/jpegrutils.cpp b/lib/src/jpegrutils.cpp index 42338479..f7062e79 100644 --- a/lib/src/jpegrutils.cpp +++ b/lib/src/jpegrutils.cpp @@ -60,9 +60,9 @@ DataStruct::~DataStruct() { void* DataStruct::getData() { return data; } -int DataStruct::getLength() { return length; } +size_t DataStruct::getLength() { return length; } -int DataStruct::getBytesWritten() { return writePos; } +size_t DataStruct::getBytesWritten() { return writePos; } bool DataStruct::write8(uint8_t value) { uint8_t v = value; @@ -79,9 +79,9 @@ bool DataStruct::write32(uint32_t value) { return write(&v, 4); } -bool DataStruct::write(const void* src, int size) { +bool DataStruct::write(const void* src, size_t size) { if (writePos + size > length) { - ALOGE("Writing out of boundary: write position: %d, size: %d, capacity: %d", writePos, size, + ALOGE("Writing out of boundary: write position: %zd, size: %zd, capacity: %zd", writePos, size, length); return false; } @@ -93,14 +93,16 @@ bool DataStruct::write(const void* src, int size) { /* * Helper function used for writing data to destination. */ -uhdr_error_info_t Write(uhdr_compressed_image_t* destination, const void* source, int length, - int& position) { - if (position + length > (int)destination->capacity) { +uhdr_error_info_t Write(uhdr_compressed_image_t* destination, const void* source, size_t length, + size_t& position) { + if (position + length > destination->capacity) { uhdr_error_info_t status; status.error_code = UHDR_CODEC_MEM_ERROR; status.has_detail = 1; snprintf(status.detail, sizeof status.detail, - "output buffer to store compressed data is too small"); + "output buffer to store compressed data is too small: write position: %zd, size: %zd, " + "capacity: %zd", + position, length, destination->capacity); return status; } @@ -440,17 +442,17 @@ const string XMPXmlHandler::hdrCapacityMinAttrName = kMapHDRCapacityMin; const string XMPXmlHandler::hdrCapacityMaxAttrName = kMapHDRCapacityMax; const string XMPXmlHandler::baseRenditionIsHdrAttrName = kMapBaseRenditionIsHDR; -uhdr_error_info_t getMetadataFromXMP(uint8_t* xmp_data, int xmp_size, +uhdr_error_info_t getMetadataFromXMP(uint8_t* xmp_data, size_t xmp_size, uhdr_gainmap_metadata_ext_t* metadata) { string nameSpace = "http://ns.adobe.com/xap/1.0/\0"; - if (xmp_size < (int)nameSpace.size() + 2) { + if (xmp_size < nameSpace.size() + 2) { uhdr_error_info_t status; status.error_code = UHDR_CODEC_ERROR; status.has_detail = 1; snprintf(status.detail, sizeof status.detail, - "size of xmp block is expected to be atleast %d bytes, received only %d bytes", - (int)nameSpace.size() + 2, xmp_size); + "size of xmp block is expected to be atleast %zd bytes, received only %zd bytes", + nameSpace.size() + 2, xmp_size); return status; } @@ -472,8 +474,8 @@ uhdr_error_info_t getMetadataFromXMP(uint8_t* xmp_data, int xmp_size, // xml parser fails to parse packet header, wrapper. remove them before handing the data to // parser. if there is no packet header, do nothing otherwise go to the position of '<' without // '?' after it. - int offset = 0; - for (int i = 0; i < xmp_size - 1; ++i) { + size_t offset = 0; + for (size_t i = 0; i < xmp_size - 1; ++i) { if (xmp_data[i] == '<') { if (xmp_data[i + 1] != '?') { offset = i; @@ -487,7 +489,7 @@ uhdr_error_info_t getMetadataFromXMP(uint8_t* xmp_data, int xmp_size, // If there is no packet wrapper, do nothing other wise go to the position of last '>' without '?' // before it. offset = 0; - for (int i = xmp_size - 1; i >= 1; --i) { + for (size_t i = xmp_size - 1; i >= 1; --i) { if (xmp_data[i] == '>') { if (xmp_data[i - 1] != '?') { offset = xmp_size - (i + 1); @@ -625,7 +627,7 @@ uhdr_error_info_t getMetadataFromXMP(uint8_t* xmp_data, int xmp_size, return g_no_error; } -string generateXmpForPrimaryImage(int secondary_image_length, +string generateXmpForPrimaryImage(size_t secondary_image_length, uhdr_gainmap_metadata_ext_t& metadata) { const vector kConDirSeq({kConDirectory, string("rdf:Seq")}); const vector kLiItem({string("rdf:li"), kConItem}); diff --git a/lib/src/multipictureformat.cpp b/lib/src/multipictureformat.cpp index 59efc66e..4a82a8bf 100644 --- a/lib/src/multipictureformat.cpp +++ b/lib/src/multipictureformat.cpp @@ -27,8 +27,9 @@ size_t calculateMpfSize() { kNumPictures * kMPEntrySize; // MP Entries for each image } -std::shared_ptr generateMpf(int primary_image_size, int primary_image_offset, - int secondary_image_size, int secondary_image_offset) { +std::shared_ptr generateMpf(size_t primary_image_size, size_t primary_image_offset, + size_t secondary_image_size, + size_t secondary_image_offset) { size_t mpf_size = calculateMpfSize(); std::shared_ptr dataStruct = std::make_shared(mpf_size); diff --git a/lib/src/ultrahdr_api.cpp b/lib/src/ultrahdr_api.cpp index 7cbc4ad8..2500aae1 100644 --- a/lib/src/ultrahdr_api.cpp +++ b/lib/src/ultrahdr_api.cpp @@ -512,7 +512,7 @@ uhdr_error_info_t uhdr_enc_validate_and_set_compressed_img(uhdr_codec_private_t* } else if (img->capacity < img->data_sz) { status.error_code = UHDR_CODEC_INVALID_PARAM; status.has_detail = 1; - snprintf(status.detail, sizeof status.detail, "img->capacity %d is less than img->data_sz %d", + snprintf(status.detail, sizeof status.detail, "img->capacity %zd is less than img->data_sz %zd", img->capacity, img->data_sz); } if (status.error_code != UHDR_CODEC_OK) return status; @@ -1114,8 +1114,8 @@ uhdr_error_info_t uhdr_enc_set_exif_data(uhdr_codec_private_t* enc, uhdr_mem_blo } else if (exif->capacity < exif->data_sz) { status.error_code = UHDR_CODEC_INVALID_PARAM; status.has_detail = 1; - snprintf(status.detail, sizeof status.detail, "exif->capacity %d is less than exif->data_sz %d", - exif->capacity, exif->data_sz); + snprintf(status.detail, sizeof status.detail, + "exif->capacity %zd is less than exif->data_sz %zd", exif->capacity, exif->data_sz); } if (status.error_code != UHDR_CODEC_OK) return status; @@ -1247,7 +1247,8 @@ uhdr_error_info_t uhdr_encode(uhdr_codec_private_t* enc) { auto& base_entry = handle->m_compressed_images.find(UHDR_BASE_IMG)->second; auto& gainmap_entry = handle->m_compressed_images.find(UHDR_GAIN_MAP_IMG)->second; - size_t size = (std::max)((8u * 1024), 2 * (base_entry->data_sz + gainmap_entry->data_sz)); + size_t size = + (std::max)(((size_t)8 * 1024), 2 * (base_entry->data_sz + gainmap_entry->data_sz)); handle->m_compressed_output_buffer = std::make_unique( UHDR_CG_UNSPECIFIED, UHDR_CT_UNSPECIFIED, UHDR_CR_UNSPECIFIED, size); @@ -1414,7 +1415,7 @@ uhdr_error_info_t uhdr_dec_set_image(uhdr_codec_private_t* dec, uhdr_compressed_ } else if (img->capacity < img->data_sz) { status.error_code = UHDR_CODEC_INVALID_PARAM; status.has_detail = 1; - snprintf(status.detail, sizeof status.detail, "img->capacity %d is less than img->data_sz %d", + snprintf(status.detail, sizeof status.detail, "img->capacity %zd is less than img->data_sz %zd", img->capacity, img->data_sz); } if (status.error_code != UHDR_CODEC_OK) return status; diff --git a/tests/jpegencoderhelper_test.cpp b/tests/jpegencoderhelper_test.cpp index 703d0851..0783ab2f 100644 --- a/tests/jpegencoderhelper_test.cpp +++ b/tests/jpegencoderhelper_test.cpp @@ -47,8 +47,8 @@ class JpegEncoderHelperTest : public testing::Test { public: struct Image { std::unique_ptr buffer; - size_t width; - size_t height; + unsigned int width; + unsigned int height; }; JpegEncoderHelperTest(); ~JpegEncoderHelperTest(); @@ -108,7 +108,8 @@ TEST_F(JpegEncoderHelperTest, encodeAlignedImage) { const uint8_t* uPlane = yPlane + mAlignedImage.width * mAlignedImage.height; const uint8_t* vPlane = uPlane + mAlignedImage.width * mAlignedImage.height / 4; const uint8_t* planes[3]{yPlane, uPlane, vPlane}; - const size_t strides[3]{mAlignedImage.width, mAlignedImage.width / 2, mAlignedImage.width / 2}; + const unsigned int strides[3]{mAlignedImage.width, mAlignedImage.width / 2, + mAlignedImage.width / 2}; EXPECT_EQ(encoder .compressImage(planes, strides, mAlignedImage.width, mAlignedImage.height, UHDR_IMG_FMT_12bppYCbCr420, JPEG_QUALITY, NULL, 0) @@ -123,8 +124,8 @@ TEST_F(JpegEncoderHelperTest, encodeUnalignedImage) { const uint8_t* uPlane = yPlane + mUnalignedImage.width * mUnalignedImage.height; const uint8_t* vPlane = uPlane + mUnalignedImage.width * mUnalignedImage.height / 4; const uint8_t* planes[3]{yPlane, uPlane, vPlane}; - const size_t strides[3]{mUnalignedImage.width, mUnalignedImage.width / 2, - mUnalignedImage.width / 2}; + const unsigned int strides[3]{mUnalignedImage.width, mUnalignedImage.width / 2, + mUnalignedImage.width / 2}; EXPECT_EQ(encoder .compressImage(planes, strides, mUnalignedImage.width, mUnalignedImage.height, UHDR_IMG_FMT_12bppYCbCr420, JPEG_QUALITY, NULL, 0) @@ -137,7 +138,7 @@ TEST_F(JpegEncoderHelperTest, encodeSingleChannelImage) { JpegEncoderHelper encoder; const uint8_t* yPlane = mSingleChannelImage.buffer.get(); const uint8_t* planes[1]{yPlane}; - const size_t strides[1]{mSingleChannelImage.width}; + const unsigned int strides[1]{mSingleChannelImage.width}; EXPECT_EQ( encoder .compressImage(planes, strides, mSingleChannelImage.width, mSingleChannelImage.height, @@ -151,7 +152,7 @@ TEST_F(JpegEncoderHelperTest, encodeRGBImage) { JpegEncoderHelper encoder; const uint8_t* rgbPlane = mRgbImage.buffer.get(); const uint8_t* planes[1]{rgbPlane}; - const size_t strides[1]{mRgbImage.width}; + const unsigned int strides[1]{mRgbImage.width}; EXPECT_EQ(encoder .compressImage(planes, strides, mRgbImage.width, mRgbImage.height, UHDR_IMG_FMT_24bppRGB888, JPEG_QUALITY, NULL, 0) diff --git a/tests/jpegr_test.cpp b/tests/jpegr_test.cpp index dc4cde54..82db6bf7 100644 --- a/tests/jpegr_test.cpp +++ b/tests/jpegr_test.cpp @@ -66,11 +66,11 @@ typedef enum { */ class UhdrUnCompressedStructWrapper { public: - UhdrUnCompressedStructWrapper(size_t width, size_t height, UhdrInputFormat format); + UhdrUnCompressedStructWrapper(unsigned int width, unsigned int height, UhdrInputFormat format); ~UhdrUnCompressedStructWrapper() = default; bool setChromaMode(bool isChromaContiguous); - bool setImageStride(size_t lumaStride, size_t chromaStride); + bool setImageStride(unsigned int lumaStride, unsigned int chromaStride); bool setImageColorGamut(ultrahdr_color_gamut colorGamut); bool allocateMemory(); bool loadRawResource(const char* fileName); @@ -92,7 +92,7 @@ class UhdrUnCompressedStructWrapper { */ class UhdrCompressedStructWrapper { public: - UhdrCompressedStructWrapper(size_t width, size_t height); + UhdrCompressedStructWrapper(unsigned int width, unsigned int height); ~UhdrCompressedStructWrapper() = default; bool allocateMemory(); @@ -101,11 +101,12 @@ class UhdrCompressedStructWrapper { private: std::unique_ptr mData; jpegr_compressed_struct mImg{}; - size_t mWidth; - size_t mHeight; + unsigned int mWidth; + unsigned int mHeight; }; -UhdrUnCompressedStructWrapper::UhdrUnCompressedStructWrapper(size_t width, size_t height, +UhdrUnCompressedStructWrapper::UhdrUnCompressedStructWrapper(unsigned int width, + unsigned int height, UhdrInputFormat format) { mImg.data = nullptr; mImg.width = width; @@ -127,7 +128,8 @@ bool UhdrUnCompressedStructWrapper::setChromaMode(bool isChromaContiguous) { return true; } -bool UhdrUnCompressedStructWrapper::setImageStride(size_t lumaStride, size_t chromaStride) { +bool UhdrUnCompressedStructWrapper::setImageStride(unsigned int lumaStride, + unsigned int chromaStride) { if (mLumaData.get() != nullptr) { std::cerr << "Object has sailed, no further modifications are allowed" << std::endl; return false; @@ -255,7 +257,7 @@ bool UhdrUnCompressedStructWrapper::loadRawResource(const char* fileName) { jr_uncompressed_ptr UhdrUnCompressedStructWrapper::getImageHandle() { return &mImg; } -UhdrCompressedStructWrapper::UhdrCompressedStructWrapper(size_t width, size_t height) { +UhdrCompressedStructWrapper::UhdrCompressedStructWrapper(unsigned int width, unsigned int height) { mWidth = width; mHeight = height; } @@ -287,7 +289,7 @@ static bool writeFile(const char* filename, void*& result, int length) { } #endif -static bool readFile(const char* fileName, void*& result, int maxLength, int& length) { +static bool readFile(const char* fileName, void*& result, size_t maxLength, size_t& length) { std::ifstream ifd(fileName, std::ios::binary | std::ios::ate); if (ifd.good()) { length = ifd.tellg(); @@ -1409,7 +1411,7 @@ TEST(JpegRTest, writeXmpThenRead) { metadata_expected.hdr_capacity_min = 1.0f; metadata_expected.hdr_capacity_max = metadata_expected.max_content_boost; const std::string nameSpace = "http://ns.adobe.com/xap/1.0/\0"; - const int nameSpaceLength = nameSpace.size() + 1; // need to count the null terminator + const size_t nameSpaceLength = nameSpace.size() + 1; // need to count the null terminator std::string xmp = generateXmpForSecondaryImage(metadata_expected); @@ -2209,7 +2211,7 @@ class Profiler { void timerStop() { QueryPerformanceCounter(&mEndingTime); } - int64_t elapsedTime() { + double elapsedTime() { LARGE_INTEGER frequency; LARGE_INTEGER elapsedMicroseconds; QueryPerformanceFrequency(&frequency); diff --git a/ultrahdr_api.h b/ultrahdr_api.h index f3d1128d..54c91c28 100644 --- a/ultrahdr_api.h +++ b/ultrahdr_api.h @@ -23,6 +23,8 @@ #ifndef ULTRAHDR_API_H #define ULTRAHDR_API_H +#include + #if defined(_WIN32) || defined(__CYGWIN__) #if defined(UHDR_BUILDING_SHARED_LIBRARY) #define UHDR_API __declspec(dllexport) @@ -234,8 +236,8 @@ typedef struct uhdr_raw_image { /**\brief Compressed Image Descriptor */ typedef struct uhdr_compressed_image { void* data; /**< Pointer to a block of data to decode */ - unsigned int data_sz; /**< size of the data buffer */ - unsigned int capacity; /**< maximum size of the data buffer */ + size_t data_sz; /**< size of the data buffer */ + size_t capacity; /**< maximum size of the data buffer */ uhdr_color_gamut_t cg; /**< Color Gamut */ uhdr_color_transfer_t ct; /**< Color Transfer */ uhdr_color_range_t range; /**< Color Range */ @@ -243,10 +245,10 @@ typedef struct uhdr_compressed_image { /**\brief Buffer Descriptor */ typedef struct uhdr_mem_block { - void* data; /**< Pointer to a block of data to decode */ - unsigned int data_sz; /**< size of the data buffer */ - unsigned int capacity; /**< maximum size of the data buffer */ -} uhdr_mem_block_t; /**< alias for struct uhdr_mem_block */ + void* data; /**< Pointer to a block of data to decode */ + size_t data_sz; /**< size of the data buffer */ + size_t capacity; /**< maximum size of the data buffer */ +} uhdr_mem_block_t; /**< alias for struct uhdr_mem_block */ /**\brief Gain map metadata. */ typedef struct uhdr_gainmap_metadata {