summaryrefslogtreecommitdiff
path: root/farbfeld.5
diff options
context:
space:
mode:
authorFRIGN <dev@frign.de>2016-01-05 11:10:39 +0100
committerFRIGN <dev@frign.de>2016-01-05 11:10:39 +0100
commitb52f376a0f18db0dc8b2c8707cf3f41d32fd9a10 (patch)
treed9152b5937e401ea21b4de297fb5c6dddb7ca5c1 /farbfeld.5
parentac1630a81790f5db946d7091e08b758714c8cd19 (diff)
Update example in farbfeld.5
We were close to a full program anyway, so I just completed the code so you can study and run it easily.
Diffstat (limited to 'farbfeld.5')
-rw-r--r--farbfeld.587
1 files changed, 48 insertions, 39 deletions
diff --git a/farbfeld.5 b/farbfeld.5
index 74bd02c..2ed3354 100644
--- a/farbfeld.5
+++ b/farbfeld.5
@@ -45,8 +45,8 @@ and inherent complexity involved in handling common image formats
(PNG, JPEG, GIF,...), having to rely on bloated libraries not being able
to focus on the task at hand for a given problem.
.Sh EXAMPLES
-Below is an example for a color inverter. No external libraries other
-than libc are needed to read the image data:
+Below is an example for a color inverter usable in a pipeline. No external
+libraries other than libc are needed to handle the image data:
.Bd -literal -offset left
#include <arpa/inet.h>
@@ -55,49 +55,58 @@ than libc are needed to read the image data:
#include <stdlib.h>
#include <string.h>
-(...)
+int
+main(int argc, char *argv[])
+{
+ uint32_t width, height, i, j, k;
+ uint16_t rgba[4];
+ uint8_t hdr[strlen("farbfeld") + 2 * sizeof(uint32_t)];
-uint32_t width, height, i, j, k;
-uint16_t rgba[4];
-uint8_t hdr[strlen("farbfeld") + 2 * sizeof(uint32_t)];
+ if (argc > 1) {
+ fprintf(stderr, "usage: %s\\n", argv[0]);
+ exit(1);
+ }
-if (fread(hdr, 1, sizeof(hdr), infile) != sizeof(hdr)) {
- fprintf(stderr, "incomplete header\\n");
- exit(1);
-}
-if (memcmp("farbfeld", hdr, strlen("farbfeld"))) {
- fprintf(stderr, "invalid magic\\n");
- exit(1);
-}
-width = ntohl(*((uint32_t *)(hdr + 8)));
-height = ntohl(*((uint32_t *)(hdr + 12)));
+ if (fread(hdr, 1, sizeof(hdr), stdin) != sizeof(hdr)) {
+ fprintf(stderr, "incomplete header\\n");
+ exit(1);
+ }
+ if (memcmp("farbfeld", hdr, strlen("farbfeld"))) {
+ fprintf(stderr, "invalid magic\\n");
+ exit(1);
+ }
+ width = ntohl(*((uint32_t *)(hdr + 8)));
+ height = ntohl(*((uint32_t *)(hdr + 12)));
-if (fwrite(hdr, 1, sizeof(hdr), outfile) != sizeof(hdr)) {
- fprintf(stderr, "write error\\n");
- exit(1);
-}
+ if (fwrite(hdr, 1, sizeof(hdr), stdout) != sizeof(hdr)) {
+ fprintf(stderr, "write error\\n");
+ exit(1);
+ }
-for (i = 0; i < height; i++) {
- for (j = 0; j < width; j++) {
- if (fread(rgba, sizeof(uint16_t), 4, infile) != 4) {
- fprintf(stderr, "unexpected EOF\\n");
- exit(1);
- }
- for (k = 0; k < 4; k++) {
- rgba[k] = ntohs(rgba[k]);
- }
+ for (i = 0; i < height; i++) {
+ for (j = 0; j < width; j++) {
+ if (fread(rgba, sizeof(uint16_t), 4,
+ stdin) != 4) {
+ fprintf(stderr, "unexpected EOF\\n");
+ exit(1);
+ }
+ 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 colors */
+ rgba[0] = 65535 - rgba[0];
+ rgba[1] = 65535 - rgba[1];
+ rgba[2] = 65535 - rgba[2];
- for (k = 0; k < 4; k++) {
- rgba[k] = htons(rgba[k]);
- }
- if (fwrite(rgba, sizeof(uint16_t), 4, outfile) != 4) {
- fprintf(stderr, "write error\\n");
- exit(1);
+ for (k = 0; k < 4; k++) {
+ rgba[k] = htons(rgba[k]);
+ }
+ if (fwrite(rgba, sizeof(uint16_t), 4,
+ stdout) != 4) {
+ fprintf(stderr, "write error\\n");
+ exit(1);
+ }
}
}
}