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