]>
Commit | Line | Data |
---|---|---|
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 | // Creates a surface that will use wxImage's pixel data (RGB only) | |
53 | static wxIDirectFBSurfacePtr CreateSurfaceForImage(const wxImage& image) | |
54 | { | |
55 | wxCHECK_MSG( image.Ok(), NULL, _T("invalid image") ); | |
56 | // FIXME_DFB: implement alpha handling by merging alpha buffer with RGB | |
57 | // into a temporary RGBA surface | |
58 | wxCHECK_MSG( !image.HasAlpha(), NULL, _T("alpha channel not supported") ); | |
59 | ||
60 | DFBSurfaceDescription desc; | |
61 | desc.flags = (DFBSurfaceDescriptionFlags) | |
62 | (DSDESC_CAPS | DSDESC_WIDTH | DSDESC_HEIGHT | DSDESC_PIXELFORMAT | | |
63 | DSDESC_PREALLOCATED); | |
64 | desc.caps = DSCAPS_NONE; | |
65 | desc.width = image.GetWidth(); | |
66 | desc.height = image.GetHeight(); | |
67 | desc.pixelformat = DSPF_RGB24; | |
68 | desc.preallocated[0].data = image.GetData(); | |
69 | desc.preallocated[0].pitch = 3 * desc.width; | |
70 | ||
71 | return wxIDirectFB::Get()->CreateSurface(&desc); | |
72 | } | |
73 | ||
74 | //----------------------------------------------------------------------------- | |
75 | // wxMask | |
76 | //----------------------------------------------------------------------------- | |
77 | ||
78 | IMPLEMENT_DYNAMIC_CLASS(wxMask, wxObject) | |
79 | ||
80 | wxMask::wxMask() : m_bitmap(NULL) | |
81 | { | |
82 | } | |
83 | ||
84 | wxMask::wxMask(const wxBitmap& bitmap) | |
85 | : m_bitmap(NULL) | |
86 | { | |
87 | Create(bitmap); | |
88 | } | |
89 | ||
90 | wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour) | |
91 | : m_bitmap(NULL) | |
92 | { | |
93 | Create(bitmap, colour); | |
94 | } | |
95 | ||
96 | #if wxUSE_PALETTE | |
97 | wxMask::wxMask(const wxBitmap& bitmap, int paletteIndex) | |
98 | : m_bitmap(NULL) | |
99 | { | |
100 | Create(bitmap, paletteIndex); | |
101 | } | |
102 | #endif // wxUSE_PALETTE | |
103 | ||
104 | wxMask::wxMask(const wxMask& mask) | |
105 | { | |
106 | m_bitmap = mask.m_bitmap ? new wxBitmap(*mask.m_bitmap) : NULL; | |
107 | } | |
108 | ||
109 | wxMask::~wxMask() | |
110 | { | |
111 | delete m_bitmap; | |
112 | } | |
113 | ||
114 | #warning "move this to common code" | |
115 | bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour) | |
116 | { | |
117 | delete m_bitmap; | |
118 | m_bitmap = NULL; | |
119 | ||
120 | wxColour clr(wxQuantizeColour(colour, bitmap)); | |
121 | ||
122 | wxImage imgSrc(bitmap.ConvertToImage()); | |
123 | imgSrc.SetMask(false); | |
124 | wxImage image(imgSrc.ConvertToMono(clr.Red(), clr.Green(), clr.Blue())); | |
125 | if ( !image.Ok() ) | |
126 | return false; | |
127 | ||
128 | m_bitmap = new wxBitmap(image, 1); | |
129 | ||
130 | return m_bitmap->Ok(); | |
131 | } | |
132 | ||
133 | #if wxUSE_PALETTE | |
134 | bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex) | |
135 | { | |
136 | unsigned char r,g,b; | |
137 | wxPalette *pal = bitmap.GetPalette(); | |
138 | ||
139 | wxCHECK_MSG( pal, false, wxT("Cannot create mask from bitmap without palette") ); | |
140 | ||
141 | pal->GetRGB(paletteIndex, &r, &g, &b); | |
142 | ||
143 | return Create(bitmap, wxColour(r, g, b)); | |
144 | } | |
145 | #endif // wxUSE_PALETTE | |
146 | ||
147 | bool wxMask::Create(const wxBitmap& bitmap) | |
148 | { | |
149 | delete m_bitmap; | |
150 | m_bitmap = NULL; | |
151 | ||
152 | wxCHECK_MSG( bitmap.Ok(), false, wxT("Invalid bitmap") ); | |
153 | wxCHECK_MSG( bitmap.GetDepth() == 1, false, wxT("Cannot create mask from colour bitmap") ); | |
154 | ||
155 | m_bitmap = new wxBitmap(bitmap); | |
156 | return true; | |
157 | } | |
158 | ||
159 | const wxBitmap& wxMask::GetBitmap() const | |
160 | { | |
161 | return m_bitmap ? *m_bitmap : wxNullBitmap; | |
162 | } | |
163 | ||
164 | ||
165 | //----------------------------------------------------------------------------- | |
166 | // wxBitmapRefData | |
167 | //----------------------------------------------------------------------------- | |
168 | ||
169 | class wxBitmapRefData: public wxObjectRefData | |
170 | { | |
171 | public: | |
172 | wxBitmapRefData() | |
173 | { | |
174 | m_mask = NULL; | |
175 | #if wxUSE_PALETTE | |
176 | m_palette = NULL; | |
177 | #endif | |
178 | } | |
179 | ||
180 | wxBitmapRefData(const wxBitmapRefData& data) | |
181 | { | |
182 | if ( data.m_surface ) | |
183 | m_surface = data.m_surface->Clone(); | |
184 | ||
185 | m_mask = data.m_mask ? new wxMask(*data.m_mask) : NULL; | |
186 | #if wxUSE_PALETTE | |
187 | m_palette = data.m_palette ? new wxPalette(*data.m_palette) : NULL; | |
188 | #endif | |
189 | } | |
190 | ||
191 | ~wxBitmapRefData() | |
192 | { | |
193 | delete m_mask; | |
194 | #if wxUSE_PALETTE | |
195 | delete m_palette; | |
196 | #endif | |
197 | } | |
198 | ||
199 | wxIDirectFBSurfacePtr m_surface; | |
200 | wxMask *m_mask; | |
201 | #if wxUSE_PALETTE | |
202 | wxPalette *m_palette; | |
203 | #endif | |
204 | }; | |
205 | ||
206 | #define M_BITMAP ((wxBitmapRefData *)m_refData) | |
207 | ||
208 | //----------------------------------------------------------------------------- | |
209 | // wxBitmap | |
210 | //----------------------------------------------------------------------------- | |
211 | ||
212 | IMPLEMENT_ABSTRACT_CLASS(wxBitmapHandler, wxObject) | |
213 | IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxBitmapBase) | |
214 | ||
215 | wxBitmap::wxBitmap(int width, int height, int depth) | |
216 | { | |
217 | Create(width, height, depth); | |
218 | } | |
219 | ||
220 | bool wxBitmap::Create(const wxIDirectFBSurfacePtr& surface) | |
221 | { | |
222 | UnRef(); | |
223 | ||
224 | wxCHECK_MSG( surface, false, _T("invalid surface") ); | |
225 | ||
226 | m_refData = new wxBitmapRefData(); | |
227 | M_BITMAP->m_surface = surface; | |
228 | return true; | |
229 | } | |
230 | ||
231 | bool wxBitmap::Create(int width, int height, int depth) | |
232 | { | |
233 | UnRef(); | |
234 | ||
235 | wxCHECK_MSG( width > 0 && height > 0, false, wxT("invalid bitmap size") ); | |
236 | wxCHECK_MSG( depth == -1, false, wxT("only default depth supported now") ); | |
237 | ||
238 | DFBSurfaceDescription desc; | |
239 | desc.flags = (DFBSurfaceDescriptionFlags)( | |
240 | DSDESC_CAPS | DSDESC_WIDTH | DSDESC_HEIGHT); | |
241 | desc.caps = DSCAPS_NONE; | |
242 | desc.width = width; | |
243 | desc.height = height; | |
244 | ||
245 | return Create(wxIDirectFB::Get()->CreateSurface(&desc)); | |
246 | } | |
247 | ||
248 | #warning "FIXME: move this to common code" | |
249 | bool wxBitmap::CreateFromXpm(const char **bits) | |
250 | { | |
251 | wxCHECK_MSG( bits != NULL, false, wxT("invalid bitmap data") ); | |
252 | ||
253 | #if wxUSE_IMAGE && wxUSE_XPM | |
254 | wxXPMDecoder decoder; | |
255 | wxImage img = decoder.ReadData(bits); | |
256 | wxCHECK_MSG( img.Ok(), false, wxT("invalid bitmap data") ); | |
257 | ||
258 | *this = wxBitmap(img); | |
259 | ||
260 | return true; | |
261 | #else | |
262 | wxFAIL_MSG( _T("creating bitmaps from XPMs not supported") ); | |
263 | return false; | |
264 | #endif // wxUSE_IMAGE && wxUSE_XPM | |
265 | } | |
266 | ||
267 | #if wxUSE_IMAGE | |
268 | wxBitmap::wxBitmap(const wxImage& image, int depth) | |
269 | { | |
270 | wxCHECK_RET( image.Ok(), wxT("invalid image") ); | |
271 | ||
272 | // create surface in screen's format: | |
273 | if ( !Create(image.GetWidth(), image.GetHeight(), depth) ) | |
274 | return; | |
275 | ||
276 | // then copy the image to it: | |
277 | wxIDirectFBSurfacePtr src(CreateSurfaceForImage(image)); | |
278 | wxIDirectFBSurfacePtr dst = M_BITMAP->m_surface; | |
279 | ||
280 | if ( !dst->SetBlittingFlags(DSBLIT_NOFX) ) | |
281 | return; | |
282 | if ( !dst->Blit(src->GetRaw(), NULL, 0, 0) ) | |
283 | return; | |
284 | ||
285 | // FIXME: implement mask creation from image's mask (or alpha channel?) | |
286 | wxASSERT_MSG( !image.HasMask(), _T("image masks are ignored for now") ); | |
287 | } | |
288 | ||
289 | wxImage wxBitmap::ConvertToImage() const | |
290 | { | |
291 | wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") ); | |
292 | ||
293 | wxImage img(GetWidth(), GetHeight()); | |
294 | wxIDirectFBSurfacePtr dst(CreateSurfaceForImage(img)); | |
295 | wxIDirectFBSurfacePtr src = M_BITMAP->m_surface; | |
296 | ||
297 | if ( !dst->SetBlittingFlags(DSBLIT_NOFX) ) | |
298 | return wxNullImage; | |
299 | if ( !dst->Blit(src->GetRaw(), NULL, 0, 0) ) | |
300 | return wxNullImage; | |
301 | ||
302 | // FIXME: implement mask setting in the image | |
303 | wxASSERT_MSG( GetMask() == NULL, _T("bitmap masks are ignored for now") ); | |
304 | ||
305 | return img; | |
306 | } | |
307 | #endif // wxUSE_IMAGE | |
308 | ||
309 | wxBitmap::wxBitmap(const wxString &filename, wxBitmapType type) | |
310 | { | |
311 | LoadFile(filename, type); | |
312 | } | |
313 | ||
314 | wxBitmap::wxBitmap(const char bits[], int width, int height, int depth) | |
315 | { | |
316 | wxCHECK_RET( depth == 1, wxT("can only create mono bitmap from XBM data") ); | |
317 | ||
318 | wxFAIL_MSG( _T("not implemented") ); | |
319 | } | |
320 | ||
321 | bool wxBitmap::Ok() const | |
322 | { | |
323 | return (m_refData != NULL && M_BITMAP->m_surface); | |
324 | } | |
325 | ||
326 | bool wxBitmap::operator==(const wxBitmap& bmp) const | |
327 | { | |
328 | // FIXME: is this the right way to compare bitmaps? | |
329 | return (m_refData == bmp.m_refData); | |
330 | } | |
331 | ||
332 | int wxBitmap::GetHeight() const | |
333 | { | |
334 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); | |
335 | ||
336 | int h = -1; | |
337 | M_BITMAP->m_surface->GetSize(NULL, &h); | |
338 | return h; | |
339 | } | |
340 | ||
341 | int wxBitmap::GetWidth() const | |
342 | { | |
343 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); | |
344 | ||
345 | int w = -1; | |
346 | M_BITMAP->m_surface->GetSize(&w, NULL); | |
347 | return w; | |
348 | } | |
349 | ||
350 | int wxBitmap::GetDepth() const | |
351 | { | |
352 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); | |
353 | ||
354 | return M_BITMAP->m_surface->GetDepth(); | |
355 | } | |
356 | ||
357 | wxMask *wxBitmap::GetMask() const | |
358 | { | |
359 | wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") ); | |
360 | ||
361 | return M_BITMAP->m_mask; | |
362 | } | |
363 | ||
364 | void wxBitmap::SetMask(wxMask *mask) | |
365 | { | |
366 | wxCHECK_RET( Ok(), wxT("invalid bitmap") ); | |
367 | ||
368 | delete M_BITMAP->m_mask; | |
369 | M_BITMAP->m_mask = mask; | |
370 | } | |
371 | ||
372 | bool wxBitmap::CopyFromIcon(const wxIcon& icon) | |
373 | { | |
374 | *this = *((wxBitmap*)(&icon)); | |
375 | return true; | |
376 | } | |
377 | ||
378 | wxBitmap wxBitmap::GetSubBitmap(const wxRect& rect) const | |
379 | { | |
380 | wxCHECK_MSG( Ok() && | |
381 | rect.x >= 0 && rect.y >= 0 && | |
382 | rect.x+rect.width <= GetWidth() && | |
383 | rect.y+rect.height <= GetHeight(), | |
384 | wxNullBitmap, | |
385 | wxT("invalid bitmap or bitmap region") ); | |
386 | ||
387 | // NB: DirectFB subsurfaces share the same pixels buffer, so we must | |
388 | // clone the obtained subsurface | |
389 | DFBRectangle r = { rect.x, rect.y, rect.width, rect.height }; | |
390 | return wxBitmap(M_BITMAP->m_surface->GetSubSurface(&r)->Clone()); | |
391 | } | |
392 | ||
393 | #warning "to common code" | |
394 | bool wxBitmap::LoadFile(const wxString &name, wxBitmapType type) | |
395 | { | |
396 | UnRef(); | |
397 | ||
398 | wxBitmapHandler *handler = FindHandler(type); | |
399 | ||
400 | if ( handler == NULL ) | |
401 | { | |
402 | wxImage image; | |
403 | if ( !image.LoadFile(name, type) || !image.Ok() ) | |
404 | { | |
405 | wxLogError("no bitmap handler for type %d defined.", type); | |
406 | return false; | |
407 | } | |
408 | else | |
409 | { | |
410 | *this = wxBitmap(image); | |
411 | return true; | |
412 | } | |
413 | } | |
414 | ||
415 | m_refData = new wxBitmapRefData(); | |
416 | ||
417 | return handler->LoadFile(this, name, type, -1, -1); | |
418 | } | |
419 | ||
420 | #warning "to common code" | |
421 | bool wxBitmap::SaveFile(const wxString& filename, wxBitmapType type, const wxPalette *palette) const | |
422 | { | |
423 | wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") ); | |
424 | ||
425 | wxBitmapHandler *handler = FindHandler(type); | |
426 | ||
427 | if ( handler == NULL ) | |
428 | { | |
429 | wxImage image = ConvertToImage(); | |
430 | #if wxUSE_PALETTE | |
431 | if ( palette ) | |
432 | image.SetPalette(*palette); | |
433 | #endif // wxUSE_PALETTE | |
434 | ||
435 | if ( image.Ok() ) | |
436 | return image.SaveFile(filename, type); | |
437 | else | |
438 | { | |
439 | wxLogError("no bitmap handler for type %d defined.", type); | |
440 | return false; | |
441 | } | |
442 | } | |
443 | ||
444 | return handler->SaveFile(this, filename, type, palette); | |
445 | } | |
446 | ||
447 | #if wxUSE_PALETTE | |
448 | wxPalette *wxBitmap::GetPalette() const | |
449 | { | |
450 | wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") ); | |
451 | ||
452 | return M_BITMAP->m_palette; | |
453 | } | |
454 | ||
455 | void wxBitmap::SetPalette(const wxPalette& palette) | |
456 | { | |
457 | wxCHECK_RET( Ok(), wxT("invalid bitmap") ); | |
458 | wxCHECK_RET( GetDepth() > 1 && GetDepth() <= 8, wxT("cannot set palette for bitmap of this depth") ); | |
459 | ||
460 | delete M_BITMAP->m_palette; | |
461 | M_BITMAP->m_palette = NULL; | |
462 | ||
463 | if ( !palette.Ok() ) return; | |
464 | ||
465 | M_BITMAP->m_palette = new wxPalette(palette); | |
466 | } | |
467 | #endif // wxUSE_PALETTE | |
468 | ||
469 | void wxBitmap::SetHeight(int height) | |
470 | { | |
471 | AllocExclusive(); | |
472 | ||
473 | wxFAIL_MSG( _T("SetHeight not implemented") ); | |
474 | } | |
475 | ||
476 | void wxBitmap::SetWidth(int width) | |
477 | { | |
478 | AllocExclusive(); | |
479 | ||
480 | wxFAIL_MSG( _T("SetWidth not implemented") ); | |
481 | } | |
482 | ||
483 | void wxBitmap::SetDepth(int depth) | |
484 | { | |
485 | AllocExclusive(); | |
486 | ||
487 | wxFAIL_MSG( _T("SetDepth not implemented") ); | |
488 | } | |
489 | ||
490 | wxIDirectFBSurfacePtr wxBitmap::GetDirectFBSurface() const | |
491 | { | |
492 | wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") ); | |
493 | ||
494 | return M_BITMAP->m_surface; | |
495 | } | |
496 | ||
497 | wxObjectRefData *wxBitmap::CreateRefData() const | |
498 | { | |
499 | return new wxBitmapRefData; | |
500 | } | |
501 | ||
502 | wxObjectRefData *wxBitmap::CloneRefData(const wxObjectRefData *data) const | |
503 | { | |
504 | return new wxBitmapRefData(*(wxBitmapRefData *)data); | |
505 | } | |
506 | ||
507 | ||
508 | /*static*/ | |
509 | void wxBitmap::InitStandardHandlers() | |
510 | { | |
511 | // not wxBitmap handlers, we rely on wxImage | |
512 | } |