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