]> git.saurik.com Git - wxWidgets.git/blob - include/wx/image.h
Added Set/GetQuickBestSize
[wxWidgets.git] / include / wx / image.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: image.h
3 // Purpose: wxImage class
4 // Author: Robert Roebling
5 // RCS-ID: $Id$
6 // Copyright: (c) Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #ifndef _WX_IMAGE_H_
11 #define _WX_IMAGE_H_
12
13 #include "wx/defs.h"
14 #include "wx/object.h"
15 #include "wx/string.h"
16 #include "wx/gdicmn.h"
17 #include "wx/hashmap.h"
18
19 #if wxUSE_STREAMS
20 # include "wx/stream.h"
21 #endif
22
23 #if wxUSE_IMAGE
24
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
29 #define wxIMAGE_OPTION_QUALITY wxString(_T("quality"))
30 #define wxIMAGE_OPTION_FILENAME wxString(_T("FileName"))
31
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
39 enum
40 {
41 wxIMAGE_RESOLUTION_INCHES = 1,
42 wxIMAGE_RESOLUTION_CM = 2
43 };
44
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
48 const unsigned char wxIMAGE_ALPHA_TRANSPARENT = 0;
49 const unsigned char wxIMAGE_ALPHA_THRESHOLD = 0x80;
50 const unsigned char wxIMAGE_ALPHA_OPAQUE = 0xff;
51
52 //-----------------------------------------------------------------------------
53 // classes
54 //-----------------------------------------------------------------------------
55
56 class WXDLLEXPORT wxImageHandler;
57 class WXDLLEXPORT wxImage;
58 class WXDLLEXPORT wxPalette;
59
60 //-----------------------------------------------------------------------------
61 // wxImageHandler
62 //-----------------------------------------------------------------------------
63
64 class WXDLLEXPORT wxImageHandler: public wxObject
65 {
66 public:
67 wxImageHandler()
68 : m_name(wxEmptyString), m_extension(wxEmptyString), m_mime(), m_type(0)
69 { }
70
71 #if wxUSE_STREAMS
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 );
74
75 virtual int GetImageCount( wxInputStream& stream );
76
77 bool CanRead( wxInputStream& stream ) { return CallDoCanRead(stream); }
78 bool CanRead( const wxString& name );
79 #endif // wxUSE_STREAMS
80
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; }
89
90 protected:
91 #if wxUSE_STREAMS
92 virtual bool DoCanRead( wxInputStream& stream ) = 0;
93
94 // save the stream position, call DoCanRead() and restore the position
95 bool CallDoCanRead(wxInputStream& stream);
96 #endif // wxUSE_STREAMS
97
98 wxString m_name;
99 wxString m_extension;
100 wxString m_mime;
101 long m_type;
102
103 private:
104 DECLARE_CLASS(wxImageHandler)
105 };
106
107 //-----------------------------------------------------------------------------
108 // wxImageHistogram
109 //-----------------------------------------------------------------------------
110
111 class WXDLLEXPORT wxImageHistogramEntry
112 {
113 public:
114 wxImageHistogramEntry() { index = value = 0; }
115 unsigned long index;
116 unsigned long value;
117 };
118
119 WX_DECLARE_EXPORTED_HASH_MAP(unsigned long, wxImageHistogramEntry,
120 wxIntegerHash, wxIntegerEqual,
121 wxImageHistogramBase);
122
123 class WXDLLEXPORT wxImageHistogram : public wxImageHistogramBase
124 {
125 public:
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 };
148
149 //-----------------------------------------------------------------------------
150 // wxImage
151 //-----------------------------------------------------------------------------
152
153 class WXDLLEXPORT wxImage: public wxObject
154 {
155 public:
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) {}
163 unsigned char red;
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) {}
174 double hue;
175 double saturation;
176 double value;
177 };
178
179 wxImage(){}
180 wxImage( int width, int height, bool clear = true );
181 wxImage( int width, int height, unsigned char* data, bool static_data = false );
182 wxImage( int width, int height, unsigned char* data, unsigned char* alpha, bool static_data = false );
183 wxImage( const wxString& name, long type = wxBITMAP_TYPE_ANY, int index = -1 );
184 wxImage( const wxString& name, const wxString& mimetype, int index = -1 );
185 wxImage( const char** xpmData );
186 wxImage( char** xpmData );
187
188 #if wxUSE_STREAMS
189 wxImage( wxInputStream& stream, long type = wxBITMAP_TYPE_ANY, int index = -1 );
190 wxImage( wxInputStream& stream, const wxString& mimetype, int index = -1 );
191 #endif // wxUSE_STREAMS
192
193 bool Create( int width, int height, bool clear = true );
194 bool Create( int width, int height, unsigned char* data, bool static_data = false );
195 bool Create( int width, int height, unsigned char* data, unsigned char* alpha, bool static_data = false );
196 bool Create( const char** xpmData );
197 void Destroy();
198
199 // creates an identical copy of the image (the = operator
200 // just raises the ref count)
201 wxImage Copy() const;
202
203 // return the new image with size width*height
204 wxImage GetSubImage( const wxRect& rect) const;
205
206 // Paste the image or part of this image into an image of the given size at the pos
207 // any newly exposed areas will be filled with the rgb colour
208 // by default if r = g = b = -1 then fill with this image's mask colour or find and
209 // set a suitable mask colour
210 wxImage Size( const wxSize& size, const wxPoint& pos,
211 int r = -1, int g = -1, int b = -1 ) const;
212
213 // pastes image into this instance and takes care of
214 // the mask colour and out of bounds problems
215 void Paste( const wxImage &image, int x, int y );
216
217 // return the new image with size width*height
218 wxImage Scale( int width, int height ) const;
219
220 wxImage ShrinkBy( int xFactor , int yFactor ) const ;
221
222 // rescales the image in place
223 wxImage& Rescale( int width, int height ) { return *this = Scale(width, height); }
224
225 // resizes the image in place
226 wxImage& Resize( const wxSize& size, const wxPoint& pos,
227 int r = -1, int g = -1, int b = -1 ) { return *this = Size(size, pos, r, g, b); }
228
229 // Rotates the image about the given point, 'angle' radians.
230 // Returns the rotated image, leaving this image intact.
231 wxImage Rotate(double angle, const wxPoint & centre_of_rotation,
232 bool interpolating = true, wxPoint * offset_after_rotation = (wxPoint*) NULL) const;
233
234 wxImage Rotate90( bool clockwise = true ) const;
235 wxImage Mirror( bool horizontally = true ) const;
236
237 // replace one colour with another
238 void Replace( unsigned char r1, unsigned char g1, unsigned char b1,
239 unsigned char r2, unsigned char g2, unsigned char b2 );
240
241 // Convert to greyscale image. Uses the luminance component (Y) of the image.
242 // The luma value (YUV) is calculated using (R * lr) + (G * lg) + (B * lb), defaults to ITU-T BT.601
243 wxImage ConvertToGreyscale( double lr = 0.299, double lg = 0.587, double lb = 0.114 ) const;
244
245 // convert to monochrome image (<r,g,b> will be replaced by white,
246 // everything else by black)
247 wxImage ConvertToMono( unsigned char r, unsigned char g, unsigned char b ) const;
248
249 // these routines are slow but safe
250 void SetRGB( int x, int y, unsigned char r, unsigned char g, unsigned char b );
251 void SetRGB( const wxRect& rect, unsigned char r, unsigned char g, unsigned char b );
252 unsigned char GetRed( int x, int y ) const;
253 unsigned char GetGreen( int x, int y ) const;
254 unsigned char GetBlue( int x, int y ) const;
255
256 void SetAlpha(int x, int y, unsigned char alpha);
257 unsigned char GetAlpha(int x, int y) const;
258
259 // find first colour that is not used in the image and has higher
260 // RGB values than <startR,startG,startB>
261 bool FindFirstUnusedColour( unsigned char *r, unsigned char *g, unsigned char *b,
262 unsigned char startR = 1, unsigned char startG = 0,
263 unsigned char startB = 0 ) const;
264 // Set image's mask to the area of 'mask' that has <r,g,b> colour
265 bool SetMaskFromImage(const wxImage & mask,
266 unsigned char mr, unsigned char mg, unsigned char mb);
267
268 // converts image's alpha channel to mask, if it has any, does nothing
269 // otherwise:
270 bool ConvertAlphaToMask(unsigned char threshold = wxIMAGE_ALPHA_THRESHOLD);
271
272 // This method converts an image where the original alpha
273 // information is only available as a shades of a colour
274 // (actually shades of grey) typically when you draw anti-
275 // aliased text into a bitmap. The DC drawinf routines
276 // draw grey values on the black background although they
277 // actually mean to draw white with differnt alpha values.
278 // This method reverses it, assuming a black (!) background
279 // and white text (actually only the red channel is read).
280 // The method will then fill up the whole image with the
281 // colour given.
282 bool ConvertColourToAlpha( unsigned char r, unsigned char g, unsigned char b );
283
284 static bool CanRead( const wxString& name );
285 static int GetImageCount( const wxString& name, long type = wxBITMAP_TYPE_ANY );
286 virtual bool LoadFile( const wxString& name, long type = wxBITMAP_TYPE_ANY, int index = -1 );
287 virtual bool LoadFile( const wxString& name, const wxString& mimetype, int index = -1 );
288
289 #if wxUSE_STREAMS
290 static bool CanRead( wxInputStream& stream );
291 static int GetImageCount( wxInputStream& stream, long type = wxBITMAP_TYPE_ANY );
292 virtual bool LoadFile( wxInputStream& stream, long type = wxBITMAP_TYPE_ANY, int index = -1 );
293 virtual bool LoadFile( wxInputStream& stream, const wxString& mimetype, int index = -1 );
294 #endif
295
296 virtual bool SaveFile( const wxString& name ) const;
297 virtual bool SaveFile( const wxString& name, int type ) const;
298 virtual bool SaveFile( const wxString& name, const wxString& mimetype ) const;
299
300 #if wxUSE_STREAMS
301 virtual bool SaveFile( wxOutputStream& stream, int type ) const;
302 virtual bool SaveFile( wxOutputStream& stream, const wxString& mimetype ) const;
303 #endif
304
305 bool Ok() const;
306 int GetWidth() const;
307 int GetHeight() const;
308
309 // these functions provide fastest access to wxImage data but should be
310 // used carefully as no checks are done
311 unsigned char *GetData() const;
312 void SetData( unsigned char *data, bool static_data=false );
313 void SetData( unsigned char *data, int new_width, int new_height, bool static_data=false );
314
315 unsigned char *GetAlpha() const; // may return NULL!
316 bool HasAlpha() const { return GetAlpha() != NULL; }
317 void SetAlpha(unsigned char *alpha = NULL, bool static_data=false);
318 void InitAlpha();
319
320 // return true if this pixel is masked or has alpha less than specified
321 // threshold
322 bool IsTransparent(int x, int y,
323 unsigned char threshold = wxIMAGE_ALPHA_THRESHOLD) const;
324
325 // Mask functions
326 void SetMaskColour( unsigned char r, unsigned char g, unsigned char b );
327 // Get the current mask colour or find a suitable colour
328 // returns true if using current mask colour
329 bool GetOrFindMaskColour( unsigned char *r, unsigned char *g, unsigned char *b ) const;
330 unsigned char GetMaskRed() const;
331 unsigned char GetMaskGreen() const;
332 unsigned char GetMaskBlue() const;
333 void SetMask( bool mask = true );
334 bool HasMask() const;
335
336 #if wxUSE_PALETTE
337 // Palette functions
338 bool HasPalette() const;
339 const wxPalette& GetPalette() const;
340 void SetPalette(const wxPalette& palette);
341 #endif // wxUSE_PALETTE
342
343 // Option functions (arbitrary name/value mapping)
344 void SetOption(const wxString& name, const wxString& value);
345 void SetOption(const wxString& name, int value);
346 wxString GetOption(const wxString& name) const;
347 int GetOptionInt(const wxString& name) const;
348 bool HasOption(const wxString& name) const;
349
350 unsigned long CountColours( unsigned long stopafter = (unsigned long) -1 ) const;
351
352 // Computes the histogram of the image and fills a hash table, indexed
353 // with integer keys built as 0xRRGGBB, containing wxImageHistogramEntry
354 // objects. Each of them contains an 'index' (useful to build a palette
355 // with the image colours) and a 'value', which is the number of pixels
356 // in the image with that colour.
357 // Returned value: # of entries in the histogram
358 unsigned long ComputeHistogram( wxImageHistogram &h ) const;
359
360 // Rotates the hue of each pixel of the image. angle is a double in the range
361 // -1.0..1.0 where -1.0 is -360 degrees and 1.0 is 360 degrees
362 void RotateHue(double angle);
363
364 bool operator == (const wxImage& image) const
365 { return m_refData == image.m_refData; }
366 bool operator != (const wxImage& image) const
367 { return m_refData != image.m_refData; }
368
369 static wxList& GetHandlers() { return sm_handlers; }
370 static void AddHandler( wxImageHandler *handler );
371 static void InsertHandler( wxImageHandler *handler );
372 static bool RemoveHandler( const wxString& name );
373 static wxImageHandler *FindHandler( const wxString& name );
374 static wxImageHandler *FindHandler( const wxString& extension, long imageType );
375 static wxImageHandler *FindHandler( long imageType );
376 static wxImageHandler *FindHandlerMime( const wxString& mimetype );
377
378 static wxString GetImageExtWildcard();
379
380 static void CleanUpHandlers();
381 static void InitStandardHandlers();
382
383 static HSVValue RGBtoHSV(const RGBValue& rgb);
384 static RGBValue HSVtoRGB(const HSVValue& hsv);
385
386
387 protected:
388 static wxList sm_handlers;
389
390 // return the index of the point with the given coordinates or -1 if the
391 // image is invalid of the coordinates are out of range
392 //
393 // note that index must be multiplied by 3 when using it with RGB array
394 long XYToIndex(int x, int y) const;
395
396 private:
397 friend class WXDLLEXPORT wxImageHandler;
398
399 DECLARE_DYNAMIC_CLASS(wxImage)
400 };
401
402
403 extern void WXDLLEXPORT wxInitAllImageHandlers();
404
405 extern WXDLLEXPORT_DATA(wxImage) wxNullImage;
406
407 //-----------------------------------------------------------------------------
408 // wxImage handlers
409 //-----------------------------------------------------------------------------
410
411 #include "wx/imagbmp.h"
412 #include "wx/imagpng.h"
413 #include "wx/imaggif.h"
414 #include "wx/imagpcx.h"
415 #include "wx/imagjpeg.h"
416 #include "wx/imagtiff.h"
417 #include "wx/imagpnm.h"
418 #include "wx/imagxpm.h"
419 #include "wx/imagiff.h"
420
421 #endif // wxUSE_IMAGE
422
423 #endif
424 // _WX_IMAGE_H_
425