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