summaryrefslogtreecommitdiff
path: root/ff2png.c
diff options
context:
space:
mode:
authorFRIGN <dev@frign.de>2016-01-06 15:15:06 +0100
committerFRIGN <dev@frign.de>2016-01-06 15:15:06 +0100
commit295094bbbfecd64b4e6841e5e8093cc1748a2917 (patch)
tree6313997dee1478c3097877b65dcefa392c96c5e5 /ff2png.c
parentd17e95f980060bd61439f8658bd0719c26595a2b (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 'ff2png.c')
-rw-r--r--ff2png.c52
1 files changed, 22 insertions, 30 deletions
diff --git a/ff2png.c b/ff2png.c
index 62a00f2..a1276d7 100644
--- a/ff2png.c
+++ b/ff2png.c
@@ -2,7 +2,6 @@
#include <arpa/inet.h>
#include <errno.h>
-#include <setjmp.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
@@ -12,6 +11,15 @@
#define HEADER "farbfeld########"
+static char *argv0;
+
+void
+pngerr(png_structp png_struct_p, png_const_charp msg)
+{
+ fprintf(stderr, "%s: libpng: %s\n", argv0, msg);
+ exit(1);
+}
+
int
main(int argc, char *argv[])
{
@@ -19,38 +27,37 @@ main(int argc, char *argv[])
png_infop png_info_p;
png_size_t png_row_len, j;
png_uint_32 width, height, i;
- int ret = 0;
uint16_t tmp16, *png_row;
uint8_t hdr[16];
- if (argc > 1) {
- fprintf(stderr, "usage: %s\n", argv[0]);
+ argv0 = argv[0], argc--, argv++;
+
+ if (argc) {
+ fprintf(stderr, "usage: %s\n", argv0);
return 1;
}
/* header */
if (fread(hdr, 1, strlen(HEADER), stdin) != strlen(HEADER)) {
- fprintf(stderr, "%s: incomplete header\n", argv[0]);
+ fprintf(stderr, "%s: incomplete header\n", argv0);
return 1;
}
if (memcmp("farbfeld", hdr, strlen("farbfeld"))) {
- fprintf(stderr, "%s: invalid magic value\n", argv[0]);
+ fprintf(stderr, "%s: invalid magic value\n", argv0);
return 1;
}
width = ntohl(*((uint32_t *)(hdr + 8)));
height = ntohl(*((uint32_t *)(hdr + 12)));
/* load png */
- png_struct_p = png_create_write_struct(PNG_LIBPNG_VER_STRING,
- NULL, NULL, NULL);
+ png_struct_p = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL,
+ pngerr, NULL);
png_info_p = png_create_info_struct(png_struct_p);
if (!png_struct_p || !png_info_p) {
- fprintf(stderr, "%s: failed to initialize libpng\n", argv[0]);
+ fprintf(stderr, "%s: failed to initialize libpng\n", argv0);
return 1;
}
- if (setjmp(png_jmpbuf(png_struct_p)))
- return 1;
png_init_io(png_struct_p, stdout);
png_set_IHDR(png_struct_p, png_info_p, width, height, 16,
PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
@@ -60,15 +67,13 @@ main(int argc, char *argv[])
/* write rows */
png_row_len = strlen("RGBA") * width * sizeof(uint16_t);
if (!(png_row = malloc(png_row_len))) {
- fprintf(stderr, "%s: malloc: out of memory\n", argv[0]);
+ fprintf(stderr, "%s: malloc: out of memory\n", argv0);
return 1;
}
for (i = 0; i < height; ++i) {
for (j = 0; j < png_row_len / sizeof(uint16_t); ++j) {
- if (fread(&tmp16, 1, sizeof(uint16_t), stdin) !=
- sizeof(uint16_t)) {
- fprintf(stderr, "%s: unexpected EOF\n",
- argv[0]);
+ if (fread(&tmp16, sizeof(uint16_t), 1, stdin) != 1) {
+ fprintf(stderr, "%s: unexpected EOF\n", argv0);
return 1;
}
png_row[j] = tmp16;
@@ -76,20 +81,7 @@ main(int argc, char *argv[])
png_write_row(png_struct_p, (uint8_t *)png_row);
}
png_write_end(png_struct_p, NULL);
-
png_destroy_write_struct(&png_struct_p, NULL);
- /* 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;
- }
-
- return ret;
+ return 0;
}