ff-utils

farbfeld tools and accessories
git clone git://nsmpr.xyz/ff-utils.git
Log | Files | Refs | README | LICENSE

ff-pattern.c (938B)


      1 #include <arpa/inet.h>
      2 #include <stdint.h>
      3 #include <stdio.h>
      4 #include <stdlib.h>
      5 #include <unistd.h>
      6 #include <string.h>
      7 
      8 int fid;
      9 long inx, iny, outx, outy;
     10 long size, n, i, x, y;
     11 uint32_t hbuf[4];
     12 char *buf;
     13 char *argv0;
     14 
     15 int
     16 main(int argc, char **argv)
     17 {
     18 	fid = 0;
     19 	if (argc != 3) {
     20 		fprintf(stderr, "usage: %s width height\n", argv0);
     21 		return 1;
     22 	}
     23 
     24 	outx = strtol(argv[1], 0, 10);
     25 	outy = strtol(argv[2], 0, 10);
     26 	
     27 	read(fid, hbuf, 16);
     28 
     29 	if (strncmp("farbfeld", (char*)hbuf, 8) != 0) {
     30 		fprintf(stderr, "invalid magic\n");
     31 		return 1;
     32 	}
     33 
     34 	inx = ntohl(hbuf[2]);
     35 	iny = ntohl(hbuf[3]);
     36 
     37 	size = inx * iny * 8;
     38 	buf = malloc(size);
     39 	n = read(fid, buf, size);
     40 	if (n < size) {
     41 		fprintf(stderr, "underread\n");
     42 	}
     43 	hbuf[2] = htonl(outx);
     44 	hbuf[3] = htonl(outy);
     45 	write(1, hbuf, 16);
     46 	for (i = 0; i < outx * outy; i++){
     47 		x = i % outx;
     48 		y = i / outx;
     49 		int d;
     50 		d = x%inx + y%inx * inx;
     51 		write(1, buf + d * 8, 8);
     52 	}
     53 	return 0;
     54 }