]> gitweb.hhaalo.de Git - sane-kds-s2000w-net.git/commitdiff
add gray jpg convert to pnm
authorBastian Dehn <hhaalo@arcor.de>
Sun, 26 Jan 2025 09:37:29 +0000 (10:37 +0100)
committerBastian Dehn <hhaalo@arcor.de>
Sun, 26 Jan 2025 09:37:29 +0000 (10:37 +0100)
src/kds_s2000w_image_converter_netpbm.c

index 937aeac9b01a94e0148dc865ede2697f5f285cfb..0c5a493943d75783a4c5bd0c733a390e1ba795de 100644 (file)
@@ -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)