summaryrefslogtreecommitdiffhomepage
path: root/nGenEx/PCX.CPP
blob: 42b34b2b8b0277d94bfa2c8bb1c14f87c66f5a9a (plain)
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
/*  Project nGenEx
    Destroyer Studios LLC
    Copyright © 1997-2004. All Rights Reserved.

    SUBSYSTEM:    nGenEx.lib
    FILE:         PCX.cpp
    AUTHOR:       John DiCamillo


    OVERVIEW
    ========
    PCX image file loader
*/


#include "MemDebug.h"
#include "PCX.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// +--------------------------------------------------------------------+

#define MAX_SIZE (4*1024*1024)

enum { BYTEMODE, RUNMODE };

// +--------------------------------------------------------------------+

PcxImage::PcxImage()
   : width(0), height(0), bitmap(0), himap(0), imagebytes(0)
{ }

PcxImage::PcxImage(short w, short h, unsigned char* bits, unsigned char* colors)
   : bitmap(0), himap(0)
{
   hdr.manufacturer = 10;   // Always set to 10
   hdr.version = 5;         // Always 5 for 256-color files
   hdr.encoding = 1;        // Always set to 1
   hdr.bits_per_pixel = 8;  // Should be 8 for 256-color files
   hdr.xmin = 0;
   hdr.xmax = w-1;
   hdr.ymin = 0;
   hdr.ymax = h-1;
   hdr.hres = 0x48;
   hdr.vres = 0x48;

   hdr.palette16[ 0] = (unsigned char) 0x00;
   hdr.palette16[ 1] = (unsigned char) 0x00;
   hdr.palette16[ 2] = (unsigned char) 0x00;
   hdr.palette16[ 3] = (unsigned char) 0x80;
   hdr.palette16[ 4] = (unsigned char) 0x00;
   hdr.palette16[ 5] = (unsigned char) 0x00;
   hdr.palette16[ 6] = (unsigned char) 0x00;
   hdr.palette16[ 7] = (unsigned char) 0x80;
   hdr.palette16[ 8] = (unsigned char) 0x00;
   hdr.palette16[ 9] = (unsigned char) 0x80;
   hdr.palette16[10] = (unsigned char) 0x80;
   hdr.palette16[11] = (unsigned char) 0x00;
   hdr.palette16[12] = (unsigned char) 0x00;
   hdr.palette16[13] = (unsigned char) 0x00;
   hdr.palette16[14] = (unsigned char) 0x80;
   hdr.palette16[15] = (unsigned char) 0x80;
   
   hdr.palette16[16] = (unsigned char) 0x00;
   hdr.palette16[17] = (unsigned char) 0x80;
   hdr.palette16[18] = (unsigned char) 0x00;
   hdr.palette16[19] = (unsigned char) 0x80;
   hdr.palette16[20] = (unsigned char) 0x80;
   hdr.palette16[21] = (unsigned char) 0xC0;
   hdr.palette16[22] = (unsigned char) 0xC0;
   hdr.palette16[23] = (unsigned char) 0xC0;
   hdr.palette16[24] = (unsigned char) 0xC0;
   hdr.palette16[25] = (unsigned char) 0xDC;
   hdr.palette16[26] = (unsigned char) 0xC0;
   hdr.palette16[27] = (unsigned char) 0xA6;
   hdr.palette16[28] = (unsigned char) 0xCA;
   hdr.palette16[29] = (unsigned char) 0xF0;
   hdr.palette16[30] = (unsigned char) 0x33;
   hdr.palette16[31] = (unsigned char) 0x2B;

   hdr.palette16[32] = (unsigned char) 0x1F;
   hdr.palette16[33] = (unsigned char) 0x2B;
   hdr.palette16[34] = (unsigned char) 0x23;
   hdr.palette16[35] = (unsigned char) 0x1B;
   hdr.palette16[36] = (unsigned char) 0x5F;
   hdr.palette16[37] = (unsigned char) 0x5F;
   hdr.palette16[38] = (unsigned char) 0x5F;
   hdr.palette16[39] = (unsigned char) 0x2F;
   hdr.palette16[40] = (unsigned char) 0x2F;
   hdr.palette16[41] = (unsigned char) 0x2F;
   hdr.palette16[42] = (unsigned char) 0x27;
   hdr.palette16[43] = (unsigned char) 0x27;
   hdr.palette16[44] = (unsigned char) 0x27;
   hdr.palette16[45] = (unsigned char) 0x1F;
   hdr.palette16[46] = (unsigned char) 0x1F;
   hdr.palette16[47] = (unsigned char) 0x1F;

   hdr.reserved = 0;        // Reserved for future use
   hdr.color_planes = 1;    // Color planes
   hdr.bytes_per_line = w;  // Number of bytes in 1 line of pixels
   hdr.palette_type = 1;    // Should be 1 for color palette
   
   for (unsigned int i = 0; i < 58; i++)
      hdr.filler[i] = 0;

   width      = w;
   height     = h;
   imagebytes = width * height;

   bitmap = new(__FILE__,__LINE__) unsigned char [imagebytes];

   if (bitmap) {
      for (i = 0; i < imagebytes; i++)
         bitmap[i] = bits[i];

      unsigned char* p = pal;
      for (i = 0; i < 256; i++) {   
         *p++ = *colors++;
         *p++ = *colors++;
         *p++ = *colors++;
         colors++;
      }
   }
}

