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