1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxImage class
4 // Author: Robert Roebling
6 // Copyright: (c) Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
17 #include "wx/object.h"
18 #include "wx/string.h"
19 #include "wx/gdicmn.h"
20 #include "wx/hashmap.h"
23 # include "wx/stream.h"
26 // on some systems (Unixware 7.x) index is defined as a macro in the headers
27 // which breaks the compilation below
30 #define wxIMAGE_OPTION_QUALITY wxString(_T("quality"))
31 #define wxIMAGE_OPTION_FILENAME wxString(_T("FileName"))
33 #define wxIMAGE_OPTION_RESOLUTION wxString(_T("Resolution"))
34 #define wxIMAGE_OPTION_RESOLUTIONX wxString(_T("ResolutionX"))
35 #define wxIMAGE_OPTION_RESOLUTIONY wxString(_T("ResolutionY"))
37 #define wxIMAGE_OPTION_RESOLUTIONUNIT wxString(_T("ResolutionUnit"))
39 #define wxIMAGE_OPTION_MAX_WIDTH wxString(_T("MaxWidth"))
40 #define wxIMAGE_OPTION_MAX_HEIGHT wxString(_T("MaxHeight"))
42 // constants used with wxIMAGE_OPTION_RESOLUTIONUNIT
44 // NB: don't change these values, they correspond to libjpeg constants
45 enum wxImageResolution
47 // Resolution not specified
48 wxIMAGE_RESOLUTION_NONE
= 0,
50 // Resolution specified in inches
51 wxIMAGE_RESOLUTION_INCHES
= 1,
53 // Resolution specified in centimeters
54 wxIMAGE_RESOLUTION_CM
= 2
57 // Constants for wxImage::Scale() for determining the level of quality
60 wxIMAGE_QUALITY_NORMAL
= 0,
61 wxIMAGE_QUALITY_HIGH
= 1
64 // alpha channel values: fully transparent, default threshold separating
65 // transparent pixels from opaque for a few functions dealing with alpha and
67 const unsigned char wxIMAGE_ALPHA_TRANSPARENT
= 0;
68 const unsigned char wxIMAGE_ALPHA_THRESHOLD
= 0x80;
69 const unsigned char wxIMAGE_ALPHA_OPAQUE
= 0xff;
71 //-----------------------------------------------------------------------------
73 //-----------------------------------------------------------------------------
75 class WXDLLIMPEXP_FWD_CORE wxImageHandler
;
76 class WXDLLIMPEXP_FWD_CORE wxImage
;
77 class WXDLLIMPEXP_FWD_CORE wxPalette
;
79 //-----------------------------------------------------------------------------
81 //-----------------------------------------------------------------------------
84 #include "wx/variant.h"
85 DECLARE_VARIANT_OBJECT_EXPORTED(wxImage
,WXDLLIMPEXP_CORE
)
88 //-----------------------------------------------------------------------------
90 //-----------------------------------------------------------------------------
92 class WXDLLIMPEXP_CORE wxImageHandler
: public wxObject
96 : m_name(wxEmptyString
), m_extension(wxEmptyString
), m_mime(), m_type(wxBITMAP_TYPE_INVALID
)
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 );
103 virtual int GetImageCount( wxInputStream
& stream
);
105 bool CanRead( wxInputStream
& stream
) { return CallDoCanRead(stream
); }
106 bool CanRead( const wxString
& name
);
107 #endif // wxUSE_STREAMS
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
; }
118 #if WXWIN_COMPATIBILITY_2_8
120 void SetType(long type
) { SetType((wxBitmapType
)type
); }
122 #endif // WXWIN_COMPATIBILITY_2_8
126 virtual bool DoCanRead( wxInputStream
& stream
) = 0;
128 // save the stream position, call DoCanRead() and restore the position
129 bool CallDoCanRead(wxInputStream
& stream
);
130 #endif // wxUSE_STREAMS
132 // helper for the derived classes SaveFile() implementations: returns the
133 // values of x- and y-resolution options specified as the image options if
135 static wxImageResolution
136 GetResolutionFromOptions(const wxImage
& image
, int *x
, int *y
);
140 wxString m_extension
;
145 DECLARE_CLASS(wxImageHandler
)
148 //-----------------------------------------------------------------------------
150 //-----------------------------------------------------------------------------
152 class WXDLLIMPEXP_CORE wxImageHistogramEntry
155 wxImageHistogramEntry() { index
= value
= 0; }
160 WX_DECLARE_EXPORTED_HASH_MAP(unsigned long, wxImageHistogramEntry
,
161 wxIntegerHash
, wxIntegerEqual
,
162 wxImageHistogramBase
);
164 class WXDLLIMPEXP_CORE wxImageHistogram
: public wxImageHistogramBase
167 wxImageHistogram() : wxImageHistogramBase(256) { }
169 // get the key in the histogram for the given RGB values
170 static unsigned long MakeKey(unsigned char r
,
174 return (r
<< 16) | (g
<< 8) | b
;
177 // find first colour that is not used in the image and has higher
178 // RGB values than RGB(startR, startG, startB)
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
,
185 unsigned char startR
= 1,
186 unsigned char startG
= 0,
187 unsigned char startB
= 0 ) const;
190 //-----------------------------------------------------------------------------
192 //-----------------------------------------------------------------------------
194 class WXDLLIMPEXP_CORE wxImage
: public wxObject
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
202 RGBValue(unsigned char r
=0, unsigned char g
=0, unsigned char b
=0)
203 : red(r
), green(g
), blue(b
) {}
209 // hue, saturation and value are doubles in the range 0.0..1.0
213 HSVValue(double h
=0.0, double s
=0.0, double v
=0.0)
214 : hue(h
), saturation(s
), value(v
) {}
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
);
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
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
);
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
)); }
244 // initialize the image data with zeroes
245 void Clear(unsigned char value
= 0);
247 // creates an identical copy of the image (the = operator
248 // just raises the ref count)
249 wxImage
Copy() const;
251 // return the new image with size width*height
252 wxImage
GetSubImage( const wxRect
& rect
) const;
254 // Paste the image or part of this image into an image of the given size at the pos
255 // any newly exposed areas will be filled with the rgb colour
256 // by default if r = g = b = -1 then fill with this image's mask colour or find and
257 // set a suitable mask colour
258 wxImage
Size( const wxSize
& size
, const wxPoint
& pos
,
259 int r
= -1, int g
= -1, int b
= -1 ) const;
261 // pastes image into this instance and takes care of
262 // the mask colour and out of bounds problems
263 void Paste( const wxImage
&image
, int x
, int y
);
265 // return the new image with size width*height
266 wxImage
Scale( int width
, int height
, int quality
= wxIMAGE_QUALITY_NORMAL
) const;
268 // box averager and bicubic filters for up/down sampling
269 wxImage
ResampleBox(int width
, int height
) const;
270 wxImage
ResampleBicubic(int width
, int height
) const;
272 // blur the image according to the specified pixel radius
273 wxImage
Blur(int radius
) const;
274 wxImage
BlurHorizontal(int radius
) const;
275 wxImage
BlurVertical(int radius
) const;
277 wxImage
ShrinkBy( int xFactor
, int yFactor
) const ;
279 // rescales the image in place
280 wxImage
& Rescale( int width
, int height
, int quality
= wxIMAGE_QUALITY_NORMAL
) { return *this = Scale(width
, height
, quality
); }
282 // resizes the image in place
283 wxImage
& Resize( const wxSize
& size
, const wxPoint
& pos
,
284 int r
= -1, int g
= -1, int b
= -1 ) { return *this = Size(size
, pos
, r
, g
, b
); }
286 // Rotates the image about the given point, 'angle' radians.
287 // Returns the rotated image, leaving this image intact.
288 wxImage
Rotate(double angle
, const wxPoint
& centre_of_rotation
,
289 bool interpolating
= true, wxPoint
* offset_after_rotation
= NULL
) const;
291 wxImage
Rotate90( bool clockwise
= true ) const;
292 wxImage
Mirror( bool horizontally
= true ) const;
294 // replace one colour with another
295 void Replace( unsigned char r1
, unsigned char g1
, unsigned char b1
,
296 unsigned char r2
, unsigned char g2
, unsigned char b2
);
298 // Convert to greyscale image. Uses the luminance component (Y) of the image.
299 // The luma value (YUV) is calculated using (R * lr) + (G * lg) + (B * lb), defaults to ITU-T BT.601
300 wxImage
ConvertToGreyscale( double lr
= 0.299, double lg
= 0.587, double lb
= 0.114 ) const;
302 // convert to monochrome image (<r,g,b> will be replaced by white,
303 // everything else by black)
304 wxImage
ConvertToMono( unsigned char r
, unsigned char g
, unsigned char b
) const;
306 // these routines are slow but safe
307 void SetRGB( int x
, int y
, unsigned char r
, unsigned char g
, unsigned char b
);
308 void SetRGB( const wxRect
& rect
, unsigned char r
, unsigned char g
, unsigned char b
);
309 unsigned char GetRed( int x
, int y
) const;
310 unsigned char GetGreen( int x
, int y
) const;
311 unsigned char GetBlue( int x
, int y
) const;
313 void SetAlpha(int x
, int y
, unsigned char alpha
);
314 unsigned char GetAlpha(int x
, int y
) const;
316 // find first colour that is not used in the image and has higher
317 // RGB values than <startR,startG,startB>
318 bool FindFirstUnusedColour( unsigned char *r
, unsigned char *g
, unsigned char *b
,
319 unsigned char startR
= 1, unsigned char startG
= 0,
320 unsigned char startB
= 0 ) const;
321 // Set image's mask to the area of 'mask' that has <r,g,b> colour
322 bool SetMaskFromImage(const wxImage
& mask
,
323 unsigned char mr
, unsigned char mg
, unsigned char mb
);
325 // converts image's alpha channel to mask (choosing mask colour
326 // automatically or using the specified colour for the mask), if it has
327 // any, does nothing otherwise:
328 bool ConvertAlphaToMask(unsigned char threshold
= wxIMAGE_ALPHA_THRESHOLD
);
329 void ConvertAlphaToMask(unsigned char mr
, unsigned char mg
, unsigned char mb
,
330 unsigned char threshold
= wxIMAGE_ALPHA_THRESHOLD
);
333 // This method converts an image where the original alpha
334 // information is only available as a shades of a colour
335 // (actually shades of grey) typically when you draw anti-
336 // aliased text into a bitmap. The DC drawinf routines
337 // draw grey values on the black background although they
338 // actually mean to draw white with differnt alpha values.
339 // This method reverses it, assuming a black (!) background
340 // and white text (actually only the red channel is read).
341 // The method will then fill up the whole image with the
343 bool ConvertColourToAlpha( unsigned char r
, unsigned char g
, unsigned char b
);
345 static bool CanRead( const wxString
& name
);
346 static int GetImageCount( const wxString
& name
, wxBitmapType type
= wxBITMAP_TYPE_ANY
);
347 virtual bool LoadFile( const wxString
& name
, wxBitmapType type
= wxBITMAP_TYPE_ANY
, int index
= -1 );
348 virtual bool LoadFile( const wxString
& name
, const wxString
& mimetype
, int index
= -1 );
351 static bool CanRead( wxInputStream
& stream
);
352 static int GetImageCount( wxInputStream
& stream
, wxBitmapType type
= wxBITMAP_TYPE_ANY
);
353 virtual bool LoadFile( wxInputStream
& stream
, wxBitmapType type
= wxBITMAP_TYPE_ANY
, int index
= -1 );
354 virtual bool LoadFile( wxInputStream
& stream
, const wxString
& mimetype
, int index
= -1 );
357 virtual bool SaveFile( const wxString
& name
) const;
358 virtual bool SaveFile( const wxString
& name
, wxBitmapType type
) const;
359 virtual bool SaveFile( const wxString
& name
, const wxString
& mimetype
) const;
362 virtual bool SaveFile( wxOutputStream
& stream
, wxBitmapType type
) const;
363 virtual bool SaveFile( wxOutputStream
& stream
, const wxString
& mimetype
) const;
366 bool Ok() const { return IsOk(); }
368 int GetWidth() const;
369 int GetHeight() const;
371 wxSize
GetSize() const
372 { return wxSize(GetWidth(), GetHeight()); }
374 // Gets the type of image found by LoadFile or specified with SaveFile
375 wxBitmapType
GetType() const;
377 // Set the image type, this is normally only called if the image is being
378 // created from data in the given format but not using LoadFile() (e.g.
379 // wxGIFDecoder uses this)
380 void SetType(wxBitmapType type
);
382 // these functions provide fastest access to wxImage data but should be
383 // used carefully as no checks are done
384 unsigned char *GetData() const;
385 void SetData( unsigned char *data
, bool static_data
=false );
386 void SetData( unsigned char *data
, int new_width
, int new_height
, bool static_data
=false );
388 unsigned char *GetAlpha() const; // may return NULL!
389 bool HasAlpha() const { return GetAlpha() != NULL
; }
390 void SetAlpha(unsigned char *alpha
= NULL
, bool static_data
=false);
393 // return true if this pixel is masked or has alpha less than specified
395 bool IsTransparent(int x
, int y
,
396 unsigned char threshold
= wxIMAGE_ALPHA_THRESHOLD
) const;
399 void SetMaskColour( unsigned char r
, unsigned char g
, unsigned char b
);
400 // Get the current mask colour or find a suitable colour
401 // returns true if using current mask colour
402 bool GetOrFindMaskColour( unsigned char *r
, unsigned char *g
, unsigned char *b
) const;
403 unsigned char GetMaskRed() const;
404 unsigned char GetMaskGreen() const;
405 unsigned char GetMaskBlue() const;
406 void SetMask( bool mask
= true );
407 bool HasMask() const;
411 bool HasPalette() const;
412 const wxPalette
& GetPalette() const;
413 void SetPalette(const wxPalette
& palette
);
414 #endif // wxUSE_PALETTE
416 // Option functions (arbitrary name/value mapping)
417 void SetOption(const wxString
& name
, const wxString
& value
);
418 void SetOption(const wxString
& name
, int value
);
419 wxString
GetOption(const wxString
& name
) const;
420 int GetOptionInt(const wxString
& name
) const;
421 bool HasOption(const wxString
& name
) const;
423 unsigned long CountColours( unsigned long stopafter
= (unsigned long) -1 ) const;
425 // Computes the histogram of the image and fills a hash table, indexed
426 // with integer keys built as 0xRRGGBB, containing wxImageHistogramEntry
427 // objects. Each of them contains an 'index' (useful to build a palette
428 // with the image colours) and a 'value', which is the number of pixels
429 // in the image with that colour.
430 // Returned value: # of entries in the histogram
431 unsigned long ComputeHistogram( wxImageHistogram
&h
) const;
433 // Rotates the hue of each pixel of the image. angle is a double in the range
434 // -1.0..1.0 where -1.0 is -360 degrees and 1.0 is 360 degrees
435 void RotateHue(double angle
);
437 static wxList
& GetHandlers() { return sm_handlers
; }
438 static void AddHandler( wxImageHandler
*handler
);
439 static void InsertHandler( wxImageHandler
*handler
);
440 static bool RemoveHandler( const wxString
& name
);
441 static wxImageHandler
*FindHandler( const wxString
& name
);
442 static wxImageHandler
*FindHandler( const wxString
& extension
, wxBitmapType imageType
);
443 static wxImageHandler
*FindHandler( wxBitmapType imageType
);
445 static wxImageHandler
*FindHandlerMime( const wxString
& mimetype
);
447 static wxString
GetImageExtWildcard();
449 static void CleanUpHandlers();
450 static void InitStandardHandlers();
452 static HSVValue
RGBtoHSV(const RGBValue
& rgb
);
453 static RGBValue
HSVtoRGB(const HSVValue
& hsv
);
455 #if WXWIN_COMPATIBILITY_2_8
456 wxDEPRECATED_CONSTRUCTOR(
457 wxImage(const wxString
& name
, long type
, int index
= -1)
459 LoadFile(name
, (wxBitmapType
)type
, index
);
464 wxDEPRECATED_CONSTRUCTOR(
465 wxImage(wxInputStream
& stream
, long type
, int index
= -1)
467 LoadFile(stream
, (wxBitmapType
)type
, index
);
472 bool LoadFile(wxInputStream
& stream
, long type
, int index
= -1)
474 return LoadFile(stream
, (wxBitmapType
)type
, index
);
479 bool SaveFile(wxOutputStream
& stream
, long type
) const
481 return SaveFile(stream
, (wxBitmapType
)type
);
484 #endif // wxUSE_STREAMS
487 bool LoadFile(const wxString
& name
, long type
, int index
= -1)
489 return LoadFile(name
, (wxBitmapType
)type
, index
);
494 bool SaveFile(const wxString
& name
, long type
) const
496 return SaveFile(name
, (wxBitmapType
)type
);
501 static wxImageHandler
*FindHandler(const wxString
& ext
, long type
)
503 return FindHandler(ext
, (wxBitmapType
)type
);
508 static wxImageHandler
*FindHandler(long imageType
)
510 return FindHandler((wxBitmapType
)imageType
);
513 #endif // WXWIN_COMPATIBILITY_2_8
516 static wxList sm_handlers
;
518 // return the index of the point with the given coordinates or -1 if the
519 // image is invalid of the coordinates are out of range
521 // note that index must be multiplied by 3 when using it with RGB array
522 long XYToIndex(int x
, int y
) const;
524 virtual wxObjectRefData
* CreateRefData() const;
525 virtual wxObjectRefData
* CloneRefData(const wxObjectRefData
* data
) const;
528 friend class WXDLLIMPEXP_FWD_CORE wxImageHandler
;
531 // read the image from the specified stream updating image type if
533 bool DoLoad(wxImageHandler
& handler
, wxInputStream
& stream
, int index
);
535 // write the image to the specified stream and also update the image type
537 bool DoSave(wxImageHandler
& handler
, wxOutputStream
& stream
) const;
538 #endif // wxUSE_STREAMS
540 DECLARE_DYNAMIC_CLASS(wxImage
)
544 extern void WXDLLIMPEXP_CORE
wxInitAllImageHandlers();
546 extern WXDLLIMPEXP_DATA_CORE(wxImage
) wxNullImage
;
548 //-----------------------------------------------------------------------------
550 //-----------------------------------------------------------------------------
552 #include "wx/imagbmp.h"
553 #include "wx/imagpng.h"
554 #include "wx/imaggif.h"
555 #include "wx/imagpcx.h"
556 #include "wx/imagjpeg.h"
557 #include "wx/imagtga.h"
558 #include "wx/imagtiff.h"
559 #include "wx/imagpnm.h"
560 #include "wx/imagxpm.h"
561 #include "wx/imagiff.h"
563 #endif // wxUSE_IMAGE