fix endianess, ignore alpha, add ff2col - 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 --- DIR commit 1b7f84f5eed357743b19d90188c0dcc0736db014 DIR parent 0618ea49eedccf14ae19e93f9e8ec1ffd43daa44 HTML Author: Josuah Demangeon <mail@josuah.net> Date: Mon, 29 Jan 2018 12:40:46 +0100 fix endianess, ignore alpha, add ff2col Diffstat: M Makefile | 10 +++++++--- D ff2ascii.c | 101 ------------------------------- M ff2braille.c | 2 +- A ff2col.1 | 34 +++++++++++++++++++++++++++++++ M ff2col.c | 4 ++-- M util.c | 21 +++++++++++++-------- M util.h | 2 +- 7 files changed, 58 insertions(+), 116 deletions(-) --- DIR diff --git a/Makefile b/Makefile @@ -1,17 +1,21 @@ CFLAGS = -std=c89 -pedantic -Wall -Wextra -Werror + all: ff2braille ff2col +.c.o: + ${CC} ${CFLAGS} -c $< -o $@ + ff2braille: ff2braille.o util.o util.h - cc -o $@ ff2braille.o util.o $(LDFLAGS) + ${CC} -o $@ ff2braille.o util.o $(LDFLAGS) ff2col: ff2col.o util.o util.h - cc -o $@ ff2col.o util.o $(LDFLAGS) + ${CC} -o $@ ff2col.o util.o $(LDFLAGS) install: mkdir -p ${PREFIX}/bin cp ff2braille ${PREFIX}/bin clean: - rm -f *.o ff2braille + rm -f *.o ff2braille ff2col .PHONY: all install clean DIR diff --git a/ff2ascii.c b/ff2ascii.c @@ -1,101 +0,0 @@ -/* - * Transforming an image into a braille character text is transforming a - * matrice this way: - * - * farbfeld image braille characters - * - * [[a0 a1 a2 a3 a4 a5] [[[a0 a1 [a2 a3 [a4 a5 \ - * [b0 b1 b2 b3 b4 b5] b0 b1 / b2 b3 / b4 b5 |<- One braille - * [c0 c1 c2 c3 c4 c5] c0 c1 / c2 c3 / c4 c5 | character - * [d0 d1 d2 d3 d4 d5] => d0 d1] d2 d3] d4 d5]] / - * [e0 e1 e2 e3 e4 e5] [[e0 e1 [e2 e3 [e4 e5 - * [f0 f1 f2 f3 f4 f5] f0 f1 / f2 f3 / f4 f5 - * [g0 g1 g2 g3 g4 g5] g0 g1 / g2 g3 / g4 g5 - * [h0 h1 h2 h3 h4 h5]] h0 h1] h2 h3] h4 h5]]] - * - * braille characters written as a line: - * - * [[[a0 a1 b0 b1 c0 c1 d0 d1] <- One braille character - * [a2 a3 b2 b3 c2 c3 d2 d3] - * [a4 a5 b4 b5 c4 c5 d4 d5]] <- One row of braille characters - * [[e0 e1 f0 f1 g0 g1 h0 h1] - * [e2 e3 f2 f3 g2 g3 h2 h3] - * [e4 e5 f4 f5 g4 g5 h4 h5]]] <- Two row of braille characters - * - * Although the encoding of braille keeps 1 4 - * the characters encoded with only six 2 5 - * dots first, as only these are used for 3 6 - * encoding letters: 7 8 - */ -#include <stdio.h> -#include <string.h> -#include <stdlib.h> -#include <stdint.h> - -#include "util.h" - -void -print_utf8_3byte(long rune) -{ - printf("%c%c%c", - (char)(0xe0 | (0x0f & (rune >> 12))), /* 1110xxxx */ - (char)(0x80 | (0x3f & (rune >> 6))), /* 10xxxxxx */ - (char)(0x80 | (0x3f & (rune)))); /* 10xxxxxx */ -} - -int -is_on(struct col *rows[4], uint32_t width, uint32_t height, uint32_t w, - uint32_t h) -{ - uint16_t sum; - - if (w >= width || h >= height) - return 0; - - /* divide first to avoid overflow */ - sum = rows[h][w].red / 4; - sum += rows[h][w].green / 4; - sum += rows[h][w].blue / 4; - sum += rows[h][w].alpha / 4; - return sum >= UINT16_MAX / 2; -} - -void -print_4_rows(struct col *rows[4], uint32_t width, uint32_t height) -{ - uint32_t w; - - for (w = 0; w < width; w += 2) - print_utf8_3byte(BRAILLE_START + - 0x01 * is_on(rows, width, height, w + 0, 0) + - 0x08 * is_on(rows, width, height, w + 1, 0) + - 0x02 * is_on(rows, width, height, w + 0, 1) + - 0x10 * is_on(rows, width, height, w + 1, 1) + - 0x04 * is_on(rows, width, height, w + 0, 2) + - 0x20 * is_on(rows, width, height, w + 1, 2) + - 0x40 * is_on(rows, width, height, w + 0, 3) + - 0x80 * is_on(rows, width, height, w + 1, 3)); - putchar('\n'); -} - -int -main(void) -{ - struct col buf[MAX_WIDTH * 4], *rows[4]; - uint32_t width, height, h, r, i; - - read_header(&width, &height); - - for (i = 0; i < 4; i++) - rows[i] = buf + width * i; - - for (h = 0; h < height; h += 4) { - r = fread(buf, sizeof(*buf), width * 4, stdin); - if (r % width != 0) - err("invalid line width"); - print_4_rows(rows, width, r / width); - if (ferror(stdin)) - err("fread stdin"); - } - return 0; -} DIR diff --git a/ff2braille.c b/ff2braille.c @@ -51,7 +51,7 @@ is_on(struct col *rows[4], uint32_t width, uint32_t height, if (w >= width || h >= height) return 0; - return is_bright(rows[h][w]); + return col_is_bright(rows[h][w]); } void DIR diff --git a/ff2col.1 b/ff2col.1 @@ -0,0 +1,34 @@ +.Dt FF2COL 1 +.Dd $Mdocdate: January 22 2018$ +.Os +. +. +.Sh NAME +. +.Nm ff2col +.Nd farbfeld to text column, comma, single-quote image conversion +. +. +.Sh SYNOPSIS +. +.Nm +. +. +.Sh DESCRIPTION +. +.Nm +reads a +.Xr farbfeld 5 +image from stdin and write a text representation of the image using +.Dq \&'\&.\&: +characters to stdout. +. +. +.Sh EXIT STATUS +. +.Ex -std +. +. +.Sh EXAMPLES +. +.Dl pg2ff < image.png | ff2col > file.txt DIR diff --git a/ff2col.c b/ff2col.c @@ -13,8 +13,8 @@ print_2_rows(struct col *buf, uint32_t width, int height) char *map = " '.:"; for (w = 0; w < width; w++) { - up = height > 0 && is_bright(buf[w]) ? 0x1 : 0x0; - dn = height > 1 && is_bright(buf[w + width]) ? 0x2 : 0x0; + up = height > 0 && col_is_bright(buf[w]) ? 0x1 : 0x0; + dn = height > 1 && col_is_bright(buf[w + width]) ? 0x2 : 0x0; putchar(map[up | dn]); } putchar('\n'); DIR diff --git a/util.c b/util.c @@ -32,16 +32,21 @@ read_header(uint32_t *width, uint32_t *height) err("empty image"); } -int -is_bright(struct col col) +static uint16_t +col_blend(struct col col) { - int32_t sum; + int16_t sum; /* divide first to avoid overflow */ - sum = htonl(col.red) / 4; - sum += htonl(col.green) / 4; - sum += htonl(col.blue) / 4; - sum += htonl(col.alpha) / 4; + sum = ntohs(col.red) / 3; + sum += ntohs(col.green) / 3; + sum += ntohs(col.blue) / 3; + + return sum; +} - return sum <= UINT16_MAX / 2; +int +col_is_bright(struct col col) +{ + return col_blend(col) >= UINT16_MAX / 2; } DIR diff --git a/util.h b/util.h @@ -13,4 +13,4 @@ struct col { void err(char *); void read_header(uint32_t *, uint32_t *); -int is_bright(struct col); +int col_is_bright(struct col);