From: Bastian Dehn Date: Sun, 26 Jan 2025 09:58:38 +0000 (+0100) Subject: refactor jpg color and gray to pnm to methods X-Git-Tag: v1.0.29^2~3^2~25 X-Git-Url: https://gitweb.hhaalo.de/?a=commitdiff_plain;h=cfe2a4a5d570f9116e964acf1d295a9cf8b88741;p=sane-kds-s2000w-net.git refactor jpg color and gray to pnm to methods --- diff --git a/src/kds_s2000w_image_converter_netpbm.c b/src/kds_s2000w_image_converter_netpbm.c index 0c5a493..5f83beb 100644 --- a/src/kds_s2000w_image_converter_netpbm.c +++ b/src/kds_s2000w_image_converter_netpbm.c @@ -74,6 +74,92 @@ 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, + FILE* pnm_stream) +{ + size_t decompress_size = RGB * 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 * 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); + } + + 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]; + currwidth++; + if (currwidth >= cinfo->output_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; +} + +void _kds_s2000w_image_gray_jpg_to_pnm(j_decompress_ptr cinfo, + FILE* jpeg_stream, + FILE* pnm_stream) +{ + size_t decompress_size = GRAY * 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]; + jpeg_read_scanlines(cinfo, &row, 1); + } + fclose(jpeg_stream); + + gray** pixels = malloc(sizeof(gray*) * cinfo->output_height); + for (int i = 0; i < cinfo->output_height; i++) { + pixels[i] = malloc(sizeof(gray) * cinfo->output_width); + } + + int currwidth = 0; + int currheight = 0; + for (int i = 0; i < decompress_size; i += GRAY) { + pixels[currheight][currwidth] = decompress_data[i]; + currwidth++; + if (currwidth >= cinfo->output_width) { + currwidth = 0; + currheight++; + } + } + + pgm_writepgm(pnm_stream, pixels, cinfo->output_width, cinfo->output_height, PNM_MAXMAXVAL, 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; +} + void kds_s2000w_image_converter_metadata_from_scanner_image(blobdata* image, image_metadata* mdata) { if (image->size < 2) @@ -103,105 +189,22 @@ void kds_s2000w_image_converter_jpg_to_pnm(blobdata* in, blobdata* out, int dept jpeg_read_header(&cinfo, TRUE); jpeg_start_decompress(&cinfo); - if (cinfo.out_color_space == JCS_RGB) { - size_t decompress_size = RGB * 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 * 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); - } + char** outdata = malloc(sizeof(char*)); + FILE* pnm_stream = open_memstream(outdata, &out->size); - 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]; - currwidth++; - if (currwidth >= cinfo.output_width) { - currwidth = 0; - currheight++; - } - } + if (cinfo.out_color_space == JCS_RGB) + _kds_s2000w_image_color_jpg_to_pnm(&cinfo, jpeg_stream, pnm_stream); - char** outdata = malloc(sizeof(char*)); - FILE* pnm_stream = open_memstream(outdata, &out->size); - pnm_writepnm(pnm_stream, pixels, cinfo.output_width, cinfo.output_height, PNM_MAXMAXVAL, PPM_FORMAT, 0); - fclose(pnm_stream); - out->data = malloc(sizeof(char) * out->size); - memcpy(out->data, *outdata, out->size); - free(*outdata); - *outdata = NULL; - free(outdata); - outdata = NULL; - - for (int i = 0; i < cinfo.output_height; i++) { - free(pixels[i]); - pixels[i] = NULL; - } - free(pixels); - pixels = NULL; + if (cinfo.out_color_space == JCS_GRAYSCALE) + _kds_s2000w_image_gray_jpg_to_pnm(&cinfo, jpeg_stream, pnm_stream); - free(decompress_data); - decompress_data = NULL; - } - - if (cinfo.out_color_space == JCS_GRAYSCALE) { - size_t decompress_size = GRAY * 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]; - jpeg_read_scanlines(&cinfo, &row, 1); - } - fclose(jpeg_stream); - - gray** pixels = malloc(sizeof(gray*) * cinfo.output_height); - for (int i = 0; i < cinfo.output_height; i++) { - pixels[i] = malloc(sizeof(gray) * cinfo.output_width); - } - - int currwidth = 0; - int currheight = 0; - for (int i = 0; i < decompress_size; i += GRAY) { - pixels[currheight][currwidth] = decompress_data[i]; - currwidth++; - if (currwidth >= cinfo.output_width) { - currwidth = 0; - currheight++; - } - } - - char** outdata = malloc(sizeof(char*)); - FILE* pnm_stream = open_memstream(outdata, &out->size); - pgm_writepgm(pnm_stream, pixels, cinfo.output_width, cinfo.output_height, PNM_MAXMAXVAL, 0); - fclose(pnm_stream); - out->data = malloc(sizeof(char) * out->size); - memcpy(out->data, *outdata, out->size); - free(*outdata); - *outdata = NULL; - free(outdata); - outdata = NULL; - - 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; - } + fclose(pnm_stream); + out->data = malloc(sizeof(char) * out->size); + memcpy(out->data, *outdata, out->size); + free(*outdata); + *outdata = NULL; + free(outdata); + outdata = NULL; jpeg_finish_decompress(&cinfo); jpeg_destroy_decompress(&cinfo);