diff --git a/modules/imgcodecs/src/grfmt_spng.cpp b/modules/imgcodecs/src/grfmt_spng.cpp index e3c61164e4..fa15bd46c7 100644 --- a/modules/imgcodecs/src/grfmt_spng.cpp +++ b/modules/imgcodecs/src/grfmt_spng.cpp @@ -521,7 +521,6 @@ int SPngEncoder::writeDataToBuf(void *ctx, void *user, void *dst_src, size_t len bool SPngEncoder::write(const Mat &img, const std::vector ¶ms) { - int fmt; spng_ctx *ctx = spng_ctx_new(SPNG_CTX_ENCODER); FILE *volatile f = 0; int width = img.cols, height = img.rows; @@ -558,11 +557,10 @@ bool SPngEncoder::write(const Mat &img, const std::vector ¶ms) isBilevel = params[i + 1] != 0; } } - fmt = channels == 1 ? SPNG_COLOR_TYPE_GRAYSCALE : channels == 3 ? SPNG_COLOR_TYPE_TRUECOLOR - : SPNG_COLOR_TYPE_TRUECOLOR_ALPHA; ihdr.bit_depth = depth == CV_8U ? isBilevel ? 1 : 8 : 16; - ihdr.color_type = fmt; + ihdr.color_type = (uint8_t)(channels == 1 ? SPNG_COLOR_TYPE_GRAYSCALE : channels == 3 ? SPNG_COLOR_TYPE_TRUECOLOR + : SPNG_COLOR_TYPE_TRUECOLOR_ALPHA); ihdr.interlace_method = SPNG_INTERLACE_NONE; ihdr.filter_method = SPNG_FILTER_NONE; ihdr.compression_method = 0; @@ -597,7 +595,7 @@ bool SPngEncoder::write(const Mat &img, const std::vector ¶ms) ret = spng_encode_image(ctx, nullptr, 0, SPNG_FMT_PNG, SPNG_ENCODE_PROGRESSIVE); if (channels > 1) { - int error; + int error = SPNG_OK; if (ret == SPNG_OK) { if (depth == CV_16U) @@ -659,7 +657,7 @@ bool SPngEncoder::write(const Mat &img, const std::vector ¶ms) } else { - int error; + int error = SPNG_OK; for (int y = 0; y < height; y++) { error = spng_encode_row(ctx, img.data + y * img.step, width * channels * (depth == CV_16U ? 2 : 1)); @@ -711,17 +709,17 @@ void spngCvt_BGRA2Gray_8u_C4C1R(const uchar *bgra, int rgba_step, uchar *gray, int gray_step, cv::Size size, int _swap_rb) { - int i; for (; size.height--; gray += gray_step) { double cBGR0 = 0.1140441895; + double cBGR1 = 0.5869750977; double cBGR2 = 0.2989807129; + if (_swap_rb) std::swap(cBGR0, cBGR2); - for (i = 0; i < size.width; i++, bgra += 4) + for (int i = 0; i < size.width; i++, bgra += 4) { - int t = cBGR0 * bgra[0] + 0.5869750977 * bgra[1] + cBGR2 * bgra[2]; - gray[i] = (uchar)t; + gray[i] = cv::saturate_cast(cBGR0 * bgra[0] + cBGR1 * bgra[1] + cBGR2 * bgra[2]); } bgra += rgba_step - size.width * 4; @@ -732,17 +730,17 @@ void spngCvt_BGRA2Gray_16u_CnC1R(const ushort *bgr, int bgr_step, ushort *gray, int gray_step, cv::Size size, int ncn, int _swap_rb) { - int i; for (; size.height--; gray += gray_step) { double cBGR0 = 0.1140441895; + double cBGR1 = 0.5869750977; double cBGR2 = 0.2989807129; + if (_swap_rb) std::swap(cBGR0, cBGR2); - for (i = 0; i < size.width; i++, bgr += ncn) + for (int i = 0; i < size.width; i++, bgr += ncn) { - int t = cBGR0 * bgr[0] + 0.5869750977 * bgr[1] + cBGR2 * bgr[2]; - gray[i] = (ushort)t; + gray[i] = (ushort)(cBGR0 * bgr[0] + cBGR1 * bgr[1] + cBGR2 * bgr[2]); } bgr += bgr_step - size.width * ncn;