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 /png2ff.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 'png2ff.c')
-rw-r--r-- | png2ff.c | 30 |
1 files changed, 12 insertions, 18 deletions
@@ -23,9 +23,9 @@ main(int argc, char *argv[]) { png_structp pngs; png_infop pngi; - uint32_t width, height, outrowlen, tmp32, r, i; - uint16_t *outrow; - uint8_t **png_row_p; + uint32_t width, height, rowlen, tmp32, r, i; + uint16_t *row; + uint8_t **pngrows; argv0 = argv[0], argc--, argv++; @@ -54,15 +54,13 @@ main(int argc, char *argv[]) PNG_TRANSFORM_EXPAND, NULL); width = png_get_image_width(pngs, pngi); height = png_get_image_height(pngs, pngi); - png_row_p = png_get_rows(pngs, pngi); - + pngrows = png_get_rows(pngs, pngi); /* allocate output row buffer */ - outrowlen = width * strlen("RGBA"); - if (!(outrow = malloc(outrowlen * sizeof(uint16_t)))) { + rowlen = width * strlen("RGBA"); + if (!(row = malloc(rowlen * sizeof(uint16_t)))) { fprintf(stderr, "%s: malloc: out of memory\n", argv0); return 1; } - /* write header */ fputs("farbfeld", stdout); tmp32 = htonl(width); @@ -71,27 +69,23 @@ main(int argc, char *argv[]) tmp32 = htonl(height); if (fwrite(&tmp32, sizeof(uint32_t), 1, stdout) != 1) goto writerr; - /* write data */ switch(png_get_bit_depth(pngs, pngi)) { case 8: for (r = 0; r < height; ++r) { - for (i = 0; i < outrowlen; i++) { - outrow[i] = htons(257 * png_row_p[r][i]); + for (i = 0; i < rowlen; i++) { + row[i] = htons(257 * pngrows[r][i]); } - if (fwrite(outrow, sizeof(uint16_t), outrowlen, - stdout) != outrowlen) { + if (fwrite(row, sizeof(uint16_t), rowlen, + stdout) != rowlen) { goto writerr; } } break; case 16: for (r = 0; r < height; ++r) { - for (i = 0; i < outrowlen; ++i) { - outrow[i] = ((uint16_t *)png_row_p[r])[i]; - } - if (fwrite(outrow, sizeof(uint16_t), outrowlen, - stdout) != outrowlen) { + if (fwrite(pngrows[r], sizeof(uint16_t), + rowlen, stdout) != rowlen) { goto writerr; } } |