From: Bastian Dehn Date: Wed, 29 Jan 2025 19:17:30 +0000 (+0100) Subject: change write pnm with tiff X-Git-Tag: v1.0.29^2~3^2~13 X-Git-Url: https://gitweb.hhaalo.de/?a=commitdiff_plain;h=4001c1202a4ce90952ec572baeff7591d9163d3c;p=sane-kds-s2000w-net.git change write pnm with tiff --- diff --git a/src/kds_s2000w_image_converter_netpbm.c b/src/kds_s2000w_image_converter_netpbm.c index d282ded..6067801 100644 --- a/src/kds_s2000w_image_converter_netpbm.c +++ b/src/kds_s2000w_image_converter_netpbm.c @@ -12,9 +12,9 @@ #define MAXBUFFER 6 enum imgformat { - RGB = 3, + BW = 0, GRAY = 1, - BW = 1 + RGB = 3 }; tsize_t _kds_s2000w_image_converter_tiff_read(thandle_t handler, tdata_t data, tsize_t size) @@ -132,6 +132,45 @@ void _kds_s2000w_image_converter_jpeg_metadata(blobdata* image, image_metadata* fclose(jpeg_stream); } +void _kds_s2000w_image_converter_write_bw_pnm(unsigned char* data, + size_t size, + int width, + int height, + FILE* pnm_stream) +{ + kds_s2000w_debug_printf(ALL, "kds_s2000w_image_converter_write_bw_pnm"); + + 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 < size; i++) { + pixels[currheight][currwidth].r = 0; + pixels[currheight][currwidth].g = 0; + pixels[currheight][currwidth].b = 0; + if (data[i] < 1) + pixels[currheight][currwidth].b = PGM_MAXMAXVAL; + + currwidth++; + if (currwidth >= width) { + currwidth = 0; + currheight++; + } + } + + pnm_writepnm(pnm_stream, pixels, width, height, PNM_MAXMAXVAL, PBM_FORMAT, 0); + + for (int i = 0; i < height; i++) { + free(pixels[i]); + pixels[i] = NULL; + } + free(pixels); + pixels = NULL; +} + void _kds_s2000w_image_converter_write_pnm(unsigned char* data, size_t size, int width, @@ -141,6 +180,15 @@ void _kds_s2000w_image_converter_write_pnm(unsigned char* data, { kds_s2000w_debug_printf(ALL, "kds_s2000w_image_converter_write_pnm"); + if (format == BW) { + _kds_s2000w_image_converter_write_bw_pnm(data, + size, + width, + height, + pnm_stream); + return; + } + xel** pixels = malloc(sizeof(xel*) * height); for (int i = 0; i < height; i++) { pixels[i] = malloc(sizeof(xel) * width); @@ -169,6 +217,13 @@ void _kds_s2000w_image_converter_write_pnm(unsigned char* data, 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); + + for (int i = 0; i < height; i++) { + free(pixels[i]); + pixels[i] = NULL; + } + free(pixels); + pixels = NULL; } void _kds_s2000w_image_converter_jpg_to_pnm(j_decompress_ptr cinfo, @@ -282,36 +337,36 @@ void kds_s2000w_image_converter_tiff_to_pnm(blobdata* in, blobdata* out) TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &width); TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &height); - bit** bits = malloc(sizeof(bit*) * height); - for (int i = 0; i < height; i++) { - bits[i] = malloc(sizeof(bit) * width); - } + size_t decompress_size = width * height; + unsigned char* decompress_data = malloc(sizeof(unsigned char) * decompress_size); - unsigned char* rowbuf = malloc(sizeof(unsigned char) * width); + unsigned char* row = NULL; for (int i = 0; i < height; i++) { - TIFFReadScanline(tiff, bits[i], i, 0); + row = &decompress_data[width * i]; + TIFFReadScanline(tiff, row, i, 0); } - free(rowbuf); - rowbuf = NULL; + row = NULL; char** outdata = malloc(sizeof(char*)); FILE* pnm_stream = open_memstream(outdata, &out->size); - pbm_writepbm(pnm_stream, bits, width, height, 0); + _kds_s2000w_image_converter_write_pnm(decompress_data, + decompress_size, + width, + height, + BW, + pnm_stream); fclose(pnm_stream); pnm_stream = NULL; out->data = malloc(sizeof(char) * out->size); memcpy(out->data, *outdata, out->size); + free(decompress_data); + decompress_data = NULL; free(*outdata); *outdata = NULL; free(outdata); outdata = NULL; TIFFClose(tiff); - for (int i = 0; i < height; i++) { - free(bits[i]); - bits[i] = NULL; - } - free(bits); - bits = NULL; + tiff = NULL; }