diff options
author | FRIGN <dev@frign.de> | 2016-01-06 15:15:06 +0100 |
---|---|---|
committer | FRIGN <dev@frign.de> | 2016-01-06 15:15:06 +0100 |
commit | 295094bbbfecd64b4e6841e5e8093cc1748a2917 (patch) | |
tree | 6313997dee1478c3097877b65dcefa392c96c5e5 /jpg2ff.c | |
parent | d17e95f980060bd61439f8658bd0719c26595a2b (diff) |
Improve error-handling in the tools
We don't need the jumps, but rather pass a nice function pointer to
libpng. The jump in libjpg was also useless.
We check each fwrite-call so there's an early bailout in case the output
file is full.
Flushing at the end worked as well, but it took too long for complex
images. We don't want to "block" a pipe here and the approach in jpg2ff
was better.
The iHDR-read was useless. Rather use the get*-functions in libpng,
saves us 2 local variables as well.
Diffstat (limited to 'jpg2ff.c')
-rw-r--r-- | jpg2ff.c | 53 |
1 files changed, 19 insertions, 34 deletions
@@ -6,17 +6,17 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> -#include <setjmp.h> #include <jpeglib.h> -static jmp_buf error_jump; +static char *argv0; METHODDEF(void) jpeg_error(j_common_ptr cinfo) { + fprintf(stderr, "%s: libjpeg: ", argv0); (*cinfo->err->output_message)(cinfo); - longjmp(error_jump, 1); + exit(1); } int @@ -25,23 +25,20 @@ 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; + uint16_t *ff_row; size_t jpeg_row_len, ff_row_len, i, dx, sx; - int ret = 1; JSAMPARRAY buffer; /* output row buffer */ + argv0 = argv[0], argc--, argv++; + if (argc > 1) { - fprintf(stderr, "usage: %s\n", argv[0]); + fprintf(stderr, "usage: %s\n", argv0); return 1; } /* load jpg */ cinfo.err = jpeg_std_error(&jerr); - jerr.error_exit = jpeg_error; - if (setjmp(error_jump)) { - goto cleanup; - } jpeg_create_decompress(&cinfo); jpeg_stdio_src(&cinfo, stdin); @@ -62,16 +59,18 @@ main(int argc, char *argv[]) JPOOL_IMAGE, jpeg_row_len, 1); ff_row_len = strlen("RGBA") * sizeof(uint16_t) * width; if(!(ff_row = malloc(ff_row_len))) { - fprintf(stderr, "%s: malloc: out of memory\n", argv[0]); + fprintf(stderr, "%s: malloc: out of memory\n", argv0); return 1; } /* write header */ fprintf(stdout, "farbfeld"); val_be = htonl(width); - fwrite(&val_be, sizeof(uint32_t), 1, stdout); + if (fwrite(&val_be, sizeof(uint32_t), 1, stdout) != 1) + goto writerr; val_be = htonl(height); - fwrite(&val_be, sizeof(uint32_t), 1, stdout); + if (fwrite(&val_be, sizeof(uint32_t), 1, stdout) != 1) + goto writerr; while (cinfo.output_scanline < cinfo.output_height) { /* jpeg_read_scanlines expects an array of pointers to scanlines. @@ -87,29 +86,15 @@ main(int argc, char *argv[]) } /* write data */ - if (fwrite(ff_row, 1, ff_row_len, stdout) != ff_row_len) { - fprintf(stderr, "%s: fwrite: ", argv[0]); - perror(NULL); - goto cleanup; - } + if (fwrite(ff_row, 1, ff_row_len, stdout) != ff_row_len) + goto writerr; } jpeg_finish_decompress(&cinfo); - ret = 0; - - /* flush output */ - if (fflush(stdout)) { - fprintf(stderr, "%s: fflush stdout: ", argv[0]); - perror(NULL); - ret = 1; - } - if (fclose(stdout) && !ret) { - fprintf(stderr, "%s: fclose stdout: ", argv[0]); - perror(NULL); - ret = 1; - } -cleanup: - free(ff_row); jpeg_destroy_decompress(&cinfo); - return ret; + return 0; +writerr: + fprintf(stderr, "%s: fwrite: ", argv0); + perror(NULL); + return 1; } |