From ef58dfeb4a6c81ee2508935416dbefb7615a3065 Mon Sep 17 00:00:00 2001 From: Omar Rizwan Date: Thu, 15 Jun 2023 14:12:41 -0400 Subject: Add image saveAsJpeg --- virtual-programs/images.folk | 59 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) (limited to 'virtual-programs') diff --git a/virtual-programs/images.folk b/virtual-programs/images.folk index e2af03af..5e9bdfac 100644 --- a/virtual-programs/images.folk +++ b/virtual-programs/images.folk @@ -23,6 +23,65 @@ namespace eval ::image { data [format 0x%x $subdata] } } + + set cc [c create] + $cc cflags -L[lindex [exec /usr/sbin/ldconfig -p | grep libjpeg] end] + defineImageType $cc + $cc include + $cc include + $cc include + + $cc code { + #include + #include + #include + + void + jpeg(FILE* dest, uint8_t* rgb, uint32_t width, uint32_t height, int quality) + { + JSAMPARRAY image; + image = calloc(height, sizeof (JSAMPROW)); + for (size_t i = 0; i < height; i++) { + image[i] = calloc(width * 3, sizeof (JSAMPLE)); + for (size_t j = 0; j < width; j++) { + image[i][j * 3 + 0] = rgb[(i * width + j)]; + image[i][j * 3 + 1] = rgb[(i * width + j)]; + image[i][j * 3 + 2] = rgb[(i * width + j)]; + } + } + + struct jpeg_compress_struct compress; + struct jpeg_error_mgr error; + compress.err = jpeg_std_error(&error); + jpeg_create_compress(&compress); + jpeg_stdio_dest(&compress, dest); + + compress.image_width = width; + compress.image_height = height; + compress.input_components = 3; + compress.in_color_space = JCS_RGB; + jpeg_set_defaults(&compress); + jpeg_set_quality(&compress, quality, TRUE); + jpeg_start_compress(&compress, TRUE); + jpeg_write_scanlines(&compress, image, height); + jpeg_finish_compress(&compress); + jpeg_destroy_compress(&compress); + + for (size_t i = 0; i < height; i++) { + free(image[i]); + } + free(image); + } + + } + $cc proc saveAsJpeg {image_t im char* filename} void { + FILE* out = fopen(filename, "w"); + jpeg(out, im.data, im.width, im.height, 100); + fclose(out); + } + c loadlib [lindex [exec /usr/sbin/ldconfig -p | grep libjpeg] end] + $cc compile + namespace export * namespace ensemble create } -- cgit v1.2.3