rnd-20000718-1-src
[rocksndiamonds.git] / src / pcx.c
1 /***********************************************************
2 *  Rocks'n'Diamonds -- McDuffin Strikes Back!              *
3 *----------------------------------------------------------*
4 *  (c) 1995-98 Artsoft Entertainment                       *
5 *              Holger Schemel                              *
6 *              Oststrasse 11a                              *
7 *              33604 Bielefeld                             *
8 *              phone: ++49 +521 290471                     *
9 *              email: aeglos@valinor.owl.de                *
10 *----------------------------------------------------------*
11 *  pcx.c                                                   *
12 ***********************************************************/
13
14 #include "pcx.h"
15 #include "image.h"
16 #include "misc.h"
17
18 #define PCX_DEBUG               FALSE
19
20 #define PCX_MAGIC               0x0a    /* first byte in a PCX image file    */
21 #define PCX_LAST_VERSION        5       /* last acceptable version number    */
22 #define PCX_ENCODING            1       /* PCX encoding method               */
23 #define PCX_256COLORS_MAGIC     0x0c    /* first byte of a PCX 256 color map */
24 #define PCX_MAXDEPTH            8       /* supports up to 8 bits per pixel   */
25 #define PCX_MAXCOLORS           256     /* maximum number of colors          */
26
27 #define PCX_HEADER_SIZE         128
28 #define PCX_COLORMAP_SIZE       (3 * PCX_MAXCOLORS)
29
30 struct PCX_Header
31 {
32   unsigned char signature;      /* PCX file identifier                 */
33   unsigned char version;        /* version compatibility level         */
34   unsigned char encoding;       /* encoding method                     */
35   unsigned char bits_per_pixel; /* bits per pixel, or depth            */
36   unsigned short xmin;          /* X position of left edge             */
37   unsigned short ymin;          /* Y position of top edge              */
38   unsigned short xmax;          /* X position of right edge            */
39   unsigned short ymax;          /* Y position of bottom edge           */
40   unsigned short hres;          /* X screen resolution of source image */
41   unsigned short vres;          /* Y screen resolution of source image */
42   unsigned char palette[16][3]; /* PCX color map                       */
43   unsigned char reserved;       /* should be 0, 1 if std res fax       */
44   unsigned char color_planes;   /* bit planes in image                 */
45   unsigned short bytes_per_line;/* byte delta between scanlines        */
46   unsigned short palette_type;  /* 0 = undef, 1 = color, 2 = grayscale */
47   unsigned char filler[58];     /* fill to struct size of 128          */
48 };
49
50 /* global PCX error value */
51 int errno_pcx = PCX_Success;
52
53 static byte *PCX_ReadBitmap(Image *image, byte *buffer_ptr, byte *buffer_last)
54 {
55   /* Run Length Encoding: If the two high bits are set,
56    * then the low 6 bits contain a repeat count, and the byte to
57    * repeat is the next byte in the file.  If the two high bits are
58    * not set, then this is the byte to write.
59    */
60
61   unsigned int bytes_per_pixel = (image->depth + 7) / 8;
62   register byte *bitmap_ptr, *bitmap_last;
63   register byte value, count;
64
65   bitmap_ptr = image->data;
66   bitmap_last = bitmap_ptr + (image->width * image->height * bytes_per_pixel);
67
68   while (bitmap_ptr < bitmap_last && buffer_ptr < buffer_last)
69   {
70     value = *buffer_ptr++;
71
72     if ((value & 0xc0) == 0xc0)         /* this is a repeat count byte */
73     {
74       count = value & 0x3f;             /* extract repeat count from byte */
75       value = *buffer_ptr++;            /* next byte is value to repeat */
76
77       for (; count && bitmap_ptr < bitmap_last; count--)
78         *bitmap_ptr++ = value;
79
80       if (count)                        /* repeat count spans end of bitmap */
81         return NULL;
82     }
83     else
84       *bitmap_ptr++ = value;
85
86     image->rgb.color_used[value] = TRUE;
87   }
88
89   /* check if end of buffer was reached before end of bitmap */
90   if (bitmap_ptr < bitmap_last)
91     return NULL;
92
93   /* return current buffer position for next decoding function */
94   return buffer_ptr;
95 }
96
97 static byte *PCX_ReadColormap(Image *image,byte *buffer_ptr, byte *buffer_last)
98 {
99   int i, magic;
100
101   /* read colormap magic byte */
102   magic = *buffer_ptr++;
103
104   /* check magic colormap header byte */
105   if (magic != PCX_256COLORS_MAGIC)
106     return NULL;
107
108   /* check if enough bytes left for a complete colormap */
109   if (buffer_ptr + PCX_COLORMAP_SIZE > buffer_last)
110     return NULL;
111
112   /* read 256 colors from PCX colormap */
113   for (i=0; i<PCX_MAXCOLORS; i++)
114   {
115     image->rgb.red[i]   = *buffer_ptr++ << 8;
116     image->rgb.green[i] = *buffer_ptr++ << 8;
117     image->rgb.blue[i]  = *buffer_ptr++ << 8;
118   }
119
120   /* return current buffer position for next decoding function */
121   return buffer_ptr;
122 }
123
124 Image *Read_PCX_to_Image(char *filename)
125 {
126   FILE *file;
127   byte *file_buffer;
128   byte *buffer_ptr, *buffer_last;
129   unsigned int file_length;
130   struct PCX_Header pcx;
131   Image *image;
132   int width, height, depth;
133   int i;
134
135   errno_pcx = PCX_Success;
136
137   if (!(file = fopen(filename, "r")))
138   {
139     errno_pcx = PCX_OpenFailed;
140     return NULL;
141   }
142
143   if (fseek(file, 0, SEEK_END) == -1)
144   {
145     fclose(file);
146     errno_pcx = PCX_ReadFailed;
147     return NULL;
148   }
149
150   file_length = ftell(file);
151   rewind(file);
152
153   if (file_length < PCX_HEADER_SIZE)
154   {
155     /* PCX file is too short to contain a valid PCX header */
156     fclose(file);
157
158     errno_pcx = PCX_FileInvalid;
159     return NULL;
160   }
161
162   file_buffer = checked_malloc(file_length);
163
164   if (fread(file_buffer, 1, file_length, file) != file_length)
165   {
166     fclose(file);
167     errno_pcx = PCX_ReadFailed;
168     return NULL;
169   }
170
171   fclose(file);
172
173   pcx.signature      = file_buffer[0];
174   pcx.version        = file_buffer[1];
175   pcx.encoding       = file_buffer[2];
176   pcx.bits_per_pixel = file_buffer[3];
177   pcx.xmin           = file_buffer[4]  + 256 * file_buffer[5];
178   pcx.ymin           = file_buffer[6]  + 256 * file_buffer[7];
179   pcx.xmax           = file_buffer[8]  + 256 * file_buffer[9];
180   pcx.ymax           = file_buffer[10] + 256 * file_buffer[11];
181   pcx.color_planes   = file_buffer[65];
182   pcx.bytes_per_line = file_buffer[66] + 256 * file_buffer[67];
183   pcx.palette_type   = file_buffer[68] + 256 * file_buffer[69];
184
185   width  = pcx.xmax - pcx.xmin + 1;
186   height = pcx.ymax - pcx.ymin + 1;
187   depth  = pcx.bits_per_pixel;
188
189   if (pcx.signature != PCX_MAGIC || pcx.version > PCX_LAST_VERSION ||
190       pcx.encoding != PCX_ENCODING || pcx.color_planes > PCX_MAXDEPTH ||
191       width < 0 || height < 0)
192   {
193     free(file_buffer);
194
195     errno_pcx = PCX_FileInvalid;
196     return NULL;
197   }
198
199 #if PCX_DEBUG
200   if (options.verbose)
201   {
202     printf("%s is a %dx%d PC Paintbrush image with %d bitplanes\n",
203            filename, width, height,
204            pcx.color_planes);
205     printf("depth: %d\n", pcx.bits_per_pixel);
206     printf("color_planes: %d\n", pcx.color_planes);
207     printf("bytes_per_line: %d\n", pcx.bytes_per_line);
208     printf("palette type: %s\n",
209            (pcx.palette_type == 1 ? "color" :
210             pcx.palette_type == 2 ? "grayscale" : "undefined"));
211   }
212 #endif
213
214   /* allocate new image structure */
215   image = newImage(width, height, depth);
216
217   buffer_ptr  = file_buffer + PCX_HEADER_SIZE;
218   buffer_last = file_buffer + file_length;
219
220   /* read compressed bitmap data */
221   if ((buffer_ptr = PCX_ReadBitmap(image, buffer_ptr, buffer_last)) == NULL)
222   {
223     free(file_buffer);
224     freeImage(image);
225
226     errno_pcx = PCX_FileInvalid;
227     return NULL;
228   }
229
230   if (file_length < PCX_HEADER_SIZE + PCX_COLORMAP_SIZE)
231   {
232     /* PCX file is too short to contain a valid 256 colors colormap */
233     fclose(file);
234     errno_pcx = PCX_ColorFailed;
235     return NULL;
236   }
237
238   /* read colormap data */
239   if (!PCX_ReadColormap(image, buffer_ptr, buffer_last))
240   {
241     free(file_buffer);
242     freeImage(image);
243     errno_pcx = PCX_ColorFailed;
244     return NULL;
245   }
246
247   free(file_buffer);
248
249   /* determine number of used colormap entries */
250   image->rgb.used = 0;
251   for (i=0; i<PCX_MAXCOLORS; i++)
252     if (image->rgb.color_used[i])
253       image->rgb.used++;
254
255 #if PCX_DEBUG
256   if (options.verbose)
257     printf("Read_PCX_to_Image: %d colors found\n", image->rgb.used);
258 #endif
259
260   return image;
261 }