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