diff options
author | FRIGN <dev@frign.de> | 2016-02-01 13:36:55 +0100 |
---|---|---|
committer | FRIGN <dev@frign.de> | 2016-02-01 13:36:55 +0100 |
commit | de61085a0413f2f7570a89df345eb875d1a0298c (patch) | |
tree | 4168cc28ada1f515a66fcc63b3efde431f2fd501 /ff2png.c | |
parent | 7b03f52a130a12355d87bc05f028db31963112cc (diff) |
Refactor tools and increase performance by ~70%
Instead of calling fwrite on each channel, we write one big chunk
of a line.
This increases performance by around 70% compared to version 1 and
the farbfeld tools are now roughly fast as imagemagick's convert.
I also refactored the code, removed unnecessary variables and unified
the variable naming and error reporting a bit.
Inside jpg2ff, the loop didn't need 3 variables.
Diffstat (limited to 'ff2png.c')
-rw-r--r-- | ff2png.c | 32 |
1 files changed, 16 insertions, 16 deletions
@@ -9,7 +9,7 @@ #include <png.h> -#define HEADER "farbfeld########" +#define HDR "farbfeld########" static char *argv0; @@ -25,9 +25,9 @@ main(int argc, char *argv[]) { png_structp pngs; png_infop pngi; - size_t png_row_len, j; + size_t rowlen; uint32_t width, height, i; - uint16_t tmp16, *png_row; + uint16_t *row; uint8_t hdr[16]; argv0 = argv[0], argc--, argv++; @@ -38,11 +38,10 @@ main(int argc, char *argv[]) } /* header */ - if (fread(hdr, 1, strlen(HEADER), stdin) != strlen(HEADER)) { - fprintf(stderr, "%s: incomplete header\n", argv0); - return 1; + if (fread(hdr, 1, sizeof(HDR) - 1, stdin) != sizeof(HDR) - 1) { + goto readerr; } - if (memcmp("farbfeld", hdr, strlen("farbfeld"))) { + if (memcmp("farbfeld", hdr, sizeof("farbfeld") - 1)) { fprintf(stderr, "%s: invalid magic value\n", argv0); return 1; } @@ -65,23 +64,24 @@ main(int argc, char *argv[]) png_write_info(pngs, pngi); /* write rows */ - png_row_len = strlen("RGBA") * width * sizeof(uint16_t); - if (!(png_row = malloc(png_row_len))) { + rowlen = (sizeof("RGBA") - 1) * width; + if (!(row = malloc(rowlen * sizeof(uint16_t)))) { 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, sizeof(uint16_t), 1, stdin) != 1) { - fprintf(stderr, "%s: unexpected EOF\n", argv0); - return 1; - } - png_row[j] = tmp16; + if (fread(row, sizeof(uint16_t), rowlen, stdin) != rowlen) { + goto readerr; } - png_write_row(pngs, (uint8_t *)png_row); + png_write_row(pngs, (uint8_t *)row); } png_write_end(pngs, NULL); png_destroy_write_struct(&pngs, NULL); return 0; +readerr: + fprintf(stderr, "%s: fread: ", argv0); + perror(NULL); + + return 1; } |