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