diff options
-rw-r--r-- | Makefile | 17 | ||||
-rw-r--r-- | config.mk | 4 | ||||
-rw-r--r-- | jpg2if.c | 109 |
3 files changed, 123 insertions, 7 deletions
@@ -3,10 +3,10 @@ include config.mk -SRC = png2if.c if2png.c +SRC = png2if.c if2png.c jpg2if.c OBJ = ${SRC:.c=.o} -all: options png2if if2png +all: options png2if if2png jpg2if options: @echo imagefile build options: @@ -20,23 +20,28 @@ options: ${OBJ}: config.mk +jpg2if: jpg2if.o + @echo CC -o $@ + @${CC} -o $@ jpg2if.o ${LDFLAGS} ${JPEG_LIBS} + png2if: png2if.o @echo CC -o $@ - @${CC} -o $@ png2if.o ${LDFLAGS} + @${CC} -o $@ png2if.o ${LDFLAGS} ${PNG_LIBS} if2png: if2png.o @echo CC -o $@ - @${CC} -o $@ if2png.o ${LDFLAGS} + @${CC} -o $@ if2png.o ${LDFLAGS} ${PNG_LIBS} clean: @echo cleaning - @rm -f png2if if2png ${OBJ} + @rm -f png2if if2png jpg2if ${OBJ} install: all @echo installing executable files to ${DESTDIR}${PREFIX}/bin @mkdir -p ${DESTDIR}${PREFIX}/bin - @cp -f png2if if2png ${DESTDIR}${PREFIX}/bin + @cp -f png2if if2png jpg2if ${DESTDIR}${PREFIX}/bin @chmod 755 ${DESTDIR}${PREFIX}/bin/png2if + @chmod 755 ${DESTDIR}${PREFIX}/bin/jpg2if @chmod 755 ${DESTDIR}${PREFIX}/bin/if2png uninstall: @@ -4,7 +4,9 @@ PREFIX = /usr/local # libs -LIBS = -lpng +LIBS = +PNG_LIBS = -lpng +JPEG_LIBS = -ljpeg # flags CFLAGS = -std=c90 -ansi -pedantic -Wall -Wextra -Os diff --git a/jpg2if.c b/jpg2if.c new file mode 100644 index 0000000..7b19171 --- /dev/null +++ b/jpg2if.c @@ -0,0 +1,109 @@ +/* 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> +#include "arg.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; + size_t jpeg_row_len; + int status = EXIT_FAILURE; + JSAMPARRAY buffer; /* output row buffer */ + + ARGBEGIN { + default: + usage(); + } ARGEND; + + if (argc != 0) + 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); + + /* change output for imagefile */ + cinfo.output_components = 4; /* # of color components per pixel */ + cinfo.out_color_space = JCS_EXT_RGBA; /* colorspace of input image */ + + jpeg_start_decompress(&cinfo); + jpeg_row_len = cinfo.output_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); + + width = cinfo.image_width; + height = cinfo.image_height; + + /* write header with big endian width and height-values */ + fprintf(stdout, "imagefile"); + 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); + + /* write data */ + if (fwrite(buffer[0], 1, jpeg_row_len, stdout) != jpeg_row_len) { + fprintf(stderr, "fwrite() failed\n"); + goto cleanup; + } + } + jpeg_finish_decompress(&cinfo); + status = EXIT_SUCCESS; + +cleanup: + jpeg_destroy_decompress(&cinfo); + + return status; +} |