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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
/* mrftopbm - convert mrf to pbm
* public domain by RJM
*
* Adapted to Netpbm by Bryan Henderson 2003.08.09. Bryan got his copy from
* ftp://ibiblio.org/pub/linux/apps/convert, dated 1997.08.19.
*
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include "pm_c_util.h"
#include "nstring.h"
#include "pbm.h"
static int bitbox;
static int bitsleft;
static void
bit_init(void) {
bitbox=0;
bitsleft=0;
}
static int
bit_input(FILE * const in) {
if (bitsleft == 0) {
bitbox = fgetc(in);
bitsleft = 8;
}
--bitsleft;
return((bitbox&(1<<bitsleft))?1:0);
}
static void
doSquare(FILE * const in,
unsigned char * const image,
int const ox,
int const oy,
int const w,
int const size) {
if (size == 1 || bit_input(in)) {
/* It's all black or all white. Next bit says which. */
unsigned int const c = bit_input(in);
unsigned int y;
for (y = 0; y < size; ++y) {
unsigned int x;
for (x = 0; x < size; ++x)
image[(oy+y)*w+ox+x] = c;
}
} else {
/* not all one color, so recurse. */
doSquare(in, image, ox, oy, w, size >> 1);
doSquare(in, image, ox+size, oy, w, size >> 1);
doSquare(in, image, ox, oy+size,w, size >> 1);
doSquare(in, image, ox+size, oy+size,w, size >> 1);
}
}
static void
writeOutput(FILE * const ofP,
int const cols,
int const rows,
const unsigned char * const image) {
/* w64 is units-of-64-bits width, h64 same for height */
unsigned int const w64 = (cols+63)/64;
bit * bitrow;
unsigned int row;
pbm_writepbminit(ofP, cols, rows, FALSE);
bitrow = pbm_allocrow(cols);
for (row = 0; row < rows; ++row) {
unsigned int col;
for (col = 0; col < cols; ++col)
bitrow[col] =
(image[row * (w64*64) + col] == 1) ? PBM_WHITE : PBM_BLACK;
pbm_writepbmrow(ofP, bitrow, cols, FALSE);
}
pbm_freerow(bitrow);
}
static void
readMrfImage(FILE * const ifP,
bool const expandAll,
unsigned char ** const imageP,
unsigned int * const colsP,
unsigned int * const rowsP) {
static unsigned char buf[128];
unsigned int rows;
unsigned int cols;
unsigned int w64, h64;
unsigned char * image;
fread(buf, 1, 13, ifP);
if (memcmp(buf, "MRF1", 4) != 0)
pm_error("Input is not an mrf image. "
"We know this because it does not start with 'MRF1'.");
if (buf[12] != 0)
pm_error("can't handle file subtype %u", buf[12]);
cols = (buf[4] << 24) | (buf[5] << 16) | (buf[06] << 8) | buf[07] << 0;
rows = (buf[8] << 24) | (buf[9] << 16) | (buf[10] << 8) | buf[11] << 0;
/* w64 is units-of-64-bits width, h64 same for height */
w64 = (cols+63)/64;
h64 = (rows+63)/64;
if (expandAll) {
*colsP = w64*64;
*rowsP = h64*64;
} else {
*colsP = cols;
*rowsP = rows;
}
if (UINT_MAX/w64/64/h64/64 == 0)
pm_error("Ridiculously large, unprocessable image: %u cols x %u rows",
cols, rows);
image = calloc(w64*h64*64*64,1);
if (image == NULL)
pm_error("Unable to get memory for raster");
/* now recursively input squares. */
bit_init();
{
unsigned int row;
for (row = 0; row < h64; ++row) {
unsigned int col;
for (col = 0; col < w64; ++col)
doSquare(ifP, image, col*64, row*64, w64*64, 64);
}
}
*imageP = image;
}
int
main(int argc, char *argv[]) {
FILE *ifP;
FILE *ofP;
unsigned char *image;
bool expandAll;
unsigned int cols, rows;
pbm_init(&argc, argv);
expandAll = FALSE; /* initial assumption */
if (argc-1 >= 1 && STREQ(argv[1], "-a")) {
expandAll = TRUE;
argc--,argv++;
}
if (argc-1 > 1)
pm_error("Too many arguments: %d. Only argument is input file",
argc-1);
if (argc-1 == 1)
ifP = pm_openr(argv[1]);
else
ifP = stdin;
ofP = stdout;
readMrfImage(ifP, expandAll, &image, &cols, &rows);
pm_close(ifP);
writeOutput(ofP, cols, rows, image);
free(image);
return 0;
}
|