colors

extract colors from pictures
git clone git://git.2f30.org/colors
Log | Files | Refs | README | LICENSE

ff.c (1034B)


      1 /* See LICENSE file for copyright and license details. */
      2 #include <arpa/inet.h>
      3 #include <sys/types.h>
      4 
      5 #include <err.h>
      6 #include <limits.h>
      7 #include <stdio.h>
      8 #include <stdlib.h>
      9 #include <string.h>
     10 
     11 #include "colors.h"
     12 #include "util.h"
     13 
     14 void
     15 parseimg_ff(FILE *fp, void (*fn)(int, int, int))
     16 {
     17 	uint32_t hdr[4], width, height;
     18 	uint16_t *row;
     19 	size_t rowlen, i, j;
     20 
     21 	if (fread(hdr, sizeof(*hdr), 4, fp) != 4)
     22 		err(1, "fread");
     23 	if (memcmp("farbfeld", hdr, sizeof("farbfeld") - 1))
     24 		errx(1, "invalid magic value");
     25 
     26 	width = ntohl(hdr[2]);
     27 	height = ntohl(hdr[3]);
     28 
     29 	if (!(row = reallocarray(NULL, width, (sizeof("RGBA") - 1) * sizeof(uint16_t))))
     30 		err(1, "reallocarray");
     31 	rowlen = width * (sizeof("RGBA") - 1);
     32 
     33 	for (i = 0; i < height; ++i) {
     34 		if (fread(row, sizeof(uint16_t), rowlen, fp) != rowlen) {
     35 			if (ferror(fp))
     36 				err(1, "fread");
     37 			else
     38 				errx(1, "unexpected end of file");
     39 		}
     40 		for (j = 0; j < rowlen; j += 4) {
     41 			if (!row[j + 3])
     42 				continue;
     43 			fn(row[j] / 257, row[j+1] / 257, row[j+2] / 257);
     44 		}
     45 	}
     46 }