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