]> git.saurik.com Git - wxWidgets.git/blame - include/wx/image.h
more extra semicolons removed (patch 1303724)
[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,
487659e0
VZ
121 wxImageHistogramBase);
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#if wxABI_VERSION >= 20602
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) {}
164 unsigned char red;
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) {}
175 double hue;
176 double saturation;
177 double value;
178 };
179#endif // wxABI_VERSION >= 2.6.2
180
1b941f2d 181 wxImage(){}
ff865c13 182 wxImage( int width, int height, bool clear = true );
7beb59f3 183 wxImage( int width, int height, unsigned char* data, bool static_data = false );
4ea56379 184 wxImage( int width, int height, unsigned char* data, unsigned char* alpha, bool static_data = false );
60d43ad8
VS
185 wxImage( const wxString& name, long type = wxBITMAP_TYPE_ANY, int index = -1 );
186 wxImage( const wxString& name, const wxString& mimetype, int index = -1 );
cad61c3e
JS
187 wxImage( const char** xpmData );
188 wxImage( char** xpmData );
aaf46fd6
VZ
189
190#if wxUSE_STREAMS
60d43ad8
VS
191 wxImage( wxInputStream& stream, long type = wxBITMAP_TYPE_ANY, int index = -1 );
192 wxImage( wxInputStream& stream, const wxString& mimetype, int index = -1 );
aaf46fd6 193#endif // wxUSE_STREAMS
01111366 194
be25e480
RR
195 wxImage( const wxImage& image );
196 wxImage( const wxImage* image );
9e9ee68e 197
aaa97828 198 bool Create( int width, int height, bool clear = true );
7beb59f3 199 bool Create( int width, int height, unsigned char* data, bool static_data = false );
4ea56379 200 bool Create( int width, int height, unsigned char* data, unsigned char* alpha, bool static_data = false );
cad61c3e 201 bool Create( const char** xpmData );
be25e480 202 void Destroy();
01111366 203
f6bcfd97
BP
204 // creates an identical copy of the image (the = operator
205 // just raises the ref count)
206 wxImage Copy() const;
aaf46fd6 207
be25e480 208 // return the new image with size width*height
b737ad10
RR
209 wxImage GetSubImage( const wxRect& rect) const;
210
211 // Paste the image or part of this image into an image of the given size at the pos
212 // any newly exposed areas will be filled with the rgb colour
2ecf902b 213 // by default if r = g = b = -1 then fill with this image's mask colour or find and
b737ad10 214 // set a suitable mask colour
2ecf902b 215 wxImage Size( const wxSize& size, const wxPoint& pos,
b737ad10 216 int r = -1, int g = -1, int b = -1 ) const;
aaf46fd6 217
f6bcfd97
BP
218 // pastes image into this instance and takes care of
219 // the mask colour and out of bounds problems
aaf46fd6 220 void Paste( const wxImage &image, int x, int y );
23280650 221
be25e480
RR
222 // return the new image with size width*height
223 wxImage Scale( int width, int height ) const;
bf78c81c 224
534f0312 225 wxImage ShrinkBy( int xFactor , int yFactor ) const ;
7b2471a0 226
be25e480
RR
227 // rescales the image in place
228 wxImage& Rescale( int width, int height ) { return *this = Scale(width, height); }
ce9a75d2 229
b737ad10 230 // resizes the image in place
2ecf902b 231 wxImage& Resize( const wxSize& size, const wxPoint& pos,
b737ad10
RR
232 int r = -1, int g = -1, int b = -1 ) { return *this = Size(size, pos, r, g, b); }
233
7a632f10
JS
234 // Rotates the image about the given point, 'angle' radians.
235 // Returns the rotated image, leaving this image intact.
236 wxImage Rotate(double angle, const wxPoint & centre_of_rotation,
7beb59f3 237 bool interpolating = true, wxPoint * offset_after_rotation = (wxPoint*) NULL) const;
f6bcfd97 238
7beb59f3
WS
239 wxImage Rotate90( bool clockwise = true ) const;
240 wxImage Mirror( bool horizontally = true ) const;
7a632f10 241
be25e480
RR
242 // replace one colour with another
243 void Replace( unsigned char r1, unsigned char g1, unsigned char b1,
244 unsigned char r2, unsigned char g2, unsigned char b2 );
aaf46fd6 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#if wxABI_VERSION >= 20602
362 // Rotates the hue of each pixel of the image. angle is a double in the range
363 // -1.0..1.0 where -1.0 is -360 degrees and 1.0 is 360 degrees
364 void RotateHue(double angle);
365#endif // wxABI_VERSION >= 2.6.2
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#if wxABI_VERSION >= 20602
394 static HSVValue RGBtoHSV(const RGBValue& rgb);
395 static RGBValue HSVtoRGB(const HSVValue& hsv);
396#endif // wxABI_VERSION >= 2.6.2
397
398
01111366 399protected:
5e5437e0 400 static wxList sm_handlers;
01111366 401
5644ac46
VZ
402 // return the index of the point with the given coordinates or -1 if the
403 // image is invalid of the coordinates are out of range
404 //
405 // note that index must be multiplied by 3 when using it with RGB array
406 long XYToIndex(int x, int y) const;
407
be25e480
RR
408private:
409 friend class WXDLLEXPORT wxImageHandler;
23280650 410
be25e480 411 DECLARE_DYNAMIC_CLASS(wxImage)
01111366
RR
412};
413
8f493002 414
a091d18c 415extern void WXDLLEXPORT wxInitAllImageHandlers();
b5a4a47d 416
16cba29d 417extern WXDLLEXPORT_DATA(wxImage) wxNullImage;
8f493002
VS
418
419//-----------------------------------------------------------------------------
420// wxImage handlers
421//-----------------------------------------------------------------------------
422
423#include "wx/imagbmp.h"
424#include "wx/imagpng.h"
425#include "wx/imaggif.h"
426#include "wx/imagpcx.h"
427#include "wx/imagjpeg.h"
428#include "wx/imagtiff.h"
429#include "wx/imagpnm.h"
775c6f0c 430#include "wx/imagxpm.h"
4b6b4dfc 431#include "wx/imagiff.h"
775c6f0c
VS
432
433#endif // wxUSE_IMAGE
8f493002 434
01111366
RR
435#endif
436 // _WX_IMAGE_H_
9e9ee68e 437