farbfeld

suckless image format with conversion tools
git clone git://git.suckless.org/farbfeld
Log | Files | Refs | README | LICENSE

commit d25f3c73afa5beb83121c9b033bc0146c755ca3a
parent d0ce307972fbc95073666e92043fc7012ffbefdf
Author: Laslo Hunhold <dev@frign.de>
Date:   Thu, 30 Mar 2017 09:49:48 +0200

Add util.{c|h} to deduplicate code

Diffstat:
Mff2jpg.c | 19+++++--------------
Mff2pam.c | 18++++--------------
Mff2png.c | 16++++------------
Mff2ppm.c | 16+++-------------
Mjpg2ff.c | 15++++-----------
Mpng2ff.c | 15++++-----------
Autil.c | 51+++++++++++++++++++++++++++++++++++++++++++++++++++
Autil.h | 10++++++++++
8 files changed, 85 insertions(+), 75 deletions(-)

diff --git a/ff2jpg.c b/ff2jpg.c @@ -11,8 +11,7 @@ #include <jpeglib.h> #include "arg.h" - -char *argv0; +#include "util.h" METHODDEF(void) jpeg_error(j_common_ptr js) @@ -37,7 +36,7 @@ main(int argc, char *argv[]) struct jpeg_error_mgr jerr; size_t rowlen; uint64_t a; - uint32_t hdr[4], width, height, i, j, k, l; + uint32_t width, height, i, j, k, l; uint16_t *row, mask[3] = { 0xffff, 0xffff, 0xffff }; uint8_t *rowout; char *color, colfmt[] = "%#x%#x%#x"; @@ -75,19 +74,11 @@ main(int argc, char *argv[]) usage(); } ARGEND - if (argc) + if (argc) { usage(); - - /* header */ - if (fread(hdr, sizeof(*hdr), 4, stdin) != 4) { - goto readerr; } - if (memcmp("farbfeld", hdr, sizeof("farbfeld") - 1)) { - fprintf(stderr, "%s: invalid magic value\n", argv0); - return 1; - } - width = ntohl(hdr[2]); - height = ntohl(hdr[3]); + + read_ff_header(&width, &height); if (width > SIZE_MAX / ((sizeof("RGBA") - 1) * sizeof(uint16_t))) { fprintf(stderr, "%s: row length integer overflow\n", argv0); diff --git a/ff2pam.c b/ff2pam.c @@ -8,12 +8,12 @@ #include <string.h> #include <unistd.h> -static char *argv0; +#include "util.h" int main(int argc, char *argv[]) { - uint32_t hdr[4], width, height; + uint32_t width, height; char buf[BUFSIZ]; size_t n, t; @@ -24,19 +24,9 @@ main(int argc, char *argv[]) return 1; } - /* header */ - if (fread(hdr, sizeof(*hdr), 4, stdin) != 4) { - fprintf(stderr, "%s: file too short\n", argv0); - return 1; - } - if (memcmp("farbfeld", hdr, sizeof("farbfeld") - 1)) { - fprintf(stderr, "%s: invalid magic value\n", argv0); - return 1; - } - width = ntohl(hdr[2]); - height = ntohl(hdr[3]); + read_ff_header(&width, &height); - /* write header */ + /* write PAM header */ printf("P7\n" "WIDTH %" PRIu32 "\n" "HEIGHT %" PRIu32 "\n" diff --git a/ff2png.c b/ff2png.c @@ -9,11 +9,12 @@ #include <png.h> -static char *argv0; +#include "util.h" void pngerr(png_structp pngs, const char *msg) { + (void)pngs; fprintf(stderr, "%s: libpng: %s\n", argv0, msg); exit(1); } @@ -24,7 +25,7 @@ main(int argc, char *argv[]) png_structp pngs; png_infop pngi; size_t rowlen; - uint32_t hdr[4], width, height, i; + uint32_t width, height, i; uint16_t *row; argv0 = argv[0], argc--, argv++; @@ -34,16 +35,7 @@ main(int argc, char *argv[]) return 1; } - /* header */ - if (fread(hdr, sizeof(*hdr), 4, stdin) != 4) { - goto readerr; - } - if (memcmp("farbfeld", hdr, sizeof("farbfeld") - 1)) { - fprintf(stderr, "%s: invalid magic value\n", argv0); - return 1; - } - width = ntohl(hdr[2]); - height = ntohl(hdr[3]); + read_ff_header(&width, &height); /* load png */ pngs = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, pngerr, diff --git a/ff2ppm.c b/ff2ppm.c @@ -9,8 +9,7 @@ #include <string.h> #include "arg.h" - -char *argv0; +#include "util.h" static void usage(void) @@ -24,7 +23,7 @@ main(int argc, char *argv[]) { size_t rowlen; uint64_t a; - uint32_t hdr[4], width, height, i, j, k, l; + uint32_t width, height, i, j, k, l; uint16_t *row, mask[3] = { 0xffff, 0xffff, 0xffff }; uint8_t *rowout; char *color, colfmt[] = "%#x%#x%#x"; @@ -58,16 +57,7 @@ main(int argc, char *argv[]) if (argc) usage(); - /* header */ - if (fread(hdr, sizeof(*hdr), 4, stdin) != 4) { - goto readerr; - } - if (memcmp("farbfeld", hdr, sizeof("farbfeld") - 1)) { - fprintf(stderr, "%s: invalid magic value\n", argv0); - return 1; - } - width = ntohl(hdr[2]); - height = ntohl(hdr[3]); + read_ff_header(&width, &height); if (width > SIZE_MAX / ((sizeof("RGBA") - 1) * sizeof(uint16_t))) { fprintf(stderr, "%s: row length integer overflow\n", argv0); diff --git a/jpg2ff.c b/jpg2ff.c @@ -8,7 +8,7 @@ #include <jpeglib.h> -static char *argv0; +#include "util.h" METHODDEF(void) jpeg_error(j_common_ptr js) @@ -23,7 +23,7 @@ main(int argc, char *argv[]) { struct jpeg_decompress_struct js; struct jpeg_error_mgr jerr; - uint32_t width, height, tmp32; + uint32_t width, height; uint16_t *row; size_t rowlen, i; JSAMPARRAY jpgrow; @@ -63,14 +63,8 @@ main(int argc, char *argv[]) return 1; } - /* write header */ - fputs("farbfeld", stdout); - tmp32 = htonl(width); - if (fwrite(&tmp32, sizeof(uint32_t), 1, stdout) != 1) - goto writerr; - tmp32 = htonl(height); - if (fwrite(&tmp32, sizeof(uint32_t), 1, stdout) != 1) - goto writerr; + /* write data */ + write_ff_header(width, height); while (js.output_scanline < js.output_height) { /* jpeg_read_scanlines expects an array of pointers to @@ -87,7 +81,6 @@ main(int argc, char *argv[]) row[4*i + 3] = htons(65535); } - /* write data */ if (fwrite(row, sizeof(uint16_t), rowlen, stdout) != rowlen) goto writerr; } diff --git a/png2ff.c b/png2ff.c @@ -8,7 +8,7 @@ #include <png.h> -static char *argv0; +#include "util.h" void pngerr(png_structp pngs, const char *msg) @@ -22,7 +22,7 @@ main(int argc, char *argv[]) { png_structp pngs; png_infop pngi; - uint32_t width, height, rowlen, tmp32, r, i; + uint32_t width, height, rowlen, r, i; uint16_t *row; uint8_t **pngrows; @@ -66,16 +66,9 @@ main(int argc, char *argv[]) return 1; } - /* write header */ - fputs("farbfeld", stdout); - tmp32 = htonl(width); - if (fwrite(&tmp32, sizeof(uint32_t), 1, stdout) != 1) - goto writerr; - tmp32 = htonl(height); - if (fwrite(&tmp32, sizeof(uint32_t), 1, stdout) != 1) - goto writerr; - /* write data */ + write_ff_header(width, height); + switch(png_get_bit_depth(pngs, pngi)) { case 8: for (r = 0; r < height; ++r) { diff --git a/util.c b/util.c @@ -0,0 +1,51 @@ +/* See LICENSE file for copyright and license details. */ +#include <arpa/inet.h> + +#include <errno.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "util.h" + +char *argv0; + +void +read_ff_header(uint32_t *width, uint32_t *height) +{ + uint32_t hdr[4]; + + if (fread(hdr, sizeof(*hdr), LEN(hdr), stdin) != LEN(hdr)) { + fprintf(stderr, "%s: fread: %s", argv0, strerror(errno)); + exit(1); + } + + if (memcmp("farbfeld", hdr, sizeof("farbfeld") - 1)) { + fprintf(stderr, "%s: invalid magic value\n", argv0); + exit(1); + } + + *width = ntohl(hdr[2]); + *height = ntohl(hdr[3]); +} + +void +write_ff_header(uint32_t width, uint32_t height) +{ + uint32_t tmp; + + fputs("farbfeld", stdout); + + tmp = htonl(width); + if (fwrite(&tmp, sizeof(tmp), 1, stdout) != 1) { + fprintf(stderr, "%s: write: %s", argv0, strerror(errno)); + exit(1); + } + + tmp = htonl(height); + if (fwrite(&tmp, sizeof(tmp), 1, stdout) != 1) { + fprintf(stderr, "%s: write: %s", argv0, strerror(errno)); + exit(1); + } +} diff --git a/util.h b/util.h @@ -0,0 +1,10 @@ +/* See LICENSE file for copyright and license details. */ +#include <stdint.h> +#include <stdio.h> + +extern char *argv0; + +#define LEN(x) (sizeof (x) / sizeof *(x)) + +void read_ff_header(uint32_t *width, uint32_t *height); +void write_ff_header(uint32_t width, uint32_t height);