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