summaryrefslogtreecommitdiffstats
path: root/virtual-programs
diff options
context:
space:
mode:
authorOmar Rizwan <omar@omar.website>2023-06-15 18:12:41 +0000
committerOmar Rizwan <omar@omar.website>2023-06-15 18:12:41 +0000
commitef58dfeb4a6c81ee2508935416dbefb7615a3065 (patch)
tree2717cff463f8db8c722ba2a3ca995dfe2af9e52f /virtual-programs
parentUse diffs to make peering more efficient. Narrow change detect (diff)
downloadfolk-ef58dfeb4a6c81ee2508935416dbefb7615a3065.tar.gz
folk-ef58dfeb4a6c81ee2508935416dbefb7615a3065.zip
Add image saveAsJpeg
Diffstat (limited to 'virtual-programs')
-rw-r--r--virtual-programs/images.folk59
1 files changed, 59 insertions, 0 deletions
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 <stdlib.h>
+ $cc include <string.h>
+ $cc include <jpeglib.h>
+
+ $cc code {
+ #include <jpeglib.h>
+ #include <stdint.h>
+ #include <unistd.h>
+
+ 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
}