sites

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

commit ec9fce1544347f2c7ddc41d7de1d411dbaf00a69
parent 2d98ea2e90d1b6a8211df1798acb0d93fb9f252f
Author: Laslo Hunhold <dev@frign.de>
Date:   Fri, 14 Apr 2017 21:49:12 +0200

Update invert.c according to repo-changes

It was my interest to create an example that is not just an
artificial demonstration, but that can actually be used in
"production". This bloats the code up a bit, but with the comments
and so on it actually gets much more readable.

The 0BSD allows use without any strings attached.

Diffstat:
Mtools.suckless.org/farbfeld/invert.c | 98+++++++++++++++++++++++++++++++++++++++++++++++++++----------------------------
1 file changed, 63 insertions(+), 35 deletions(-)

diff --git a/tools.suckless.org/farbfeld/invert.c b/tools.suckless.org/farbfeld/invert.c @@ -1,29 +1,52 @@ /* - * Copy me if you can. - * by FRIGN + * 0BSD-License + * + * (c) 2017 Laslo Hunhold <dev@frign.de> + * + * Permission to use, copy, modify, and/or distribute this software for + * any purpose with or without fee is hereby granted. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL + * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE + * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL + * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR + * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER + * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR + * PERFORMANCE OF THIS SOFTWARE. */ #include <arpa/inet.h> +#include <errno.h> #include <stdint.h> #include <stdio.h> -#include <stdlib.h> #include <string.h> +#define LEN(x) (sizeof (x) / sizeof *(x)) + +static void +invert(uint16_t rgba[4]) +{ + rgba[0] = UINT16_MAX - rgba[0]; + rgba[1] = UINT16_MAX - rgba[1]; + rgba[2] = UINT16_MAX - rgba[2]; +} + int main(int argc, char *argv[]) { - uint64_t rowlen; - uint32_t hdr[4], width, height, i, j; - uint16_t *row, *rgba; + uint32_t hdr[4], width, height, i, j, k; + uint16_t rgba[4]; - if (argc > 1) { + /* arguments */ + if (argc != 1) { fprintf(stderr, "usage: %s\n", argv[0]); return 1; } - if (fread(hdr, sizeof(uint32_t), 4, stdin) != 4) { - fprintf(stderr, "%s: fread error\n", argv[0]); - return 1; + /* read header */ + if (fread(hdr, sizeof(*hdr), LEN(hdr), stdin) != LEN(hdr)) { + goto readerr; } if (memcmp("farbfeld", hdr, sizeof("farbfeld") - 1)) { fprintf(stderr, "%s: invalid magic value\n", argv[0]); @@ -32,40 +55,45 @@ main(int argc, char *argv[]) width = ntohl(hdr[2]); height = ntohl(hdr[3]); - if (fwrite(hdr, sizeof(uint32_t), 4, stdout) != 4) { - fprintf(stderr, "%s: fwrite error\n", argv[0]); - return 1; + /* write data */ + if (fwrite(hdr, sizeof(*hdr), LEN(hdr), stdout) != 4) { + goto writerr; } - rowlen = (sizeof("RGBA") - 1) * width; - if (!(row = malloc(rowlen * sizeof(uint16_t)))) { - fprintf(stderr, "%s: malloc: out of memory\n", argv[0]); - return 1; - } for (i = 0; i < height; i++) { - if (fread(row, sizeof(uint16_t), rowlen, stdin) != rowlen) { - fprintf(stderr, "%s: fread error\n", argv[0]); - return 1; - } - for (rgba = row; (rgba - row) < rowlen; rgba += 4) { - for (j = 0; j < 4; j++) { - rgba[j] = ntohs(rgba[j]); + for (j = 0; j < width; j++) { + if (fread(rgba, sizeof(*rgba), LEN(rgba), + stdin) != LEN(rgba)) { + goto readerr; + } + for (k = 0; k < 4; k++) { + rgba[k] = ntohs(rgba[k]); } - /* invert colors */ - rgba[0] = 65535 - rgba[0]; - rgba[1] = 65535 - rgba[1]; - rgba[2] = 65535 - rgba[2]; + invert(rgba); - for (j = 0; j < 4; j++) { - rgba[j] = htons(rgba[j]); + for (k = 0; k < 4; k++) { + rgba[k] = htons(rgba[k]); + } + if (fwrite(rgba, sizeof(*rgba), LEN(rgba), + stdout) != LEN(rgba)) { + goto writerr; } } - if (fwrite(row, sizeof(uint16_t), rowlen, stdout) != rowlen) { - fprintf(stderr, "%s: fwrite error\n", argv[0]); - return 1; - } + } + + /* clean up */ + if (fclose(stdout)) { + fprintf(stderr, "%s: fclose: %s\n", argv[0], + strerror(errno)); + return 1; } return 0; +readerr: + fprintf(stderr, "%s: fread: Unexpected EOF\n", argv[0]); + return 1; +writerr: + fprintf(stderr, "%s: fwrite: %s\n", argv[0], strerror(errno)); + return 1; }