]> gitweb.hhaalo.de Git - sane-kds-s2000w-net.git/commitdiff
change write pnm one method
authorBastian Dehn <hhaalo@arcor.de>
Wed, 29 Jan 2025 17:34:58 +0000 (18:34 +0100)
committerBastian Dehn <hhaalo@arcor.de>
Wed, 29 Jan 2025 17:35:15 +0000 (18:35 +0100)
src/kds_s2000w_image_converter_netpbm.c

index 9dffe0fdc9167011a09d3149ddd36ff23c753ec1..d282ded4e6de83de28477fa08f26e501307cecb5 100644 (file)
 #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);