]>
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"
24 #include "wx/wfstream.h"
25 #include "wx/module.h"
28 //-----------------------------------------------------------------------------
30 //-----------------------------------------------------------------------------
32 void RLEencode(unsigned char *p
, unsigned int size
, wxOutputStream
& s
)
36 void RLEdecode(unsigned char *p
, unsigned int size
, wxInputStream
& s
)
38 unsigned int i
, data
, cont
;
40 // Read 'size' bytes. The PCX official specs say there will be
41 // a decoding break at the end of each scanline (but not at the
42 // end of each plane inside a scanline). Only use this function
43 // to read one or more _complete_ scanlines. Else, more than
44 // 'size' bytes might be read and the buffer might overflow.
48 data
= (unsigned char)s
.GetC();
50 // If ((data & 0xC0) != 0xC0), then the value read is a data
51 // byte. Else, it is a counter (cont = val & 0x3F) and the
52 // next byte is the data byte.
54 if ((data
& 0xC0) != 0xC0)
62 data
= (unsigned char)s
.GetC();
63 for (i
= 1; i
<= cont
; i
++)
73 #define HDR_ENCODING 2
74 #define HDR_BITSPERPIXEL 3
79 #define HDR_NPLANES 65
80 #define HDR_BYTESPERLINE 66
83 #define IMAGE_8BIT 0 // 8 bpp, 1 plane (8 bit)
84 #define IMAGE_24BIT 1 // 8 bpp, 3 planes (24 bit)
87 #define E_OK 0 // everything was OK
88 #define E_FORMATO 1 // error in pcx file format
89 #define E_MEMORIA 2 // error allocating memory
90 #define E_VERSION 3 // error in pcx version number
94 // Loads a PCX file into the wxImage object pointed by image.
95 // Returns E_OK on success, or an error code otherwise (see
96 // above for error codes)
98 int ReadPCX(wxImage
*image
, wxInputStream
& stream
)
100 unsigned char hdr
[128]; // PCX header
101 unsigned char pal
[768]; // palette for 8 bit images
102 unsigned char *p
; // space to store one scanline
103 unsigned char *dst
; // pointer into wxImage data
104 unsigned int width
, height
; // size of the image
105 unsigned int bytesperline
; // bytes per line (each plane)
106 int bitsperpixel
; // bits per pixel (each plane)
107 int nplanes
; // number of planes
108 int encoding
; // is the image RLE encoded?
109 int format
; // image format (8 bit, 24 bit)
113 // Read PCX header and check the version number (it must
114 // be at least 5 or higher for 8 bit and 24 bit images).
116 stream
.Read(hdr
, 128);
118 if (hdr
[HDR_VERSION
] < 5) return E_VERSION
;
120 // Extract all image info from the PCX header.
122 encoding
= hdr
[HDR_ENCODING
];
123 nplanes
= hdr
[HDR_NPLANES
];
124 bitsperpixel
= hdr
[HDR_BITSPERPIXEL
];
125 bytesperline
= hdr
[HDR_BYTESPERLINE
] + 256 * hdr
[HDR_BYTESPERLINE
+ 1];
126 width
= (hdr
[HDR_XMAX
] + 256 * hdr
[HDR_XMAX
+ 1]) -
127 (hdr
[HDR_XMIN
] + 256 * hdr
[HDR_XMIN
+ 1]) + 1;
128 height
= (hdr
[HDR_YMAX
] + 256 * hdr
[HDR_YMAX
+ 1]) -
129 (hdr
[HDR_YMIN
] + 256 * hdr
[HDR_YMIN
+ 1]) + 1;
131 // Check image format. Currently supported formats are
132 // 8 bits (8 bpp, 1 plane) and 24 bits (8 bpp, 3 planes).
134 if ((nplanes
== 3) && (bitsperpixel
== 8))
135 format
= IMAGE_24BIT
;
136 else if ((nplanes
== 1) && (bitsperpixel
== 8))
141 // If the image is of type IMAGE_8BIT, then there is a
142 // palette at the end of the file. Read it now before
145 if (format
== IMAGE_8BIT
)
147 pos
= stream
.TellI();
148 stream
.SeekI(-769, wxFromEnd
);
150 if (stream
.GetC() != 12)
153 stream
.Read(pal
, 768);
154 stream
.SeekI(pos
, wxFromStart
);
157 // Allocate memory for a scanline and resize the image.
159 image
->Create(width
, height
);
164 if ((p
= (unsigned char *) malloc(bytesperline
* nplanes
)) == NULL
)
167 // Now start reading the file, line by line, and store
168 // the data in the format required by wxImage.
170 dst
= image
->GetData();
172 for (; height
; height
--)
175 RLEdecode(p
, bytesperline
* nplanes
, stream
);
177 stream
.Read(p
, bytesperline
* nplanes
);
183 for (i
= 0; i
< width
; i
++)
185 *(dst
++) = pal
[ 3 * (p
[i
]) ];
186 *(dst
++) = pal
[ 3 * (p
[i
]) + 1];
187 *(dst
++) = pal
[ 3 * (p
[i
]) + 2];
193 for (i
= 0; i
< width
; i
++)
196 *(dst
++) = p
[i
+ bytesperline
];
197 *(dst
++) = p
[i
+ 2 * bytesperline
];
210 //-----------------------------------------------------------------------------
212 //-----------------------------------------------------------------------------
214 #if !USE_SHARED_LIBRARIES
215 IMPLEMENT_DYNAMIC_CLASS(wxPCXHandler
,wxImageHandler
)
220 bool wxPCXHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
, bool verbose
)
224 if (!CanRead(stream
))
227 wxLogError(_T("wxPCXHandler: this is not a PCX file"));
234 if ((error
= ReadPCX(image
, stream
)) != E_OK
)
240 case E_FORMATO
: wxLogError(_T("wxPCXHandler: image format unsupported")); break;
241 case E_MEMORIA
: wxLogError(_T("wxPCXHandler: couldn't allocate memory")); break;
242 case E_VERSION
: wxLogError(_T("wxPCXHandler: version number too low")); break;
243 default: wxLogError(_T("wxPCXHandler: unknown error !!!"));
253 bool wxPCXHandler::SaveFile( wxImage
*image
, wxOutputStream
& stream
, bool verbose
)
256 wxLogError(_T("wxPCXHandler::SaveFile still not implemented"));
261 bool wxPCXHandler::CanRead( wxInputStream
& stream
)
266 pos
= stream
.TellI();
267 stream
.SeekI(0, wxFromStart
);
269 stream
.SeekI(pos
, wxFromStart
);
271 // not very safe, but this is all we can get from PCX header :-(
276 #endif // wxUSE_STREAMS