farbfeld

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

ff2pam.c (1050B)


      1 /* See LICENSE file for copyright and license details. */
      2 #include <arpa/inet.h>
      3 
      4 #include <errno.h>
      5 #include <inttypes.h>
      6 #include <stdint.h>
      7 #include <stdio.h>
      8 #include <stdlib.h>
      9 #include <string.h>
     10 #include <unistd.h>
     11 
     12 #include "util.h"
     13 
     14 static void
     15 usage(void)
     16 {
     17 	die("usage: %s", argv0);
     18 }
     19 
     20 int
     21 main(int argc, char *argv[])
     22 {
     23 	size_t rowlen;
     24 	uint32_t width, height, i;
     25 	uint16_t *row;
     26 
     27 	/* arguments */
     28 	argv0 = argv[0], argc--, argv++;
     29 
     30 	if (argc) {
     31 		usage();
     32 	}
     33 
     34 	/* prepare */
     35 	ff_read_header(&width, &height);
     36 	row = ereallocarray(NULL, width, (sizeof("RGBA") - 1) * sizeof(uint16_t));
     37 	rowlen = width * (sizeof("RGBA") - 1);
     38 
     39 	/* write data */
     40 	printf("P7\n"
     41 	       "WIDTH %" PRIu32 "\n"
     42 	       "HEIGHT %" PRIu32 "\n"
     43 	       "DEPTH 4\n" /* number of channels */
     44 	       "MAXVAL 65535\n"
     45 	       "TUPLTYPE RGB_ALPHA\n"
     46 	       "ENDHDR\n",
     47 	       width, height);
     48 
     49 	for (i = 0; i < height; i++) {
     50 		efread(row, sizeof(uint16_t), rowlen, stdin);
     51 		efwrite(row, sizeof(uint16_t), rowlen, stdout);
     52 	}
     53 
     54 	return fshut(stdout, "<stdout>");
     55 }