sites

public wiki contents of suckless.org
git clone git://git.suckless.org/sites
Log | Files | Refs

invert.c (2295B)


      1 /*
      2  * 0BSD-License
      3  *
      4  * (c) 2017 Laslo Hunhold <dev@frign.de>
      5  *
      6  * Permission to use, copy, modify, and/or distribute this software for
      7  * any purpose with or without fee is hereby granted.
      8  *
      9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
     10  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
     11  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
     12  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
     13  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
     14  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
     15  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
     16  * PERFORMANCE OF THIS SOFTWARE.
     17  */
     18 #include <arpa/inet.h>
     19 
     20 #include <errno.h>
     21 #include <stdint.h>
     22 #include <stdio.h>
     23 #include <string.h>
     24 
     25 #define LEN(x) (sizeof (x) / sizeof *(x))
     26 
     27 static void
     28 invert(uint16_t rgba[4])
     29 {
     30 	rgba[0] = UINT16_MAX - rgba[0];
     31 	rgba[1] = UINT16_MAX - rgba[1];
     32 	rgba[2] = UINT16_MAX - rgba[2];
     33 }
     34 
     35 int
     36 main(int argc, char *argv[])
     37 {
     38 	uint32_t hdr[4], width, height, i, j, k;
     39 	uint16_t rgba[4];
     40 
     41 	/* arguments */
     42 	if (argc != 1) {
     43 		fprintf(stderr, "usage: %s\n", argv[0]);
     44 		return 1;
     45 	}
     46 
     47 	/* read header */
     48 	if (fread(hdr, sizeof(*hdr), LEN(hdr), stdin) != LEN(hdr)) {
     49 		goto readerr;
     50 	}
     51 	if (memcmp("farbfeld", hdr, sizeof("farbfeld") - 1)) {
     52 		fprintf(stderr, "%s: invalid magic value\n", argv[0]);
     53 		return 1;
     54 	}
     55 	width = ntohl(hdr[2]);
     56 	height = ntohl(hdr[3]);
     57 
     58 	/* write data */
     59 	if (fwrite(hdr, sizeof(*hdr), LEN(hdr), stdout) != 4) {
     60 		goto writerr;
     61 	}
     62 
     63 	for (i = 0; i < height; i++) {
     64 		for (j = 0; j < width; j++) {
     65 			if (fread(rgba, sizeof(*rgba), LEN(rgba),
     66 			          stdin) != LEN(rgba)) {
     67 				goto readerr;
     68 			}
     69 			for (k = 0; k < 4; k++) {
     70 				rgba[k] = ntohs(rgba[k]);
     71 			}
     72 
     73 			invert(rgba);
     74 
     75 			for (k = 0; k < 4; k++) {
     76 				rgba[k] = htons(rgba[k]);
     77 			}
     78 			if (fwrite(rgba, sizeof(*rgba), LEN(rgba),
     79 			           stdout) != LEN(rgba)) {
     80 				goto writerr;
     81 			}
     82 		}
     83 	}
     84 
     85 	/* clean up */
     86 	if (fclose(stdout)) {
     87 		fprintf(stderr, "%s: fclose: %s\n", argv[0],
     88 		        strerror(errno));
     89 		return 1;
     90 	}
     91 
     92 	return 0;
     93 readerr:
     94 	fprintf(stderr, "%s: fread: Unexpected EOF\n", argv[0]);
     95 	return 1;
     96 writerr:
     97 	fprintf(stderr, "%s: fwrite: %s\n", argv[0], strerror(errno));
     98 	return 1;
     99 }