]>
Commit | Line | Data |
---|---|---|
b3c86150 VS |
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 | ||
b3c86150 VS |
27 | #include "wx/dfb/private.h" |
28 | ||
29 | //----------------------------------------------------------------------------- | |
30 | // helpers | |
31 | //----------------------------------------------------------------------------- | |
32 | ||
a46d4814 VS |
33 | // Creates a surface that will use wxImage's pixel data (RGB only) |
34 | static wxIDirectFBSurfacePtr CreateSurfaceForImage(const wxImage& image) | |
35 | { | |
36 | wxCHECK_MSG( image.Ok(), NULL, _T("invalid image") ); | |
37 | // FIXME_DFB: implement alpha handling by merging alpha buffer with RGB | |
38 | // into a temporary RGBA surface | |
39 | wxCHECK_MSG( !image.HasAlpha(), NULL, _T("alpha channel not supported") ); | |
40 | ||
41 | DFBSurfaceDescription desc; | |
42 | desc.flags = (DFBSurfaceDescriptionFlags) | |
43 | (DSDESC_CAPS | DSDESC_WIDTH | DSDESC_HEIGHT | DSDESC_PIXELFORMAT | | |
44 | DSDESC_PREALLOCATED); | |
45 | desc.caps = DSCAPS_NONE; | |
46 | desc.width = image.GetWidth(); | |
47 | desc.height = image.GetHeight(); | |
48 | desc.pixelformat = DSPF_RGB24; | |
49 | desc.preallocated[0].data = image.GetData(); | |
50 | desc.preallocated[0].pitch = 3 * desc.width; | |
51 | ||
52 | return wxIDirectFB::Get()->CreateSurface(&desc); | |
53 | } | |
b3c86150 | 54 | |
b3c86150 VS |
55 | //----------------------------------------------------------------------------- |
56 | // wxBitmapRefData | |
57 | //----------------------------------------------------------------------------- | |
58 | ||
59 | class wxBitmapRefData: public wxObjectRefData | |
60 | { | |
61 | public: | |
62 | wxBitmapRefData() | |
63 | { | |
64 | m_mask = NULL; | |
65 | #if wxUSE_PALETTE | |
66 | m_palette = NULL; | |
67 | #endif | |
68 | } | |
69 | ||
70 | wxBitmapRefData(const wxBitmapRefData& data) | |
71 | { | |
a5b31f4e VS |
72 | if ( data.m_surface ) |
73 | m_surface = data.m_surface->Clone(); | |
74 | ||
b3c86150 VS |
75 | m_mask = data.m_mask ? new wxMask(*data.m_mask) : NULL; |
76 | #if wxUSE_PALETTE | |
77 | m_palette = data.m_palette ? new wxPalette(*data.m_palette) : NULL; | |
78 | #endif | |
79 | } | |
80 | ||
81 | ~wxBitmapRefData() | |
82 | { | |
83 | delete m_mask; | |
84 | #if wxUSE_PALETTE | |
85 | delete m_palette; | |
86 | #endif | |
87 | } | |
88 | ||
52c8d32a VS |
89 | wxIDirectFBSurfacePtr m_surface; |
90 | wxMask *m_mask; | |
b3c86150 | 91 | #if wxUSE_PALETTE |
52c8d32a | 92 | wxPalette *m_palette; |
b3c86150 VS |
93 | #endif |
94 | }; | |
95 | ||
96 | #define M_BITMAP ((wxBitmapRefData *)m_refData) | |
97 | ||
98 | //----------------------------------------------------------------------------- | |
99 | // wxBitmap | |
100 | //----------------------------------------------------------------------------- | |
101 | ||
452418c4 | 102 | IMPLEMENT_ABSTRACT_CLASS(wxBitmapHandler, wxBitmapHandlerBase) |
b3c86150 VS |
103 | IMPLEMENT_DYNAMIC_CLASS(wxBitmap, wxBitmapBase) |
104 | ||
105 | wxBitmap::wxBitmap(int width, int height, int depth) | |
106 | { | |
107 | Create(width, height, depth); | |
108 | } | |
109 | ||
4562386d VS |
110 | bool wxBitmap::Create(const wxIDirectFBSurfacePtr& surface) |
111 | { | |
112 | UnRef(); | |
113 | ||
114 | wxCHECK_MSG( surface, false, _T("invalid surface") ); | |
115 | ||
116 | m_refData = new wxBitmapRefData(); | |
117 | M_BITMAP->m_surface = surface; | |
118 | return true; | |
119 | } | |
120 | ||
b3c86150 VS |
121 | bool wxBitmap::Create(int width, int height, int depth) |
122 | { | |
123 | UnRef(); | |
124 | ||
125 | wxCHECK_MSG( width > 0 && height > 0, false, wxT("invalid bitmap size") ); | |
949e0a38 | 126 | wxCHECK_MSG( depth == -1, false, wxT("only default depth supported now") ); |
b3c86150 VS |
127 | |
128 | DFBSurfaceDescription desc; | |
129 | desc.flags = (DFBSurfaceDescriptionFlags)( | |
130 | DSDESC_CAPS | DSDESC_WIDTH | DSDESC_HEIGHT); | |
131 | desc.caps = DSCAPS_NONE; | |
132 | desc.width = width; | |
133 | desc.height = height; | |
134 | ||
4562386d | 135 | return Create(wxIDirectFB::Get()->CreateSurface(&desc)); |
b3c86150 VS |
136 | } |
137 | ||
b3c86150 VS |
138 | #if wxUSE_IMAGE |
139 | wxBitmap::wxBitmap(const wxImage& image, int depth) | |
140 | { | |
141 | wxCHECK_RET( image.Ok(), wxT("invalid image") ); | |
a46d4814 VS |
142 | |
143 | // create surface in screen's format: | |
144 | if ( !Create(image.GetWidth(), image.GetHeight(), depth) ) | |
145 | return; | |
146 | ||
147 | // then copy the image to it: | |
148 | wxIDirectFBSurfacePtr src(CreateSurfaceForImage(image)); | |
149 | wxIDirectFBSurfacePtr dst = M_BITMAP->m_surface; | |
150 | ||
151 | if ( !dst->SetBlittingFlags(DSBLIT_NOFX) ) | |
152 | return; | |
153 | if ( !dst->Blit(src->GetRaw(), NULL, 0, 0) ) | |
154 | return; | |
155 | ||
156 | // FIXME: implement mask creation from image's mask (or alpha channel?) | |
157 | wxASSERT_MSG( !image.HasMask(), _T("image masks are ignored for now") ); | |
b3c86150 VS |
158 | } |
159 | ||
160 | wxImage wxBitmap::ConvertToImage() const | |
161 | { | |
162 | wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") ); | |
163 | ||
a46d4814 VS |
164 | wxImage img(GetWidth(), GetHeight()); |
165 | wxIDirectFBSurfacePtr dst(CreateSurfaceForImage(img)); | |
166 | wxIDirectFBSurfacePtr src = M_BITMAP->m_surface; | |
167 | ||
168 | if ( !dst->SetBlittingFlags(DSBLIT_NOFX) ) | |
169 | return wxNullImage; | |
170 | if ( !dst->Blit(src->GetRaw(), NULL, 0, 0) ) | |
171 | return wxNullImage; | |
172 | ||
173 | // FIXME: implement mask setting in the image | |
174 | wxASSERT_MSG( GetMask() == NULL, _T("bitmap masks are ignored for now") ); | |
175 | ||
176 | return img; | |
b3c86150 VS |
177 | } |
178 | #endif // wxUSE_IMAGE | |
179 | ||
180 | wxBitmap::wxBitmap(const wxString &filename, wxBitmapType type) | |
181 | { | |
182 | LoadFile(filename, type); | |
183 | } | |
184 | ||
185 | wxBitmap::wxBitmap(const char bits[], int width, int height, int depth) | |
186 | { | |
187 | wxCHECK_RET( depth == 1, wxT("can only create mono bitmap from XBM data") ); | |
949e0a38 VS |
188 | |
189 | wxFAIL_MSG( _T("not implemented") ); | |
b3c86150 VS |
190 | } |
191 | ||
b7cacb43 | 192 | bool wxBitmap::IsOk() const |
b3c86150 VS |
193 | { |
194 | return (m_refData != NULL && M_BITMAP->m_surface); | |
195 | } | |
196 | ||
197 | bool wxBitmap::operator==(const wxBitmap& bmp) const | |
198 | { | |
199 | // FIXME: is this the right way to compare bitmaps? | |
200 | return (m_refData == bmp.m_refData); | |
201 | } | |
202 | ||
203 | int wxBitmap::GetHeight() const | |
204 | { | |
205 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); | |
206 | ||
207 | int h = -1; | |
52c8d32a | 208 | M_BITMAP->m_surface->GetSize(NULL, &h); |
b3c86150 VS |
209 | return h; |
210 | } | |
211 | ||
212 | int wxBitmap::GetWidth() const | |
213 | { | |
214 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); | |
215 | ||
216 | int w = -1; | |
52c8d32a | 217 | M_BITMAP->m_surface->GetSize(&w, NULL); |
b3c86150 VS |
218 | return w; |
219 | } | |
220 | ||
221 | int wxBitmap::GetDepth() const | |
222 | { | |
223 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); | |
224 | ||
a5b31f4e | 225 | return M_BITMAP->m_surface->GetDepth(); |
b3c86150 VS |
226 | } |
227 | ||
228 | wxMask *wxBitmap::GetMask() const | |
229 | { | |
230 | wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") ); | |
231 | ||
232 | return M_BITMAP->m_mask; | |
233 | } | |
234 | ||
235 | void wxBitmap::SetMask(wxMask *mask) | |
236 | { | |
237 | wxCHECK_RET( Ok(), wxT("invalid bitmap") ); | |
238 | ||
239 | delete M_BITMAP->m_mask; | |
240 | M_BITMAP->m_mask = mask; | |
241 | } | |
242 | ||
243 | bool wxBitmap::CopyFromIcon(const wxIcon& icon) | |
244 | { | |
245 | *this = *((wxBitmap*)(&icon)); | |
246 | return true; | |
247 | } | |
248 | ||
249 | wxBitmap wxBitmap::GetSubBitmap(const wxRect& rect) const | |
250 | { | |
251 | wxCHECK_MSG( Ok() && | |
252 | rect.x >= 0 && rect.y >= 0 && | |
253 | rect.x+rect.width <= GetWidth() && | |
254 | rect.y+rect.height <= GetHeight(), | |
255 | wxNullBitmap, | |
256 | wxT("invalid bitmap or bitmap region") ); | |
257 | ||
4562386d VS |
258 | // NB: DirectFB subsurfaces share the same pixels buffer, so we must |
259 | // clone the obtained subsurface | |
260 | DFBRectangle r = { rect.x, rect.y, rect.width, rect.height }; | |
261 | return wxBitmap(M_BITMAP->m_surface->GetSubSurface(&r)->Clone()); | |
b3c86150 VS |
262 | } |
263 | ||
264 | #warning "to common code" | |
265 | bool wxBitmap::LoadFile(const wxString &name, wxBitmapType type) | |
266 | { | |
267 | UnRef(); | |
268 | ||
269 | wxBitmapHandler *handler = FindHandler(type); | |
270 | ||
271 | if ( handler == NULL ) | |
272 | { | |
273 | wxImage image; | |
274 | if ( !image.LoadFile(name, type) || !image.Ok() ) | |
275 | { | |
276 | wxLogError("no bitmap handler for type %d defined.", type); | |
277 | return false; | |
278 | } | |
279 | else | |
280 | { | |
281 | *this = wxBitmap(image); | |
282 | return true; | |
283 | } | |
284 | } | |
285 | ||
286 | m_refData = new wxBitmapRefData(); | |
287 | ||
288 | return handler->LoadFile(this, name, type, -1, -1); | |
289 | } | |
290 | ||
291 | #warning "to common code" | |
292 | bool wxBitmap::SaveFile(const wxString& filename, wxBitmapType type, const wxPalette *palette) const | |
293 | { | |
294 | wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") ); | |
295 | ||
296 | wxBitmapHandler *handler = FindHandler(type); | |
297 | ||
298 | if ( handler == NULL ) | |
299 | { | |
300 | wxImage image = ConvertToImage(); | |
301 | #if wxUSE_PALETTE | |
302 | if ( palette ) | |
303 | image.SetPalette(*palette); | |
304 | #endif // wxUSE_PALETTE | |
305 | ||
306 | if ( image.Ok() ) | |
307 | return image.SaveFile(filename, type); | |
308 | else | |
309 | { | |
310 | wxLogError("no bitmap handler for type %d defined.", type); | |
311 | return false; | |
312 | } | |
313 | } | |
314 | ||
315 | return handler->SaveFile(this, filename, type, palette); | |
316 | } | |
317 | ||
318 | #if wxUSE_PALETTE | |
319 | wxPalette *wxBitmap::GetPalette() const | |
320 | { | |
321 | wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") ); | |
322 | ||
323 | return M_BITMAP->m_palette; | |
324 | } | |
325 | ||
326 | void wxBitmap::SetPalette(const wxPalette& palette) | |
327 | { | |
328 | wxCHECK_RET( Ok(), wxT("invalid bitmap") ); | |
329 | wxCHECK_RET( GetDepth() > 1 && GetDepth() <= 8, wxT("cannot set palette for bitmap of this depth") ); | |
330 | ||
331 | delete M_BITMAP->m_palette; | |
332 | M_BITMAP->m_palette = NULL; | |
333 | ||
334 | if ( !palette.Ok() ) return; | |
335 | ||
336 | M_BITMAP->m_palette = new wxPalette(palette); | |
337 | } | |
338 | #endif // wxUSE_PALETTE | |
339 | ||
340 | void wxBitmap::SetHeight(int height) | |
341 | { | |
342 | AllocExclusive(); | |
3278b06e VS |
343 | |
344 | wxFAIL_MSG( _T("SetHeight not implemented") ); | |
b3c86150 VS |
345 | } |
346 | ||
347 | void wxBitmap::SetWidth(int width) | |
348 | { | |
349 | AllocExclusive(); | |
3278b06e VS |
350 | |
351 | wxFAIL_MSG( _T("SetWidth not implemented") ); | |
b3c86150 VS |
352 | } |
353 | ||
354 | void wxBitmap::SetDepth(int depth) | |
355 | { | |
356 | AllocExclusive(); | |
3278b06e VS |
357 | |
358 | wxFAIL_MSG( _T("SetDepth not implemented") ); | |
b3c86150 VS |
359 | } |
360 | ||
52c8d32a | 361 | wxIDirectFBSurfacePtr wxBitmap::GetDirectFBSurface() const |
b3c86150 VS |
362 | { |
363 | wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") ); | |
364 | ||
365 | return M_BITMAP->m_surface; | |
366 | } | |
367 | ||
368 | wxObjectRefData *wxBitmap::CreateRefData() const | |
369 | { | |
370 | return new wxBitmapRefData; | |
371 | } | |
372 | ||
373 | wxObjectRefData *wxBitmap::CloneRefData(const wxObjectRefData *data) const | |
374 | { | |
375 | return new wxBitmapRefData(*(wxBitmapRefData *)data); | |
376 | } | |
377 | ||
378 | ||
379 | /*static*/ | |
380 | void wxBitmap::InitStandardHandlers() | |
381 | { | |
382 | // not wxBitmap handlers, we rely on wxImage | |
383 | } |