From: Bastian Dehn Date: Sun, 26 Jan 2025 09:37:29 +0000 (+0100) Subject: add gray jpg convert to pnm X-Git-Tag: v1.0.29^2~3^2~26 X-Git-Url: https://gitweb.hhaalo.de/?a=commitdiff_plain;h=2720f2fa7cf81c0b9638cc82fc376587664d2a8a;p=sane-kds-s2000w-net.git add gray jpg convert to pnm --- diff --git a/src/kds_s2000w_image_converter_netpbm.c b/src/kds_s2000w_image_converter_netpbm.c index 937aeac..0c5a493 100644 --- a/src/kds_s2000w_image_converter_netpbm.c +++ b/src/kds_s2000w_image_converter_netpbm.c @@ -8,6 +8,7 @@ #define SPACE_WIDTH_HEIGHT 0x20 #define MAXBUFFER 6 #define RGB 3 +#define GRAY 1 int _kds_s2000w_image_converter_find_char(blobdata* image, int start, const char cfind) { @@ -102,55 +103,108 @@ void kds_s2000w_image_converter_jpg_to_pnm(blobdata* in, blobdata* out, int dept jpeg_read_header(&cinfo, TRUE); jpeg_start_decompress(&cinfo); - size_t decompress_size = RGB * cinfo.output_width * cinfo.output_height; - unsigned char* decompress_data = malloc(sizeof(unsigned char) * decompress_size); + 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); + 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*) * 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++; + 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++; + } } + + 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; + + free(decompress_data); + decompress_data = NULL; } - 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; + 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; } - free(pixels); - pixels = NULL; + jpeg_finish_decompress(&cinfo); jpeg_destroy_decompress(&cinfo); - free(decompress_data); - decompress_data = NULL; } void kds_s2000w_image_converter_tiff_to_pnm(blobdata* in, blobdata* out)