-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patha_image.cpp
710 lines (592 loc) · 14.4 KB
/
a_image.cpp
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
/****************************************************************
xerxes engine
a_image.cpp
****************************************************************/
#include "xerxes.h"
/***************************** data *****************************/
int width = 0, depth = 0;
byte palconv[768] = { 0 };
/***************************** code *****************************/
int img_load_CheckPCX(void *header)
{
if( *((byte *)header)==10)
header=(byte *)header+1;
else return 0;
if( *((byte *)header)==5)
return 1;
else return 0;
}
int img_load_CheckBMP(void *header)
{
if(*((word *)header)==19778)
return 1;
else
return 0;
}
int img_load_CheckGIF(void *header)
{
if(strncmp((char *)header,"GIF",3))
return 0;
else
return 1;
}
/********************* GIF imaging routines *********************/
#define TRUE 1
#define FALSE 0
typedef unsigned char u8;
typedef unsigned short u16;
typedef unsigned long u32;
typedef signed char s8;
typedef signed short s16;
typedef signed long s32;
typedef struct
{
u8 bits;
u8 background;
u8 * palette;
u8 * image;
s16 wide, deep;
} gif_image_info;
typedef struct
{
char sig[7];
s16 screenwide, screendeep;
u8 hflags;
u8 background;
u8 aspect;
} gif_header;
typedef struct
{
s16 top, left;
s16 wide, deep;
u8 iflags;
} gif_image_descriptor;
int NO_CODE = -1,// TRUE = 1,
ERROR_EOF = 0, ERROR_BAD_CODE = 1,
ERROR_BAD_HEADER = 2, ERROR_BAD_STARTCODE = 3,
ERROR_BAD_FIRST_CODE = 4, ERROR_BAD_FILE = 5,
ERROR_NO_IMAGE = 6;
char* gif_error_messages[] =
{
"Unexpected end of file\n",
"Bad code\n",
"Bad gif header\n",
"Bad symbol size\n",
"Bad first code\n",
"Error opening file\n",
"This file doesn't contain an image\n"
};
u8* gif_read_palette(FILE* fp, s32 bytes)
{
s32 i = 0;
u8* block = 0L;
s32 components = (bytes / 3) * 3;
block = new u8[components];
for (i = 0; i < components; ++i)
block[i] = fgetc(fp);
return block;
}
// read a block of bytes into memory
s32 block_mem_read(FILE* fp, u8* buffer, s32 bytes)
{
s32 status = 0;
status = fread(buffer, 1, bytes, fp);
if (status != bytes) return EOF;
return TRUE;
}
// read a unsigned 16 bit value from file, low byte first; note that this
// is reverse endian-ness (ie. fwrite(&s,1,2,fp); writes high byte first).
s16 read_word_lbf(FILE* fp)
{
s32 a, b;
a = fgetc(fp);
b = fgetc(fp);
return (s16) ((b << 8) | a);
}
// read the GIF file header structure into a sequence
gif_header* get_gif_header(FILE* fp)
{
gif_header* h = 0L;
h = new gif_header;
fread(h->sig, 1, 6, fp);
h->sig[6] = 0;
if (strncmp(h->sig, "GIF", 3) != 0)
return NULL;
h->screenwide = read_word_lbf(fp); width=h->screenwide;
h->screendeep = read_word_lbf(fp); depth=h->screendeep;
h->hflags = fgetc(fp);
h->background = fgetc(fp);
h->aspect = fgetc(fp);
return h;
}
// gif file can contain more than one image,
// each image is preceeded by a header structure
gif_image_descriptor* get_image_descriptor(FILE* fp)
{
gif_image_descriptor* id = 0L;
id = new gif_image_descriptor;
id->left = read_word_lbf(fp);
id->top = read_word_lbf(fp);
id->wide = read_word_lbf(fp);
id->deep = read_word_lbf(fp);
id->iflags = fgetc(fp);
return id;
}
static u16 word_mask_table[] =
{
0x0000, 0x0001, 0x0003, 0x0007,
0x000F, 0x001F, 0x003F, 0x007F,
0x00FF, 0x01FF, 0x03FF, 0x07FF,
0x0FFF, 0x1FFF, 0x3FFF, 0x7FFF
};
static u8 inc_table[] = { 8,8,4,2,0 };
static u8 start_table[] = { 0,4,2,1,0 };
// enables me to use indices as per EUPHORiA (ie. converts to C's 0 base)
#define eui(i) ((i)-1)
// unpack an LZW compressed image
// returns a sequence containing screen display lines of the image
u8* unpack_image(FILE* fp, s32 start_code_size, u32 width, u32 depth, u32 flags)
{
u8* buffer;
u8* line_buffer;
u16 first_code_stack[4096];
u16 last_code_stack[4096];
u16 code_stack[4096];
s32 bits_left;
s32 clear_code;
s32 code_size;
s32 code_size2;
s32 next_code;
s32 this_code;
s32 old_token;
s32 current_code;
s32 old_code;
s32 block_size=0;
s32 line;
s32 a_byte;
s32 pass;
s32 u;
u8 b[256]; // read buffer; for block reads
u8* p; // current byte in read buffer
u8* q; // last byte in read buffer + 1
line_buffer = new u8[width];
buffer = new u8[width * depth];
a_byte = 0;
line = 0;
pass = 0;
bits_left = 8;
if (start_code_size < 2 || start_code_size > 8)
err("ERROR_BAD_STARTCODE"); // bad symbol size
p = b;
q = b;
clear_code = 1 << start_code_size; //pow(2, start_code_size);
next_code = clear_code + 2;
code_size = start_code_size + 1;
code_size2 = 1 << code_size; //pow(2, code_size);
old_code = NO_CODE;
old_token = NO_CODE;
while (1)
{
if (bits_left == 8)
{
++p;
if (p >= q)
{
block_size = fgetc(fp);
if (block_mem_read(fp, b, block_size) == EOF)
err("ERROR_EOF");
p = b;
q = b + block_size;
}
bits_left = 0;
}
this_code = *p;
current_code = code_size + bits_left;
if (current_code <= 8)
{
*p = *p >> code_size;
bits_left = current_code;
}
else
{
++p;
if (p >= q)
{
block_size = fgetc(fp);
if (block_mem_read(fp, b, block_size) == EOF)
err("ERROR_EOF");
p = b;
q = b + block_size;
}
this_code |= (*p << (8 - bits_left));
if (current_code <= 16)
{
bits_left = current_code - 8;
*p = *p >> bits_left;
}
else
{
if (++p >= q)
{
block_size = fgetc(fp);
if (block_mem_read(fp, b, block_size) == EOF)
err("ERROR_EOF");
p = b;
q = b + block_size;
}
this_code |= (*p << (16 - bits_left));
bits_left = current_code - 16;
*p = *p >> bits_left;
}
}
this_code &= word_mask_table[code_size];
current_code = this_code;
if (this_code == (clear_code+1) || block_size == 0)
break;
if (this_code > next_code)
err("ERROR_BAD_CODE");
if (this_code == clear_code)
{
next_code = clear_code + 2;
code_size = start_code_size + 1;
code_size2 = 1 << code_size; //pow(2, code_size);
old_code = NO_CODE;
old_token = NO_CODE;
}
else
{
u = 1;
if (this_code == next_code)
{
if (old_code == NO_CODE)
err("ERROR_BAD_FIRST_CODE");
first_code_stack[eui(u)] = (u16) old_token;
u++;
this_code = old_code;
}
while (this_code >= clear_code)
{
first_code_stack[eui(u)] = last_code_stack[eui(this_code)];
u++;
this_code = code_stack[eui(this_code)];
}
old_token = this_code;
while (1)
{
line_buffer[a_byte] = (u8) this_code;
a_byte++;
if (a_byte >= (signed) width)
{
// full image line so add it into screen image
memcpy(buffer + (line * width), line_buffer, width);
a_byte = 0;
if (flags & 0x40)
{
line += inc_table[pass];
if (line >= (signed) depth)
{
pass++;
line = start_table[pass];
}
}
else
{
line++;
}
}
// no more bytes on stack
if (u == 1) break;
u--;
this_code = first_code_stack[eui(u)];
}
if (next_code < 4096 && old_code != NO_CODE)
{
code_stack[eui(next_code)] = (u16) old_code;
last_code_stack[eui(next_code)] = (u16) old_token;
next_code++;
if (next_code >= code_size2 && code_size < 12)
{
code_size++;
code_size2 = 1 << code_size; //pow(2, code_size);
}
}
old_code = current_code;
}
}
// completed reading the image so return it
delete [] line_buffer;
return buffer;
}
// skip the extension blocks as we are only after the image
void skip_extension(FILE* fp)
{
s32 n;
char temp[256];
n = fgetc(fp); // throwaway extension function code
n = fgetc(fp); // get length of block
while (n > 0 && n != EOF)
{
// throwaway block
fread(temp, 1, n, fp);
n = fgetc(fp); // get length of next block
}
}
// unpack the GIF file
// returns ImageInfo sequence containing image and image data
gif_image_info* unpack_gif(char* filename)
{
VFILE *f;
FILE* fp;
s32 c, b;
gif_header* h = 0L;
gif_image_info* ii = 0L;
gif_image_descriptor* id = 0L;
u8* local_palette = 0L;
ii = new gif_image_info;
f = vopen(filename);
if (!f) err("unpack_gif(), could not open GIF file %s.",filename);
fp = f->fp;
if (!fp) err("Bad filename");
// file starts with the Logical Screen Descriptor structure
h = get_gif_header(fp);
// Size of Global Color Table
ii->bits = (h->hflags & 7) + 1;
ii->background = h->background;
// get Global colour palette if there is one
if (h->hflags & 0x80) // is flags bit 8 set?
{
c = 3 << ii->bits; // size of global colour map
ii->palette = gif_read_palette(fp, c);
memcpy(palconv, ii->palette, 768);
delete [] ii -> palette;
}
delete h;
c = fgetc(fp);
while (c == 0x2c || c == 0x21 || c == 0)
{
// image separator so unpack the image
if (c == 0x2c)
{
id = get_image_descriptor(fp); // get the Image Descriptor
// if there is a local Color Table then overwrite the global table
if (id->iflags & 0x80)
{
ii->bits = (id->iflags & 7) + 1;
b = 3 << ii->bits;
if (local_palette)
delete [] local_palette;
local_palette = gif_read_palette(fp, b);
memcpy(palconv, local_palette, 768);
delete [] local_palette;
}
c = fgetc(fp); // get the LZW Minimum Code Size
ii->image = unpack_image(fp, c, id->wide, id->deep, id->iflags);
vclose(f);
// error reading image
if (!ii->image)
err("error reading image data");
ii->wide = id->wide;
ii->deep = id->deep;
delete id;
// return imagedata
return ii;
}
// extension introducer
else if (c == 0x21)
{
skip_extension(fp); // throw the extension away
}
c = fgetc(fp);
}
// no image?
return NULL;
}
byte *LoadGIFBuf(char *fname)
{
gif_image_info *ii=0;
byte *t;
ii = unpack_gif(fname);
width = ii->wide;
depth = ii->deep;
t = (byte *) ii->image;
delete ii;
return t;
}
// ========================= PCX Imaging routines ============================
int TwentyFourBit;
// ================================= Code ====================================
void pcx_decode_scan(VFILE* vf, byte* start, short skip, int width)
{
int c, n, run;
n = 0;
while (n < width)
{
run = 1;
c = vgetc(vf) & 0xff;
if ((c & 0xc0) == 0xc0)
{
run = c & 0x3f;
c = vgetc(vf);
}
n += run;
do
{
*start = (byte) c;
start += skip;
run -= 1;
}
while (run);
}
}
static byte *LoadPCXBuf(char *fname)
{
#pragma pack(1)
typedef struct
{
char manufacturer; // always 10
char version;
char encoding;
char bpp; // bits per pixel
word xmin, ymin;
word xmax, ymax;
word hres, vres;
char palette[48];
char reserved;
char color_planes;
word bpl; // bytes per line
word palette_type;
char filler[58];
} header;
#pragma pack()
header h={0};
VFILE *vfp=0;
byte *buf=0, *dest=0;
int i=0;
vfp=vopen(fname);
if (!vfp)
err("LoadPCXBuf(), could not open PCX file %s.", fname);
vread(&h, 128, vfp);
// do some checking
if (10 != h.manufacturer)
err("%s is not a PCX file.", fname);
if (8 != h.bpp)
err("%s is not a valid 8-bit PCX. (reported %i bits)", fname, h.bpp);
TwentyFourBit = (3 == h.color_planes);
width=h.xmax-h.xmin+1;
depth=h.ymax-h.ymin+1;
buf = new byte[width * depth * (TwentyFourBit ? 3 : 1)];
// decode image into buf
if (!TwentyFourBit)
for (dest=buf,i=depth; i--; dest+=width)
pcx_decode_scan(vfp, dest, 1, h.bpl);
else
for (dest=buf,i=depth; i--; dest+=(width*3))
{
pcx_decode_scan(vfp, dest+0, 3, h.bpl);
pcx_decode_scan(vfp, dest+1, 3, h.bpl);
pcx_decode_scan(vfp, dest+2, 3, h.bpl);
}
// palette available?
if (12 == vgetc(vfp))
vread(palconv, 768, vfp);
vclose(vfp);
return buf;
}
// *********************
// * Imaging Interface *
// *********************
int imagetype;
void vstrlwr(char *c)
{
while (*c)
{
if ((char) *c >= 'A' && (char) *c <= 'Z')
{
*c = *c - 'A';
*c = *c + 'a';
}
c++;
}
}
void DetermineFileType(char *fname)
{
vstrlwr(fname);
if (!strcmp(fname+(strlen(fname)-3),"pcx")) imagetype=0;
if (!strcmp(fname+(strlen(fname)-3),"gif")) imagetype=1;
}
image *xLoadImage(char *name)
{
byte *blah;
image *b;
width=depth=0;
imagetype = 255;
DetermineFileType(name);
if (imagetype == 255) err("%s is not a recognized image type.",name);
switch (imagetype)
{
case 0: blah = LoadPCXBuf(name);
if (!TwentyFourBit)
{
b = ImageFrom8bpp(blah, width, depth, palconv);
break;
}
else
{
b = ImageFrom24bpp(blah, width, depth);
break;
}
case 1: blah = LoadGIFBuf(name); b = ImageFrom8bpp(blah, width, depth, palconv); break;
default: err("xLoadImage: Internal error.");
}
delete [] blah;
return b;
}
image *xLoadImage0(char *name)
{
byte *blah;
image *b;
width=depth=0;
imagetype = 255;
DetermineFileType(name);
if (imagetype == 255) err("%s is not a recognized image type.",name);
switch (imagetype)
{
case 0: blah = LoadPCXBuf(name);
if (!TwentyFourBit)
{
palconv[0]=255; palconv[1]=0; palconv[2]=255;
b = ImageFrom8bpp(blah, width, depth, palconv);
break;
}
else
{
b = ImageFrom24bpp(blah, width, depth);
break;
}
case 1: blah = LoadGIFBuf(name); palconv[0]=255; palconv[1]=0; palconv[2]=255; b = ImageFrom8bpp(blah, width, depth, palconv); break;
default: err("xLoadImage0: Internal error.");
}
delete [] blah;
return b;
}
byte *xLoadImage8bpp(char *name)
{
byte *blah;
width=depth=0;
imagetype = 255;
DetermineFileType(name);
if (imagetype == 255) err("%s is not a recognized image type.",name);
switch (imagetype)
{
case 0: blah = LoadPCXBuf(name);
if (TwentyFourBit)
err("Cannot load 24bpp PCX in 8bit mode.");
break;
case 1: blah = LoadGIFBuf(name); break;
default: err("xLoadImage8bpp: Internal error.");
}
for (int i=0; i<256; i++)
pal[i] = MakeColor(palconv[i * 3], palconv[(i * 3) + 1], palconv[(i * 3) + 2]);
memcpy(base_pal, pal, sizeof pal);
return blah;
}