Signed-off-by: Mattias Andrée <[email protected]>
---
 Makefile |  2 +-
 ff2pam.1 | 41 +++++++++++++++++++++++++++++++++++++
 ff2pam.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 112 insertions(+), 1 deletion(-)
 create mode 100644 ff2pam.1
 create mode 100644 ff2pam.c

diff --git a/Makefile b/Makefile
index 2177d25..e2b5746 100644
--- a/Makefile
+++ b/Makefile
@@ -2,7 +2,7 @@
 # See LICENSE file for copyright and license details
 include config.mk
 
-BIN = png2ff ff2png jpg2ff ff2jpg ff2ppm
+BIN = png2ff ff2png jpg2ff ff2jpg ff2ppm ff2pam
 SCRIPTS = 2ff
 SRC = ${BIN:=.c}
 HDR = arg.h
diff --git a/ff2pam.1 b/ff2pam.1
new file mode 100644
index 0000000..3224acd
--- /dev/null
+++ b/ff2pam.1
@@ -0,0 +1,41 @@
+.Dd 2017-01-09
+.Dt FF2PAM 1
+.Os suckless.org
+.Sh NAME
+.Nm ff2pam
+.Nd convert farbfeld to PAM
+.Sh SYNOPSIS
+.Nm
+.Sh DESCRIPTION
+.Nm
+reads a
+.Xr farbfeld 5
+image from stdin, converts it to a 16-bit RGBA PAM image and
+writes the result to stdout.
+.Pp
+In case of an error
+.Nm
+writes a diagnostic message to stderr.
+.Sh EXIT STATUS
+.Bl -tag -width Ds
+.It 0
+Image processed successfully.
+.It 1
+An error occurred.
+.El
+.Sh EXAMPLES
+$
+.Nm
+< image.ff > image.pam
+.Pp
+$ bunzip2 < image.ff.bz2 |
+.Nm
+> image.pam
+.Sh SEE ALSO
+.Xr 2ff 1 ,
+.Xr bunzip2 1 ,
+.Xr bzip2 1 ,
+.Xr png2ff 1 ,
+.Xr farbfeld 5
+.Sh AUTHORS
+.An Mattias Andrée Aq Mt [email protected]
diff --git a/ff2pam.c b/ff2pam.c
new file mode 100644
index 0000000..b244c68
--- /dev/null
+++ b/ff2pam.c
@@ -0,0 +1,70 @@
+/* See LICENSE file for copyright and license details. */
+#include <arpa/inet.h>
+
+#include <errno.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];
+       ssize_t r, p, n;
+
+       argv0 = argv[0], argc--, argv++;
+
+       if (argc) {
+               fprintf(stderr, "usage: %s\n", argv0);
+               return 1;
+       }
+
+       /* header */
+       if (read(STDIN_FILENO, hdr, sizeof(hdr)) != sizeof(hdr))
+               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]);
+
+       /* write header */
+       printf("P7\n"
+              "WIDTH %lu\n"
+              "HEIGHT %lu\n"
+              "DEPTH 4\n" /* actually the number of channel */
+              "MAXVAL 65535\n"
+              "TUPLTYPE RGB_ALPHA\n"
+              "ENDHDR\n",
+              (unsigned long int)width, (unsigned long int)height);
+       fflush(stdout);
+       if (ferror(stdout))
+               goto writeerr;
+
+       /* write image */
+       for (;;) {
+               n = read(STDIN_FILENO, buf, sizeof(buf));
+               if (n < 0)
+                       goto readerr;
+               if (!n)
+                       break;
+               for (p = 0; p < n; p += r) {
+                       r = write(STDOUT_FILENO, buf + p, n - p);
+                       if (r < 0)
+                               goto writeerr;
+               }
+       }
+
+       return 0;
+readerr:
+       fprintf(stderr, "%s: read: %s\n", argv0, strerror(errno));
+       return 1;
+writeerr:
+       fprintf(stderr, "%s: write: %s\n", argv0, strerror(errno));
+       return 1;
+}
-- 
2.11.0


Reply via email to