3 * Purpose: Platform Independent PCX Image Class Loader and Writer
\r
4 * 05/Jan/2002 Davide Pizzolato - www.xdp.it
\r
5 * CxImage version 6.0.0 02/Feb/2008
\r
7 * based on ppmtopcx.c - convert a portable pixmap to PCX
\r
8 * Copyright (C) 1994 by Ingo Wilken (Ingo.Wilken@informatik.uni-oldenburg.de)
\r
9 * based on ppmtopcx.c by Michael Davidson
\r
12 #include "ximapcx.h"
\r
14 #if CXIMAGE_SUPPORT_PCX
\r
16 #include "xmemfile.h"
\r
18 #define PCX_MAGIC 0X0A // PCX magic number
\r
19 #define PCX_256_COLORS 0X0C // magic number for 256 colors
\r
20 #define PCX_HDR_SIZE 128 // size of PCX header
\r
21 #define PCX_MAXCOLORS 256
\r
22 #define PCX_MAXPLANES 4
\r
23 #define PCX_MAXVAL 255
\r
25 ////////////////////////////////////////////////////////////////////////////////
\r
26 #if CXIMAGE_SUPPORT_DECODE
\r
27 ////////////////////////////////////////////////////////////////////////////////
\r
28 bool CxImagePCX::Decode(CxFile *hFile)
\r
30 if (hFile == NULL) return false;
\r
32 PCXHEADER pcxHeader;
\r
33 int i, x, y, y2, nbytes, count, Height, Width;
\r
34 BYTE c, ColorMap[PCX_MAXCOLORS][3];
\r
35 BYTE *pcximage = NULL, *lpHead1 = NULL, *lpHead2 = NULL;
\r
36 BYTE *pcxplanes, *pcxpixels;
\r
40 if (hFile->Read(&pcxHeader,sizeof(PCXHEADER),1)==0) cx_throw("Can't read PCX image");
\r
42 PCX_toh(&pcxHeader);
\r
44 if (pcxHeader.Manufacturer != PCX_MAGIC) cx_throw("Error: Not a PCX file");
\r
45 // Check for PCX run length encoding
\r
46 if (pcxHeader.Encoding != 1) cx_throw("PCX file has unknown encoding scheme");
\r
48 Width = (pcxHeader.Xmax - pcxHeader.Xmin) + 1;
\r
49 Height = (pcxHeader.Ymax - pcxHeader.Ymin) + 1;
\r
50 info.xDPI = pcxHeader.Hres;
\r
51 info.yDPI = pcxHeader.Vres;
\r
53 if (info.nEscape == -1){
\r
54 head.biWidth = Width;
\r
55 head.biHeight= Height;
\r
56 info.dwType = CXIMAGE_FORMAT_PCX;
\r
60 // Check that we can handle this image format
\r
61 if (pcxHeader.ColorPlanes > 4)
\r
62 cx_throw("Can't handle image with more than 4 planes");
\r
65 if (pcxHeader.ColorPlanes >= 3 && pcxHeader.BitsPerPixel == 8){
\r
66 Create (Width, Height, 24, CXIMAGE_FORMAT_PCX);
\r
67 #if CXIMAGE_SUPPORT_ALPHA
\r
68 if (pcxHeader.ColorPlanes==4) AlphaCreate();
\r
69 #endif //CXIMAGE_SUPPORT_ALPHA
\r
70 } else if (pcxHeader.ColorPlanes == 4 && pcxHeader.BitsPerPixel == 1)
\r
71 Create (Width, Height, 4, CXIMAGE_FORMAT_PCX);
\r
73 Create (Width, Height, pcxHeader.BitsPerPixel, CXIMAGE_FORMAT_PCX);
\r
75 if (info.nEscape) cx_throw("Cancelled"); // <vho> - cancel decoding
\r
77 //Read the image and check if it's ok
\r
78 nbytes = pcxHeader.BytesPerLine * pcxHeader.ColorPlanes * Height;
\r
79 lpHead1 = pcximage = (BYTE*)malloc(nbytes);
\r
81 if (hFile == NULL || hFile->Eof()) cx_throw("corrupted PCX");
\r
83 hFile->Read(&c,1,1);
\r
84 if ((c & 0XC0) != 0XC0){ // Repeated group
\r
89 count = c & 0X3F; // extract count
\r
90 hFile->Read(&c,1,1);
\r
91 if (count > nbytes) cx_throw("repeat count spans end of image");
\r
94 while (--count >=0) *pcximage++ = c;
\r
99 for (i = 0; i < 16; i++){
\r
100 ColorMap[i][0] = pcxHeader.ColorMap[i][0];
\r
101 ColorMap[i][1] = pcxHeader.ColorMap[i][1];
\r
102 ColorMap[i][2] = pcxHeader.ColorMap[i][2];
\r
104 if (pcxHeader.BitsPerPixel == 8 && pcxHeader.ColorPlanes == 1){
\r
105 hFile->Read(&c,1,1);
\r
106 if (c != PCX_256_COLORS) cx_throw("bad color map signature");
\r
108 for (i = 0; i < PCX_MAXCOLORS; i++){
\r
109 hFile->Read(&ColorMap[i][0],1,1);
\r
110 hFile->Read(&ColorMap[i][1],1,1);
\r
111 hFile->Read(&ColorMap[i][2],1,1);
\r
114 if (pcxHeader.BitsPerPixel == 1 && pcxHeader.ColorPlanes == 1){
\r
115 ColorMap[0][0] = ColorMap[0][1] = ColorMap[0][2] = 0;
\r
116 ColorMap[1][0] = ColorMap[1][1] = ColorMap[1][2] = 255;
\r
119 for (DWORD idx=0; idx<head.biClrUsed; idx++) SetPaletteColor((BYTE)idx,ColorMap[idx][0],ColorMap[idx][1],ColorMap[idx][2]);
\r
121 lpHead2 = pcxpixels = (BYTE *)malloc(Width + pcxHeader.BytesPerLine * 8);
\r
122 // Convert the image
\r
123 for (y = 0; y < Height; y++){
\r
125 if (info.nEscape) cx_throw("Cancelled"); // <vho> - cancel decoding
\r
128 pcxpixels = lpHead2;
\r
129 pcxplanes = pcximage + (y * pcxHeader.BytesPerLine * pcxHeader.ColorPlanes);
\r
131 if (pcxHeader.ColorPlanes == 3 && pcxHeader.BitsPerPixel == 8){
\r
132 // Deal with 24 bit color image
\r
133 for (x = 0; x < Width; x++){
\r
134 SetPixelColor(x,y2,RGB(pcxplanes[x],pcxplanes[pcxHeader.BytesPerLine + x],pcxplanes[2*pcxHeader.BytesPerLine + x]));
\r
137 #if CXIMAGE_SUPPORT_ALPHA
\r
138 } else if (pcxHeader.ColorPlanes == 4 && pcxHeader.BitsPerPixel == 8){
\r
139 for (x = 0; x < Width; x++){
\r
140 SetPixelColor(x,y2,RGB(pcxplanes[x],pcxplanes[pcxHeader.BytesPerLine + x],pcxplanes[2*pcxHeader.BytesPerLine + x]));
\r
141 AlphaSet(x,y2,pcxplanes[3*pcxHeader.BytesPerLine + x]);
\r
144 #endif //CXIMAGE_SUPPORT_ALPHA
\r
145 } else if (pcxHeader.ColorPlanes == 1) {
\r
146 if (!PCX_UnpackPixels(pcxpixels, pcxplanes, pcxHeader.BytesPerLine, pcxHeader.ColorPlanes, pcxHeader.BitsPerPixel)){
\r
147 cx_throw("PCX_UnpackPixels: Can't handle packed pixels with more than 1 plane");
\r
150 if (!PCX_PlanesToPixels(pcxpixels, pcxplanes, pcxHeader.BytesPerLine, pcxHeader.ColorPlanes, pcxHeader.BitsPerPixel)){
\r
151 cx_throw("PCX_PlanesToPixels: more than 4 planes or more than 1 bit per pixel");
\r
154 for (x = 0; x < Width; x++) SetPixelIndex(x,y2,pcxpixels[x]);
\r
158 if (strcmp(message,"")) strncpy(info.szLastError,message,255);
\r
159 if (lpHead1){ free(lpHead1); lpHead1 = NULL; }
\r
160 if (lpHead2){ free(lpHead2); lpHead2 = NULL; }
\r
163 if (lpHead1){ free(lpHead1); lpHead1 = NULL; }
\r
164 if (lpHead2){ free(lpHead2); lpHead2 = NULL; }
\r
167 ////////////////////////////////////////////////////////////////////////////////
\r
168 #endif //CXIMAGE_SUPPORT_DECODE
\r
169 ////////////////////////////////////////////////////////////////////////////////
\r
170 #if CXIMAGE_SUPPORT_ENCODE
\r
171 ////////////////////////////////////////////////////////////////////////////////
\r
172 bool CxImagePCX::Encode(CxFile * hFile)
\r
174 if (EncodeSafeCheck(hFile)) return false;
\r
178 PCXHEADER pcxHeader;
\r
179 memset(&pcxHeader,0,sizeof(pcxHeader));
\r
180 pcxHeader.Manufacturer = PCX_MAGIC;
\r
181 pcxHeader.Version = 5;
\r
182 pcxHeader.Encoding = 1;
\r
183 pcxHeader.Xmin = 0;
\r
184 pcxHeader.Ymin = 0;
\r
185 pcxHeader.Xmax = (WORD)head.biWidth-1;
\r
186 pcxHeader.Ymax = (WORD)head.biHeight-1;
\r
187 pcxHeader.Hres = (WORD)info.xDPI;
\r
188 pcxHeader.Vres = (WORD)info.yDPI;
\r
189 pcxHeader.Reserved = 0;
\r
190 pcxHeader.PaletteType = head.biClrUsed==0;
\r
192 switch(head.biBitCount){
\r
196 pcxHeader.BitsPerPixel = 8;
\r
197 pcxHeader.ColorPlanes = head.biClrUsed==0 ? 3 : 1;
\r
198 #if CXIMAGE_SUPPORT_ALPHA
\r
199 if (AlphaIsValid() && head.biClrUsed==0) pcxHeader.ColorPlanes =4;
\r
200 #endif //CXIMAGE_SUPPORT_ALPHA
\r
201 pcxHeader.BytesPerLine = (WORD)head.biWidth;
\r
205 pcxHeader.BitsPerPixel = 1;
\r
206 pcxHeader.ColorPlanes = head.biClrUsed==16 ? 4 : 1;
\r
207 pcxHeader.BytesPerLine = (WORD)((head.biWidth * pcxHeader.BitsPerPixel + 7)>>3);
\r
210 if (pcxHeader.BitsPerPixel == 1 && pcxHeader.ColorPlanes == 1){
\r
211 pcxHeader.ColorMap[0][0] = pcxHeader.ColorMap[0][1] = pcxHeader.ColorMap[0][2] = 0;
\r
212 pcxHeader.ColorMap[1][0] = pcxHeader.ColorMap[1][1] = pcxHeader.ColorMap[1][2] = 255;
\r
214 if (pcxHeader.BitsPerPixel == 1 && pcxHeader.ColorPlanes == 4){
\r
216 for (int i = 0; i < 16; i++){
\r
217 c=GetPaletteColor(i);
\r
218 pcxHeader.ColorMap[i][0] = c.rgbRed;
\r
219 pcxHeader.ColorMap[i][1] = c.rgbGreen;
\r
220 pcxHeader.ColorMap[i][2] = c.rgbBlue;
\r
224 pcxHeader.BytesPerLine = (pcxHeader.BytesPerLine + 1)&(~1);
\r
226 PCX_toh(&pcxHeader);
\r
227 if (hFile->Write(&pcxHeader, sizeof(pcxHeader), 1) == 0 )
\r
228 cx_throw("cannot write PCX header");
\r
229 PCX_toh(&pcxHeader);
\r
236 if (head.biClrUsed==0){
\r
237 for (y = head.biHeight-1; y >=0 ; y--){
\r
238 for (int p=0; p<pcxHeader.ColorPlanes; p++){
\r
240 for (x = 0; x<head.biWidth; x++){
\r
242 PCX_PackPixels(BlindGetPixelColor(x,y).rgbRed,c,n,buffer);
\r
244 PCX_PackPixels(BlindGetPixelColor(x,y).rgbGreen,c,n,buffer);
\r
246 PCX_PackPixels(BlindGetPixelColor(x,y).rgbBlue,c,n,buffer);
\r
247 #if CXIMAGE_SUPPORT_ALPHA
\r
249 PCX_PackPixels(BlindAlphaGet(x,y),c,n,buffer);
\r
250 #endif //CXIMAGE_SUPPORT_ALPHA
\r
252 PCX_PackPixels(-1-(head.biWidth&0x1),c,n,buffer);
\r
256 hFile->Write(buffer.GetBuffer(false),buffer.Tell(),1);
\r
258 } else if (head.biBitCount==8) {
\r
260 for (y = head.biHeight-1; y >=0 ; y--){
\r
262 for (x = 0; x<head.biWidth; x++){
\r
263 PCX_PackPixels(GetPixelIndex(x,y),c,n,buffer);
\r
265 PCX_PackPixels(-1-(head.biWidth&0x1),c,n,buffer);
\r
268 hFile->Write(buffer.GetBuffer(false),buffer.Tell(),1);
\r
270 if (head.biBitCount == 8){
\r
272 BYTE* pal = (BYTE*)malloc(768);
\r
274 for (int i=0;i<256;i++){
\r
275 c=GetPaletteColor(i);
\r
276 pal[3*i+0] = c.rgbRed;
\r
277 pal[3*i+1] = c.rgbGreen;
\r
278 pal[3*i+2] = c.rgbBlue;
\r
280 hFile->Write(pal,768,1);
\r
283 } else { //(head.biBitCount==4) || (head.biBitCount==1)
\r
285 RGBQUAD *rgb = GetPalette();
\r
286 bool binvert = false;
\r
287 if (CompareColors(&rgb[0],&rgb[1])>0) binvert=(head.biBitCount==1);
\r
289 BYTE* plane = (BYTE*)malloc(pcxHeader.BytesPerLine);
\r
290 BYTE* raw = (BYTE*)malloc(head.biWidth);
\r
292 for(y = head.biHeight-1; y >=0 ; y--) {
\r
294 for( x = 0; x < head.biWidth; x++) raw[x] = (BYTE)GetPixelIndex(x,y);
\r
296 if (binvert) for( x = 0; x < head.biWidth; x++) raw[x] = 1-raw[x];
\r
298 for( x = 0; x < pcxHeader.ColorPlanes; x++ ) {
\r
299 PCX_PixelsToPlanes(raw, head.biWidth, plane, x);
\r
300 PCX_PackPlanes(plane, pcxHeader.BytesPerLine, buffer);
\r
307 hFile->Write(buffer.GetBuffer(false),buffer.Tell(),1);
\r
312 if (strcmp(message,"")) strncpy(info.szLastError,message,255);
\r
317 ////////////////////////////////////////////////////////////////////////////////
\r
318 #endif // CXIMAGE_SUPPORT_ENCODE
\r
319 ////////////////////////////////////////////////////////////////////////////////
\r
320 // Convert multi-plane format into 1 pixel per byte
\r
321 // from unpacked file data bitplanes[] into pixel row pixels[]
\r
322 // image Height rows, with each row having planes image planes each
\r
323 // bytesperline bytes
\r
324 bool CxImagePCX::PCX_PlanesToPixels(BYTE * pixels, BYTE * bitplanes, short bytesperline, short planes, short bitsperpixel)
\r
328 if (planes > 4) return false;
\r
329 if (bitsperpixel != 1) return false;
\r
331 // Clear the pixel buffer
\r
332 npixels = (bytesperline * 8) / bitsperpixel;
\r
334 while (--npixels >= 0) *p++ = 0;
\r
336 // Do the format conversion
\r
337 for (i = 0; i < planes; i++){
\r
338 int pixbit, bits, mask;
\r
340 pixbit = (1 << i); // pixel bit for this plane
\r
341 for (j = 0; j < bytesperline; j++){
\r
342 bits = *bitplanes++;
\r
343 for (mask = 0X80; mask != 0; mask >>= 1, p++)
\r
344 if (bits & mask) *p |= pixbit;
\r
349 ////////////////////////////////////////////////////////////////////////////////
\r
350 // convert packed pixel format into 1 pixel per byte
\r
351 // from unpacked file data bitplanes[] into pixel row pixels[]
\r
352 // image Height rows, with each row having planes image planes each
\r
353 // bytesperline bytes
\r
354 bool CxImagePCX::PCX_UnpackPixels(BYTE * pixels, BYTE * bitplanes, short bytesperline, short planes, short bitsperpixel)
\r
357 if (planes != 1) return false;
\r
359 if (bitsperpixel == 8){ // 8 bits/pixels, no unpacking needed
\r
360 while (bytesperline-- > 0) *pixels++ = *bitplanes++;
\r
361 } else if (bitsperpixel == 4){ // 4 bits/pixel, two pixels per byte
\r
362 while (bytesperline-- > 0){
\r
363 bits = *bitplanes++;
\r
364 *pixels++ = (BYTE)((bits >> 4) & 0X0F);
\r
365 *pixels++ = (BYTE)((bits) & 0X0F);
\r
367 } else if (bitsperpixel == 2){ // 2 bits/pixel, four pixels per byte
\r
368 while (bytesperline-- > 0){
\r
369 bits = *bitplanes++;
\r
370 *pixels++ = (BYTE)((bits >> 6) & 0X03);
\r
371 *pixels++ = (BYTE)((bits >> 4) & 0X03);
\r
372 *pixels++ = (BYTE)((bits >> 2) & 0X03);
\r
373 *pixels++ = (BYTE)((bits) & 0X03);
\r
375 } else if (bitsperpixel == 1){ // 1 bits/pixel, 8 pixels per byte
\r
376 while (bytesperline-- > 0){
\r
377 bits = *bitplanes++;
\r
378 *pixels++ = ((bits & 0X80) != 0);
\r
379 *pixels++ = ((bits & 0X40) != 0);
\r
380 *pixels++ = ((bits & 0X20) != 0);
\r
381 *pixels++ = ((bits & 0X10) != 0);
\r
382 *pixels++ = ((bits & 0X08) != 0);
\r
383 *pixels++ = ((bits & 0X04) != 0);
\r
384 *pixels++ = ((bits & 0X02) != 0);
\r
385 *pixels++ = ((bits & 0X01) != 0);
\r
390 ////////////////////////////////////////////////////////////////////////////////
\r
391 /* PCX_PackPixels(const long p,BYTE &c, BYTE &n, long &l, CxFile &f)
\r
392 * p = current pixel (-1 ends the line -2 ends odd line)
\r
393 * c = previous pixel
\r
394 * n = number of consecutive pixels
\r
396 void CxImagePCX::PCX_PackPixels(const long p,BYTE &c, BYTE &n, CxFile &f)
\r
399 if (n==1 && c<0xC0){
\r
412 if (p==-2) f.PutC(0);
\r
416 ////////////////////////////////////////////////////////////////////////////////
\r
417 void CxImagePCX::PCX_PackPlanes(BYTE* buff, const long size, CxFile &f)
\r
420 BYTE c, previous, count;
\r
424 previous = *start++;
\r
427 while (start < end) {
\r
429 if (c == previous && count < 63) {
\r
434 if (count > 1 || (previous & 0xc0) == 0xc0) {
\r
435 f.PutC( count | 0xc0 );
\r
442 if (count > 1 || (previous & 0xc0) == 0xc0) {
\r
448 ////////////////////////////////////////////////////////////////////////////////
\r
449 void CxImagePCX::PCX_PixelsToPlanes(BYTE* raw, long width, BYTE* buf, long plane)
\r
452 unsigned char *cp = buf-1;
\r
456 for( x = 0; x < width; x++ ) {
\r
461 if( raw[x] & mask )
\r
466 ////////////////////////////////////////////////////////////////////////////////
\r
467 void CxImagePCX::PCX_toh(PCXHEADER* p)
\r
469 p->Xmin = ntohs(p->Xmin);
\r
470 p->Ymin = ntohs(p->Ymin);
\r
471 p->Xmax = ntohs(p->Xmax);
\r
472 p->Ymax = ntohs(p->Ymax);
\r
473 p->Hres = ntohs(p->Hres);
\r
474 p->Vres = ntohs(p->Vres);
\r
475 p->BytesPerLine = ntohs(p->BytesPerLine);
\r
476 p->PaletteType = ntohs(p->PaletteType);
\r
478 ////////////////////////////////////////////////////////////////////////////////
\r
479 #endif // CXIMAGE_SUPPORT_PCX
\r