summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFRIGN <dev@frign.de>2015-11-23 00:11:17 +0100
committerFRIGN <dev@frign.de>2015-11-23 00:11:17 +0100
commit2c4b975c421456bf14c2891a753da09b4a50cb0a (patch)
treea1215f6527b33a2874ea38ccbaab5451a88dd3a1
parenta593273a24500f14c66e5ca0c2a9948db22f36a7 (diff)
(Re)add jpg2ff
Thanks z3bra for porting this! Also change 2ff to use case instead of if-blocks.
-rwxr-xr-x2ff10
-rw-r--r--Makefile10
-rw-r--r--config.mk2
-rw-r--r--jpg2ff.c117
4 files changed, 128 insertions, 11 deletions
diff --git a/2ff b/2ff
index 8901c78..af4d7b3 100755
--- a/2ff
+++ b/2ff
@@ -1,8 +1,8 @@
#!/bin/sh
FORMAT=`file -ib "$1" | cut -d ";" -f 1`
-if [ "$FORMAT" = "image/png" ]; then
- png2ff < "$1"
-else
- convert "$1" png:- | png2ff
-fi;
+case "$FORMAT" in
+ image/png) png2ff < "$1" ;;
+ image/jpeg) jpg2ff < "$1" ;;
+ *) convert "$1" png:- | png2ff ;;
+esac
diff --git a/Makefile b/Makefile
index 966bf3f..f54f7b4 100644
--- a/Makefile
+++ b/Makefile
@@ -1,23 +1,23 @@
-# farbfeld - tools to convert between png and ff
+# farbfeld - tools to convert between png/jpg and ff
# See LICENSE file for copyright and license details
include config.mk
-SRC = png2ff.c ff2png.c
+SRC = png2ff.c ff2png.c jpg2ff.c
-all: png2ff ff2png
+all: png2ff ff2png jpg2ff
.c:
@echo CC $<
@${CC} -o $@ ${CFLAGS} ${LIBS} ${LDFLAGS} $<
clean:
- rm -f png2ff ff2png
+ rm -f png2ff ff2png jpg2ff
install:
@echo installing into ${DESTDIR}${PREFIX}/bin
@mkdir -p ${DESTDIR}${PREFIX}/bin
- @cp -f png2ff ff2png 2ff ${DESTDIR}${PREFIX}/bin
+ @cp -f jpg2ff png2ff ff2png 2ff ${DESTDIR}${PREFIX}/bin
uninstall:
@echo removing from ${DESTDIR}${PREFIX}/bin
diff --git a/config.mk b/config.mk
index 2bd55ed..d8a62b3 100644
--- a/config.mk
+++ b/config.mk
@@ -7,7 +7,7 @@ PNGLIB = /usr/local/lib
PNGINC = /usr/local/include
INCS = -I${PNGINC}
-LIBS = -L${PNGLIB} -lpng
+LIBS = -L${PNGLIB} -lpng -ljpeg
# flags
CPPFLAGS = -D_DEFAULT_SOURCE
diff --git a/jpg2ff.c b/jpg2ff.c
new file mode 100644
index 0000000..c6d8a02
--- /dev/null
+++ b/jpg2ff.c
@@ -0,0 +1,117 @@
+/* See LICENSE file for copyright and license details. */
+#include <arpa/inet.h>
+#include <errno.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <setjmp.h>
+#include <jpeglib.h>
+
+char *argv0;
+
+static jmp_buf setjmp_buffer;
+
+static void
+usage(void)
+{
+ fprintf(stderr, "usage: %s\n", argv0);
+ exit(EXIT_FAILURE);
+}
+
+METHODDEF(void)
+if_jpeg_error(j_common_ptr cinfo)
+{
+ /* Always display the message. */
+ /* We could postpone this until after returning, if we chose. */
+ (*cinfo->err->output_message) (cinfo);
+
+ /* Return control to the setjmp point */
+ longjmp(setjmp_buffer, 1);
+}
+
+int
+main(int argc, char *argv[])
+{
+ struct jpeg_decompress_struct cinfo;
+ struct jpeg_error_mgr jerr;
+ uint32_t width, height, val_be;
+ uint16_t *ff_row = NULL;
+ size_t jpeg_row_len, ff_row_len, i, dx, sx;
+ int status = EXIT_FAILURE;
+ JSAMPARRAY buffer; /* output row buffer */
+
+ argv0 = argv[0];
+ if (argc > 1)
+ usage();
+
+ /* load jpeg */
+ cinfo.err = jpeg_std_error(&jerr);
+
+ jerr.error_exit = if_jpeg_error;
+ /* Establish the setjmp return context for my_error_exit to use. */
+ if (setjmp(setjmp_buffer)) {
+ /* If we get here, the JPEG code has signaled an error.
+ * We need to clean up the JPEG object, close the input file, and return. */
+ goto cleanup;
+ }
+
+ jpeg_create_decompress(&cinfo);
+ jpeg_stdio_src(&cinfo, stdin);
+
+ jpeg_read_header(&cinfo, TRUE);
+ width = cinfo.image_width;
+ height = cinfo.image_height;
+
+ /* change output for farbfeld */
+ cinfo.output_components = 3; /* # of color components per pixel */
+ cinfo.out_color_space = JCS_RGB; /* colorspace of input image */
+
+ jpeg_start_decompress(&cinfo);
+ jpeg_row_len = width * cinfo.output_components;
+
+ /* Make a one-row-high sample array that will go away when done with image */
+ buffer = (*cinfo.mem->alloc_sarray)
+ ((j_common_ptr) &cinfo, JPOOL_IMAGE, jpeg_row_len, 1);
+ ff_row_len = strlen("RRGGBBAA") * width;
+ if(!(ff_row = malloc(ff_row_len))) {
+ fprintf(stderr, "Can't malloc\n");
+ return EXIT_FAILURE;
+ }
+
+ /* write header with big endian width and height-values */
+ fprintf(stdout, "farbfeld");
+ val_be = htonl(width);
+ fwrite(&val_be, sizeof(uint32_t), 1, stdout);
+ val_be = htonl(height);
+ fwrite(&val_be, sizeof(uint32_t), 1, stdout);
+
+ while (cinfo.output_scanline < cinfo.output_height) {
+ /* jpeg_read_scanlines expects an array of pointers to scanlines.
+ * Here the array is only one element long, but you could ask for
+ * more than one scanline at a time if that's more convenient. */
+ (void)jpeg_read_scanlines(&cinfo, buffer, 1);
+
+ for(i = 0, dx = 0, sx = 0; i < width; i++, sx += 3, dx += 4) {
+ ff_row[dx] = htons(buffer[0][sx] * 257);
+ ff_row[dx+1] = htons(buffer[0][sx+1] * 257);
+ ff_row[dx+2] = htons(buffer[0][sx+2] * 257);
+ ff_row[dx+3] = htons(65535);
+ }
+
+ /* write data */
+ if (fwrite(ff_row, 1, ff_row_len, stdout) != ff_row_len) {
+ fprintf(stderr, "fwrite() failed\n");
+ goto cleanup;
+ }
+ }
+ jpeg_finish_decompress(&cinfo);
+ status = EXIT_SUCCESS;
+
+cleanup:
+ free(ff_row);
+ jpeg_destroy_decompress(&cinfo);
+
+ return status;
+}