]> git.sur5r.net Git - cc65/blob - src/sp65/pcx.c
7dc9d9c9b46bb1a2443822c8e8a8c89a2ecaea0a
[cc65] / src / sp65 / pcx.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                   pcx.c                                   */
4 /*                                                                           */
5 /*                              Read PCX files                               */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2012,      Ullrich von Bassewitz                                      */
10 /*                Roemerstrasse 52                                           */
11 /*                D-70794 Filderstadt                                        */
12 /* EMail:         uz@cc65.org                                                */
13 /*                                                                           */
14 /*                                                                           */
15 /* This software is provided 'as-is', without any expressed or implied       */
16 /* warranty.  In no event will the authors be held liable for any damages    */
17 /* arising from the use of this software.                                    */
18 /*                                                                           */
19 /* Permission is granted to anyone to use this software for any purpose,     */
20 /* including commercial applications, and to alter it and redistribute it    */
21 /* freely, subject to the following restrictions:                            */
22 /*                                                                           */
23 /* 1. The origin of this software must not be misrepresented; you must not   */
24 /*    claim that you wrote the original software. If you use this software   */
25 /*    in a product, an acknowledgment in the product documentation would be  */
26 /*    appreciated but is not required.                                       */
27 /* 2. Altered source versions must be plainly marked as such, and must not   */
28 /*    be misrepresented as being the original software.                      */
29 /* 3. This notice may not be removed or altered from any source              */
30 /*    distribution.                                                          */
31 /*                                                                           */
32 /*****************************************************************************/
33
34
35
36 #include <errno.h>
37 #include <stdio.h>
38 #include <string.h>
39
40 /* common */
41 #include "print.h"
42 #include "xmalloc.h"
43
44 /* sp65 */
45 #include "attr.h"
46 #include "error.h"
47 #include "fileio.h"
48 #include "pcx.h"
49
50
51
52 /*****************************************************************************/
53 /*                                  Macros                                   */
54 /*****************************************************************************/
55
56
57
58 /* Some PCX constants */
59 #define PCX_MAGIC_ID            0x0A
60 #define PCX_MAX_PLANES          4
61
62 /* A raw PCX header is just a block of bytes */
63 typedef unsigned char           RawPCXHeader[128];
64
65 /* Structured PCX header */
66 typedef struct PCXHeader PCXHeader;
67 struct PCXHeader {
68     unsigned        Id;
69     unsigned        FileVersion;
70     unsigned        Compressed;
71     unsigned        BPP;
72     unsigned        XMin;
73     unsigned        YMin;
74     unsigned        XMax;
75     unsigned        YMax;
76     unsigned        XDPI;
77     unsigned        YDPI;
78     unsigned        Planes;
79     unsigned        BytesPerPlane;
80     unsigned        PalInfo;
81     unsigned        ScreenWidth;
82     unsigned        ScreenHeight;
83
84     /* Calculated data */
85     unsigned        Width;
86     unsigned        Height;
87 };
88
89 /* Read a little endian word from a byte array at offset O */
90 #define WORD(H, O)              ((H)[O] | ((H)[O+1] << 8))
91
92
93
94 /*****************************************************************************/
95 /*                                   Code                                    */
96 /*****************************************************************************/
97
98
99
100 static PCXHeader* NewPCXHeader (void)
101 /* Allocate a new PCX header and return it */
102 {
103     /* No initialization here */
104     return xmalloc (sizeof (PCXHeader));
105 }
106
107
108
109 static void FreePCXHeader (PCXHeader* H)
110 /* Free a PCX header structure */
111 {
112     xfree (H);
113 }
114
115
116
117 static PCXHeader* ReadPCXHeader (FILE* F, const char* Name)
118 /* Read a structured PCX header from the given file and return it */
119 {
120     RawPCXHeader H;
121
122     /* Allocate a new PCXHeader structure */
123     PCXHeader* P = NewPCXHeader ();
124
125     /* Read the raw header */
126     ReadData (F, H, sizeof (H));
127
128     /* Convert the data into structured form */
129     P->Id               = H[0];
130     P->FileVersion      = H[1];
131     P->Compressed       = H[2];
132     P->BPP              = H[3];
133     P->XMin             = WORD (H, 4);
134     P->YMin             = WORD (H, 6);
135     P->XMax             = WORD (H, 8);
136     P->YMax             = WORD (H, 10);
137     P->XDPI             = WORD (H, 12);
138     P->YDPI             = WORD (H, 14);
139     P->Planes           = H[65];
140     P->BytesPerPlane    = WORD (H, 66);
141     P->PalInfo          = WORD (H, 68);
142     P->ScreenWidth      = WORD (H, 70);
143     P->ScreenHeight     = WORD (H, 72);
144     P->Width            = P->XMax - P->XMin + 1;
145     P->Height           = P->YMax - P->YMin + 1;
146
147     /* Check the header data */
148     if (P->Id != PCX_MAGIC_ID || P->FileVersion == 1 || P->FileVersion > 5) {
149         Error ("`%s' is not a PCX file", Name);
150     }
151     if (P->Compressed > 1) {
152         Error ("Unsupported compression (%d) in PCX file `%s'",
153                P->Compressed, Name);
154     }
155     /* We support:
156      *   - one plane with either 1 or 8 bits per pixel
157      *   - three planes with 8 bits per pixel
158      *   - four planes with 8 bits per pixel (does this exist?)
159      */
160     if (!((P->BPP == 1 && P->Planes == 1) ||
161           (P->BPP == 8 && (P->Planes == 1 || P->Planes == 3 || P->Planes == 4)))) {
162         /* We could support others, but currently we don't */
163         Error ("Unsupported PCX format: %u planes, %u bpp in PCX file `%s'",
164                P->Planes, P->BPP, Name);
165     }
166     if (P->PalInfo != 1 && P->PalInfo != 2) {
167         Error ("Unsupported palette info (%u) in PCX file `%s'",
168                P->PalInfo, Name);
169     }
170     if (!ValidBitmapSize (P->Width, P->Height)) {
171         Error ("PCX file `%s' has an unsupported size (w=%u, h=%d)",
172                Name, P->Width, P->Height);
173     }
174
175     /* Return the structured header */
176     return P;
177 }
178
179
180
181 static void DumpPCXHeader (const PCXHeader* P, const char* Name)
182 /* Dump the header of the PCX file in readable form to stdout */
183 {
184     printf ("File name:       %s\n", Name);
185     printf ("PCX Version:     ");
186     switch (P->FileVersion) {
187         case 0: puts ("2.5");                             break;
188         case 2: puts ("2.8 with palette");                break;
189         case 3: puts ("2.8 without palette");             break;
190         case 4: puts ("PCX for Windows without palette"); break;
191         case 5: puts ("3.0");                             break;
192     }
193     printf ("Image type:      %s\n", P->PalInfo? "color" : "grayscale");
194     printf ("Compression:     %s\n", P->Compressed? "RLE" : "None");
195     printf ("Structure:       %u planes of %u bits\n", P->Planes, P->BPP);
196     printf ("Bounding box:    [%u/%u - %u/%u]\n", P->XMin, P->YMin, P->XMax, P->YMax);
197     printf ("Resolution:      %u/%u DPI\n", P->XDPI, P->YDPI);
198     printf ("Screen size:     %u/%u\n", P->ScreenWidth, P->ScreenHeight);
199     printf ("Bytes per plane: %u\n", P->BytesPerPlane);
200 }
201
202
203
204 static void ReadPlane (FILE* F, PCXHeader* P, unsigned char* L)
205 /* Read one (possibly compressed) plane from the file */
206 {
207     if (P->Compressed) {
208
209         /* Uncompress RLE data */
210         unsigned Remaining = P->Width;
211         while (Remaining) {
212
213             unsigned char C;
214
215             /* Read the next byte */
216             unsigned char B = Read8 (F);
217
218             /* Check for a run length */
219             if ((B & 0xC0) == 0xC0) {
220                 C = (B & 0x3F);         /* Count */
221                 B = Read8 (F);          /* Value */
222             } else {
223                 C = 1;
224             }
225
226             /* Write the data to the buffer */
227             if (C > Remaining) {
228                 C = Remaining;
229             }
230             memset (L, B, C);
231
232             /* Bump counters */
233             L += C;
234             Remaining -= C;
235
236         }
237     } else {
238
239         /* Just read one line */
240         ReadData (F, L, P->Width);
241
242     }
243 }
244
245
246
247 Bitmap* ReadPCXFile (const Collection* A)
248 /* Read a bitmap from a PCX file */
249 {
250     PCXHeader* P;
251     Bitmap* B;
252     unsigned char* L;
253     Pixel* Px;
254     unsigned MaxIdx = 0;
255     unsigned X, Y;
256
257
258     /* Get the file name */
259     const char* Name = NeedAttrVal (A, "name", "read pcx file");
260
261     /* Open the file */
262     FILE* F = fopen (Name, "rb");
263     if (F == 0) {
264         Error ("Cannot open PCX file `%s': %s", Name, strerror (errno));
265     }
266
267     /* Read the PCX header */
268     P = ReadPCXHeader (F, Name);
269
270     /* Dump the header if requested */
271     if (Verbosity > 0) {
272         DumpPCXHeader (P, Name);
273     }
274
275     /* Create the bitmap */
276     B = NewBitmap (P->Width, P->Height);
277
278     /* Determine the type of the bitmap */
279     switch (P->Planes) {
280         case 1: B->Type = (P->PalInfo? bmIndexed : bmMonochrome);       break;
281         case 3: B->Type = bmRGB;                                        break;
282         case 4: B->Type = bmRGBA;                                       break;
283         default:Internal ("Unexpected number of planes");
284     }
285
286     /* Copy the name */
287     SB_CopyStr (&B->Name, Name);
288
289     /* Allocate memory for the scan line */
290     L = xmalloc (P->Width);
291
292     /* Read the pixel data */
293     Px = B->Data;
294     if (P->Planes == 1) {
295
296         /* This is either monochrome or indexed */
297         if (P->BPP == 1) {
298             /* Monochrome */
299             for (Y = 0, Px = B->Data; Y < P->Height; ++Y) {
300
301                 unsigned I;
302                 unsigned char Mask;
303
304                 /* Read the plane */
305                 ReadPlane (F, P, L);
306
307                 /* Create pixels */
308                 for (X = 0, I = 0, Mask = 0x01; X < P->Width; ++Px) {
309                     Px->Index = (L[I] & Mask) != 0;
310                     if (Mask == 0x80) {
311                         Mask = 0x01;
312                         ++I;
313                     } else {
314                         Mask <<= 1;
315                     }
316                 }
317
318             }
319         } else {
320             /* One plane with 8bpp is indexed */
321             for (Y = 0, Px = B->Data; Y < P->Height; ++Y) {
322
323                 /* Read the plane */
324                 ReadPlane (F, P, L);
325
326                 /* Create pixels */
327                 for (X = 0; X < P->Width; ++X, ++Px) {
328                     if (L[X] > MaxIdx) {
329                         MaxIdx = L[X];
330                     }
331                     Px->Index = L[X];
332                 }
333             }
334         }
335     } else {
336         /* 3 or 4 planes are RGB or RGBA (don't know if this exists) */
337         for (Y = 0, Px = B->Data; Y < P->Height; ++Y) {
338
339             /* Read the R plane and move the data */
340             ReadPlane (F, P, L);
341             for (X = 0; X < P->Width; ++X, ++Px) {
342                 Px->C.R = L[X];
343             }
344
345             /* Read the G plane and move the data */
346             ReadPlane (F, P, L);
347             for (X = 0; X < P->Width; ++X, ++Px) {
348                 Px->C.G = L[X];
349             }
350
351             /* Read the B plane and move the data */
352             ReadPlane (F, P, L);
353             for (X = 0; X < P->Width; ++X, ++Px) {
354                 Px->C.B = L[X];
355             }
356
357             /* Either read the A plane or clear it */
358             if (P->Planes == 4) {
359                 ReadPlane (F, P, L);
360                 for (X = 0; X < P->Width; ++X, ++Px) {
361                     Px->C.A = L[X];
362                 }
363             } else {
364                 for (X = 0; X < P->Width; ++X, ++Px) {
365                     Px->C.A = 0;
366                 }
367             }
368         }
369     }
370
371     /* One plane means we have a palette which is either part of the header
372      * or follows.
373      */
374     if (B->Type == bmMonochrome) {
375
376         /* Create the monochrome palette */
377         B->Pal = NewMonochromePalette ();
378
379     } else if (B->Type == bmIndexed) {
380
381         unsigned      Count;
382         unsigned      I;
383         unsigned char Palette[256][3];
384         unsigned long EndPos;
385
386         /* Determine the current file position */
387         unsigned long CurPos = FileGetPos (F);
388
389         /* Seek to the end of the file */
390         (void) fseek (F, 0, SEEK_END);
391
392         /* Get this position */
393         EndPos = FileGetPos (F);
394
395         /* There's a palette if the old location is 769 bytes from the end */
396         if (EndPos - CurPos == sizeof (Palette) + 1) {
397
398             /* Seek back */
399             FileSetPos (F, CurPos);
400
401             /* Check for palette marker */
402             if (Read8 (F) != 0x0C) {
403                 Error ("Invalid palette marker in PCX file `%s'", Name);
404             }
405
406         } else if (EndPos == CurPos) {
407
408             /* The palette is in the header */
409             FileSetPos (F, 16);
410
411             /* Check the maximum index for safety */
412             if (MaxIdx > 15) {
413                 Error ("PCX file `%s' contains more than 16 indexed colors "
414                        "but no extra palette", Name);
415             }
416
417         } else {
418             Error ("Error in PCX file `%s': %lu bytes at end of pixel data",
419                    Name, EndPos - CurPos);
420         }
421
422         /* Read the palette. We will just read what we need. */
423         Count = MaxIdx + 1;
424         ReadData (F, Palette, Count * sizeof (Palette[0]));
425
426         /* Create the palette from the data */
427         B->Pal = NewPalette (Count);
428         for (I = 0; I < Count; ++I) {
429             B->Pal->Entries[I].R = Palette[I][0];
430             B->Pal->Entries[I].G = Palette[I][1];
431             B->Pal->Entries[I].B = Palette[I][2];
432             B->Pal->Entries[I].A = 0;
433         }
434
435     }
436
437     /* Close the file */
438     fclose (F);
439
440     /* Free memory for the scan line */
441     xfree (L);
442
443     /* Free the PCX header */
444     FreePCXHeader (P);
445
446     /* Return the bitmap */
447     return B;
448 }
449
450
451