summaryrefslogtreecommitdiff
path: root/ff2pam.c
diff options
context:
space:
mode:
authorMattias Andrée <maandree@kth.se>2017-01-09 05:23:27 +0100
committerLaslo Hunhold <dev@frign.de>2017-01-09 19:42:39 +0100
commit3d80d7237d18bdbf53e327a576b52b35b36af3ad (patch)
tree58467dd7e0fed26f0f77d128db2660dc0d6f832d /ff2pam.c
parente9db1f49b75b183c8aeda321ca54aff5b436a4f6 (diff)
Add ff2pam(1): convert farbfeld to 16-bit RGBA Portable Arbitrary Map
Laslo: Minor changes Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat (limited to 'ff2pam.c')
-rw-r--r--ff2pam.c74
1 files changed, 74 insertions, 0 deletions
diff --git a/ff2pam.c b/ff2pam.c
new file mode 100644
index 0000000..93bd26c
--- /dev/null
+++ b/ff2pam.c
@@ -0,0 +1,74 @@
+/* See LICENSE file for copyright and license details. */
+#include <arpa/inet.h>
+
+#include <errno.h>
+#include <inttypes.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+static char *argv0;
+
+int
+main(int argc, char *argv[])
+{
+ uint32_t hdr[4], width, height;
+ char buf[BUFSIZ];
+ size_t n, t;
+
+ argv0 = argv[0], argc--, argv++;
+
+ if (argc) {
+ fprintf(stderr, "usage: %s\n", argv0);
+ 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]);
+
+ /* write header */
+ printf("P7\n"
+ "WIDTH %" PRIu32 "\n"
+ "HEIGHT %" PRIu32 "\n"
+ "DEPTH 4\n" /* number of channels */
+ "MAXVAL 65535\n"
+ "TUPLTYPE RGB_ALPHA\n"
+ "ENDHDR\n",
+ width, height);
+
+ /* write image */
+ t = (size_t)width * (size_t)height * sizeof(uint16_t) * (sizeof("RGBA") - 1);
+ for (; (n = fread(buf, 1, sizeof(buf) <= t ? sizeof(buf) : t, stdin)); ) {
+ t -= n;
+ fwrite(buf, 1, n, stdout);
+
+ if (feof(stdin)) {
+ break;
+ }
+ if (ferror(stdin)) {
+ fprintf(stderr, "%s: read: %s\n", argv0, strerror(errno));
+ return 1;
+ }
+ if (ferror(stdout)) {
+ fprintf(stderr, "%s: write: %s\n", argv0, strerror(errno));
+ return 1;
+ }
+ }
+
+ if (t > 0) {
+ fprintf(stderr, "%s: file too short\n", argv0);
+ return 1;
+ }
+
+ return 0;
+}