]> gitweb.hhaalo.de Git - sane-kds-s2000w-net.git/commitdiff
add implemtation for jpeg to pnm
authorBastian Dehn <hhaalo@arcor.de>
Sat, 25 Jan 2025 22:32:54 +0000 (23:32 +0100)
committerBastian Dehn <hhaalo@arcor.de>
Sat, 25 Jan 2025 22:32:54 +0000 (23:32 +0100)
src/kds_s2000w_image_converter_netpbm.c

index 9e2acb71ffe372de8328c4595cec1e5a4ac20268..d2b7945488054f68edaba679f31b0780d478042d 100644 (file)
@@ -1,10 +1,13 @@
+#include <stdio.h>
 #include <string.h>
 #include <netpbm/pnm.h>
+#include <jpeglib.h>
 #include "kds_s2000w_image_converter.h"
 
 #define SPACE 0x0a
 #define SPACE_WIDTH_HEIGHT 0x20
 #define MAXBUFFER 6
+#define RGB 3
 
 int _kds_s2000w_image_converter_find_char(blobdata* image, int start, const char cfind)
 {
@@ -65,7 +68,52 @@ void kds_s2000w_image_converter_metadata_from_scanner_image(blobdata* image, ima
 
 void kds_s2000w_image_converter_jpg_to_pnm(blobdata* in, blobdata* out, int depth)
 {
-       return;
+       FILE* jpeg_stream = fmemopen(in->data, in->size, "r");
+
+       struct jpeg_decompress_struct cinfo;
+       struct jpeg_error_mgr err;
+
+       cinfo.err = jpeg_std_error(&err);
+       jpeg_create_decompress(&cinfo);
+       jpeg_stdio_src(&cinfo, jpeg_stream);
+       jpeg_read_header(&cinfo, TRUE);
+       jpeg_start_decompress(&cinfo);
+
+       int 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];
+               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 += 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 = (char*) out->data;
+       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);
+
+       jpeg_finish_decompress(&cinfo);
+       jpeg_destroy_decompress(&cinfo);
 }
 
 void kds_s2000w_image_converter_tiff_to_pnm(blobdata* in, blobdata* out)