rnd-20020803-1-src
[rocksndiamonds.git] / src / libgame / pcx.c
1 /***********************************************************
2 * Artsoft Retro-Game Library                               *
3 *----------------------------------------------------------*
4 * (c) 1994-2002 Artsoft Entertainment                      *
5 *               Holger Schemel                             *
6 *               Detmolder Strasse 189                      *
7 *               33604 Bielefeld                            *
8 *               Germany                                    *
9 *               e-mail: info@artsoft.org                   *
10 *----------------------------------------------------------*
11 * pcx.c                                                    *
12 ***********************************************************/
13
14 #ifndef TARGET_SDL
15
16 #include <stdio.h>
17
18 #include "pcx.h"
19 #include "misc.h"
20
21
22 #define PCX_DEBUG               FALSE
23
24 #define PCX_MAGIC               0x0a    /* first byte in a PCX image file    */
25 #define PCX_SUPPORTED_VERSION   5       /* last acceptable version number    */
26 #define PCX_ENCODING            1       /* PCX encoding method               */
27 #define PCX_256COLORS_MAGIC     0x0c    /* first byte of a PCX 256 color map */
28 #define PCX_MAXCOLORS           256     /* maximum number of colors          */
29
30 #define PCX_HEADER_SIZE         128
31 #define PCX_COLORMAP_SIZE       (3 * PCX_MAXCOLORS)
32
33 struct PCX_Header
34 {
35   unsigned char signature;      /* PCX file identifier                 */
36   unsigned char version;        /* version compatibility level         */
37   unsigned char encoding;       /* encoding method                     */
38   unsigned char bits_per_pixel; /* bits per pixel (not depth!)         */
39   unsigned short xmin;          /* X position of left edge             */
40   unsigned short ymin;          /* Y position of top edge              */
41   unsigned short xmax;          /* X position of right edge            */
42   unsigned short ymax;          /* Y position of bottom edge           */
43   unsigned short hres;          /* X screen resolution of source image */
44   unsigned short vres;          /* Y screen resolution of source image */
45   unsigned char palette[16][3]; /* PCX color map                       */
46   unsigned char reserved;       /* should be 0, 1 if std res fax       */
47   unsigned char color_planes;   /* "color planes" in image             */
48   unsigned short bytes_per_line;/* byte delta between scanlines        */
49   unsigned short palette_type;  /* 0 = undef, 1 = color, 2 = grayscale */
50   unsigned char filler[58];     /* fill to struct size of 128          */
51 };
52
53 /* global PCX error value */
54 int errno_pcx = PCX_Success;
55
56 #if 0
57 static byte *PCX_ReadBitmap(Image *image, byte *buffer_ptr, byte *buffer_last)
58 {
59   /* Run Length Encoding: If the two high bits are set,
60    * then the low 6 bits contain a repeat count, and the byte to
61    * repeat is the next byte in the file.  If the two high bits are
62    * not set, then this is the byte to write.
63    */
64
65   unsigned int bytes_per_pixel = (image->depth + 7) / 8;
66   register byte *bitmap_ptr, *bitmap_last;
67   register byte value, count;
68
69   bitmap_ptr = image->data;
70   bitmap_last = bitmap_ptr + (image->width * image->height * bytes_per_pixel);
71
72   while (bitmap_ptr < bitmap_last && buffer_ptr < buffer_last)
73   {
74     value = *buffer_ptr++;
75
76     if ((value & 0xc0) == 0xc0)         /* this is a repeat count byte */
77     {
78       count = value & 0x3f;             /* extract repeat count from byte */
79       value = *buffer_ptr++;            /* next byte is value to repeat */
80
81       for (; count && bitmap_ptr < bitmap_last; count--)
82         *bitmap_ptr++ = value;
83
84       if (count)                        /* repeat count spans end of bitmap */
85         return NULL;
86     }
87     else
88       *bitmap_ptr++ = value;
89
90     image->rgb.color_used[value] = TRUE;
91   }
92
93   /* check if end of buffer was reached before end of bitmap */
94   if (bitmap_ptr < bitmap_last)
95     return NULL;
96
97   /* return current buffer position for next decoding function */
98   return buffer_ptr;
99 }
100 #endif
101
102 static boolean PCX_ReadBitmap(FILE *file, struct PCX_Header *pcx, Image *image)
103 {
104   int width = image->width;
105   int height = image->height;
106   int pcx_depth = pcx->bits_per_pixel * pcx->color_planes;
107   int bytes_per_row = pcx->color_planes * pcx->bytes_per_line;
108   byte *row_buffer = checked_malloc(bytes_per_row);
109   byte *bitmap_ptr = image->data;
110   int y;
111
112   for (y = 0; y < height; y++)
113   {
114     /* decode a scan line into a temporary buffer first */
115     byte *dst_ptr = (pcx_depth == 8) ? bitmap_ptr : row_buffer;
116     byte value = 0, count = 0;
117     int value_int;
118     int i;
119
120     for (i = 0; i < bytes_per_row; i++)
121     {
122       if (count == 0)
123       {
124         if ((value_int = fgetc(file)) == EOF)
125           return FALSE;
126         value = (byte)value_int;
127
128         if ((value & 0xc0) == 0xc0)     /* this is a repeat count byte */
129         {
130           count = value & 0x3f;         /* extract repeat count from byte */
131           if ((value_int = fgetc(file)) == EOF)
132             return FALSE;
133           value = (byte)value_int;
134         }
135         else
136           count = 1;
137       }
138
139       dst_ptr[i] = value;
140       count--;
141
142       if (pcx_depth == 8)
143         image->rgb.color_used[value] = TRUE;
144     }
145
146     if (pcx_depth <= 4)                 /* expand planes to 1 byte/pixel */
147     {
148       byte *src_ptr = row_buffer;
149       int plane;
150
151       for (plane = 0; plane < pcx->color_planes; plane++)
152       {
153         int i, j, x = 0;
154
155         for(i = 0; i < pcx->bytes_per_line; i++)
156         {
157           byte value = *src_ptr++;
158
159           for(j = 7; j >= 0; j--)
160           {
161             byte bit = (value >> j) & 1;
162
163             bitmap_ptr[x++] |= bit << plane;
164           }
165         }
166       }
167     }
168     else if (pcx_depth == 24)           /* de-interlace planes */
169     {
170       byte *src_ptr = row_buffer;
171       int plane;
172
173       for(plane = 0; plane < pcx->color_planes; plane++)
174       {
175         int x;
176
177         dst_ptr = bitmap_ptr + plane;
178         for(x = 0; x < width; x++)
179         {
180           *dst_ptr = *src_ptr++;
181           dst_ptr += pcx->color_planes;
182         }
183       }
184     }
185
186     bitmap_ptr += image->bytes_per_row;
187   }
188
189   return TRUE;
190 }
191
192 #if 0
193 static byte *PCX_ReadColormap(Image *image,byte *buffer_ptr, byte *buffer_last)
194 {
195   int i, magic;
196
197   /* read colormap magic byte */
198   magic = *buffer_ptr++;
199
200   /* check magic colormap header byte */
201   if (magic != PCX_256COLORS_MAGIC)
202     return NULL;
203
204   /* check if enough bytes left for a complete colormap */
205   if (buffer_ptr + PCX_COLORMAP_SIZE > buffer_last)
206     return NULL;
207
208   /* read 256 colors from PCX colormap */
209   for (i=0; i<PCX_MAXCOLORS; i++)
210   {
211     image->rgb.red[i]   = *buffer_ptr++ << 8;
212     image->rgb.green[i] = *buffer_ptr++ << 8;
213     image->rgb.blue[i]  = *buffer_ptr++ << 8;
214   }
215
216   /* return current buffer position for next decoding function */
217   return buffer_ptr;
218 }
219 #endif
220
221 static boolean PCX_ReadColormap(FILE *file,struct PCX_Header *pcx,Image *image)
222 {
223   int pcx_depth = pcx->bits_per_pixel * pcx->color_planes;
224   int num_colors = (1 << pcx_depth);
225   int i;
226
227   if (image->depth != 8)
228     return TRUE;
229
230   if (pcx_depth == 8)
231   {
232     byte value;
233     int value_int;
234
235     /* look for a 256-colour palette */
236     do
237     {
238       if ((value_int = fgetc(file)) == EOF)
239         return FALSE;
240       value = (byte)value_int;
241     }
242     while (value != PCX_256COLORS_MAGIC);
243
244     /* read 256 colors from PCX colormap */
245     for(i = 0; i < PCX_MAXCOLORS; i++)
246     {
247       image->rgb.red[i]   = (byte)fgetc(file) << 8;
248       image->rgb.green[i] = (byte)fgetc(file) << 8;
249       image->rgb.blue[i]  = (byte)fgetc(file) << 8;
250     }
251   }
252   else
253   {
254     for(i = 0; i < num_colors; i++)
255     {
256       image->rgb.red[i]   = pcx->palette[i][0] << 8;
257       image->rgb.green[i] = pcx->palette[i][1] << 8;
258       image->rgb.blue[i]  = pcx->palette[i][2] << 8;
259     }
260   }
261
262   return TRUE;
263 }
264
265 Image *Read_PCX_to_Image(char *filename)
266 {
267   FILE *file;
268   byte header_buffer[PCX_HEADER_SIZE];
269   struct PCX_Header pcx;
270   Image *image;
271   int width, height, depth, pcx_depth;
272   int i;
273
274   errno_pcx = PCX_Success;
275
276   if (!(file = fopen(filename, MODE_READ)))
277   {
278     errno_pcx = PCX_OpenFailed;
279     return NULL;
280   }
281
282   if (fread(header_buffer, 1, PCX_HEADER_SIZE, file) != PCX_HEADER_SIZE)
283   {
284     fclose(file);
285
286     errno_pcx = PCX_ReadFailed;
287     return NULL;
288   }
289
290   pcx.signature      = header_buffer[0];
291   pcx.version        = header_buffer[1];
292   pcx.encoding       = header_buffer[2];
293   pcx.bits_per_pixel = header_buffer[3];
294   pcx.xmin           = (header_buffer[5]  << 8) | header_buffer[4];
295   pcx.ymin           = (header_buffer[7]  << 8) | header_buffer[6];
296   pcx.xmax           = (header_buffer[9]  << 8) | header_buffer[8];
297   pcx.ymax           = (header_buffer[11] << 8) | header_buffer[10];
298   pcx.color_planes   = header_buffer[65];
299   pcx.bytes_per_line = (header_buffer[67] << 8) | header_buffer[66];
300   pcx.palette_type   = (header_buffer[69] << 8) | header_buffer[68];
301
302   for (i = 0; i < 48; i++)
303     pcx.palette[i / 3][i % 3] = header_buffer[16 + i];
304
305   width  = pcx.xmax - pcx.xmin + 1;
306   height = pcx.ymax - pcx.ymin + 1;
307   pcx_depth = pcx.bits_per_pixel * pcx.color_planes;
308   depth = ((pcx_depth + 7) / 8) * 8;
309
310   if (pcx.signature != PCX_MAGIC ||
311       pcx.version != PCX_SUPPORTED_VERSION ||
312       pcx.encoding != PCX_ENCODING ||
313       width < 0 || height < 0)
314   {
315     fclose(file);
316
317     errno_pcx = PCX_FileInvalid;
318     return NULL;
319   }
320
321 #if PCX_DEBUG
322   if (options.verbose)
323   {
324     printf("%s is a %dx%d PC Paintbrush image\n", filename, width, height);
325     printf("depth: %d\n", depth);
326     printf("bits_per_pixel: %d\n", pcx.bits_per_pixel);
327     printf("color_planes: %d\n", pcx.color_planes);
328     printf("bytes_per_line: %d\n", pcx.bytes_per_line);
329     printf("palette type: %s\n",
330            (pcx.palette_type == 1 ? "color" :
331             pcx.palette_type == 2 ? "grayscale" : "undefined"));
332   }
333 #endif
334
335   /* allocate new image structure */
336   image = newImage(width, height, depth);
337
338   /* read compressed bitmap data */
339   if (!PCX_ReadBitmap(file, &pcx, image))
340   {
341     fclose(file);
342     freeImage(image);
343
344     errno_pcx = PCX_FileInvalid;
345     return NULL;
346   }
347
348   /* read colormap data */
349   if (!PCX_ReadColormap(file, &pcx, image))
350   {
351     fclose(file);
352     freeImage(image);
353
354     errno_pcx = PCX_ColorFailed;
355     return NULL;
356   }
357
358   fclose(file);
359
360   if (pcx_depth == 8)
361   {
362     /* determine number of used colormap entries for 8-bit PCX images */
363     image->rgb.used = 0;
364     for (i=0; i<PCX_MAXCOLORS; i++)
365       if (image->rgb.color_used[i])
366         image->rgb.used++;
367   }
368
369 #if PCX_DEBUG
370   if (options.verbose)
371     printf("Read_PCX_to_Image: %d colors found\n", image->rgb.used);
372 #endif
373
374   return image;
375 }
376
377 #endif /* !TARGET_SDL */