[wiki] [sites] Update invert.c according to repo-changes || Laslo Hunhold

From: <git_AT_suckless.org>
Date: Fri, 14 Apr 2017 21:50:39 +0200

commit ec9fce1544347f2c7ddc41d7de1d411dbaf00a69
Author: Laslo Hunhold <dev_AT_frign.de>
Date: Fri Apr 14 21:49:12 2017 +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.

diff --git a/tools.suckless.org/farbfeld/invert.c b/tools.suckless.org/farbfeld/invert.c
index de6b875..f523797 100644
--- a/tools.suckless.org/farbfeld/invert.c
+++ b/tools.suckless.org/farbfeld/invert.c
_AT_@ -1,29 +1,52 @@
 /*
- * Copy me if you can.
- * by FRIGN
+ * 0BSD-License
+ *
+ * (c) 2017 Laslo Hunhold <dev_AT_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
", argv[0]);
                 return 1;
         }
 
- if (fread(hdr, sizeof(uint32_t), 4, stdin) != 4) {
- fprintf(stderr, "%s: fread error
", 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
", argv[0]);
_AT_@ -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
", 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
", argv[0]);
- return 1;
- }
         for (i = 0; i < height; i++) {
- if (fread(row, sizeof(uint16_t), rowlen, stdin) != rowlen) {
- fprintf(stderr, "%s: fread error
", 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
", argv[0]);
- return 1;
- }
+ }
+
+ /* clean up */
+ if (fclose(stdout)) {
+ fprintf(stderr, "%s: fclose: %s
", argv[0],
+ strerror(errno));
+ return 1;
         }
 
         return 0;
+readerr:
+ fprintf(stderr, "%s: fread: Unexpected EOF
", argv[0]);
+ return 1;
+writerr:
+ fprintf(stderr, "%s: fwrite: %s
", argv[0], strerror(errno));
+ return 1;
 }
Received on Fri Apr 14 2017 - 21:50:39 CEST

This archive was generated by hypermail 2.3.0 : Fri Apr 14 2017 - 22:00:27 CEST