PcxImage::PcxImage(short w, short h, unsigned long* hibits)
   : bitmap(0), himap(0)
{
   hdr.manufacturer = 10;   // Always set to 10
   hdr.version = 5;         // Always 5 for true color files
   hdr.encoding = 1;        // Always set to 1
   hdr.bits_per_pixel = 8;  // Should be 8 for true color files
   hdr.xmin = 0;
   hdr.xmax = w-1;
   hdr.ymin = 0;
   hdr.ymax = h-1;
   hdr.hres = 0x48;
   hdr.vres = 0x48;

   hdr.palette16[ 0] = (unsigned char) 0x00;
   hdr.palette16[ 1] = (unsigned char) 0x00;
   hdr.palette16[ 2] = (unsigned char) 0x00;
   hdr.palette16[ 3] = (unsigned char) 0x80;
   hdr.palette16[ 4] = (unsigned char) 0x00;
   hdr.palette16[ 5] = (unsigned char) 0x00;
   hdr.palette16[ 6] = (unsigned char) 0x00;
   hdr.palette16[ 7] = (unsigned char) 0x80;
   hdr.palette16[ 8] = (unsigned char) 0x00;
   hdr.palette16[ 9] = (unsigned char) 0x80;
   hdr.palette16[10] = (unsigned char) 0x80;
   hdr.palette16[11] = (unsigned char) 0x00;
   hdr.palette16[12] = (unsigned char) 0x00;
   hdr.palette16[13] = (unsigned char) 0x00;
   hdr.palette16[14] = (unsigned char) 0x80;
   hdr.palette16[15] = (unsigned char) 0x80;

   hdr.palette16[16] = (unsigned char) 0x00;
   hdr.palette16[17] = (unsigned char) 0x80;
   hdr.palette16[18] = (unsigned char) 0x00;
   hdr.palette16[19] = (unsigned char) 0x80;
   hdr.palette16[20] = (unsigned char) 0x80;
   hdr.palette16[21] = (unsigned char) 0xC0;
   hdr.palette16[22] = (unsigned char) 0xC0;
   hdr.palette16[23] = (unsigned char) 0xC0;
   hdr.palette16[24] = (unsigned char) 0xC0;
   hdr.palette16[25] = (unsigned char) 0xDC;
   hdr.palette16[26] = (unsigned char) 0xC0;
   hdr.palette16[27] = (unsigned char) 0xA6;
   hdr.palette16[28] = (unsigned char) 0xCA;
   hdr.palette16[29] = (unsigned char) 0xF0;
   hdr.palette16[30] = (unsigned char) 0x33;
   hdr.palette16[31] = (unsigned char) 0x2B;

   hdr.palette16[32] = (unsigned char) 0x1F;
   hdr.palette16[33] = (unsigned char) 0x2B;
   hdr.palette16[34] = (unsigned char) 0x23;
   hdr.palette16[35] = (unsigned char) 0x1B;
   hdr.palette16[36] = (unsigned char) 0x5F;
   hdr.palette16[37] = (unsigned char) 0x5F;
   hdr.palette16[38] = (unsigned char) 0x5F;
   hdr.palette16[39] = (unsigned char) 0x2F;
   hdr.palette16[40] = (unsigned char) 0x2F;
   hdr.palette16[41] = (unsigned char) 0x2F;
   hdr.palette16[42] = (unsigned char) 0x27;
   hdr.palette16[43] = (unsigned char) 0x27;
   hdr.palette16[44] = (unsigned char) 0x27;
   hdr.palette16[45] = (unsigned char) 0x1F;
   hdr.palette16[46] = (unsigned char) 0x1F;
   hdr.palette16[47] = (unsigned char) 0x1F;

   hdr.reserved = 0;        // Reserved for future use
   hdr.color_planes = 3;    // Color planes
   hdr.bytes_per_line = w;  // Number of bytes in 1 line of pixels
   hdr.palette_type = 1;    // Should be 1 for color palette

   for (unsigned int i = 0; i < 58; i++)
      hdr.filler[i] = 0;

   width      = w;
   height     = h;
   imagebytes = width * height;

   himap = new(__FILE__,__LINE__) unsigned long [imagebytes];

   if (himap) {
      for (i = 0; i < imagebytes; i++)
         himap[i] = hibits[i];
   }
}

