use C++ wrappers around DirectFB API for easier use
[wxWidgets.git] / src / dfb / bitmap.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/dfb/bitmap.cpp
3 // Purpose: wxBitmap implementation
4 // Author: Vaclav Slavik
5 // Created: 2006-08-04
6 // RCS-ID: $Id$
7 // Copyright: (c) 2006 REA Elektronik GmbH
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
13
14 #ifdef __BORLANDC__
15 #pragma hdrstop
16 #endif
17
18 #ifndef WX_PRECOMP
19 #include "wx/app.h"
20 #include "wx/log.h"
21 #endif
22
23 #include "wx/bitmap.h"
24 #include "wx/colour.h"
25 #include "wx/image.h"
26
27 #warning "move this to common"
28 #include "wx/xpmdecod.h"
29
30 #include "wx/dfb/private.h"
31
32 //-----------------------------------------------------------------------------
33 // helpers
34 //-----------------------------------------------------------------------------
35
36 // Convert wxColour into it's quantized value in lower-precision
37 // pixel format (needed for masking by colour).
38 static wxColour wxQuantizeColour(const wxColour& clr, const wxBitmap& bmp)
39 {
40 #if 0
41 pixel_format_t *pf = bmp.GetMGLbitmap_t()->pf;
42
43 if ( pf->redAdjust == 0 && pf->greenAdjust == 0 && pf->blueAdjust == 0 )
44 return clr;
45 else
46 return wxColour((unsigned char)((clr.Red() >> pf->redAdjust) << pf->redAdjust),
47 (unsigned char)((clr.Green() >> pf->greenAdjust) << pf->greenAdjust),
48 (unsigned char)((clr.Blue() >> pf->blueAdjust) << pf->blueAdjust));
49 #endif
50 }
51
52
53 //-----------------------------------------------------------------------------
54 // wxMask
55 //-----------------------------------------------------------------------------
56
57 IMPLEMENT_DYNAMIC_CLASS(wxMask, wxObject)
58
59 wxMask::wxMask() : m_bitmap(NULL)
60 {
61 }
62
63 wxMask::wxMask(const wxBitmap& bitmap)
64 : m_bitmap(NULL)
65 {
66 Create(bitmap);
67 }
68
69 wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour)
70 : m_bitmap(NULL)
71 {
72 Create(bitmap, colour);
73 }
74
75 #if wxUSE_PALETTE
76 wxMask::wxMask(const wxBitmap& bitmap, int paletteIndex)
77 : m_bitmap(NULL)
78 {
79 Create(bitmap, paletteIndex);
80 }
81 #endif // wxUSE_PALETTE
82
83 wxMask::wxMask(const wxMask& mask)
84 {
85 m_bitmap = mask.m_bitmap ? new wxBitmap(*mask.m_bitmap) : NULL;
86 }
87
88 wxMask::~wxMask()
89 {
90 delete m_bitmap;
91 }
92
93 #warning "move this to common code"
94 bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour)
95 {
96 delete m_bitmap;
97 m_bitmap = NULL;
98
99 wxColour clr(wxQuantizeColour(colour, bitmap));
100
101 wxImage imgSrc(bitmap.ConvertToImage());
102 imgSrc.SetMask(false);
103 wxImage image(imgSrc.ConvertToMono(clr.Red(), clr.Green(), clr.Blue()));
104 if ( !image.Ok() )
105 return false;
106
107 m_bitmap = new wxBitmap(image, 1);
108
109 return m_bitmap->Ok();
110 }
111
112 #if wxUSE_PALETTE
113 bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex)
114 {
115 unsigned char r,g,b;
116 wxPalette *pal = bitmap.GetPalette();
117
118 wxCHECK_MSG( pal, false, wxT("Cannot create mask from bitmap without palette") );
119
120 pal->GetRGB(paletteIndex, &r, &g, &b);
121
122 return Create(bitmap, wxColour(r, g, b));
123 }
124 #endif // wxUSE_PALETTE
125
126 bool wxMask::Create(const wxBitmap& bitmap)
127 {
128 delete m_bitmap;
129 m_bitmap = NULL;
130
131 wxCHECK_MSG( bitmap.Ok(), false, wxT("Invalid bitmap") );
132 wxCHECK_MSG( bitmap.GetDepth() == 1, false, wxT("Cannot create mask from colour bitmap") );
133
134 m_bitmap = new wxBitmap(bitmap);
135 return true;
136 }
137
138 const wxBitmap& wxMask::GetBitmap() const
139 {
140 return m_bitmap ? *m_bitmap : wxNullBitmap;
141 }
142
143
144 //-----------------------------------------------------------------------------
145 // wxBitmapRefData
146 //-----------------------------------------------------------------------------
147
148 class wxBitmapRefData: public wxObjectRefData
149 {
150 public:
151 wxBitmapRefData()
152 {
153 m_mask = NULL;
154 #if wxUSE_PALETTE
155 m_palette = NULL;
156 #endif
157 }
158
159 wxBitmapRefData(const wxBitmapRefData& data)
160 {
161 m_surface = wxDfbCloneSurface(data.m_surface,
162 wxDfbCloneSurface_NoPixels);
163 m_mask = data.m_mask ? new wxMask(*data.m_mask) : NULL;
164 #if wxUSE_PALETTE
165 m_palette = data.m_palette ? new wxPalette(*data.m_palette) : NULL;
166 #endif
167 }
168
169 ~wxBitmapRefData()
170 {
171 delete m_mask;
172 #if wxUSE_PALETTE
173 delete m_palette;
174 #endif
175 }
176
177 wxIDirectFBSurfacePtr m_surface;
178 wxMask *m_mask;
179 #if wxUSE_PALETTE
180 wxPalette *m_palette;
181 #endif
182 };
183
184 #define M_BITMAP ((wxBitmapRefData *)m_refData)
185
186 //-----------------------------------------------------------------------------
187 // wxBitmap
188 //-----------------------------------------------------------------------------
189
190 IMPLEMENT_ABSTRACT_CLASS(wxBitmapHandler, wxObject)
191 IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxBitmapBase)
192
193 wxBitmap::wxBitmap(int width, int height, int depth)
194 {
195 Create(width, height, depth);
196 }
197
198 bool wxBitmap::Create(int width, int height, int depth)
199 {
200 UnRef();
201
202 wxCHECK_MSG( width > 0 && height > 0, false, wxT("invalid bitmap size") );
203
204 DFBSurfaceDescription desc;
205 desc.flags = (DFBSurfaceDescriptionFlags)(
206 DSDESC_CAPS | DSDESC_WIDTH | DSDESC_HEIGHT);
207 desc.caps = DSCAPS_NONE;
208 desc.width = width;
209 desc.height = height;
210
211 wxIDirectFBSurfacePtr surface(wxIDirectFB::Get()->CreateSurface(&desc));
212 if ( !surface )
213 return false;
214
215 m_refData = new wxBitmapRefData();
216 M_BITMAP->m_surface = surface;
217
218 return true;
219 }
220
221 #warning "FIXME: move this to common code"
222 bool wxBitmap::CreateFromXpm(const char **bits)
223 {
224 wxCHECK_MSG( bits != NULL, false, wxT("invalid bitmap data") );
225
226 #if wxUSE_IMAGE && wxUSE_XPM
227 wxXPMDecoder decoder;
228 wxImage img = decoder.ReadData(bits);
229 wxCHECK_MSG( img.Ok(), false, wxT("invalid bitmap data") );
230
231 *this = wxBitmap(img);
232
233 return true;
234 #else
235 wxFAIL_MSG( _T("creating bitmaps from XPMs not supported") );
236 return false;
237 #endif // wxUSE_IMAGE && wxUSE_XPM
238 }
239
240 #if wxUSE_IMAGE
241 wxBitmap::wxBitmap(const wxImage& image, int depth)
242 {
243 wxCHECK_RET( image.Ok(), wxT("invalid image") );
244 }
245
246 wxImage wxBitmap::ConvertToImage() const
247 {
248 wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") );
249
250 return wxNullImage; // FIXME
251 }
252 #endif // wxUSE_IMAGE
253
254 wxBitmap::wxBitmap(const wxString &filename, wxBitmapType type)
255 {
256 LoadFile(filename, type);
257 }
258
259 wxBitmap::wxBitmap(const char bits[], int width, int height, int depth)
260 {
261 wxCHECK_RET( depth == 1, wxT("can only create mono bitmap from XBM data") );
262 }
263
264 bool wxBitmap::Ok() const
265 {
266 return (m_refData != NULL && M_BITMAP->m_surface);
267 }
268
269 bool wxBitmap::operator==(const wxBitmap& bmp) const
270 {
271 // FIXME: is this the right way to compare bitmaps?
272 return (m_refData == bmp.m_refData);
273 }
274
275 int wxBitmap::GetHeight() const
276 {
277 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
278
279 int h = -1;
280 M_BITMAP->m_surface->GetSize(NULL, &h);
281 return h;
282 }
283
284 int wxBitmap::GetWidth() const
285 {
286 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
287
288 int w = -1;
289 M_BITMAP->m_surface->GetSize(&w, NULL);
290 return w;
291 }
292
293 int wxBitmap::GetDepth() const
294 {
295 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
296
297 return wxDfbGetSurfaceDepth(M_BITMAP->m_surface);
298 }
299
300 wxMask *wxBitmap::GetMask() const
301 {
302 wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") );
303
304 return M_BITMAP->m_mask;
305 }
306
307 void wxBitmap::SetMask(wxMask *mask)
308 {
309 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
310
311 delete M_BITMAP->m_mask;
312 M_BITMAP->m_mask = mask;
313 }
314
315 bool wxBitmap::CopyFromIcon(const wxIcon& icon)
316 {
317 *this = *((wxBitmap*)(&icon));
318 return true;
319 }
320
321 wxBitmap wxBitmap::GetSubBitmap(const wxRect& rect) const
322 {
323 wxCHECK_MSG( Ok() &&
324 rect.x >= 0 && rect.y >= 0 &&
325 rect.x+rect.width <= GetWidth() &&
326 rect.y+rect.height <= GetHeight(),
327 wxNullBitmap,
328 wxT("invalid bitmap or bitmap region") );
329
330 }
331
332 #warning "to common code"
333 bool wxBitmap::LoadFile(const wxString &name, wxBitmapType type)
334 {
335 UnRef();
336
337 wxBitmapHandler *handler = FindHandler(type);
338
339 if ( handler == NULL )
340 {
341 wxImage image;
342 if ( !image.LoadFile(name, type) || !image.Ok() )
343 {
344 wxLogError("no bitmap handler for type %d defined.", type);
345 return false;
346 }
347 else
348 {
349 *this = wxBitmap(image);
350 return true;
351 }
352 }
353
354 m_refData = new wxBitmapRefData();
355
356 return handler->LoadFile(this, name, type, -1, -1);
357 }
358
359 #warning "to common code"
360 bool wxBitmap::SaveFile(const wxString& filename, wxBitmapType type, const wxPalette *palette) const
361 {
362 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
363
364 wxBitmapHandler *handler = FindHandler(type);
365
366 if ( handler == NULL )
367 {
368 wxImage image = ConvertToImage();
369 #if wxUSE_PALETTE
370 if ( palette )
371 image.SetPalette(*palette);
372 #endif // wxUSE_PALETTE
373
374 if ( image.Ok() )
375 return image.SaveFile(filename, type);
376 else
377 {
378 wxLogError("no bitmap handler for type %d defined.", type);
379 return false;
380 }
381 }
382
383 return handler->SaveFile(this, filename, type, palette);
384 }
385
386 #if wxUSE_PALETTE
387 wxPalette *wxBitmap::GetPalette() const
388 {
389 wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") );
390
391 return M_BITMAP->m_palette;
392 }
393
394 void wxBitmap::SetPalette(const wxPalette& palette)
395 {
396 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
397 wxCHECK_RET( GetDepth() > 1 && GetDepth() <= 8, wxT("cannot set palette for bitmap of this depth") );
398
399 delete M_BITMAP->m_palette;
400 M_BITMAP->m_palette = NULL;
401
402 if ( !palette.Ok() ) return;
403
404 M_BITMAP->m_palette = new wxPalette(palette);
405 }
406 #endif // wxUSE_PALETTE
407
408 void wxBitmap::SetHeight(int height)
409 {
410 AllocExclusive();
411 #warning "todo"
412 }
413
414 void wxBitmap::SetWidth(int width)
415 {
416 AllocExclusive();
417 #warning "todo"
418 }
419
420 void wxBitmap::SetDepth(int depth)
421 {
422 AllocExclusive();
423 #warning "todo"
424 }
425
426 wxIDirectFBSurfacePtr wxBitmap::GetDirectFBSurface() const
427 {
428 wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") );
429
430 return M_BITMAP->m_surface;
431 }
432
433 wxObjectRefData *wxBitmap::CreateRefData() const
434 {
435 return new wxBitmapRefData;
436 }
437
438 wxObjectRefData *wxBitmap::CloneRefData(const wxObjectRefData *data) const
439 {
440 return new wxBitmapRefData(*(wxBitmapRefData *)data);
441 }
442
443
444 /*static*/
445 void wxBitmap::InitStandardHandlers()
446 {
447 // not wxBitmap handlers, we rely on wxImage
448 }