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