PcxImage::~PcxImage()
{
   delete [] bitmap;
   delete [] himap;
}

// +--------------------------------------------------------------------+

int PcxImage::Load(char *filename)
{
   unsigned long i;
   short  mode=BYTEMODE, bytecount;
   unsigned char abyte, *p;
   FILE *f;

   f = fopen(filename,"rb");
   if (f == NULL)
      return PCX_NOFILE;

   fread(&hdr, sizeof(PcxHeader), 1, f);
   
   // read 256 color PCX file
   if (hdr.color_planes == 1) {
      width      = 1 + hdr.xmax - hdr.xmin;
      height     = 1 + hdr.ymax - hdr.ymin;
      imagebytes = width * height;

      if (imagebytes > MAX_SIZE)
         return PCX_TOOBIG;

      // get palette from pcx file
      fseek(f,-768L,SEEK_END);
      fread(pal,768,1,f);

      // now go back and read the pixel data:
      fseek(f,sizeof(PcxHeader),SEEK_SET);

      delete [] himap;  himap = 0;
      delete [] bitmap; bitmap = 0;

      himap = new(__FILE__,__LINE__) unsigned long [imagebytes];
      if (himap == NULL)
         return PCX_NOMEM;

      // force alpha channel to 255
      memset(himap, 0xff, imagebytes*4);

      unsigned long* pix = himap;
      for (i=0; i<imagebytes; i++) {
         if (mode == BYTEMODE) {
            abyte = fgetc(f);

            if (abyte > 0xbf) {
               bytecount = abyte & 0x3f;
               abyte = fgetc(f);
               if (--bytecount > 0)
                  mode = RUNMODE;
            }
         }
         else if (--bytecount == 0) {
            mode = BYTEMODE;
         }

         *pix++ = 0xff000000 | (pal[3*abyte] << 16) | (pal[3*abyte+1] << 8) | (pal[3*abyte+2]);
      }
   }
   
   // read 24-bit (true COLOR) PCX file
   else {
      width      = 1 + hdr.xmax - hdr.xmin;
      height     = 1 + hdr.ymax - hdr.ymin;
      imagebytes = width * height;

      if (imagebytes > MAX_SIZE)
         return PCX_TOOBIG;

      delete [] himap;  himap = 0;
      delete [] bitmap; bitmap = 0;

      himap = new(__FILE__,__LINE__) unsigned long [imagebytes];
      if (himap == NULL)
         return PCX_NOMEM;

      // force alpha channel to 255
      memset(himap, 0xff, imagebytes*4);

      for (int row = 0; row < height; row++) {
         // RED, GREEN, BLUE
         for (int plane = 2; plane >= 0; plane--) {
            p = ((unsigned char*) himap) + width*row*4 + plane;
            for (int col = 0; col < width; col++) {
               if (mode == BYTEMODE) {
                  abyte = fgetc(f);

                  if (abyte > 0xbf) {
                     bytecount = abyte & 0x3f;
                     abyte = fgetc(f);
                     if (--bytecount > 0)
                        mode = RUNMODE;
                  }
               }
               else if (--bytecount == 0) {
                  mode = BYTEMODE;
               }

               *p = abyte;
               p += 4;
            }
         }
      }
   }

   fclose(f);
   return PCX_OK;
}

// +--------------------------------------------------------------------+

