This repository has been archived by the owner on Jun 16, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
xvwbmp.c
345 lines (280 loc) · 6.57 KB
/
xvwbmp.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
* xvwbmp.c - i/o routings for WBMP files
* defined by OMA (http://www.openmobilealliance.com)
* as a standard for images for micro devices.
*
* exports :
*
* LoadWBMP(fname, numcols);
* WriteWBMP(fp, pic, ptype, w, h, r, g, b, numcols, style);
*
* author: Pawel S. Veselov <[email protected]>
* http://manticore.2y.net/
*
*/
#include "xv.h"
typedef short int16;
typedef unsigned char uint8;
typedef unsigned short uint16; /* sizeof (uint16) must == 2 */
#if defined(__alpha) || _MIPS_SZLONG == 64
typedef int int32;
typedef unsigned int uint32; /* sizeof (uint32) must == 4 */
#else
typedef long int32;
typedef unsigned long uint32; /* sizeof (uint32) must == 4 */
#endif
#define MUST(a) if (!(a)) {\
close(fd); \
return fail(st_fname, st_err); }
#define READU8(fd,u) if ((read(fd, &u, 1)<1)) {\
myfree(); \
close(fd); \
return fail(st_fname, err_ueof); }
#define SREADU8(fd, u) if ((read(fd, &u, 1,)<1)) {\
{ st_err = err_ueof; return 0; }
#define SREADC(fd, str, l) { \
str = (char*)mymalloc(l); \
if (!str) { \
myfree(); \
FatalError("LoadWBMP: can't malloc extension buffer"); \
} \
if (read(fd, str, l)<l) { \
st_err = err_ueof; \
return 0; \
}
static const char err_ueof[] = "Unexpected EOF";
static const char err_unst[] = "Unsupported image type";
static const char err_extf[] = "Extensions are forbidden";
static const char err_inmb[] = "Invalid multibyte integer";
static const char *st_fname;
static const char *st_err;
static int fail PARM((const char *, const char *));
static int read_mb PARM((int *, int));
static void write_mb PARM((uint32, FILE *));
static int read_ext PARM((int, uint8));
static void *mymalloc PARM((int));
static void myfree PARM((void));
static uint8 *render1 PARM((uint8 *, int, int));
static void **mymem = NULL;
static int mymems = 0;
int LoadWBMP(fname, pinfo)
char *fname;
PICINFO *pinfo;
{
int fd;
int im_type; /* image type (only type 0 supported) */
uint8 fix_header; /* fixed header field */
int width, height;
unsigned int npixels, raw_size, aux;
uint8 * raw;
st_fname = fname;
fd = open(fname, O_RDONLY);
if (fd < 0) {
return fail(fname, "Couldn't open the file");
}
MUST(read_mb(&im_type, fd));
if (im_type) {
return fail(fname, err_unst);
}
READU8(fd, fix_header);
MUST(read_ext(fd, fix_header));
MUST(read_mb(&width, fd));
MUST(read_mb(&height, fd));
npixels = width * height;
raw_size = (npixels+7) / 8;
if (width <= 0 || height <= 0 || npixels/width != height ||
npixels+7 < npixels)
{
return fail(fname, "image dimensions out of range");
}
raw = mymalloc(raw_size);
if (!raw) {
myfree();
FatalError("LoadWBMP: can't malloc image buffer");
}
aux = read(fd, raw, raw_size);
if (aux < raw_size) {
fail(fname, "Image size shrank");
raw_size = aux;
}
pinfo->r[0] = 0;
pinfo->g[0] = 0;
pinfo->b[0] = 0;
pinfo->r[1] = 255;
pinfo->g[1] = 255;
pinfo->b[1] = 255;
pinfo->pic = render1(raw, raw_size, npixels);
pinfo->type = PIC8;
pinfo->w = pinfo->normw = width;
pinfo->h = pinfo->normh = height;
pinfo->frmType = F_BWDITHER;
sprintf(pinfo->fullInfo, "WBMP, 1 bit per pixel, %d bytes", raw_size);
sprintf(pinfo->shrtInfo, "%dx%d WBMP (WAP/OMA).", width, height);
pinfo->comment = (char*)NULL;
close(fd);
myfree();
return 1;
}
int WriteWBMP(fp, pic, ptype, w, h, rmap, gmap, bmap, numcols, colorstyle)
FILE *fp;
byte *pic;
int ptype, w, h;
byte *rmap, *gmap, *bmap;
int numcols, colorstyle;
{
int count = 0;
uint8 bit = 0;
int i;
write_mb(0, fp); /* type : always 0 */
putc(0, fp); /* fixed header : always 0 for type 0 */
write_mb((uint32)w, fp);
write_mb((uint32)h, fp);
/* ready to write data */
for (i=0; i<w*h; i++) {
bit |= (((pic[i]&1)<<(7-(count++))));
if (count == 8) {
putc(bit, fp);
count = 0;
}
}
if (!count) {
putc(bit, fp);
}
return 0;
}
static int fail(name, msg)
const char *name, *msg;
{
SetISTR(ISTR_WARNING, "%s : %s", name, msg);
return 0;
}
static void write_mb(data, f)
uint32 data;
FILE *f;
{
int i = 32;
uint32 aux = data;
int no;
if (!aux) {
i = 1;
} else {
while (!(aux & 0x80000000)) {
aux <<= 1;
i--;
}
}
/* i tells us how many bits are left to encode */
no = (i / 7 + ((i % 7)?1:0))-1;
/*
fprintf(stderr, "writing %x, bits to write=%d, passes=%d\n",
data, i, no);
*/
do {
uint8 value = no?0x80:0x0;
value |= ((data >> (no*7)) & 0x7f);
putc(value, f);
} while ((no--)>0);
}
static int read_mb(dst, fd)
int *dst, fd;
{
int ac = 0;
int ct = 0;
while (1) {
uint8 bt;
if ((ct++)==6) {
st_err = err_inmb;
return 0;
}
if ((read(fd, &bt, 1)) < 1) {
st_err = err_ueof;
return 0;
}
ac = (ac << 7) | (bt & 0x7f); /* accumulates up to 42 bits?? FIXME */
if (!(bt & 0x80))
break;
}
*dst = ac;
return 1;
}
static int read_ext(fd, fixed)
int fd;
uint8 fixed;
{
if (!(fixed&0x7f)) { /* no extensions */
return 1;
}
/*
* The only described type is WBMP 0, that must not
* have extensions.
*/
st_err = err_extf;
return 0;
/*
fixed = (fixed >> 5)&0x3;
switch (fixed) {
case 0:
while (true) {
SREADU8(fd, fixed);
if (!(fixed & 0x7f)) { break; }
}
break;
case 0x3:
{
char * par;
char * val;
SREADU8(fd, fixed);
SREADC(fd, par, (fixed>>4)&0x6);
SREADC(fd, val, fixed&0xf);
}
break;
}
*/
}
static void *mymalloc(numbytes)
int numbytes;
{
mymem = (void**)realloc(mymem, mymems+1);
if (!mymem)
FatalError("LoadWBMP: can't realloc buffer");
return (mymem[mymems++] = malloc(numbytes));
}
static void myfree()
{
int i;
if (mymem) {
for (i=0; i<mymems; i++) {
if (mymem[i])
free(mymem[i]);
}
free(mymem);
}
mymem = (void**)NULL;
mymems = 0;
}
static uint8 *render1(data, size, npixels)
uint8 *data;
int size, npixels;
{
byte * pic;
int i;
int cnt = 0;
uint8 cb = *data;
pic = calloc(npixels,1); /* checked for overflow by caller */
if (!pic) {
myfree();
FatalError("LoadWBMP: can't allocate 'pic' buffer");
}
/* expand bits into bytes */
/* memset(pic, 0, npixels); */
for (i=0; i<npixels; i++) {
pic[i] = (cb>>7)&1;
if ((++cnt)==8) {
cb = *(++data);
cnt = 0;
} else {
cb <<=1;
}
}
return pic;
}