util.c - ff2txt - farbfeld image to plain text visualization
HTML git clone git://bitreich.org/ff2txt git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/ff2txt
DIR Log
DIR Files
DIR Refs
DIR Tags
DIR README
---
util.c (822B)
---
1 #include <arpa/inet.h>
2
3 #include <stdint.h>
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <string.h>
7
8 #include "util.h"
9
10 void
11 err(char *msg)
12 {
13 perror(msg);
14 exit(1);
15 }
16
17 void
18 read_header(uint32_t *width, uint32_t *height)
19 {
20 uint32_t header[4];
21
22 if (fread(header, sizeof(*header), LEN(header), stdin) != LEN(header))
23 err("fread");
24
25 if (memcmp("farbfeld", header, sizeof("farbfeld") - 1))
26 err("invalid magic value\n");
27
28 *width = ntohl(header[2]);
29 *height = ntohl(header[3]);
30
31 if (width == 0 || height == 0)
32 err("empty image");
33 }
34
35 static uint16_t
36 col_blend(struct col col)
37 {
38 int16_t sum;
39
40 /* divide first to avoid overflow */
41 sum = ntohs(col.red) / 3;
42 sum += ntohs(col.green) / 3;
43 sum += ntohs(col.blue) / 3;
44
45 return sum;
46 }
47
48 int
49 col_is_bright(struct col col)
50 {
51 return col_blend(col) >= UINT16_MAX / 2;
52 }