int PcxImage::LoadBuffer(unsigned char* buf, int len)
{
   unsigned long i;
   short  mode=BYTEMODE, bytecount;
   unsigned char abyte, *p;
   unsigned char* fp;

   if (buf == NULL)
      return PCX_NOFILE;

   fp = buf;
   memcpy(&hdr, buf, sizeof(PcxHeader));
   fp += sizeof(PcxHeader);
   
   // read 256 color PCX file
   if (hdr.color_planes == 1) {
      width      = 1 + hdr.xmax - hdr.xmin;
      height     = 1 + hdr.ymax - hdr.ymin;
      imagebytes = width * height;

      if (imagebytes > MAX_SIZE)
         return PCX_TOOBIG;

      // get palette from end of pcx file
      memcpy(pal,buf+len-768,768);

      delete [] himap;  himap = 0;
      delete [] bitmap; bitmap = 0;

      himap = new(__FILE__,__LINE__) unsigned long [imagebytes];
      if (himap == NULL)
         return PCX_NOMEM;

      memset(himap, 0, imagebytes*4);

      unsigned long* pix = himap;
      for (i=0; i<imagebytes; i++) {
         if (mode == BYTEMODE) {
            abyte = *fp++;

            if (abyte > 0xbf) {
               bytecount = abyte & 0x3f;
               abyte = *fp++;
               if (--bytecount > 0)
                  mode = RUNMODE;
            }
         }
         else if (--bytecount == 0) {
            mode = BYTEMODE;
         }

         *pix++ = 0xff000000 | (pal[3*abyte] << 16) | (pal[3*abyte+1] << 8) | (pal[3*abyte+2]);
      }
   }

   // read 24-bit (true COLOR) PCX file
   else {
      width      = 1 + hdr.xmax - hdr.xmin;
      height     = 1 + hdr.ymax - hdr.ymin;
      imagebytes = width * height;

      if (imagebytes > MAX_SIZE)
         return PCX_TOOBIG;

      delete [] himap;  himap = 0;
      delete [] bitmap; bitmap = 0;

      himap = new(__FILE__,__LINE__) unsigned long [imagebytes];
      if (himap == NULL)
         return PCX_NOMEM;

      memset(himap, 0, imagebytes*4);

      for (int row = 0; row < height; row++) {
         // RED, GREEN, BLUE
         for (int plane = 2; plane >= 0; plane--) {
            p = ((unsigned char*) himap) + width*row*4 + plane;
            for (int col = 0; col < width; col++) {
               if (mode == BYTEMODE) {
                  abyte = *fp++;

                  if (abyte > 0xbf) {
                     bytecount = abyte & 0x3f;
                     abyte = *fp++;
                     if (--bytecount > 0)
                        mode = RUNMODE;
                  }
               }
               else if (--bytecount == 0) {
                  mode = BYTEMODE;
               }

               *p = abyte;
               p += 4;
            }
         }
      }
   }

   return PCX_OK;
}

// +--------------------------------------------------------------------+

int PcxImage::Save(char *filename)
{
   short  mode=BYTEMODE;
   FILE *f;

   f = fopen(filename,"wb");
   if (f == NULL)
      return PCX_NOFILE;

   fwrite(&hdr, sizeof(PcxHeader), 1, f);

   if (hdr.color_planes == 1) {
      unsigned char *p    = bitmap;
      unsigned long total = imagebytes;
      unsigned long row   = 0;
      unsigned char palette_marker = 12;

      while (total) {
         unsigned char* start = p;
         unsigned char  count = 0;

         while (*start == *p && count < 0x3f && row < width) {
            p++;
            count++;
            row++;
         }

         if (count > 1 || *start > 0xbf) {
            unsigned char b[2];
            b[0] = 0xc0 | count;
            b[1] = *start;
            fwrite(b, 2, 1, f);
         }
         else {
            fwrite(start, 1, 1, f);
         }

         total -= count;
         
         if (row == width)
            row = 0;
      }

      fwrite(&palette_marker,1,1,f);
      fwrite(pal,768,1,f);
   }
   
   // write 24-bit (TRUE COLOR) PCX file
   else {
      for (int row = 0; row < height; row++) {
         for (int plane = 2; plane >= 0; plane--) {
            unsigned long  col   = 0;
            unsigned char* p     = ((unsigned char*) himap) + width*row*4 + plane;
            
            while (col < width) {
               unsigned char* start = p;
               unsigned char  count = 0;

               while (*start == *p && count < 0x3f && col < width) {
                  p += 4;
                  count++;
                  col++;
               }
               
               if (count > 1 || *start > 0xbf) {
                  unsigned char b[2];
                  b[0] = 0xc0 | count;
                  b[1] = *start;
                  fwrite(b, 2, 1, f);
               }
               else {
                  fwrite(start, 1, 1, f);
               }
            }
         }
      }
   }

   fclose(f);
   return PCX_OK;                // return success
}