]> git.saurik.com Git - wxWidgets.git/blame - include/wx/image.h
wxXML load/save improvements: added ability to not ignore whitespace and specify...
[wxWidgets.git] / include / wx / image.h
CommitLineData
01111366 1/////////////////////////////////////////////////////////////////////////////
155ecd4c 2// Name: wx/image.h
01111366
RR
3// Purpose: wxImage class
4// Author: Robert Roebling
5// RCS-ID: $Id$
6// Copyright: (c) Robert Roebling
65571936 7// Licence: wxWindows licence
01111366
RR
8/////////////////////////////////////////////////////////////////////////////
9
10#ifndef _WX_IMAGE_H_
11#define _WX_IMAGE_H_
12
2ecf902b 13#include "wx/defs.h"
155ecd4c
WS
14
15#if wxUSE_IMAGE
16
01111366
RR
17#include "wx/object.h"
18#include "wx/string.h"
19#include "wx/gdicmn.h"
952ae1e8 20#include "wx/hashmap.h"
bf38cbff
JS
21
22#if wxUSE_STREAMS
b9943f8e 23# include "wx/stream.h"
bf38cbff 24#endif
01111366 25
66f21994
VZ
26// on some systems (Unixware 7.x) index is defined as a macro in the headers
27// which breaks the compilation below
28#undef index
29
1fe7a7c7 30#define wxIMAGE_OPTION_QUALITY wxString(_T("quality"))
fd94e8aa
VS
31#define wxIMAGE_OPTION_FILENAME wxString(_T("FileName"))
32
fe9308c6
VZ
33#define wxIMAGE_OPTION_RESOLUTION wxString(_T("Resolution"))
34#define wxIMAGE_OPTION_RESOLUTIONX wxString(_T("ResolutionX"))
35#define wxIMAGE_OPTION_RESOLUTIONY wxString(_T("ResolutionY"))
36
37#define wxIMAGE_OPTION_RESOLUTIONUNIT wxString(_T("ResolutionUnit"))
38
39// constants used with wxIMAGE_OPTION_RESOLUTIONUNIT
40enum
41{
42 wxIMAGE_RESOLUTION_INCHES = 1,
43 wxIMAGE_RESOLUTION_CM = 2
44};
45
21dc4be5
VZ
46// alpha channel values: fully transparent, default threshold separating
47// transparent pixels from opaque for a few functions dealing with alpha and
48// fully opaque
49const unsigned char wxIMAGE_ALPHA_TRANSPARENT = 0;
50const unsigned char wxIMAGE_ALPHA_THRESHOLD = 0x80;
51const unsigned char wxIMAGE_ALPHA_OPAQUE = 0xff;
52
01111366
RR
53//-----------------------------------------------------------------------------
54// classes
55//-----------------------------------------------------------------------------
56
57class WXDLLEXPORT wxImageHandler;
01111366 58class WXDLLEXPORT wxImage;
ed39ff57 59class WXDLLEXPORT wxPalette;
01111366
RR
60
61//-----------------------------------------------------------------------------
62// wxImageHandler
63//-----------------------------------------------------------------------------
64
65class WXDLLEXPORT wxImageHandler: public wxObject
66{
01111366 67public:
d84afea9 68 wxImageHandler()
fda7962d 69 : m_name(wxEmptyString), m_extension(wxEmptyString), m_mime(), m_type(0)
d84afea9 70 { }
01111366 71
bf38cbff 72#if wxUSE_STREAMS
7beb59f3
WS
73 virtual bool LoadFile( wxImage *image, wxInputStream& stream, bool verbose=true, int index=-1 );
74 virtual bool SaveFile( wxImage *image, wxOutputStream& stream, bool verbose=true );
8f177c8e 75
649d13e8 76 virtual int GetImageCount( wxInputStream& stream );
0828c087 77
39d16996 78 bool CanRead( wxInputStream& stream ) { return CallDoCanRead(stream); }
be25e480 79 bool CanRead( const wxString& name );
8f177c8e 80#endif // wxUSE_STREAMS
01111366 81
be25e480
RR
82 void SetName(const wxString& name) { m_name = name; }
83 void SetExtension(const wxString& ext) { m_extension = ext; }
84 void SetType(long type) { m_type = type; }
85 void SetMimeType(const wxString& type) { m_mime = type; }
86 wxString GetName() const { return m_name; }
87 wxString GetExtension() const { return m_extension; }
88 long GetType() const { return m_type; }
89 wxString GetMimeType() const { return m_mime; }
9e9ee68e 90
01111366 91protected:
8f177c8e 92#if wxUSE_STREAMS
be25e480 93 virtual bool DoCanRead( wxInputStream& stream ) = 0;
995612e2 94
39d16996
VZ
95 // save the stream position, call DoCanRead() and restore the position
96 bool CallDoCanRead(wxInputStream& stream);
55472691 97#endif // wxUSE_STREAMS
39d16996 98
be25e480
RR
99 wxString m_name;
100 wxString m_extension;
101 wxString m_mime;
102 long m_type;
aaf46fd6 103
be25e480
RR
104private:
105 DECLARE_CLASS(wxImageHandler)
8f177c8e
VZ
106};
107
01111366 108//-----------------------------------------------------------------------------
952ae1e8 109// wxImageHistogram
01111366
RR
110//-----------------------------------------------------------------------------
111
952ae1e8 112class WXDLLEXPORT wxImageHistogramEntry
c9d01afd
GRG
113{
114public:
66f21994 115 wxImageHistogramEntry() { index = value = 0; }
c9d01afd
GRG
116 unsigned long index;
117 unsigned long value;
118};
119
952ae1e8
VS
120WX_DECLARE_EXPORTED_HASH_MAP(unsigned long, wxImageHistogramEntry,
121 wxIntegerHash, wxIntegerEqual,
3f5c62f9 122 wxImageHistogramBase);
487659e0 123
bf78c81c 124class WXDLLEXPORT wxImageHistogram : public wxImageHistogramBase
487659e0
VZ
125{
126public:
127 wxImageHistogram() : wxImageHistogramBase(256) { }
128
129 // get the key in the histogram for the given RGB values
130 static unsigned long MakeKey(unsigned char r,
131 unsigned char g,
132 unsigned char b)
133 {
134 return (r << 16) | (g << 8) | b;
135 }
136
137 // find first colour that is not used in the image and has higher
138 // RGB values than RGB(startR, startG, startB)
139 //
140 // returns true and puts this colour in r, g, b (each of which may be NULL)
141 // on success or returns false if there are no more free colours
142 bool FindFirstUnusedColour(unsigned char *r,
143 unsigned char *g,
144 unsigned char *b,
145 unsigned char startR = 1,
146 unsigned char startG = 0,
147 unsigned char startB = 0 ) const;
148};
952ae1e8
VS
149
150//-----------------------------------------------------------------------------
151// wxImage
152//-----------------------------------------------------------------------------
153
01111366
RR
154class WXDLLEXPORT wxImage: public wxObject
155{
01111366 156public:
978d3d36
VZ
157 // red, green and blue are 8 bit unsigned integers in the range of 0..255
158 // We use the identifier RGBValue instead of RGB, since RGB is #defined
159 class RGBValue
160 {
161 public:
162 RGBValue(unsigned char r=0, unsigned char g=0, unsigned char b=0)
163 : red(r), green(g), blue(b) {}
ec85956a 164 unsigned char red;
978d3d36
VZ
165 unsigned char green;
166 unsigned char blue;
167 };
168
169 // hue, saturation and value are doubles in the range 0.0..1.0
170 class HSVValue
171 {
172 public:
173 HSVValue(double h=0.0, double s=0.0, double v=0.0)
174 : hue(h), saturation(s), value(v) {}
ec85956a 175 double hue;
978d3d36
VZ
176 double saturation;
177 double value;
178 };
978d3d36 179
1b941f2d 180 wxImage(){}
ff865c13 181 wxImage( int width, int height, bool clear = true );
7beb59f3 182 wxImage( int width, int height, unsigned char* data, bool static_data = false );
4ea56379 183 wxImage( int width, int height, unsigned char* data, unsigned char* alpha, bool static_data = false );
60d43ad8
VS
184 wxImage( const wxString& name, long type = wxBITMAP_TYPE_ANY, int index = -1 );
185 wxImage( const wxString& name, const wxString& mimetype, int index = -1 );
cad61c3e
JS
186 wxImage( const char** xpmData );
187 wxImage( char** xpmData );
aaf46fd6
VZ
188
189#if wxUSE_STREAMS
60d43ad8
VS
190 wxImage( wxInputStream& stream, long type = wxBITMAP_TYPE_ANY, int index = -1 );
191 wxImage( wxInputStream& stream, const wxString& mimetype, int index = -1 );
aaf46fd6 192#endif // wxUSE_STREAMS
01111366 193
aaa97828 194 bool Create( int width, int height, bool clear = true );
7beb59f3 195 bool Create( int width, int height, unsigned char* data, bool static_data = false );
4ea56379 196 bool Create( int width, int height, unsigned char* data, unsigned char* alpha, bool static_data = false );
cad61c3e 197 bool Create( const char** xpmData );
be25e480 198 void Destroy();
01111366 199
f6bcfd97
BP
200 // creates an identical copy of the image (the = operator
201 // just raises the ref count)
202 wxImage Copy() const;
aaf46fd6 203
be25e480 204 // return the new image with size width*height
b737ad10
RR
205 wxImage GetSubImage( const wxRect& rect) const;
206
207 // Paste the image or part of this image into an image of the given size at the pos
208 // any newly exposed areas will be filled with the rgb colour
2ecf902b 209 // by default if r = g = b = -1 then fill with this image's mask colour or find and
b737ad10 210 // set a suitable mask colour
2ecf902b 211 wxImage Size( const wxSize& size, const wxPoint& pos,
b737ad10 212 int r = -1, int g = -1, int b = -1 ) const;
aaf46fd6 213
f6bcfd97
BP
214 // pastes image into this instance and takes care of
215 // the mask colour and out of bounds problems
aaf46fd6 216 void Paste( const wxImage &image, int x, int y );
23280650 217
be25e480
RR
218 // return the new image with size width*height
219 wxImage Scale( int width, int height ) const;
bf78c81c 220
534f0312 221 wxImage ShrinkBy( int xFactor , int yFactor ) const ;
7b2471a0 222
be25e480
RR
223 // rescales the image in place
224 wxImage& Rescale( int width, int height ) { return *this = Scale(width, height); }
ce9a75d2 225
b737ad10 226 // resizes the image in place
2ecf902b 227 wxImage& Resize( const wxSize& size, const wxPoint& pos,
b737ad10
RR
228 int r = -1, int g = -1, int b = -1 ) { return *this = Size(size, pos, r, g, b); }
229
7a632f10
JS
230 // Rotates the image about the given point, 'angle' radians.
231 // Returns the rotated image, leaving this image intact.
232 wxImage Rotate(double angle, const wxPoint & centre_of_rotation,
7beb59f3 233 bool interpolating = true, wxPoint * offset_after_rotation = (wxPoint*) NULL) const;
f6bcfd97 234
7beb59f3
WS
235 wxImage Rotate90( bool clockwise = true ) const;
236 wxImage Mirror( bool horizontally = true ) const;
7a632f10 237
be25e480
RR
238 // replace one colour with another
239 void Replace( unsigned char r1, unsigned char g1, unsigned char b1,
240 unsigned char r2, unsigned char g2, unsigned char b2 );
aaf46fd6 241
ec85956a
JS
242 // Convert to greyscale image. Uses the luminance component (Y) of the image.
243 // The luma value (YUV) is calculated using (R * lr) + (G * lg) + (B * lb), defaults to ITU-T BT.601
244 wxImage ConvertToGreyscale( double lr = 0.299, double lg = 0.587, double lb = 0.114 ) const;
245
ff5ad794
VS
246 // convert to monochrome image (<r,g,b> will be replaced by white,
247 // everything else by black)
f515c25a 248 wxImage ConvertToMono( unsigned char r, unsigned char g, unsigned char b ) const;
ce9a75d2 249
be25e480
RR
250 // these routines are slow but safe
251 void SetRGB( int x, int y, unsigned char r, unsigned char g, unsigned char b );
b737ad10 252 void SetRGB( const wxRect& rect, unsigned char r, unsigned char g, unsigned char b );
f6bcfd97
BP
253 unsigned char GetRed( int x, int y ) const;
254 unsigned char GetGreen( int x, int y ) const;
255 unsigned char GetBlue( int x, int y ) const;
23280650 256
487659e0 257 void SetAlpha(int x, int y, unsigned char alpha);
d30ee785 258 unsigned char GetAlpha(int x, int y) const;
487659e0 259
1f5b2017
VS
260 // find first colour that is not used in the image and has higher
261 // RGB values than <startR,startG,startB>
262 bool FindFirstUnusedColour( unsigned char *r, unsigned char *g, unsigned char *b,
aaf46fd6 263 unsigned char startR = 1, unsigned char startG = 0,
e0a76d8d 264 unsigned char startB = 0 ) const;
1f5b2017 265 // Set image's mask to the area of 'mask' that has <r,g,b> colour
aaf46fd6 266 bool SetMaskFromImage(const wxImage & mask,
1f5b2017 267 unsigned char mr, unsigned char mg, unsigned char mb);
52b64b0a 268
ff5ad794
VS
269 // converts image's alpha channel to mask, if it has any, does nothing
270 // otherwise:
21dc4be5 271 bool ConvertAlphaToMask(unsigned char threshold = wxIMAGE_ALPHA_THRESHOLD);
ff5ad794 272
6408deed
RR
273 // This method converts an image where the original alpha
274 // information is only available as a shades of a colour
275 // (actually shades of grey) typically when you draw anti-
276 // aliased text into a bitmap. The DC drawinf routines
277 // draw grey values on the black background although they
278 // actually mean to draw white with differnt alpha values.
279 // This method reverses it, assuming a black (!) background
16cba29d 280 // and white text (actually only the red channel is read).
6408deed
RR
281 // The method will then fill up the whole image with the
282 // colour given.
16cba29d 283 bool ConvertColourToAlpha( unsigned char r, unsigned char g, unsigned char b );
6408deed 284
be25e480 285 static bool CanRead( const wxString& name );
649d13e8 286 static int GetImageCount( const wxString& name, long type = wxBITMAP_TYPE_ANY );
60d43ad8
VS
287 virtual bool LoadFile( const wxString& name, long type = wxBITMAP_TYPE_ANY, int index = -1 );
288 virtual bool LoadFile( const wxString& name, const wxString& mimetype, int index = -1 );
bf38cbff
JS
289
290#if wxUSE_STREAMS
be25e480 291 static bool CanRead( wxInputStream& stream );
649d13e8 292 static int GetImageCount( wxInputStream& stream, long type = wxBITMAP_TYPE_ANY );
60d43ad8
VS
293 virtual bool LoadFile( wxInputStream& stream, long type = wxBITMAP_TYPE_ANY, int index = -1 );
294 virtual bool LoadFile( wxInputStream& stream, const wxString& mimetype, int index = -1 );
bf38cbff
JS
295#endif
296
45647dcf 297 virtual bool SaveFile( const wxString& name ) const;
e0a76d8d
VS
298 virtual bool SaveFile( const wxString& name, int type ) const;
299 virtual bool SaveFile( const wxString& name, const wxString& mimetype ) const;
bf38cbff
JS
300
301#if wxUSE_STREAMS
e0a76d8d
VS
302 virtual bool SaveFile( wxOutputStream& stream, int type ) const;
303 virtual bool SaveFile( wxOutputStream& stream, const wxString& mimetype ) const;
bf38cbff 304#endif
01111366 305
be25e480
RR
306 bool Ok() const;
307 int GetWidth() const;
308 int GetHeight() const;
309
487659e0
VZ
310 // these functions provide fastest access to wxImage data but should be
311 // used carefully as no checks are done
312 unsigned char *GetData() const;
4013de12
RD
313 void SetData( unsigned char *data, bool static_data=false );
314 void SetData( unsigned char *data, int new_width, int new_height, bool static_data=false );
487659e0
VZ
315
316 unsigned char *GetAlpha() const; // may return NULL!
317 bool HasAlpha() const { return GetAlpha() != NULL; }
4013de12 318 void SetAlpha(unsigned char *alpha = NULL, bool static_data=false);
828f0936 319 void InitAlpha();
5e5437e0 320
21dc4be5
VZ
321 // return true if this pixel is masked or has alpha less than specified
322 // threshold
323 bool IsTransparent(int x, int y,
324 unsigned char threshold = wxIMAGE_ALPHA_THRESHOLD) const;
325
5e5437e0 326 // Mask functions
be25e480 327 void SetMaskColour( unsigned char r, unsigned char g, unsigned char b );
b737ad10
RR
328 // Get the current mask colour or find a suitable colour
329 // returns true if using current mask colour
330 bool GetOrFindMaskColour( unsigned char *r, unsigned char *g, unsigned char *b ) const;
be25e480
RR
331 unsigned char GetMaskRed() const;
332 unsigned char GetMaskGreen() const;
333 unsigned char GetMaskBlue() const;
7beb59f3 334 void SetMask( bool mask = true );
be25e480
RR
335 bool HasMask() const;
336
d275c7eb 337#if wxUSE_PALETTE
5e5437e0
JS
338 // Palette functions
339 bool HasPalette() const;
340 const wxPalette& GetPalette() const;
341 void SetPalette(const wxPalette& palette);
d275c7eb 342#endif // wxUSE_PALETTE
5e5437e0
JS
343
344 // Option functions (arbitrary name/value mapping)
345 void SetOption(const wxString& name, const wxString& value);
346 void SetOption(const wxString& name, int value);
347 wxString GetOption(const wxString& name) const;
348 int GetOptionInt(const wxString& name) const;
349 bool HasOption(const wxString& name) const;
3f4fc796 350
e0a76d8d 351 unsigned long CountColours( unsigned long stopafter = (unsigned long) -1 ) const;
952ae1e8
VS
352
353 // Computes the histogram of the image and fills a hash table, indexed
354 // with integer keys built as 0xRRGGBB, containing wxImageHistogramEntry
bf78c81c
RD
355 // objects. Each of them contains an 'index' (useful to build a palette
356 // with the image colours) and a 'value', which is the number of pixels
952ae1e8
VS
357 // in the image with that colour.
358 // Returned value: # of entries in the histogram
e0a76d8d 359 unsigned long ComputeHistogram( wxImageHistogram &h ) const;
be25e480 360
978d3d36
VZ
361 // Rotates the hue of each pixel of the image. angle is a double in the range
362 // -1.0..1.0 where -1.0 is -360 degrees and 1.0 is 360 degrees
363 void RotateHue(double angle);
978d3d36 364
2b5f62a0 365 bool operator == (const wxImage& image) const
be25e480 366 { return m_refData == image.m_refData; }
2b5f62a0 367 bool operator != (const wxImage& image) const
be25e480
RR
368 { return m_refData != image.m_refData; }
369
370 static wxList& GetHandlers() { return sm_handlers; }
371 static void AddHandler( wxImageHandler *handler );
372 static void InsertHandler( wxImageHandler *handler );
373 static bool RemoveHandler( const wxString& name );
374 static wxImageHandler *FindHandler( const wxString& name );
375 static wxImageHandler *FindHandler( const wxString& extension, long imageType );
376 static wxImageHandler *FindHandler( long imageType );
377 static wxImageHandler *FindHandlerMime( const wxString& mimetype );
378
939fadc8
JS
379 static wxString GetImageExtWildcard();
380
be25e480
RR
381 static void CleanUpHandlers();
382 static void InitStandardHandlers();
c9d01afd 383
978d3d36
VZ
384 static HSVValue RGBtoHSV(const RGBValue& rgb);
385 static RGBValue HSVtoRGB(const HSVValue& hsv);
978d3d36
VZ
386
387
01111366 388protected:
5e5437e0 389 static wxList sm_handlers;
01111366 390
5644ac46
VZ
391 // return the index of the point with the given coordinates or -1 if the
392 // image is invalid of the coordinates are out of range
393 //
394 // note that index must be multiplied by 3 when using it with RGB array
395 long XYToIndex(int x, int y) const;
396
a0f81e9f
PC
397 virtual wxObjectRefData* CreateRefData() const;
398 virtual wxObjectRefData* CloneRefData(const wxObjectRefData* data) const;
399
be25e480
RR
400private:
401 friend class WXDLLEXPORT wxImageHandler;
23280650 402
be25e480 403 DECLARE_DYNAMIC_CLASS(wxImage)
01111366
RR
404};
405
8f493002 406
a091d18c 407extern void WXDLLEXPORT wxInitAllImageHandlers();
b5a4a47d 408
16cba29d 409extern WXDLLEXPORT_DATA(wxImage) wxNullImage;
8f493002
VS
410
411//-----------------------------------------------------------------------------
412// wxImage handlers
413//-----------------------------------------------------------------------------
414
415#include "wx/imagbmp.h"
416#include "wx/imagpng.h"
417#include "wx/imaggif.h"
418#include "wx/imagpcx.h"
419#include "wx/imagjpeg.h"
420#include "wx/imagtiff.h"
421#include "wx/imagpnm.h"
775c6f0c 422#include "wx/imagxpm.h"
4b6b4dfc 423#include "wx/imagiff.h"
775c6f0c
VS
424
425#endif // wxUSE_IMAGE
8f493002 426
01111366
RR
427#endif
428 // _WX_IMAGE_H_