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

index a1c331b4d9b98ec853be6f42ea13be0a18304327..937aeac9b01a94e0148dc865ede2697f5f285cfb 100644 (file)
@@ -102,12 +102,12 @@ void kds_s2000w_image_converter_jpg_to_pnm(blobdata* in, blobdata* out, int dept
        jpeg_read_header(&cinfo, TRUE);
        jpeg_start_decompress(&cinfo);
 
-       int decompress_size = RGB * cinfo.output_width * cinfo.output_height;
+       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];
+               row = &decompress_data[cinfo.output_width * cinfo.output_scanline * RGB];
                jpeg_read_scanlines(&cinfo, &row, 1);
        }
        fclose(jpeg_stream);
@@ -130,13 +130,27 @@ void kds_s2000w_image_converter_jpg_to_pnm(blobdata* in, blobdata* out, int dept
                }
        }
 
-       char* outdata = (char*) out->data;
-       FILE* pnm_stream = open_memstream(&outdata, &out->size);
+       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;
        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)