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