Refactor ff2pam(1) - farbfeld - suckless image format with conversion tools
 (HTM) git clone git://git.suckless.org/farbfeld
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
 (DIR) LICENSE
       ---
 (DIR) commit a78b5dc6efaca25830c31ac17ef3ff81a493314d
 (DIR) parent 9a992f810d2a11751026ac352f435be1717e0979
 (HTM) Author: Laslo Hunhold <dev@frign.de>
       Date:   Wed, 12 Apr 2017 23:39:41 +0200
       
       Refactor ff2pam(1)
       
       I chose to go with a row-based-approach here, which is a bit easier
       to read and is somewhat "closer" to the input data.
       
       Diffstat:
         M ff2pam.c                            |      52 ++++++++++++++++---------------
       
       1 file changed, 27 insertions(+), 25 deletions(-)
       ---
 (DIR) diff --git a/ff2pam.c b/ff2pam.c
       @@ -5,28 +5,39 @@
        #include <inttypes.h>
        #include <stdint.h>
        #include <stdio.h>
       +#include <stdlib.h>
        #include <string.h>
        #include <unistd.h>
        
        #include "util.h"
        
       +static void
       +usage(void)
       +{
       +        fprintf(stderr, "usage: %s\n", argv0);
       +        exit(1);
       +}
       +
        int
        main(int argc, char *argv[])
        {
       -        uint32_t width, height;
       -        char buf[BUFSIZ];
       -        size_t n, t;
       +        size_t rowlen;
       +        uint32_t width, height, i;
       +        uint16_t *row;
        
       +        /* arguments */
                argv0 = argv[0], argc--, argv++;
        
                if (argc) {
       -                fprintf(stderr, "usage: %s\n", argv0);
       -                return 1;
       +                usage();
                }
        
       -        read_ff_header(&width, &height);
       +        /* prepare */
       +        ff_read_header(&width, &height);
       +        row = ereallocarray(NULL, width, (sizeof("RGBA") - 1) * sizeof(uint16_t));
       +        rowlen = width * (sizeof("RGBA") - 1);
        
       -        /* write PAM header */
       +        /* write data */
                printf("P7\n"
                       "WIDTH %" PRIu32 "\n"
                       "HEIGHT %" PRIu32 "\n"
       @@ -36,29 +47,20 @@ main(int argc, char *argv[])
                       "ENDHDR\n",
                       width, height);
        
       -        /* write image */
       -        t = (size_t)width * (size_t)height * sizeof(uint16_t) * (sizeof("RGBA") - 1);
       -        for (; (n = fread(buf, 1, sizeof(buf) <= t ? sizeof(buf) : t, stdin)); ) {
       -                t -= n;
       -                fwrite(buf, 1, n, stdout);
       -
       -                if (feof(stdin)) {
       -                        break;
       -                }
       -                if (ferror(stdin)) {
       -                        fprintf(stderr, "%s: read: %s\n", argv0, strerror(errno));
       +        for (i = 0; i < height; i++) {
       +                if (fread(row, sizeof(uint16_t), rowlen, stdin) != rowlen) {
       +                        if (ferror(stdin)) {
       +                                fprintf(stderr, "%s: fread: %s\n", argv0, strerror(errno));
       +                        } else {
       +                                fprintf(stderr, "%s: unexpected end of file\n", argv0);
       +                        }
                                return 1;
                        }
       -                if (ferror(stdout)) {
       -                        fprintf(stderr, "%s: write: %s\n", argv0, strerror(errno));
       +                if (fwrite(row, sizeof(uint16_t), rowlen, stdout) != rowlen) {
       +                        fprintf(stderr, "%s: fwrite: %s\n", argv0, strerror(errno));
                                return 1;
                        }
                }
        
       -        if (t > 0) {
       -                fprintf(stderr, "%s: file too short\n", argv0);
       -                return 1;
       -        }
       -
                return 0;
        }