rocksndiamonds-1.2.0
[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 static byte *PCX_ReadBitmap(Image *image, byte *buffer_ptr, byte *buffer_last)
49 {
50   /* Run Length Encoding: If the two high bits are set,
51    * then the low 6 bits contain a repeat count, and the byte to
52    * repeat is the next byte in the file.  If the two high bits are
53    * not set, then this is the byte to write.
54    */
55
56   unsigned int bytes_per_pixel = (image->depth + 7) / 8;
57   register byte *bitmap_ptr, *bitmap_last;
58   register byte value, count;
59
60   bitmap_ptr = image->data;
61   bitmap_last = bitmap_ptr + (image->width * image->height * bytes_per_pixel);
62
63   while (bitmap_ptr < bitmap_last && buffer_ptr < buffer_last)
64   {
65     value = *buffer_ptr++;
66
67     if ((value & 0xc0) == 0xc0)         /* this is a repeat count byte */
68     {
69       count = value & 0x3f;             /* extract repeat count from byte */
70       value = *buffer_ptr++;            /* next byte is value to repeat */
71
72       for (; count && bitmap_ptr < bitmap_last; count--)
73         *bitmap_ptr++ = value;
74
75       if (count)                        /* repeat count spans end of bitmap */
76         return NULL;
77     }
78     else
79       *bitmap_ptr++ = value;
80
81     image->rgb.color_used[value] = TRUE;
82   }
83
84   /* check if end of buffer was reached before end of bitmap */
85   if (bitmap_ptr < bitmap_last)
86     return NULL;
87
88   /* return current buffer position for next decoding function */
89   return buffer_ptr;
90 }
91
92 static byte *PCX_ReadColormap(Image *image,byte *buffer_ptr, byte *buffer_last)
93 {
94   int i, magic;
95
96   /* read colormap magic byte */
97   magic = *buffer_ptr++;
98
99   /* check magic colormap header byte */
100   if (magic != PCX_256COLORS_MAGIC)
101     return NULL;
102
103   /* check if enough bytes left for a complete colormap */
104   if (buffer_ptr + PCX_COLORMAP_SIZE > buffer_last)
105     return NULL;
106
107   /* read 256 colors from PCX colormap */
108   for (i=0; i<PCX_MAXCOLORS; i++)
109   {
110     image->rgb.red[i]   = *buffer_ptr++ << 8;
111     image->rgb.green[i] = *buffer_ptr++ << 8;
112     image->rgb.blue[i]  = *buffer_ptr++ << 8;
113   }
114
115   /* return current buffer position for next decoding function */
116   return buffer_ptr;
117 }
118
119 Image *Read_PCX_to_Image(char *filename)
120 {
121   FILE *file;
122   byte *file_buffer;
123   byte *buffer_ptr, *buffer_last;
124   unsigned int file_length;
125   struct PCX_Header pcx;
126   Image *image;
127   int width, height, depth;
128   int i;
129
130   if (!(file = fopen(filename, "r")))
131     return NULL;
132
133   if (fseek(file, 0, SEEK_END) == -1)
134   {
135     fclose(file);
136     return NULL;
137   }
138
139   file_length = ftell(file);
140   rewind(file);
141
142   if (file_length < PCX_HEADER_SIZE + PCX_COLORMAP_SIZE)
143   {
144     fclose(file);
145     return NULL;
146   }
147
148   file_buffer = checked_malloc(file_length);
149
150   if (fread(file_buffer, 1, file_length, file) != file_length)
151   {
152     fclose(file);
153     return NULL;
154   }
155
156   fclose(file);
157
158   pcx.signature      = file_buffer[0];
159   pcx.version        = file_buffer[1];
160   pcx.encoding       = file_buffer[2];
161   pcx.bits_per_pixel = file_buffer[3];
162   pcx.xmin           = file_buffer[4]  + 256 * file_buffer[5];
163   pcx.ymin           = file_buffer[6]  + 256 * file_buffer[7];
164   pcx.xmax           = file_buffer[8]  + 256 * file_buffer[9];
165   pcx.ymax           = file_buffer[10] + 256 * file_buffer[11];
166   pcx.color_planes   = file_buffer[65];
167   pcx.bytes_per_line = file_buffer[66] + 256 * file_buffer[67];
168   pcx.palette_type   = file_buffer[68] + 256 * file_buffer[69];
169
170   width  = pcx.xmax - pcx.xmin + 1;
171   height = pcx.ymax - pcx.ymin + 1;
172   depth  = pcx.bits_per_pixel;
173
174   if (pcx.signature != PCX_MAGIC || pcx.version > PCX_LAST_VERSION ||
175       pcx.encoding != PCX_ENCODING || pcx.color_planes > PCX_MAXDEPTH ||
176       width < 0 || height < 0)
177   {
178     free(file_buffer);
179     return NULL;
180   }
181
182   if (options.verbose)
183   {
184     printf("%s is a %dx%d PC Paintbrush image with %d bitplanes\n",
185            filename, pcx.xmax, pcx.ymax,
186            pcx.color_planes);
187     printf("depth: %d\n", pcx.bits_per_pixel);
188     printf("bytes_per_line: %d\n", pcx.bytes_per_line);
189     printf("palette type: %s\n",
190            (pcx.palette_type == 1 ? "color" :
191             pcx.palette_type == 2 ? "grayscale" : "undefined"));
192   }
193
194   /* allocate new image structure */
195   image = newImage(width, height, depth);
196
197   buffer_ptr  = file_buffer + PCX_HEADER_SIZE;
198   buffer_last = file_buffer + file_length;
199
200   /* read compressed bitmap data */
201   if ((buffer_ptr = PCX_ReadBitmap(image, buffer_ptr, buffer_last)) == NULL)
202   {
203     free(file_buffer);
204     freeImage(image);
205     return NULL;
206   }
207
208   /* read colormap data */
209   if (!PCX_ReadColormap(image, buffer_ptr, buffer_last))
210   {
211     free(file_buffer);
212     freeImage(image);
213     return NULL;
214   }
215
216   free(file_buffer);
217
218   /* determine number of used colormap entries */
219   image->rgb.used = 0;
220   for (i=0; i<PCX_MAXCOLORS; i++)
221     if (image->rgb.color_used[i])
222       image->rgb.used++;
223
224   if (options.verbose)
225     printf("Read_PCX_to_Image: %d colors found\n", image->rgb.used);
226
227   return image;
228 }