]>
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 | ||
b3c86150 VS |
197 | int wxBitmap::GetHeight() const |
198 | { | |
199 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); | |
200 | ||
201 | int h = -1; | |
52c8d32a | 202 | M_BITMAP->m_surface->GetSize(NULL, &h); |
b3c86150 VS |
203 | return h; |
204 | } | |
205 | ||
206 | int wxBitmap::GetWidth() const | |
207 | { | |
208 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); | |
209 | ||
210 | int w = -1; | |
52c8d32a | 211 | M_BITMAP->m_surface->GetSize(&w, NULL); |
b3c86150 VS |
212 | return w; |
213 | } | |
214 | ||
215 | int wxBitmap::GetDepth() const | |
216 | { | |
217 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); | |
218 | ||
a5b31f4e | 219 | return M_BITMAP->m_surface->GetDepth(); |
b3c86150 VS |
220 | } |
221 | ||
222 | wxMask *wxBitmap::GetMask() const | |
223 | { | |
224 | wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") ); | |
225 | ||
226 | return M_BITMAP->m_mask; | |
227 | } | |
228 | ||
229 | void wxBitmap::SetMask(wxMask *mask) | |
230 | { | |
231 | wxCHECK_RET( Ok(), wxT("invalid bitmap") ); | |
232 | ||
55ccdb93 | 233 | AllocExclusive(); |
b3c86150 VS |
234 | delete M_BITMAP->m_mask; |
235 | M_BITMAP->m_mask = mask; | |
236 | } | |
237 | ||
238 | bool wxBitmap::CopyFromIcon(const wxIcon& icon) | |
239 | { | |
240 | *this = *((wxBitmap*)(&icon)); | |
241 | return true; | |
242 | } | |
243 | ||
244 | wxBitmap wxBitmap::GetSubBitmap(const wxRect& rect) const | |
245 | { | |
246 | wxCHECK_MSG( Ok() && | |
247 | rect.x >= 0 && rect.y >= 0 && | |
248 | rect.x+rect.width <= GetWidth() && | |
249 | rect.y+rect.height <= GetHeight(), | |
250 | wxNullBitmap, | |
251 | wxT("invalid bitmap or bitmap region") ); | |
252 | ||
4562386d VS |
253 | // NB: DirectFB subsurfaces share the same pixels buffer, so we must |
254 | // clone the obtained subsurface | |
255 | DFBRectangle r = { rect.x, rect.y, rect.width, rect.height }; | |
256 | return wxBitmap(M_BITMAP->m_surface->GetSubSurface(&r)->Clone()); | |
b3c86150 VS |
257 | } |
258 | ||
259 | #warning "to common code" | |
260 | bool wxBitmap::LoadFile(const wxString &name, wxBitmapType type) | |
261 | { | |
262 | UnRef(); | |
263 | ||
264 | wxBitmapHandler *handler = FindHandler(type); | |
265 | ||
266 | if ( handler == NULL ) | |
267 | { | |
268 | wxImage image; | |
269 | if ( !image.LoadFile(name, type) || !image.Ok() ) | |
270 | { | |
271 | wxLogError("no bitmap handler for type %d defined.", type); | |
272 | return false; | |
273 | } | |
274 | else | |
275 | { | |
276 | *this = wxBitmap(image); | |
277 | return true; | |
278 | } | |
279 | } | |
280 | ||
281 | m_refData = new wxBitmapRefData(); | |
282 | ||
283 | return handler->LoadFile(this, name, type, -1, -1); | |
284 | } | |
285 | ||
286 | #warning "to common code" | |
287 | bool wxBitmap::SaveFile(const wxString& filename, wxBitmapType type, const wxPalette *palette) const | |
288 | { | |
289 | wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") ); | |
290 | ||
291 | wxBitmapHandler *handler = FindHandler(type); | |
292 | ||
293 | if ( handler == NULL ) | |
294 | { | |
295 | wxImage image = ConvertToImage(); | |
296 | #if wxUSE_PALETTE | |
297 | if ( palette ) | |
298 | image.SetPalette(*palette); | |
299 | #endif // wxUSE_PALETTE | |
300 | ||
301 | if ( image.Ok() ) | |
302 | return image.SaveFile(filename, type); | |
303 | else | |
304 | { | |
305 | wxLogError("no bitmap handler for type %d defined.", type); | |
306 | return false; | |
307 | } | |
308 | } | |
309 | ||
310 | return handler->SaveFile(this, filename, type, palette); | |
311 | } | |
312 | ||
313 | #if wxUSE_PALETTE | |
314 | wxPalette *wxBitmap::GetPalette() const | |
315 | { | |
316 | wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") ); | |
317 | ||
318 | return M_BITMAP->m_palette; | |
319 | } | |
320 | ||
321 | void wxBitmap::SetPalette(const wxPalette& palette) | |
322 | { | |
323 | wxCHECK_RET( Ok(), wxT("invalid bitmap") ); | |
324 | wxCHECK_RET( GetDepth() > 1 && GetDepth() <= 8, wxT("cannot set palette for bitmap of this depth") ); | |
325 | ||
55ccdb93 | 326 | AllocExclusive(); |
b3c86150 VS |
327 | delete M_BITMAP->m_palette; |
328 | M_BITMAP->m_palette = NULL; | |
329 | ||
330 | if ( !palette.Ok() ) return; | |
331 | ||
332 | M_BITMAP->m_palette = new wxPalette(palette); | |
333 | } | |
334 | #endif // wxUSE_PALETTE | |
335 | ||
336 | void wxBitmap::SetHeight(int height) | |
337 | { | |
338 | AllocExclusive(); | |
3278b06e VS |
339 | |
340 | wxFAIL_MSG( _T("SetHeight not implemented") ); | |
b3c86150 VS |
341 | } |
342 | ||
343 | void wxBitmap::SetWidth(int width) | |
344 | { | |
345 | AllocExclusive(); | |
3278b06e VS |
346 | |
347 | wxFAIL_MSG( _T("SetWidth not implemented") ); | |
b3c86150 VS |
348 | } |
349 | ||
350 | void wxBitmap::SetDepth(int depth) | |
351 | { | |
352 | AllocExclusive(); | |
3278b06e VS |
353 | |
354 | wxFAIL_MSG( _T("SetDepth not implemented") ); | |
b3c86150 VS |
355 | } |
356 | ||
52c8d32a | 357 | wxIDirectFBSurfacePtr wxBitmap::GetDirectFBSurface() const |
b3c86150 VS |
358 | { |
359 | wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") ); | |
360 | ||
361 | return M_BITMAP->m_surface; | |
362 | } | |
363 | ||
364 | wxObjectRefData *wxBitmap::CreateRefData() const | |
365 | { | |
366 | return new wxBitmapRefData; | |
367 | } | |
368 | ||
369 | wxObjectRefData *wxBitmap::CloneRefData(const wxObjectRefData *data) const | |
370 | { | |
371 | return new wxBitmapRefData(*(wxBitmapRefData *)data); | |
372 | } | |
373 | ||
374 | ||
375 | /*static*/ | |
376 | void wxBitmap::InitStandardHandlers() | |
377 | { | |
378 | // not wxBitmap handlers, we rely on wxImage | |
379 | } |