-
Notifications
You must be signed in to change notification settings - Fork 0
/
xvrle.c
382 lines (298 loc) · 9.58 KB
/
xvrle.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*
* xvrle.c - load routine for rle (Utah Raster Toolkit) format pictures
*
* LoadRLE(fname, numcols) - loads an RLE file
*/
#include "copyright.h"
#include "xv.h"
/* Definitions needed to parse RLE format */
/* snarfed from rle_code.h, part of the Utah Raster Toolkit */
#define LONG_OP 0x40
#define RSkipLinesOp 1
#define RSetColorOp 2
#define RSkipPixelsOp 3
#define RByteDataOp 5
#define RRunDataOp 6
#define REOFOp 7
#define H_CLEARFIRST 0x1 /* clear framebuffer flag */
#define H_NO_BACKGROUND 0x2 /* if set, no bg color supplied */
#define H_ALPHA 0x4 /* if set, alpha channel (-1) present */
#define H_COMMENT 0x8 /* if set, comments present */
#define GETINT(fp) (c=getc(fp), c1=getc(fp), (c1<<8) + c )
static void read_rle PARM((FILE *, byte *, int, int, int, int));
static int rleError PARM((const char *, const char *));
/*******************************************/
int LoadRLE(fname, pinfo)
char *fname;
PICINFO *pinfo;
/*******************************************/
{
FILE *fp;
int c, c1, i, j, k;
byte bgcol[256];
byte maps[3][256];
int xpos, ypos, w, h, flags, ncolors, pixelbits, ncmap, cmaplen;
int cmtlen;
byte *img;
long filesize;
const char *bname;
const char *errstr;
pinfo->type = PIC8;
pinfo->pic = (byte *) NULL;
pinfo->comment = (char *) NULL;
bname = BaseName(fname);
/* open the stream */
fp = xv_fopen(fname,"r");
if (!fp) return (rleError(bname, "unable to open file"));
/* figure out the file size */
fseek(fp, 0L, 2);
filesize = ftell(fp);
fseek(fp, 0L, 0);
/* read the magic number */
c = getc(fp); c1 = getc(fp);
if ((c != 0x52) || (c1 != 0xcc))
return(rleError(bname, "unrecognized magic number"));
xpos = GETINT(fp); /* read rest of header info */
ypos = GETINT(fp);
w = GETINT(fp);
h = GETINT(fp);
flags = getc(fp);
ncolors = getc(fp);
pixelbits = getc(fp);
ncmap = getc(fp);
c = getc(fp);
cmaplen = (1L << c);
if (DEBUG) {
fprintf(stderr,"RLE: %dx%d image at %d,%d\n", w, h, xpos, ypos);
fprintf(stderr,"flags: 0x%02x (%s%s%s%s)\n",
flags,
(flags & H_CLEARFIRST) ? "CLEARFIRST " : "",
(flags & H_NO_BACKGROUND) ? "NO_BG " : "",
(flags & H_ALPHA) ? "ALPHA " : "",
(flags & H_COMMENT) ? "COMMENT" : "");
fprintf(stderr, "%d bands, %d pixelbits, %d cmap bands, %d cmap entries\n",
ncolors, pixelbits, ncmap, cmaplen);
}
if (!(flags & H_NO_BACKGROUND)) {
if (DEBUG) fprintf(stderr, "background value: ");
for (i=0; i<ncolors; i++) {
bgcol[i] = getc(fp);
if (DEBUG) fprintf(stderr, "0x%02x ", bgcol[i]);
}
if (DEBUG) fprintf(stderr,"\n");
}
else {
getc(fp); /* skip filler byte */
}
if ((ncolors % 2) == 0) getc(fp); /* get on a word boundary */
/* read colormap(s) */
for (i=0; i<ncmap; i++) {
for (j = 0; j<cmaplen; j++) {
k = GETINT(fp);
if (i<3) maps[i][j] = k>>8;
}
}
if (DEBUG) {
if (ncmap) {
fprintf(stderr, "Colormap:\n");
for (i=0; i<cmaplen; i++) {
fprintf(stderr,"(");
for (j=0; (j<ncmap && j<3); j++) {
fprintf(stderr, "%02x ", maps[j][i]);
}
fprintf(stderr,") ");
}
fprintf(stderr,"\n\n");
}
else fprintf(stderr,"No colormap\n");
}
/* read (skip over, actually) the comments, if any */
if (flags & H_COMMENT) {
cmtlen = GETINT(fp);
if (cmtlen) {
pinfo->comment = (char *) malloc((size_t) (cmtlen + 1));
if (DEBUG) fprintf(stderr,"Comment: (%d bytes) '", cmtlen);
for (i=0; i<cmtlen; i++) {
c = getc(fp);
if (c==EOF) break;
if (pinfo->comment) {
if (c == '\0') c = '\n'; /* translate NUL to NL */
pinfo->comment[i] = (char) c;
}
if (DEBUG) fprintf(stderr,"%c",c);
}
if (pinfo->comment) pinfo->comment[i] = '\0';
if (cmtlen % 2) getc(fp); /* get on a word boundary */
if (DEBUG) fprintf(stderr,"'\n\n");
}
}
if (ferror(fp) || feof(fp)) {
fclose(fp);
if (pinfo->comment) free(pinfo->comment); pinfo->comment = (char *) NULL;
return rleError(bname, "EOF reached in RLE header.\n");
}
/*
* Acceptable cases:
* ncolors = 1, 3, or >3 (extra planes ignored)
* pixelbits = 8
* ncmap = 0 (interpreted as TrueColor/TrueGray)
* 1 (TrueColor/TrueGray with a gamma curve)
* 3 | ncolors (TrueColor with three gamma curves, or
* PseudoColor if ncolors==1)
*/
errstr = NULL;
if (ncolors == 0 || ncolors == 2)
errstr = "Unsupt. # of channels in RLE file.\n";
if (pixelbits != 8)
errstr = "Only 8-bit pixels supported in RLE files.\n";
if (ncmap==0 || ncmap==1 || ncmap == 3 || ncmap == ncolors) { /* ok */ }
else errstr = "Invalid # of colormap channels in RLE file.\n";
if (w<1 || h<1)
errstr = "Bogus size in RLE header.\n";
if (errstr) {
fclose(fp);
if (pinfo->comment) free(pinfo->comment); pinfo->comment = (char *) NULL;
return rleError(bname, errstr);
}
/* allocate image memory */
if (ncolors == 1) img = (byte *) calloc((size_t) w * h, (size_t) 1);
else img = (byte *) calloc((size_t) w * h * 3, (size_t) 1);
if (!img) {
fclose(fp);
if (pinfo->comment) free(pinfo->comment); pinfo->comment = (char *) NULL;
return rleError(bname, "unable to allocate image data.\n");
}
/* set background, if necessary */
if ((flags & H_CLEARFIRST) && !(flags & H_NO_BACKGROUND)) {
byte *ip;
if (ncolors == 1) {
for (i=0, ip=img; i<w*h; i++, ip++) *ip = bgcol[0];
}
else {
for (i=0, ip=img; i<w*h; i++)
for (j=0; j<3; j++, ip++) *ip = bgcol[j];
}
}
read_rle(fp, img, w, h, ncolors, ncmap);
if (ferror(fp) || feof(fp)) /* just a warning */
rleError(bname, "RLE file appears to be truncated.");
fclose(fp);
/* apply gamma curve(s) to image (if grayscale or truecolor) */
if (ncmap) {
byte *ip;
int imagelen, cmask;
imagelen = (ncolors==1) ? w*h : w*h*3;
cmask = (cmaplen-1);
if (ncmap == 1) { /* single gamma curve */
for (i=0, ip=img; i<imagelen; i++, ip++) *ip = maps[0][*ip & cmask];
}
else if (ncmap >= 3 && ncolors >=3) { /* one curve per band */
for (i=0, ip=img; i<w*h; i++) {
*ip = maps[0][*ip & cmask]; ip++;
*ip = maps[1][*ip & cmask]; ip++;
*ip = maps[2][*ip & cmask]; ip++;
}
}
}
/* finally, convert into XV internal format */
pinfo->pic = img;
pinfo->w = w;
pinfo->h = h;
pinfo->normw = pinfo->w; pinfo->normh = pinfo->h;
pinfo->frmType = -1; /* no default format to save in */
if (ncolors == 1) { /* grayscale or PseudoColor */
pinfo->type = PIC8;
if (ncmap == 0 || ncmap == 1) { /* grey, or grey with gamma curve */
pinfo->colType = F_GREYSCALE;
sprintf(pinfo->fullInfo, "Greyscale RLE. (%ld bytes)", filesize);
for (i=0; i<256; i++)
pinfo->r[i] = pinfo->g[i] = pinfo->b[i] = i;
}
else {
pinfo->colType = F_FULLCOLOR;
sprintf(pinfo->fullInfo, "PseudoColor RLE. (%ld bytes)", filesize);
for (i=0; i<256; i++) {
pinfo->r[i] = maps[0][i];
pinfo->g[i] = maps[1][i];
pinfo->b[i] = maps[2][i];
}
}
sprintf(pinfo->shrtInfo, "%dx%d RLE.",w, h);
}
else { /* true color */
pinfo->type = PIC24;
pinfo->colType = F_FULLCOLOR;
sprintf(pinfo->fullInfo, "TrueColor RLE. (%ld bytes)", filesize);
sprintf(pinfo->shrtInfo, "%dx%d RLE.", w, h);
}
return 1;
}
/*******************************************/
static void read_rle(fp, img, w, h, ncolors, ncmap)
FILE *fp;
byte *img;
int w, h, ncolors, ncmap;
{
int posx, posy, plane, bperpix, i, pixval, skipcalls;
int opcode, operand, done, c, c1;
byte *ip;
posx = posy = plane = done = skipcalls = 0;
if (ncolors == 1) bperpix = 1;
else bperpix = 3;
while (!done && (opcode=getc(fp)) != EOF) {
switch (opcode & 0x3f) {
case RSkipLinesOp:
if (opcode & LONG_OP) { getc(fp); operand = GETINT(fp); }
else operand = getc(fp);
posx = 0;
posy += operand;
skipcalls++;
if ((skipcalls & 0x7f)==0) WaitCursor();
break;
case RSetColorOp:
operand = getc(fp);
plane = operand;
posx = 0;
break;
case RSkipPixelsOp:
if (opcode & LONG_OP) { getc(fp); operand = GETINT(fp); }
else operand = getc(fp);
posx += operand;
break;
case RByteDataOp:
if (opcode & LONG_OP) { getc(fp); operand = GETINT(fp); }
else operand = getc(fp);
ip = img + ((h-posy-1) * w*bperpix) + posx*bperpix + plane;
operand++;
for (i=0; i<operand; i++, ip+=bperpix) {
c = getc(fp);
if (plane<ncolors && posy<h && (posx+i < w)) *ip = c;
}
if (operand & 1) getc(fp); /* word boundary */
posx += operand;
break;
case RRunDataOp:
if (opcode & LONG_OP) { getc(fp); operand = GETINT(fp); }
else operand = getc(fp);
pixval = getc(fp); getc(fp);
operand++;
ip = img + ((h-posy-1) * w*bperpix) + posx*bperpix + plane;
for (i=0; i<operand; i++, ip+=bperpix) {
if (plane<ncolors && posy<h && (posx+i < w)) *ip = pixval;
}
/* if (operand & 1) getc(fp); */ /* word boundary */
posx += operand;
break;
case REOFOp:
done = 1;
break;
}
}
}
/*******************************************/
static int rleError(fname,st)
const char *fname, *st;
{
SetISTR(ISTR_WARNING,"%s: %s", fname, st);
return 0;
}