aboutsummaryrefslogtreecommitdiffstats
path: root/src/blind-flop.c
blob: c5826be7b5f604590fa47162f60d4c9f949f1894 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/* See LICENSE file for copyright and license details. */
#include "stream.h"
#include "util.h"

#include <inttypes.h>
#include <string.h>
#include <unistd.h>

USAGE("")

static struct stream stream;
static char *buf, *image;
static size_t n, m, ps;

#define PROCESS(TYPE)\
	do {\
		size_t i, j, pst = ps / sizeof(TYPE);\
		size_t nt = n / sizeof(TYPE);\
		size_t mt = m / sizeof(TYPE);\
		for (i = 0; i < pst; i++)\
			for (j = 0; j < nt; j += pst)\
				((TYPE *)image)[mt - j + i] = ((TYPE *)buf)[i + j];\
	} while (0)

static void process_double(void) {PROCESS(double);}
static void process_float (void) {PROCESS(float);}
static void process_char  (void) {PROCESS(char);}

int
main(int argc, char *argv[])
{
	void (*process)(void);

	UNOFLAGS(argc);

	eopen_stream(&stream, NULL);
	fprint_stream_head(stdout, &stream);
	efflush(stdout, "<stdout>");

	echeck_frame_size(stream.width, 1, stream.pixel_size, 0, stream.file);
	n = stream.width * (ps = stream.pixel_size);
	buf   = emalloc(n);
	image = emalloc(n);

	process = !(ps % sizeof(double)) ? process_double :
		  !(ps % sizeof(float))  ? process_float  : process_char;

	m = n - ps;
	while (eread_row(&stream, buf, n)) {
		process();
		ewriteall(STDOUT_FILENO, image, n, "<stdout>");
	}

	free(buf);
	free(image);
	return 0;
}