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