Optimize ff2ppm(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 f0a4ce113d0e9dc50110a0ad9e98433d05aa2307
 (DIR) parent 0da581eb1d2da5300d7b3e36ab6aa84b69ec2525
 (HTM) Author: FRIGN <dev@frign.de>
       Date:   Mon, 21 Mar 2016 22:26:37 +0100
       
       Optimize ff2ppm(1)
       
       use the proper multiplication factors :)
       
       Diffstat:
         M ff2ppm.c                            |      63 +++++++++++++------------------
       
       1 file changed, 27 insertions(+), 36 deletions(-)
       ---
 (DIR) diff --git a/ff2ppm.c b/ff2ppm.c
       @@ -23,45 +23,33 @@ int
        main(int argc, char *argv[])
        {
                size_t rowlen;
       -        uint32_t hdr[4], width, height, i, j, k;
       -        uint16_t *row, mr = 0xffff, mg = 0xffff, mb = 0xffff;
       +        uint64_t a;
       +        uint32_t hdr[4], width, height, i, j, k, l;
       +        uint16_t *row, mask[3] = { 0xffff, 0xffff, 0xffff };
                uint8_t *rowout;
       -        char *color;
       -        unsigned int r = 0xff, g = 0xff, b = 0xff;
       -        float a;
       +        char *color, colfmt[] = "%#x%#x%#x";
       +        unsigned int collen, col[3], colfac;
        
                argv0 = argv[0];
                ARGBEGIN {
                case 'b':
       -                for (color = EARGF(usage()); *color && *color == '#'; color++)
       -                        ;
       -
       -                switch (strlen(color)) {
       -                case 3:
       -                        if (sscanf(color, "%1x%1x%1x", &r, &g, &b) != 3)
       -                                usage();
       -                        mr = (r | r << 4) * 257;
       -                        mg = (g | g << 4) * 257;
       -                        mb = (b | b << 4) * 257;
       -                        break;
       -                case 6:
       -                        if (sscanf(color, "%2x%2x%2x", &r, &g, &b) != 3)
       -                                usage();
       -                        mr = r * 257;
       -                        mg = g * 257;
       -                        mb = b * 257;
       -                        break;
       -                case 12:
       -                        if (sscanf(color, "%4x%4x%4x", &r, &g, &b) != 3)
       -                                usage();
       -                        mr = r;
       -                        mg = g;
       -                        mb = b;
       -                        break;
       -                default:
       +                color = EARGF(usage());
       +                if (color[0] == '#') {
       +                        color++;
       +                }
       +                collen = strlen(color);
       +                if (collen != 3 && collen != 6 && collen != 12) {
       +                        usage();
       +                }
       +                colfmt[1] = colfmt[4] = colfmt[7] = ((collen / 3) - '0');
       +                if (sscanf(color, colfmt, col, col + 1, col + 2) != 3) {
                                usage();
                        }
       -                break;
       +                /* UINT16_MAX / 255 = 257; UINT16_MAX / 15 = 4369 */
       +                colfac = (collen == 3) ? 4369 : (collen == 6) ? 257 : 1;
       +                for (i = 0; i < 3; i++) {
       +                        mask[i] = col[i] * colfac;
       +                }
                default:
                        usage();
                } ARGEND
       @@ -111,15 +99,18 @@ main(int argc, char *argv[])
                                return 1;
                        }
                        for (j = 0, k = 0; j < rowlen; j += 4, k += 3) {
       -                        a = ntohs(row[j + 3]) / 65535.0f;
       -                        rowout[k]     = ((ntohs(row[j]) * a)     + (mr * (1 - a))) / 257;
       -                        rowout[k + 1] = ((ntohs(row[j + 1]) * a) + (mg * (1 - a))) / 257;
       -                        rowout[k + 2] = ((ntohs(row[j + 2]) * a) + (mb * (1 - a))) / 257;
       +                        a = ntohs(row[j + 3]);
       +                        for (l = 0; l < 3; l++) {
       +                                rowout[k + l] = (a * ntohs(row[j + l]) +
       +                                                 (65535 - a) * mask[l]) /
       +                                                (257 * 65535);
       +                        }
                        }
                        if (fwrite(rowout, 3, width, stdout) != width) {
                                fprintf(stderr, "%s: fwrite: %s\n", argv0, strerror(errno));
                                return 1;
                        }
                }
       +
                return 0;
        }