Skip to content

Commit

Permalink
fix (thumbnailer): format update & fix png artifact
Browse files Browse the repository at this point in the history
  • Loading branch information
mickael-kerjean committed Nov 20, 2023
1 parent 70e43cd commit 837f440
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 9 deletions.
1 change: 1 addition & 0 deletions config/mime.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions server/plugin/plg_image_c/image_jpeg.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
14 changes: 7 additions & 7 deletions server/plugin/plg_image_c/image_png.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,25 +52,25 @@ 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) {
ERROR("0 dimensions");
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;
Expand All @@ -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);
}
}
}
Expand All @@ -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) {
Expand Down

0 comments on commit 837f440

Please sign in to comment.