summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLaslo Hunhold <dev@frign.de>2017-03-30 09:49:48 +0200
committerLaslo Hunhold <dev@frign.de>2017-03-30 09:49:48 +0200
commitd25f3c73afa5beb83121c9b033bc0146c755ca3a (patch)
tree2d2407a2d07cb87f3da44e98cdd6131b3c9e5bd3
parentd0ce307972fbc95073666e92043fc7012ffbefdf (diff)
Add util.{c|h} to deduplicate code
-rw-r--r--ff2jpg.c19
-rw-r--r--ff2pam.c18
-rw-r--r--ff2png.c16
-rw-r--r--ff2ppm.c16
-rw-r--r--jpg2ff.c15
-rw-r--r--png2ff.c15
-rw-r--r--util.c51
-rw-r--r--util.h10
8 files changed, 85 insertions, 75 deletions
diff --git a/ff2jpg.c b/ff2jpg.c
index 6f88a23..51f1fe6 100644
--- 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
index 93bd26c..aca15cb 100644
--- 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
index 5731082..360ab00 100644
--- 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
index 2a67eb2..f0f3956 100644
--- 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
index 8ebc81d..85c935d 100644
--- 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
index 55456e3..515979d 100644
--- 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
new file mode 100644
index 0000000..b374df9
--- /dev/null
+++ 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
new file mode 100644
index 0000000..02aee25
--- /dev/null
+++ 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);