move misc surface helpers to wxIDirectFBSurface class
[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 if ( data.m_surface )
162 m_surface = data.m_surface->Clone();
163
164 m_mask = data.m_mask ? new wxMask(*data.m_mask) : NULL;
165 #if wxUSE_PALETTE
166 m_palette = data.m_palette ? new wxPalette(*data.m_palette) : NULL;
167 #endif
168 }
169
170 ~wxBitmapRefData()
171 {
172 delete m_mask;
173 #if wxUSE_PALETTE
174 delete m_palette;
175 #endif
176 }
177
178 wxIDirectFBSurfacePtr m_surface;
179 wxMask *m_mask;
180 #if wxUSE_PALETTE
181 wxPalette *m_palette;
182 #endif
183 };
184
185 #define M_BITMAP ((wxBitmapRefData *)m_refData)
186
187 //-----------------------------------------------------------------------------
188 // wxBitmap
189 //-----------------------------------------------------------------------------
190
191 IMPLEMENT_ABSTRACT_CLASS(wxBitmapHandler, wxObject)
192 IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxBitmapBase)
193
194 wxBitmap::wxBitmap(int width, int height, int depth)
195 {
196 Create(width, height, depth);
197 }
198
199 bool wxBitmap::Create(int width, int height, int depth)
200 {
201 UnRef();
202
203 wxCHECK_MSG( width > 0 && height > 0, false, wxT("invalid bitmap size") );
204
205 DFBSurfaceDescription desc;
206 desc.flags = (DFBSurfaceDescriptionFlags)(
207 DSDESC_CAPS | DSDESC_WIDTH | DSDESC_HEIGHT);
208 desc.caps = DSCAPS_NONE;
209 desc.width = width;
210 desc.height = height;
211
212 wxIDirectFBSurfacePtr surface(wxIDirectFB::Get()->CreateSurface(&desc));
213 if ( !surface )
214 return false;
215
216 m_refData = new wxBitmapRefData();
217 M_BITMAP->m_surface = surface;
218
219 return true;
220 }
221
222 #warning "FIXME: move this to common code"
223 bool wxBitmap::CreateFromXpm(const char **bits)
224 {
225 wxCHECK_MSG( bits != NULL, false, wxT("invalid bitmap data") );
226
227 #if wxUSE_IMAGE && wxUSE_XPM
228 wxXPMDecoder decoder;
229 wxImage img = decoder.ReadData(bits);
230 wxCHECK_MSG( img.Ok(), false, wxT("invalid bitmap data") );
231
232 *this = wxBitmap(img);
233
234 return true;
235 #else
236 wxFAIL_MSG( _T("creating bitmaps from XPMs not supported") );
237 return false;
238 #endif // wxUSE_IMAGE && wxUSE_XPM
239 }
240
241 #if wxUSE_IMAGE
242 wxBitmap::wxBitmap(const wxImage& image, int depth)
243 {
244 wxCHECK_RET( image.Ok(), wxT("invalid image") );
245 }
246
247 wxImage wxBitmap::ConvertToImage() const
248 {
249 wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") );
250
251 return wxNullImage; // FIXME
252 }
253 #endif // wxUSE_IMAGE
254
255 wxBitmap::wxBitmap(const wxString &filename, wxBitmapType type)
256 {
257 LoadFile(filename, type);
258 }
259
260 wxBitmap::wxBitmap(const char bits[], int width, int height, int depth)
261 {
262 wxCHECK_RET( depth == 1, wxT("can only create mono bitmap from XBM data") );
263 }
264
265 bool wxBitmap::Ok() const
266 {
267 return (m_refData != NULL && M_BITMAP->m_surface);
268 }
269
270 bool wxBitmap::operator==(const wxBitmap& bmp) const
271 {
272 // FIXME: is this the right way to compare bitmaps?
273 return (m_refData == bmp.m_refData);
274 }
275
276 int wxBitmap::GetHeight() const
277 {
278 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
279
280 int h = -1;
281 M_BITMAP->m_surface->GetSize(NULL, &h);
282 return h;
283 }
284
285 int wxBitmap::GetWidth() const
286 {
287 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
288
289 int w = -1;
290 M_BITMAP->m_surface->GetSize(&w, NULL);
291 return w;
292 }
293
294 int wxBitmap::GetDepth() const
295 {
296 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
297
298 return M_BITMAP->m_surface->GetDepth();
299 }
300
301 wxMask *wxBitmap::GetMask() const
302 {
303 wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") );
304
305 return M_BITMAP->m_mask;
306 }
307
308 void wxBitmap::SetMask(wxMask *mask)
309 {
310 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
311
312 delete M_BITMAP->m_mask;
313 M_BITMAP->m_mask = mask;
314 }
315
316 bool wxBitmap::CopyFromIcon(const wxIcon& icon)
317 {
318 *this = *((wxBitmap*)(&icon));
319 return true;
320 }
321
322 wxBitmap wxBitmap::GetSubBitmap(const wxRect& rect) const
323 {
324 wxCHECK_MSG( Ok() &&
325 rect.x >= 0 && rect.y >= 0 &&
326 rect.x+rect.width <= GetWidth() &&
327 rect.y+rect.height <= GetHeight(),
328 wxNullBitmap,
329 wxT("invalid bitmap or bitmap region") );
330
331 }
332
333 #warning "to common code"
334 bool wxBitmap::LoadFile(const wxString &name, wxBitmapType type)
335 {
336 UnRef();
337
338 wxBitmapHandler *handler = FindHandler(type);
339
340 if ( handler == NULL )
341 {
342 wxImage image;
343 if ( !image.LoadFile(name, type) || !image.Ok() )
344 {
345 wxLogError("no bitmap handler for type %d defined.", type);
346 return false;
347 }
348 else
349 {
350 *this = wxBitmap(image);
351 return true;
352 }
353 }
354
355 m_refData = new wxBitmapRefData();
356
357 return handler->LoadFile(this, name, type, -1, -1);
358 }
359
360 #warning "to common code"
361 bool wxBitmap::SaveFile(const wxString& filename, wxBitmapType type, const wxPalette *palette) const
362 {
363 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
364
365 wxBitmapHandler *handler = FindHandler(type);
366
367 if ( handler == NULL )
368 {
369 wxImage image = ConvertToImage();
370 #if wxUSE_PALETTE
371 if ( palette )
372 image.SetPalette(*palette);
373 #endif // wxUSE_PALETTE
374
375 if ( image.Ok() )
376 return image.SaveFile(filename, type);
377 else
378 {
379 wxLogError("no bitmap handler for type %d defined.", type);
380 return false;
381 }
382 }
383
384 return handler->SaveFile(this, filename, type, palette);
385 }
386
387 #if wxUSE_PALETTE
388 wxPalette *wxBitmap::GetPalette() const
389 {
390 wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") );
391
392 return M_BITMAP->m_palette;
393 }
394
395 void wxBitmap::SetPalette(const wxPalette& palette)
396 {
397 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
398 wxCHECK_RET( GetDepth() > 1 && GetDepth() <= 8, wxT("cannot set palette for bitmap of this depth") );
399
400 delete M_BITMAP->m_palette;
401 M_BITMAP->m_palette = NULL;
402
403 if ( !palette.Ok() ) return;
404
405 M_BITMAP->m_palette = new wxPalette(palette);
406 }
407 #endif // wxUSE_PALETTE
408
409 void wxBitmap::SetHeight(int height)
410 {
411 AllocExclusive();
412 #warning "todo"
413 }
414
415 void wxBitmap::SetWidth(int width)
416 {
417 AllocExclusive();
418 #warning "todo"
419 }
420
421 void wxBitmap::SetDepth(int depth)
422 {
423 AllocExclusive();
424 #warning "todo"
425 }
426
427 wxIDirectFBSurfacePtr wxBitmap::GetDirectFBSurface() const
428 {
429 wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") );
430
431 return M_BITMAP->m_surface;
432 }
433
434 wxObjectRefData *wxBitmap::CreateRefData() const
435 {
436 return new wxBitmapRefData;
437 }
438
439 wxObjectRefData *wxBitmap::CloneRefData(const wxObjectRefData *data) const
440 {
441 return new wxBitmapRefData(*(wxBitmapRefData *)data);
442 }
443
444
445 /*static*/
446 void wxBitmap::InitStandardHandlers()
447 {
448 // not wxBitmap handlers, we rely on wxImage
449 }