]>
git.saurik.com Git - wxWidgets.git/blob - src/common/imagpcx.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxImage PCX handler
4 // Author: Guillermo Rodriguez Garcia <guille@iies.es>
7 // Copyright: (c) 1999 Guillermo Rodriguez Garcia
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
12 We don't put pragma implement in this file because it is already present in
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
25 #include "wx/wfstream.h"
26 #include "wx/module.h"
31 //-----------------------------------------------------------------------------
33 //-----------------------------------------------------------------------------
35 void RLEencode(unsigned char *p
, unsigned int size
, wxOutputStream
& s
)
39 void RLEdecode(unsigned char *p
, unsigned int size
, wxInputStream
& s
)
41 unsigned int i
, data
, cont
;
43 // Read 'size' bytes. The PCX official specs say there will be
44 // a decoding break at the end of each scanline (but not at the
45 // end of each plane inside a scanline). Only use this function
46 // to read one or more _complete_ scanlines. Else, more than
47 // 'size' bytes might be read and the buffer might overflow.
51 data
= (unsigned char)s
.GetC();
53 // If ((data & 0xC0) != 0xC0), then the value read is a data
54 // byte. Else, it is a counter (cont = val & 0x3F) and the
55 // next byte is the data byte.
57 if ((data
& 0xC0) != 0xC0)
65 data
= (unsigned char)s
.GetC();
66 for (i
= 1; i
<= cont
; i
++)
76 #define HDR_ENCODING 2
77 #define HDR_BITSPERPIXEL 3
82 #define HDR_NPLANES 65
83 #define HDR_BYTESPERLINE 66
86 #define IMAGE_8BIT 0 // 8 bpp, 1 plane (8 bit)
87 #define IMAGE_24BIT 1 // 8 bpp, 3 planes (24 bit)
90 #define E_OK 0 // everything was OK
91 #define E_FORMATO 1 // error in pcx file format
92 #define E_MEMORIA 2 // error allocating memory
93 #define E_VERSION 3 // error in pcx version number
97 // Loads a PCX file into the wxImage object pointed by image.
98 // Returns E_OK on success, or an error code otherwise (see
99 // above for error codes)
101 int ReadPCX(wxImage
*image
, wxInputStream
& stream
)
103 unsigned char hdr
[128]; // PCX header
104 unsigned char pal
[768]; // palette for 8 bit images
105 unsigned char *p
; // space to store one scanline
106 unsigned char *dst
; // pointer into wxImage data
107 unsigned int width
, height
; // size of the image
108 unsigned int bytesperline
; // bytes per line (each plane)
109 int bitsperpixel
; // bits per pixel (each plane)
110 int nplanes
; // number of planes
111 int encoding
; // is the image RLE encoded?
112 int format
; // image format (8 bit, 24 bit)
116 // Read PCX header and check the version number (it must
117 // be at least 5 or higher for 8 bit and 24 bit images).
119 stream
.Read(hdr
, 128);
121 if (hdr
[HDR_VERSION
] < 5) return E_VERSION
;
123 // Extract all image info from the PCX header.
125 encoding
= hdr
[HDR_ENCODING
];
126 nplanes
= hdr
[HDR_NPLANES
];
127 bitsperpixel
= hdr
[HDR_BITSPERPIXEL
];
128 bytesperline
= hdr
[HDR_BYTESPERLINE
] + 256 * hdr
[HDR_BYTESPERLINE
+ 1];
129 width
= (hdr
[HDR_XMAX
] + 256 * hdr
[HDR_XMAX
+ 1]) -
130 (hdr
[HDR_XMIN
] + 256 * hdr
[HDR_XMIN
+ 1]) + 1;
131 height
= (hdr
[HDR_YMAX
] + 256 * hdr
[HDR_YMAX
+ 1]) -
132 (hdr
[HDR_YMIN
] + 256 * hdr
[HDR_YMIN
+ 1]) + 1;
134 // Check image format. Currently supported formats are
135 // 8 bits (8 bpp, 1 plane) and 24 bits (8 bpp, 3 planes).
137 if ((nplanes
== 3) && (bitsperpixel
== 8))
138 format
= IMAGE_24BIT
;
139 else if ((nplanes
== 1) && (bitsperpixel
== 8))
144 // If the image is of type IMAGE_8BIT, then there is a
145 // palette at the end of the file. Read it now before
148 if (format
== IMAGE_8BIT
)
150 pos
= stream
.TellI();
151 stream
.SeekI(-769, wxFromEnd
);
153 if (stream
.GetC() != 12)
156 stream
.Read(pal
, 768);
157 stream
.SeekI(pos
, wxFromStart
);
160 // Allocate memory for a scanline and resize the image.
162 image
->Create(width
, height
);
167 if ((p
= (unsigned char *) malloc(bytesperline
* nplanes
)) == NULL
)
170 // Now start reading the file, line by line, and store
171 // the data in the format required by wxImage.
173 dst
= image
->GetData();
175 for (; height
; height
--)
178 RLEdecode(p
, bytesperline
* nplanes
, stream
);
180 stream
.Read(p
, bytesperline
* nplanes
);
186 for (i
= 0; i
< width
; i
++)
188 *(dst
++) = pal
[ 3 * (p
[i
]) ];
189 *(dst
++) = pal
[ 3 * (p
[i
]) + 1];
190 *(dst
++) = pal
[ 3 * (p
[i
]) + 2];
196 for (i
= 0; i
< width
; i
++)
199 *(dst
++) = p
[i
+ bytesperline
];
200 *(dst
++) = p
[i
+ 2 * bytesperline
];
213 //-----------------------------------------------------------------------------
215 //-----------------------------------------------------------------------------
217 #if !USE_SHARED_LIBRARIES
218 IMPLEMENT_DYNAMIC_CLASS(wxPCXHandler
,wxImageHandler
)
223 bool wxPCXHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
, bool verbose
)
227 if (!CanRead(stream
))
230 wxLogError(_T("wxPCXHandler: this is not a PCX file"));
237 if ((error
= ReadPCX(image
, stream
)) != E_OK
)
243 case E_FORMATO
: wxLogError(_T("wxPCXHandler: image format unsupported")); break;
244 case E_MEMORIA
: wxLogError(_T("wxPCXHandler: couldn't allocate memory")); break;
245 case E_VERSION
: wxLogError(_T("wxPCXHandler: version number too low")); break;
246 default: wxLogError(_T("wxPCXHandler: unknown error !!!"));
256 bool wxPCXHandler::SaveFile( wxImage
*image
, wxOutputStream
& stream
, bool verbose
)
259 wxLogError(_T("wxPCXHandler::SaveFile still not implemented"));
264 bool wxPCXHandler::CanRead( wxInputStream
& stream
)
269 pos
= stream
.TellI();
270 stream
.SeekI(0, wxFromStart
);
272 stream
.SeekI(pos
, wxFromStart
);
274 // not very safe, but this is all we can get from PCX header :-(
278 #endif // wxUSE_STREAMS