]> git.saurik.com Git - wxWidgets.git/blame - include/wx/image.h
wxEncodingConverter should and can be compiled
[wxWidgets.git] / include / wx / image.h
CommitLineData
01111366
RR
1/////////////////////////////////////////////////////////////////////////////
2// Name: image.h
3// Purpose: wxImage class
4// Author: Robert Roebling
5// RCS-ID: $Id$
6// Copyright: (c) Robert Roebling
65571936 7// Licence: wxWindows licence
01111366
RR
8/////////////////////////////////////////////////////////////////////////////
9
10#ifndef _WX_IMAGE_H_
11#define _WX_IMAGE_H_
12
12028905 13#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
01111366
RR
14#pragma interface "image.h"
15#endif
16
17#include "wx/setup.h"
18#include "wx/object.h"
19#include "wx/string.h"
20#include "wx/gdicmn.h"
952ae1e8 21#include "wx/hashmap.h"
bf38cbff
JS
22
23#if wxUSE_STREAMS
b9943f8e 24# include "wx/stream.h"
bf38cbff 25#endif
01111366 26
775c6f0c
VS
27#if wxUSE_IMAGE
28
66f21994
VZ
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
fd94e8aa
VS
33#define wxIMAGE_OPTION_FILENAME wxString(_T("FileName"))
34
01111366
RR
35//-----------------------------------------------------------------------------
36// classes
37//-----------------------------------------------------------------------------
38
39class WXDLLEXPORT wxImageHandler;
01111366 40class WXDLLEXPORT wxImage;
ed39ff57 41class WXDLLEXPORT wxPalette;
01111366
RR
42
43//-----------------------------------------------------------------------------
44// wxImageHandler
45//-----------------------------------------------------------------------------
46
47class WXDLLEXPORT wxImageHandler: public wxObject
48{
01111366 49public:
d84afea9 50 wxImageHandler()
fda7962d 51 : m_name(wxEmptyString), m_extension(wxEmptyString), m_mime(), m_type(0)
d84afea9 52 { }
01111366 53
bf38cbff 54#if wxUSE_STREAMS
7beb59f3
WS
55 virtual bool LoadFile( wxImage *image, wxInputStream& stream, bool verbose=true, int index=-1 );
56 virtual bool SaveFile( wxImage *image, wxOutputStream& stream, bool verbose=true );
8f177c8e 57
649d13e8 58 virtual int GetImageCount( wxInputStream& stream );
0828c087 59
39d16996 60 bool CanRead( wxInputStream& stream ) { return CallDoCanRead(stream); }
be25e480 61 bool CanRead( const wxString& name );
8f177c8e 62#endif // wxUSE_STREAMS
01111366 63
be25e480
RR
64 void SetName(const wxString& name) { m_name = name; }
65 void SetExtension(const wxString& ext) { m_extension = ext; }
66 void SetType(long type) { m_type = type; }
67 void SetMimeType(const wxString& type) { m_mime = type; }
68 wxString GetName() const { return m_name; }
69 wxString GetExtension() const { return m_extension; }
70 long GetType() const { return m_type; }
71 wxString GetMimeType() const { return m_mime; }
9e9ee68e 72
01111366 73protected:
8f177c8e 74#if wxUSE_STREAMS
be25e480 75 virtual bool DoCanRead( wxInputStream& stream ) = 0;
995612e2 76
39d16996
VZ
77 // save the stream position, call DoCanRead() and restore the position
78 bool CallDoCanRead(wxInputStream& stream);
55472691 79#endif // wxUSE_STREAMS
39d16996 80
be25e480
RR
81 wxString m_name;
82 wxString m_extension;
83 wxString m_mime;
84 long m_type;
aaf46fd6 85
be25e480
RR
86private:
87 DECLARE_CLASS(wxImageHandler)
8f177c8e
VZ
88};
89
01111366 90//-----------------------------------------------------------------------------
952ae1e8 91// wxImageHistogram
01111366
RR
92//-----------------------------------------------------------------------------
93
952ae1e8 94class WXDLLEXPORT wxImageHistogramEntry
c9d01afd
GRG
95{
96public:
66f21994 97 wxImageHistogramEntry() { index = value = 0; }
c9d01afd
GRG
98 unsigned long index;
99 unsigned long value;
100};
101
952ae1e8
VS
102WX_DECLARE_EXPORTED_HASH_MAP(unsigned long, wxImageHistogramEntry,
103 wxIntegerHash, wxIntegerEqual,
487659e0
VZ
104 wxImageHistogramBase);
105
bf78c81c 106class WXDLLEXPORT wxImageHistogram : public wxImageHistogramBase
487659e0
VZ
107{
108public:
109 wxImageHistogram() : wxImageHistogramBase(256) { }
110
111 // get the key in the histogram for the given RGB values
112 static unsigned long MakeKey(unsigned char r,
113 unsigned char g,
114 unsigned char b)
115 {
116 return (r << 16) | (g << 8) | b;
117 }
118
119 // find first colour that is not used in the image and has higher
120 // RGB values than RGB(startR, startG, startB)
121 //
122 // returns true and puts this colour in r, g, b (each of which may be NULL)
123 // on success or returns false if there are no more free colours
124 bool FindFirstUnusedColour(unsigned char *r,
125 unsigned char *g,
126 unsigned char *b,
127 unsigned char startR = 1,
128 unsigned char startG = 0,
129 unsigned char startB = 0 ) const;
130};
952ae1e8
VS
131
132//-----------------------------------------------------------------------------
133// wxImage
134//-----------------------------------------------------------------------------
135
01111366
RR
136class WXDLLEXPORT wxImage: public wxObject
137{
01111366 138public:
1b941f2d 139 wxImage(){}
ff865c13 140 wxImage( int width, int height, bool clear = true );
7beb59f3 141 wxImage( int width, int height, unsigned char* data, bool static_data = false );
4ea56379 142 wxImage( int width, int height, unsigned char* data, unsigned char* alpha, bool static_data = false );
60d43ad8
VS
143 wxImage( const wxString& name, long type = wxBITMAP_TYPE_ANY, int index = -1 );
144 wxImage( const wxString& name, const wxString& mimetype, int index = -1 );
aaf46fd6
VZ
145
146#if wxUSE_STREAMS
60d43ad8
VS
147 wxImage( wxInputStream& stream, long type = wxBITMAP_TYPE_ANY, int index = -1 );
148 wxImage( wxInputStream& stream, const wxString& mimetype, int index = -1 );
aaf46fd6 149#endif // wxUSE_STREAMS
01111366 150
be25e480
RR
151 wxImage( const wxImage& image );
152 wxImage( const wxImage* image );
9e9ee68e 153
aaa97828 154 bool Create( int width, int height, bool clear = true );
7beb59f3 155 bool Create( int width, int height, unsigned char* data, bool static_data = false );
4ea56379 156 bool Create( int width, int height, unsigned char* data, unsigned char* alpha, bool static_data = false );
be25e480 157 void Destroy();
01111366 158
f6bcfd97
BP
159 // creates an identical copy of the image (the = operator
160 // just raises the ref count)
161 wxImage Copy() const;
aaf46fd6 162
be25e480
RR
163 // return the new image with size width*height
164 wxImage GetSubImage( const wxRect& ) const;
aaf46fd6 165
f6bcfd97
BP
166 // pastes image into this instance and takes care of
167 // the mask colour and out of bounds problems
aaf46fd6 168 void Paste( const wxImage &image, int x, int y );
23280650 169
be25e480
RR
170 // return the new image with size width*height
171 wxImage Scale( int width, int height ) const;
bf78c81c 172
534f0312 173 wxImage ShrinkBy( int xFactor , int yFactor ) const ;
7b2471a0 174
be25e480
RR
175 // rescales the image in place
176 wxImage& Rescale( int width, int height ) { return *this = Scale(width, height); }
ce9a75d2 177
7a632f10
JS
178 // Rotates the image about the given point, 'angle' radians.
179 // Returns the rotated image, leaving this image intact.
180 wxImage Rotate(double angle, const wxPoint & centre_of_rotation,
7beb59f3 181 bool interpolating = true, wxPoint * offset_after_rotation = (wxPoint*) NULL) const;
f6bcfd97 182
7beb59f3
WS
183 wxImage Rotate90( bool clockwise = true ) const;
184 wxImage Mirror( bool horizontally = true ) const;
7a632f10 185
be25e480
RR
186 // replace one colour with another
187 void Replace( unsigned char r1, unsigned char g1, unsigned char b1,
188 unsigned char r2, unsigned char g2, unsigned char b2 );
aaf46fd6 189
ff5ad794
VS
190 // convert to monochrome image (<r,g,b> will be replaced by white,
191 // everything else by black)
f515c25a 192 wxImage ConvertToMono( unsigned char r, unsigned char g, unsigned char b ) const;
ce9a75d2 193
be25e480
RR
194 // these routines are slow but safe
195 void SetRGB( int x, int y, unsigned char r, unsigned char g, unsigned char b );
f6bcfd97
BP
196 unsigned char GetRed( int x, int y ) const;
197 unsigned char GetGreen( int x, int y ) const;
198 unsigned char GetBlue( int x, int y ) const;
23280650 199
487659e0 200 void SetAlpha(int x, int y, unsigned char alpha);
d30ee785 201 unsigned char GetAlpha(int x, int y) const;
487659e0 202
1f5b2017
VS
203 // find first colour that is not used in the image and has higher
204 // RGB values than <startR,startG,startB>
205 bool FindFirstUnusedColour( unsigned char *r, unsigned char *g, unsigned char *b,
aaf46fd6 206 unsigned char startR = 1, unsigned char startG = 0,
e0a76d8d 207 unsigned char startB = 0 ) const;
1f5b2017 208 // Set image's mask to the area of 'mask' that has <r,g,b> colour
aaf46fd6 209 bool SetMaskFromImage(const wxImage & mask,
1f5b2017 210 unsigned char mr, unsigned char mg, unsigned char mb);
52b64b0a 211
ff5ad794
VS
212 // converts image's alpha channel to mask, if it has any, does nothing
213 // otherwise:
8f2b21e4 214 bool ConvertAlphaToMask(unsigned char threshold = 128);
ff5ad794 215
6408deed
RR
216 // This method converts an image where the original alpha
217 // information is only available as a shades of a colour
218 // (actually shades of grey) typically when you draw anti-
219 // aliased text into a bitmap. The DC drawinf routines
220 // draw grey values on the black background although they
221 // actually mean to draw white with differnt alpha values.
222 // This method reverses it, assuming a black (!) background
223 // and white text (actually only the red channel is read).
224 // The method will then fill up the whole image with the
225 // colour given.
226 bool ConvertColourToAlpha( unsigned char r, unsigned char g, unsigned char b );
227
be25e480 228 static bool CanRead( const wxString& name );
649d13e8 229 static int GetImageCount( const wxString& name, long type = wxBITMAP_TYPE_ANY );
60d43ad8
VS
230 virtual bool LoadFile( const wxString& name, long type = wxBITMAP_TYPE_ANY, int index = -1 );
231 virtual bool LoadFile( const wxString& name, const wxString& mimetype, int index = -1 );
bf38cbff
JS
232
233#if wxUSE_STREAMS
be25e480 234 static bool CanRead( wxInputStream& stream );
649d13e8 235 static int GetImageCount( wxInputStream& stream, long type = wxBITMAP_TYPE_ANY );
60d43ad8
VS
236 virtual bool LoadFile( wxInputStream& stream, long type = wxBITMAP_TYPE_ANY, int index = -1 );
237 virtual bool LoadFile( wxInputStream& stream, const wxString& mimetype, int index = -1 );
bf38cbff
JS
238#endif
239
45647dcf 240 virtual bool SaveFile( const wxString& name ) const;
e0a76d8d
VS
241 virtual bool SaveFile( const wxString& name, int type ) const;
242 virtual bool SaveFile( const wxString& name, const wxString& mimetype ) const;
bf38cbff
JS
243
244#if wxUSE_STREAMS
e0a76d8d
VS
245 virtual bool SaveFile( wxOutputStream& stream, int type ) const;
246 virtual bool SaveFile( wxOutputStream& stream, const wxString& mimetype ) const;
bf38cbff 247#endif
01111366 248
be25e480
RR
249 bool Ok() const;
250 int GetWidth() const;
251 int GetHeight() const;
252
487659e0
VZ
253 // these functions provide fastest access to wxImage data but should be
254 // used carefully as no checks are done
255 unsigned char *GetData() const;
256 void SetData( unsigned char *data );
257 void SetData( unsigned char *data, int new_width, int new_height );
258
259 unsigned char *GetAlpha() const; // may return NULL!
260 bool HasAlpha() const { return GetAlpha() != NULL; }
261 void SetAlpha(unsigned char *alpha = NULL);
5e5437e0
JS
262
263 // Mask functions
be25e480
RR
264 void SetMaskColour( unsigned char r, unsigned char g, unsigned char b );
265 unsigned char GetMaskRed() const;
266 unsigned char GetMaskGreen() const;
267 unsigned char GetMaskBlue() const;
7beb59f3 268 void SetMask( bool mask = true );
be25e480
RR
269 bool HasMask() const;
270
d275c7eb 271#if wxUSE_PALETTE
5e5437e0
JS
272 // Palette functions
273 bool HasPalette() const;
274 const wxPalette& GetPalette() const;
275 void SetPalette(const wxPalette& palette);
d275c7eb 276#endif // wxUSE_PALETTE
5e5437e0
JS
277
278 // Option functions (arbitrary name/value mapping)
279 void SetOption(const wxString& name, const wxString& value);
280 void SetOption(const wxString& name, int value);
281 wxString GetOption(const wxString& name) const;
282 int GetOptionInt(const wxString& name) const;
283 bool HasOption(const wxString& name) const;
3f4fc796 284
e0a76d8d 285 unsigned long CountColours( unsigned long stopafter = (unsigned long) -1 ) const;
952ae1e8
VS
286
287 // Computes the histogram of the image and fills a hash table, indexed
288 // with integer keys built as 0xRRGGBB, containing wxImageHistogramEntry
bf78c81c
RD
289 // objects. Each of them contains an 'index' (useful to build a palette
290 // with the image colours) and a 'value', which is the number of pixels
952ae1e8
VS
291 // in the image with that colour.
292 // Returned value: # of entries in the histogram
e0a76d8d 293 unsigned long ComputeHistogram( wxImageHistogram &h ) const;
be25e480
RR
294
295 wxImage& operator = (const wxImage& image)
296 {
297 if ( (*this) != image )
298 Ref(image);
299 return *this;
300 }
301
2b5f62a0 302 bool operator == (const wxImage& image) const
be25e480 303 { return m_refData == image.m_refData; }
2b5f62a0 304 bool operator != (const wxImage& image) const
be25e480
RR
305 { return m_refData != image.m_refData; }
306
307 static wxList& GetHandlers() { return sm_handlers; }
308 static void AddHandler( wxImageHandler *handler );
309 static void InsertHandler( wxImageHandler *handler );
310 static bool RemoveHandler( const wxString& name );
311 static wxImageHandler *FindHandler( const wxString& name );
312 static wxImageHandler *FindHandler( const wxString& extension, long imageType );
313 static wxImageHandler *FindHandler( long imageType );
314 static wxImageHandler *FindHandlerMime( const wxString& mimetype );
315
939fadc8
JS
316 static wxString GetImageExtWildcard();
317
be25e480
RR
318 static void CleanUpHandlers();
319 static void InitStandardHandlers();
c9d01afd 320
01111366 321protected:
5e5437e0 322 static wxList sm_handlers;
01111366 323
be25e480
RR
324private:
325 friend class WXDLLEXPORT wxImageHandler;
23280650 326
be25e480 327 DECLARE_DYNAMIC_CLASS(wxImage)
01111366
RR
328};
329
8f493002 330
a091d18c 331extern void WXDLLEXPORT wxInitAllImageHandlers();
b5a4a47d 332
fd859211 333WXDLLEXPORT_DATA(extern wxImage) wxNullImage;
8f493002
VS
334
335//-----------------------------------------------------------------------------
336// wxImage handlers
337//-----------------------------------------------------------------------------
338
339#include "wx/imagbmp.h"
340#include "wx/imagpng.h"
341#include "wx/imaggif.h"
342#include "wx/imagpcx.h"
343#include "wx/imagjpeg.h"
344#include "wx/imagtiff.h"
345#include "wx/imagpnm.h"
775c6f0c 346#include "wx/imagxpm.h"
4b6b4dfc 347#include "wx/imagiff.h"
775c6f0c
VS
348
349#endif // wxUSE_IMAGE
8f493002 350
01111366
RR
351#endif
352 // _WX_IMAGE_H_
9e9ee68e 353