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