]>
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"
30 //-----------------------------------------------------------------------------
32 //-----------------------------------------------------------------------------
34 void RLEencode(unsigned char *p
, unsigned int size
, wxOutputStream
& s
)
38 void RLEdecode(unsigned char *p
, unsigned int size
, wxInputStream
& s
)
40 unsigned int i
, data
, cont
;
42 // Read 'size' bytes. The PCX official specs say there will be
43 // a decoding break at the end of each scanline (but not at the
44 // end of each plane inside a scanline). Only use this function
45 // to read one or more _complete_ scanlines. Else, more than
46 // 'size' bytes might be read and the buffer might overflow.
50 data
= (unsigned char)s
.GetC();
52 // If ((data & 0xC0) != 0xC0), then the value read is a data
53 // byte. Else, it is a counter (cont = val & 0x3F) and the
54 // next byte is the data byte.
56 if ((data
& 0xC0) != 0xC0)
64 data
= (unsigned char)s
.GetC();
65 for (i
= 1; i
<= cont
; i
++)
75 #define HDR_ENCODING 2
76 #define HDR_BITSPERPIXEL 3
81 #define HDR_NPLANES 65
82 #define HDR_BYTESPERLINE 66
85 #define IMAGE_8BIT 0 // 8 bpp, 1 plane (8 bit)
86 #define IMAGE_24BIT 1 // 8 bpp, 3 planes (24 bit)
89 #define E_OK 0 // everything was OK
90 #define E_FORMATO 1 // error in pcx file format
91 #define E_MEMORIA 2 // error allocating memory
92 #define E_VERSION 3 // error in pcx version number
96 // Loads a PCX file into the wxImage object pointed by image.
97 // Returns E_OK on success, or an error code otherwise (see
98 // above for error codes)
100 int ReadPCX(wxImage
*image
, wxInputStream
& stream
)
102 unsigned char hdr
[128]; // PCX header
103 unsigned char pal
[768]; // palette for 8 bit images
104 unsigned char *p
; // space to store one scanline
105 unsigned char *dst
; // pointer into wxImage data
106 unsigned int width
, height
; // size of the image
107 unsigned int bytesperline
; // bytes per line (each plane)
108 int bitsperpixel
; // bits per pixel (each plane)
109 int nplanes
; // number of planes
110 int encoding
; // is the image RLE encoded?
111 int format
; // image format (8 bit, 24 bit)
115 // Read PCX header and check the version number (it must
116 // be at least 5 or higher for 8 bit and 24 bit images).
118 stream
.Read(hdr
, 128);
120 if (hdr
[HDR_VERSION
] < 5) return E_VERSION
;
122 // Extract all image info from the PCX header.
124 encoding
= hdr
[HDR_ENCODING
];
125 nplanes
= hdr
[HDR_NPLANES
];
126 bitsperpixel
= hdr
[HDR_BITSPERPIXEL
];
127 bytesperline
= hdr
[HDR_BYTESPERLINE
] + 256 * hdr
[HDR_BYTESPERLINE
+ 1];
128 width
= (hdr
[HDR_XMAX
] + 256 * hdr
[HDR_XMAX
+ 1]) -
129 (hdr
[HDR_XMIN
] + 256 * hdr
[HDR_XMIN
+ 1]) + 1;
130 height
= (hdr
[HDR_YMAX
] + 256 * hdr
[HDR_YMAX
+ 1]) -
131 (hdr
[HDR_YMIN
] + 256 * hdr
[HDR_YMIN
+ 1]) + 1;
133 // Check image format. Currently supported formats are
134 // 8 bits (8 bpp, 1 plane) and 24 bits (8 bpp, 3 planes).
136 if ((nplanes
== 3) && (bitsperpixel
== 8))
137 format
= IMAGE_24BIT
;
138 else if ((nplanes
== 1) && (bitsperpixel
== 8))
143 // If the image is of type IMAGE_8BIT, then there is a
144 // palette at the end of the file. Read it now before
147 if (format
== IMAGE_8BIT
)
149 pos
= stream
.TellI();
150 stream
.SeekI(-769, wxFromEnd
);
152 if (stream
.GetC() != 12)
155 stream
.Read(pal
, 768);
156 stream
.SeekI(pos
, wxFromStart
);
159 // Allocate memory for a scanline and resize the image.
161 image
->Create(width
, height
);
166 if ((p
= (unsigned char *) malloc(bytesperline
* nplanes
)) == NULL
)
169 // Now start reading the file, line by line, and store
170 // the data in the format required by wxImage.
172 dst
= image
->GetData();
174 for (; height
; height
--)
177 RLEdecode(p
, bytesperline
* nplanes
, stream
);
179 stream
.Read(p
, bytesperline
* nplanes
);
185 for (i
= 0; i
< width
; i
++)
187 *(dst
++) = pal
[ 3 * (p
[i
]) ];
188 *(dst
++) = pal
[ 3 * (p
[i
]) + 1];
189 *(dst
++) = pal
[ 3 * (p
[i
]) + 2];
195 for (i
= 0; i
< width
; i
++)
198 *(dst
++) = p
[i
+ bytesperline
];
199 *(dst
++) = p
[i
+ 2 * bytesperline
];
212 //-----------------------------------------------------------------------------
214 //-----------------------------------------------------------------------------
216 #if !USE_SHARED_LIBRARIES
217 IMPLEMENT_DYNAMIC_CLASS(wxPCXHandler
,wxImageHandler
)
222 bool wxPCXHandler::LoadFile( wxImage
*image
, wxInputStream
& stream
, bool verbose
)
226 if (!CanRead(stream
))
229 wxLogError(_T("wxPCXHandler: this is not a PCX file"));
236 if ((error
= ReadPCX(image
, stream
)) != E_OK
)
242 case E_FORMATO
: wxLogError(_T("wxPCXHandler: image format unsupported")); break;
243 case E_MEMORIA
: wxLogError(_T("wxPCXHandler: couldn't allocate memory")); break;
244 case E_VERSION
: wxLogError(_T("wxPCXHandler: version number too low")); break;
245 default: wxLogError(_T("wxPCXHandler: unknown error !!!"));
255 bool wxPCXHandler::SaveFile( wxImage
*image
, wxOutputStream
& stream
, bool verbose
)
258 wxLogError(_T("wxPCXHandler::SaveFile still not implemented"));
263 bool wxPCXHandler::CanRead( wxInputStream
& stream
)
268 pos
= stream
.TellI();
269 stream
.SeekI(0, wxFromStart
);
271 stream
.SeekI(pos
, wxFromStart
);
273 // not very safe, but this is all we can get from PCX header :-(
277 #endif // wxUSE_STREAMS