From 837f440e36aa4d769db365239034e67a14e19370 Mon Sep 17 00:00:00 2001 From: MickaelK Date: Mon, 20 Nov 2023 20:51:20 +1100 Subject: [PATCH] fix (thumbnailer): format update & fix png artifact --- config/mime.json | 1 + server/plugin/plg_image_c/image_jpeg.c | 4 ++-- server/plugin/plg_image_c/image_png.c | 14 +++++++------- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/config/mime.json b/config/mime.json index a45541e9a..ccfffb390 100644 --- a/config/mime.json +++ b/config/mime.json @@ -123,6 +123,7 @@ "prc": "application/x-pilot", "properties": "text/x-ini", "ps": "application/postscript", + "psd": "image/vnd.adobe.photoshop", "ra": "audio/x-realaudio", "raf": "image/x-fuji-raf", "ram": "audio/x-pn-realaudio", diff --git a/server/plugin/plg_image_c/image_jpeg.c b/server/plugin/plg_image_c/image_jpeg.c index f141e9a54..7c4b91bf7 100644 --- a/server/plugin/plg_image_c/image_jpeg.c +++ b/server/plugin/plg_image_c/image_jpeg.c @@ -19,8 +19,8 @@ int jpeg_to_jpeg(int inputDesc, int outputDesc, int targetSize) { t = clock(); #endif int status = 0; - FILE* input = fdopen(inputDesc, "r"); - FILE* output = fdopen(outputDesc, "w"); + FILE* input = fdopen(inputDesc, "rb"); + FILE* output = fdopen(outputDesc, "wb"); if (!input || !output) { return 1; } diff --git a/server/plugin/plg_image_c/image_png.c b/server/plugin/plg_image_c/image_png.c index 3c8c8b224..fa0c91b20 100644 --- a/server/plugin/plg_image_c/image_png.c +++ b/server/plugin/plg_image_c/image_png.c @@ -52,17 +52,17 @@ int png_to_webp(int inputDesc, int outputDesc, int targetSize) { if (color_type == PNG_COLOR_TYPE_PALETTE) { png_set_palette_to_rgb(png_ptr); } - if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) { + if (color_type == PNG_COLOR_TYPE_GRAY) { png_set_expand_gray_1_2_4_to_8(png_ptr); } - if (bit_depth == 16) { - png_set_strip_16(png_ptr); + if (color_type & PNG_COLOR_MASK_ALPHA) { + png_set_strip_alpha(png_ptr); } png_read_update_info(png_ptr, info_ptr); DEBUG("after png construct"); // STEP2: process the image - int scale_factor = width > targetSize ? width / targetSize : 1; + int scale_factor = height > targetSize ? height / targetSize : 1; png_uint_32 thumb_width = width / scale_factor; png_uint_32 thumb_height = height / scale_factor; if (thumb_width == 0 || thumb_height == 0) { @@ -70,7 +70,7 @@ int png_to_webp(int inputDesc, int outputDesc, int targetSize) { status = 1; goto CLEANUP_AND_ABORT_B; } - uint8_t* webp_image_data = (uint8_t*)malloc(thumb_width * thumb_height * 4); + uint8_t* webp_image_data = (uint8_t*)malloc(thumb_width * thumb_height * 3); if (!webp_image_data) { ERROR("malloc error"); status = 1; @@ -90,7 +90,7 @@ int png_to_webp(int inputDesc, int outputDesc, int targetSize) { if (x / scale_factor < thumb_width) { png_uint_32 thumb_x = x / scale_factor; png_uint_32 thumb_y = y / scale_factor; - memcpy(webp_image_data + (thumb_y * thumb_width + thumb_x) * 4, row + x * 4, 4); + memcpy(webp_image_data + (thumb_y * thumb_width + thumb_x) * 3, row + x * 3, 3); } } } @@ -102,7 +102,7 @@ int png_to_webp(int inputDesc, int outputDesc, int targetSize) { // STEP3: save as webp uint8_t* webp_output_data = NULL; - size_t webp_output_size = WebPEncodeRGBA(webp_image_data, thumb_width, thumb_height, thumb_width * 4, 75, &webp_output_data); + size_t webp_output_size = WebPEncodeRGB(webp_image_data, thumb_width, thumb_height, thumb_width * 3, 75, &webp_output_data); free(webp_image_data); DEBUG("after webp init"); if (webp_output_data == NULL) {