From 48403be73cef5eec91ef5ba3d07ce3fdc88a0f2a Mon Sep 17 00:00:00 2001 From: Bastian Dehn Date: Wed, 29 Jan 2025 18:34:58 +0100 Subject: [PATCH] change write pnm one method --- src/kds_s2000w_image_converter_netpbm.c | 109 ++++++++++-------------- 1 file changed, 44 insertions(+), 65 deletions(-) diff --git a/src/kds_s2000w_image_converter_netpbm.c b/src/kds_s2000w_image_converter_netpbm.c index 9dffe0f..d282ded 100644 --- a/src/kds_s2000w_image_converter_netpbm.c +++ b/src/kds_s2000w_image_converter_netpbm.c @@ -10,8 +10,12 @@ #define SPACE 0x0a #define SPACE_WIDTH_HEIGHT 0x20 #define MAXBUFFER 6 -#define RGB 3 -#define GRAY 1 + +enum imgformat { + RGB = 3, + GRAY = 1, + BW = 1 +}; tsize_t _kds_s2000w_image_converter_tiff_read(thandle_t handler, tdata_t data, tsize_t size) { @@ -128,94 +132,69 @@ void _kds_s2000w_image_converter_jpeg_metadata(blobdata* image, image_metadata* fclose(jpeg_stream); } -void _kds_s2000w_image_color_jpg_to_pnm(j_decompress_ptr cinfo, - FILE* jpeg_stream, +void _kds_s2000w_image_converter_write_pnm(unsigned char* data, + size_t size, + int width, + int height, + enum imgformat format, FILE* pnm_stream) { - kds_s2000w_debug_printf(ALL, "kds_s2000w_image_color_jpg_to_pnm"); - - size_t decompress_size = RGB * cinfo->output_width * cinfo->output_height; - unsigned char* decompress_data = malloc(sizeof(unsigned char) * decompress_size); + kds_s2000w_debug_printf(ALL, "kds_s2000w_image_converter_write_pnm"); - unsigned char* row = NULL; - while (cinfo->output_scanline < cinfo->output_height) { - row = &decompress_data[cinfo->output_width * cinfo->output_scanline * RGB]; - jpeg_read_scanlines(cinfo, &row, 1); - } - fclose(jpeg_stream); - - xel** pixels = malloc(sizeof(xel*) * cinfo->output_height); - for (int i = 0; i < cinfo->output_height; i++) { - pixels[i] = malloc(sizeof(xel) * cinfo->output_width); + xel** pixels = malloc(sizeof(xel*) * height); + for (int i = 0; i < height; i++) { + pixels[i] = malloc(sizeof(xel) * width); } int currwidth = 0; int currheight = 0; - for (int i = 0; i < decompress_size; i += RGB) { - pixels[currheight][currwidth].r = decompress_data[i]; - pixels[currheight][currwidth].g = decompress_data[i + 1]; - pixels[currheight][currwidth].b = decompress_data[i + 2]; + for (int i = 0; i < size; i += format) { + if (format == RGB) { + pixels[currheight][currwidth].r = data[i]; + pixels[currheight][currwidth].g = data[i + 1]; + pixels[currheight][currwidth].b = data[i + 2]; + } else { + pixels[currheight][currwidth].r = 0; + pixels[currheight][currwidth].g = 0; + pixels[currheight][currwidth].b = data[i]; + } currwidth++; - if (currwidth >= cinfo->output_width) { + if (currwidth >= width) { currwidth = 0; currheight++; } } - pnm_writepnm(pnm_stream, pixels, cinfo->output_width, cinfo->output_height, PNM_MAXMAXVAL, PPM_FORMAT, 0); - - for (int i = 0; i < cinfo->output_height; i++) { - free(pixels[i]); - pixels[i] = NULL; - } - free(pixels); - pixels = NULL; - free(decompress_data); - decompress_data = NULL; + if (format == RGB) + pnm_writepnm(pnm_stream, pixels, width, height, PNM_MAXMAXVAL, PPM_FORMAT, 0); + else + pnm_writepnm(pnm_stream, pixels, width, height, PGM_MAXMAXVAL, PGM_FORMAT, 0); } -void _kds_s2000w_image_gray_jpg_to_pnm(j_decompress_ptr cinfo, +void _kds_s2000w_image_converter_jpg_to_pnm(j_decompress_ptr cinfo, FILE* jpeg_stream, - FILE* pnm_stream) + FILE* pnm_stream, + enum imgformat format) { - kds_s2000w_debug_printf(ALL, "kds_s2000w_image_gray_jpg_to_pnm"); + kds_s2000w_debug_printf(ALL, "kds_s2000w_image_converter_jpg_to_pnm"); - size_t decompress_size = GRAY * cinfo->output_width * cinfo->output_height; + size_t decompress_size = format * cinfo->output_width * cinfo->output_height; unsigned char* decompress_data = malloc(sizeof(unsigned char) * decompress_size); unsigned char* row = NULL; while (cinfo->output_scanline < cinfo->output_height) { - row = &decompress_data[cinfo->output_width * cinfo->output_scanline * GRAY]; + row = &decompress_data[cinfo->output_width * cinfo->output_scanline * format]; jpeg_read_scanlines(cinfo, &row, 1); } fclose(jpeg_stream); - xel** pixels = malloc(sizeof(xel*) * cinfo->output_height); - for (int i = 0; i < cinfo->output_height; i++) { - pixels[i] = malloc(sizeof(xel) * cinfo->output_width); - } - - int currwidth = 0; - int currheight = 0; - for (int i = 0; i < decompress_size; i += GRAY) { - pixels[currheight][currwidth].r = 0; - pixels[currheight][currwidth].g = 0; - pixels[currheight][currwidth].b = decompress_data[i]; - currwidth++; - if (currwidth >= cinfo->output_width) { - currwidth = 0; - currheight++; - } - } - - pnm_writepnm(pnm_stream, pixels, cinfo->output_width, cinfo->output_height, PGM_MAXMAXVAL, PGM_FORMAT, 0); + _kds_s2000w_image_converter_write_pnm(decompress_data, + decompress_size, + cinfo->output_width, + cinfo->output_height, + format, + pnm_stream); - for (int i = 0; i < cinfo->output_height; i++) { - free(pixels[i]); - pixels[i] = NULL; - } - free(pixels); - pixels = NULL; free(decompress_data); decompress_data = NULL; } @@ -265,10 +244,10 @@ void kds_s2000w_image_converter_jpg_to_pnm(blobdata* in, blobdata* out, int dept FILE* pnm_stream = open_memstream(outdata, &out->size); if (cinfo.out_color_space == JCS_RGB) - _kds_s2000w_image_color_jpg_to_pnm(&cinfo, jpeg_stream, pnm_stream); + _kds_s2000w_image_converter_jpg_to_pnm(&cinfo, jpeg_stream, pnm_stream, RGB); if (cinfo.out_color_space == JCS_GRAYSCALE) - _kds_s2000w_image_gray_jpg_to_pnm(&cinfo, jpeg_stream, pnm_stream); + _kds_s2000w_image_converter_jpg_to_pnm(&cinfo, jpeg_stream, pnm_stream, GRAY); fclose(pnm_stream); out->data = malloc(sizeof(char) * out->size); -- 2.39.5