rnd-19990925-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     errno_pcx = PCX_FileInvalid;
158     return NULL;
159   }
160
161   file_buffer = checked_malloc(file_length);
162
163   if (fread(file_buffer, 1, file_length, file) != file_length)
164   {
165     fclose(file);
166     errno_pcx = PCX_ReadFailed;
167     return NULL;
168   }
169
170   fclose(file);
171
172   pcx.signature      = file_buffer[0];
173   pcx.version        = file_buffer[1];
174   pcx.encoding       = file_buffer[2];
175   pcx.bits_per_pixel = file_buffer[3];
176   pcx.xmin           = file_buffer[4]  + 256 * file_buffer[5];
177   pcx.ymin           = file_buffer[6]  + 256 * file_buffer[7];
178   pcx.xmax           = file_buffer[8]  + 256 * file_buffer[9];
179   pcx.ymax           = file_buffer[10] + 256 * file_buffer[11];
180   pcx.color_planes   = file_buffer[65];
181   pcx.bytes_per_line = file_buffer[66] + 256 * file_buffer[67];
182   pcx.palette_type   = file_buffer[68] + 256 * file_buffer[69];
183
184   width  = pcx.xmax - pcx.xmin + 1;
185   height = pcx.ymax - pcx.ymin + 1;
186   depth  = pcx.bits_per_pixel;
187
188   if (pcx.signature != PCX_MAGIC || pcx.version > PCX_LAST_VERSION ||
189       pcx.encoding != PCX_ENCODING || pcx.color_planes > PCX_MAXDEPTH ||
190       width < 0 || height < 0)
191   {
192     free(file_buffer);
193     errno_pcx = PCX_FileInvalid;
194     return NULL;
195   }
196
197 #if PCX_DEBUG
198   if (options.verbose)
199   {
200     printf("%s is a %dx%d PC Paintbrush image with %d bitplanes\n",
201            filename, pcx.xmax, pcx.ymax,
202            pcx.color_planes);
203     printf("depth: %d\n", pcx.bits_per_pixel);
204     printf("color_planes: %d\n", pcx.color_planes);
205     printf("bytes_per_line: %d\n", pcx.bytes_per_line);
206     printf("palette type: %s\n",
207            (pcx.palette_type == 1 ? "color" :
208             pcx.palette_type == 2 ? "grayscale" : "undefined"));
209   }
210 #endif
211
212   /* allocate new image structure */
213   image = newImage(width, height, depth);
214
215   buffer_ptr  = file_buffer + PCX_HEADER_SIZE;
216   buffer_last = file_buffer + file_length;
217
218   /* read compressed bitmap data */
219   if ((buffer_ptr = PCX_ReadBitmap(image, buffer_ptr, buffer_last)) == NULL)
220   {
221     free(file_buffer);
222     freeImage(image);
223     errno_pcx = PCX_FileInvalid;
224     return NULL;
225   }
226
227   if (file_length < PCX_HEADER_SIZE + PCX_COLORMAP_SIZE)
228   {
229     /* PCX file is too short to contain a valid 256 colors colormap */
230     fclose(file);
231     errno_pcx = PCX_ColorFailed;
232     return NULL;
233   }
234
235   /* read colormap data */
236   if (!PCX_ReadColormap(image, buffer_ptr, buffer_last))
237   {
238     free(file_buffer);
239     freeImage(image);
240     errno_pcx = PCX_ColorFailed;
241     return NULL;
242   }
243
244   free(file_buffer);
245
246   /* determine number of used colormap entries */
247   image->rgb.used = 0;
248   for (i=0; i<PCX_MAXCOLORS; i++)
249     if (image->rgb.color_used[i])
250       image->rgb.used++;
251
252 #if PCX_DEBUG
253   if (options.verbose)
254     printf("Read_PCX_to_Image: %d colors found\n", image->rgb.used);
255 #endif
256
257   return image;
258 }