-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpgmtobrailedux.c
189 lines (151 loc) · 4.41 KB
/
pgmtobrailedux.c
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/* pgmtobraile
* Convert a pgm image to a unicode braile image
* Copyright(c) 2013 by Christopher Abad | 20 GOTO 10
*
* email: [email protected] [email protected]
* http://www.256.bz/ http://www.twentygoto10.com/
* git: [email protected]:aempirei/Chat-Art.git
* aim: ambientempire
*/
/*
* also it includes its own pgm dithering now
* an example preparation of a jpeg for conversion using netpbm:
*
* djpeg rms.jpg | pnmscale -ysize 120 | ppmnorm | ppmtopgm | ./pgmtobraile
*
*/
/* -- FIXME: after the image is approximated via dither, then the error is calculated against the ORIGINAL image */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>
#include <errno.h>
#include <locale.h>
#include <wchar.h>
#include <wctype.h>
#include <ctype.h>
#define BIT(p,x,y,w) (p)[(x) + (y) * (w)]
#define BIT0(p,x,y,w) BIT((p),(x)+0,(y)+0,(w))
#define BIT1(p,x,y,w) BIT((p),(x)+0,(y)+1,(w))
#define BIT2(p,x,y,w) BIT((p),(x)+0,(y)+2,(w))
#define BIT3(p,x,y,w) BIT((p),(x)+1,(y)+0,(w))
#define BIT4(p,x,y,w) BIT((p),(x)+1,(y)+1,(w))
#define BIT5(p,x,y,w) BIT((p),(x)+1,(y)+2,(w))
#define BIT6(p,x,y,w) BIT((p),(x)+0,(y)+3,(w))
#define BIT7(p,x,y,w) BIT((p),(x)+1,(y)+3,(w))
const int upper = 0xff;
const int lower = 0x01;
int mid = 0x80;
int getmean(int *data, size_t sz) {
int mean = 0;
for(size_t z = 0; z < sz; z++)
mean += data[sz];
return mean / sz;
}
void dither(int *data, int *error, size_t width, size_t height) {
int *front;
size_t sz = width * height;
front = malloc(sizeof(int) * sz);
error = malloc(sizeof(int) * sz);
memset(error, 0, sizeof(int) * sz);
for(size_t z = 0; z < sz; z++)
front[z] = ((data[z] >= mid) ? upper : lower) - data[z];
for(size_t y = 0; y < height; y++) {
for(size_t x = 0; x < width; x++) {
int used = (x<width-1)+(y<height-1)+1;
int e = BIT(front,x,y,width) / used;
if(x < width - 1)
BIT(error,x+1,y,width) += e;
if(y < height - 1)
BIT(error,x,y+1,width) += e;
// BIT(error,x,y,width) -= e * used;
}
}
int max = lower;
int min = upper;
for(size_t z = 0; z < sz; z++) {
data[z] += error[z];
if(data[z] > max)
max = data[z];
if(data[z] < min)
min = data[z];
}
for(size_t z = 0; z < sz; z++) {
data[z] = ((upper - lower) * (data[z] - min) / (max - min)) + lower;
}
free(front);
}
void braileencode(FILE *fpout, int *data, size_t width, size_t height) {
for(size_t y = 0; y < height; y += 4) {
for(size_t x = 0; x < width; x += 2) {
wint_t wch = 0x2800;
if(BIT0(data,x,y,width)>mid) wch |= 0x01;
if(BIT1(data,x,y,width)>mid) wch |= 0x02;
if(BIT2(data,x,y,width)>mid) wch |= 0x04;
if(BIT3(data,x,y,width)>mid) wch |= 0x08;
if(BIT4(data,x,y,width)>mid) wch |= 0x10;
if(BIT5(data,x,y,width)>mid) wch |= 0x20;
if(BIT6(data,x,y,width)>mid) wch |= 0x40;
if(BIT7(data,x,y,width)>mid) wch |= 0x80;
fputwc(wch, fpout);
}
fputwc(L'\n', fpout);
}
}
void pgmtobraile(FILE * fpin, FILE * fpout) {
const size_t expected_maxval = 255;
const char *locale = "";
uint8_t *bytedata;
int *img;
int *error;
unsigned int width;
unsigned int height;
unsigned int maxval;
int ch;
if (setlocale(LC_CTYPE, locale) == NULL) {
fprintf(stderr, "failed to set locale LC_CTYPE=\"%s\"\n", locale);
exit(EXIT_FAILURE);
}
if(fscanf(fpin, "P5 %u %u %u", &width, &height, &maxval) != 3) {
fprintf(stderr, "failed to parse pgm header\n");
exit(EXIT_FAILURE);
}
ch = fgetc(fpin);
if(ch == EOF) {
perror("fgetc()");
exit(EXIT_FAILURE);
}
if(!isspace(ch)) {
fprintf(stderr, "unexpected character found after maxval, was expecting whitespace\n");
exit(EXIT_FAILURE);
}
if(maxval != expected_maxval) {
fprintf(stderr, "unexpected maxval of %d, was expecting %d\n", maxval, (int)expected_maxval);
}
bytedata = malloc(width * height);
if(fread(bytedata, width, height, fpin) != (size_t)height) {
perror("fread()");
exit(EXIT_FAILURE);
}
if(fgetc(fpin) != EOF || !feof(fpin)) {
fprintf(stderr, "unexpected trailing data after pgm\n");
}
img = malloc(width * height * sizeof(int));
error = malloc(width * height * sizeof(int));
for(size_t z = 0; z < width * height; z++) {
img[z] = bytedata[z];
error[z] = 0;
}
for(int i = 0; i < 5; i++)
dither(img, error, width, height);
braileencode(fpout, img, width, height);
free(bytedata);
free(error);
free(img);
}
int main(int argc, char **argv) {
if(argc == 2) mid = atoi(argv[1]);
pgmtobraile(stdin, stdout);
exit(EXIT_SUCCESS);
}