]>
Commit | Line | Data |
---|---|---|
32b8ec41 VZ |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: bitmap.cpp | |
3 | // Author: Vaclav Slavik | |
4 | // RCS-ID: $Id$ | |
8f7b34a8 | 5 | // Copyright: (c) 2001 SciTech Software, Inc. (www.scitechsoft.com) |
32b8ec41 VZ |
6 | // Licence: wxWindows licence |
7 | ///////////////////////////////////////////////////////////////////////////// | |
8 | ||
9 | #ifdef __GNUG__ | |
10 | #pragma implementation "bitmap.h" | |
11 | #endif | |
12 | ||
13 | // For compilers that support precompilation, includes "wx.h". | |
14 | #include "wx/wxprec.h" | |
15 | ||
16 | #ifdef __BORLANDC__ | |
17 | #pragma hdrstop | |
18 | #endif | |
19 | ||
20 | #include "wx/bitmap.h" | |
21 | #include "wx/icon.h" | |
22 | #include "wx/filefn.h" | |
23 | #include "wx/image.h" | |
24 | #include "wx/dcmemory.h" | |
25 | #include "wx/utils.h" | |
26 | #include "wx/log.h" | |
27 | #include "wx/intl.h" | |
28 | #include "wx/image.h" | |
29 | #include "wx/xpmdecod.h" | |
30 | ||
31 | #include "wx/mgl/private.h" | |
32 | ||
33 | #include <mgraph.hpp> | |
34 | ||
35 | //----------------------------------------------------------------------------- | |
36 | // MGL pixel formats: | |
37 | //----------------------------------------------------------------------------- | |
38 | ||
39 | static pixel_format_t gs_pixel_format_15 = | |
40 | {0x1F,0x0A,3, 0x1F,0x05,3, 0x1F,0x00,3, 0x01,0x0F,7}; // 555 15bpp | |
41 | ||
42 | static pixel_format_t gs_pixel_format_16 = | |
43 | {0x1F,0x0B,3, 0x3F,0x05,2, 0x1F,0x00,3, 0x00,0x00,0}; // 565 16bpp | |
44 | ||
45 | static pixel_format_t gs_pixel_format_24 = | |
46 | {0xFF,0x10,0, 0xFF,0x08,0, 0xFF,0x00,0, 0x00,0x00,0}; // RGB 24bpp | |
47 | ||
48 | static pixel_format_t gs_pixel_format_32 = | |
49 | {0xFF,0x18,0, 0xFF,0x10,0, 0xFF,0x08,0, 0xFF,0x00,0}; // RGBA 32bpp | |
50 | ||
51 | // FIXME_MGL -- these formats will probably have to go into another place, | |
52 | // where wxApp could use them to initialize g_displayDC | |
53 | ||
54 | ||
55 | //----------------------------------------------------------------------------- | |
56 | // wxMask | |
57 | //----------------------------------------------------------------------------- | |
58 | ||
59 | IMPLEMENT_DYNAMIC_CLASS(wxMask,wxObject) | |
60 | ||
61 | wxMask::wxMask() | |
62 | { | |
63 | m_bitmap = NULL; | |
64 | } | |
65 | ||
66 | wxMask::wxMask(const wxBitmap& bitmap, const wxColour& colour) | |
67 | { | |
68 | m_bitmap = NULL; | |
69 | Create(bitmap, colour); | |
70 | } | |
71 | ||
72 | wxMask::wxMask(const wxBitmap& bitmap, int paletteIndex) | |
73 | { | |
74 | m_bitmap = NULL; | |
75 | Create(bitmap, paletteIndex); | |
76 | } | |
77 | ||
78 | wxMask::wxMask(const wxBitmap& bitmap) | |
79 | { | |
80 | m_bitmap = NULL; | |
81 | Create(bitmap); | |
82 | } | |
83 | ||
84 | wxMask::~wxMask() | |
85 | { | |
86 | delete m_bitmap; | |
87 | } | |
88 | ||
89 | bool wxMask::Create(const wxBitmap& bitmap, const wxColour& colour) | |
90 | { | |
91 | delete m_bitmap; | |
92 | m_bitmap = NULL; | |
93 | ||
94 | wxImage image = bitmap.ConvertToImage().ConvertToMono( | |
95 | colour.Red(), colour.Green(), colour.Blue()); | |
96 | if ( !image.Ok() ) return FALSE; | |
97 | ||
98 | m_bitmap = new wxBitmap(image, 1); | |
99 | ||
100 | return m_bitmap->Ok(); | |
101 | } | |
102 | ||
103 | bool wxMask::Create(const wxBitmap& bitmap, int paletteIndex) | |
104 | { | |
105 | unsigned char r,g,b; | |
106 | wxPalette *pal = bitmap.GetPalette(); | |
107 | ||
108 | wxCHECK_MSG( pal, FALSE, wxT("Cannot create mask from bitmap without palette") ); | |
109 | ||
110 | pal->GetRGB(paletteIndex, &r, &g, &b); | |
111 | ||
112 | return Create(bitmap, wxColour(r, g, b)); | |
113 | } | |
114 | ||
115 | bool wxMask::Create(const wxBitmap& bitmap) | |
116 | { | |
117 | delete m_bitmap; | |
118 | m_bitmap = NULL; | |
119 | ||
120 | wxCHECK_MSG( bitmap.Ok(), FALSE, wxT("Invalid bitmap") ); | |
121 | wxCHECK_MSG( bitmap.GetDepth() == 1, FALSE, wxT("Cannot create mask from colour bitmap") ); | |
122 | ||
123 | m_bitmap = new wxBitmap(bitmap); | |
124 | return TRUE; | |
125 | } | |
126 | ||
127 | ||
128 | //----------------------------------------------------------------------------- | |
129 | // wxBitmap | |
130 | //----------------------------------------------------------------------------- | |
131 | ||
132 | class wxBitmapRefData: public wxObjectRefData | |
133 | { | |
134 | public: | |
135 | wxBitmapRefData(); | |
136 | ~wxBitmapRefData(); | |
137 | ||
138 | int m_width; | |
139 | int m_height; | |
140 | int m_bpp; | |
141 | wxPalette *m_palette; | |
142 | wxMask *m_mask; | |
143 | bitmap_t *m_bitmap; | |
144 | }; | |
145 | ||
146 | wxBitmapRefData::wxBitmapRefData() | |
147 | { | |
148 | m_mask = NULL; | |
149 | m_width = 0; | |
150 | m_height = 0; | |
151 | m_bpp = 0; | |
152 | m_palette = NULL; | |
153 | m_bitmap = NULL; | |
154 | } | |
155 | ||
156 | wxBitmapRefData::~wxBitmapRefData() | |
157 | { | |
158 | if ( m_bitmap ) | |
159 | MGL_unloadBitmap(m_bitmap); | |
160 | delete m_mask; | |
161 | delete m_palette; | |
162 | } | |
163 | ||
164 | //----------------------------------------------------------------------------- | |
165 | ||
166 | #define M_BMPDATA ((wxBitmapRefData *)m_refData) | |
167 | ||
168 | ||
169 | IMPLEMENT_ABSTRACT_CLASS(wxBitmapHandler,wxObject) | |
170 | IMPLEMENT_DYNAMIC_CLASS(wxBitmap,wxBitmapBase) | |
171 | ||
172 | wxBitmap::wxBitmap() | |
173 | { | |
174 | if ( wxTheBitmapList ) wxTheBitmapList->AddBitmap(this); | |
175 | } | |
176 | ||
177 | wxBitmap::wxBitmap(int width, int height, int depth) | |
178 | { | |
179 | Create(width, height, depth); | |
180 | ||
181 | if ( wxTheBitmapList ) wxTheBitmapList->AddBitmap(this); | |
182 | } | |
183 | ||
184 | ||
185 | static bitmap_t *MyMGL_createBitmap(int width, int height, | |
186 | int bpp, pixel_format_t *pf) | |
187 | { | |
188 | MGLMemoryDC mdc(width, height, bpp, pf); | |
189 | return MGL_getBitmapFromDC(mdc.getDC(), 0, 0, width, height, TRUE); | |
190 | } | |
191 | ||
192 | bool wxBitmap::Create(int width, int height, int depth) | |
193 | { | |
194 | UnRef(); | |
195 | ||
196 | wxCHECK_MSG( (width > 0) && (height > 0), FALSE, wxT("invalid bitmap size") ) | |
197 | ||
198 | pixel_format_t pf_dummy, *pf; | |
199 | int mglDepth = depth; | |
200 | ||
201 | switch ( depth ) | |
202 | { | |
203 | case -1: | |
204 | wxASSERT_MSG( g_displayDC, wxT("MGL display DC not created yet.") ); | |
205 | ||
206 | g_displayDC->getPixelFormat(pf_dummy); | |
207 | mglDepth = g_displayDC->getBitsPerPixel(); | |
208 | pf = &pf_dummy; | |
209 | break; | |
210 | case 1: | |
211 | case 8: | |
212 | pf = NULL; | |
213 | break; | |
214 | case 15: | |
215 | pf = &gs_pixel_format_15; | |
216 | break; | |
217 | case 16: | |
218 | pf = &gs_pixel_format_16; | |
219 | break; | |
220 | case 24: | |
221 | pf = &gs_pixel_format_24; | |
222 | break; | |
223 | case 32: | |
224 | pf = &gs_pixel_format_32; | |
225 | break; | |
226 | default: | |
227 | wxASSERT_MSG( 0, wxT("invalid bitmap depth") ); | |
228 | return FALSE; | |
229 | break; | |
230 | } | |
231 | ||
232 | m_refData = new wxBitmapRefData(); | |
233 | M_BMPDATA->m_mask = (wxMask *) NULL; | |
234 | M_BMPDATA->m_palette = (wxPalette *) NULL; | |
235 | M_BMPDATA->m_width = width; | |
236 | M_BMPDATA->m_height = height; | |
237 | M_BMPDATA->m_bpp = mglDepth; | |
238 | ||
239 | if ( mglDepth != 1 ) | |
240 | { | |
241 | M_BMPDATA->m_bitmap = MyMGL_createBitmap(width, height, mglDepth, pf); | |
242 | } | |
243 | else | |
244 | { | |
245 | // MGL does not support mono DCs, so we have to emulate them with | |
246 | // 8bpp ones. We do that by using a special palette with color 0 | |
247 | // set to black and all other colors set to white. | |
248 | ||
249 | M_BMPDATA->m_bitmap = MyMGL_createBitmap(width, height, 8, pf); | |
250 | SetMonoPalette(wxColour(255, 255, 255), wxColour(0, 0, 0)); | |
251 | } | |
252 | ||
253 | return Ok(); | |
254 | } | |
255 | ||
256 | bool wxBitmap::CreateFromXpm(const char **bits) | |
257 | { | |
258 | wxCHECK_MSG( bits != NULL, FALSE, wxT("invalid bitmap data") ) | |
259 | ||
260 | wxXPMDecoder decoder; | |
261 | wxImage img = decoder.ReadData(bits); | |
262 | wxCHECK_MSG( img.Ok(), FALSE, wxT("invalid bitmap data") ) | |
263 | ||
264 | *this = wxBitmap(img); | |
265 | ||
266 | if ( wxTheBitmapList ) wxTheBitmapList->AddBitmap(this); | |
267 | ||
268 | return TRUE; | |
269 | } | |
270 | ||
271 | wxBitmap::wxBitmap(const wxImage& image, int depth = -1) | |
272 | { | |
273 | long width, height; | |
274 | ||
275 | wxCHECK_RET( image.Ok(), wxT("invalid image") ) | |
276 | ||
277 | width = image.GetWidth(); | |
278 | height = image.GetHeight(); | |
279 | ||
280 | if ( !Create(width, height, depth) ) return; | |
281 | ||
282 | MGLMemoryDC idc(width, height, 24, &gs_pixel_format_24, | |
283 | width * 3, (void*)image.GetData(), NULL); | |
284 | wxASSERT_MSG( idc.isValid(), wxT("cannot create custom MGLDC") ); | |
285 | ||
286 | MGLDevCtx *bdc = CreateTmpDC(); | |
287 | ||
288 | if ( depth <= 8 && image.HasPalette() ) | |
289 | SetPalette(image.GetPalette()); | |
290 | ||
291 | bdc->bitBlt(idc, 0, 0, width, height, 0, 0, MGL_REPLACE_MODE); | |
292 | delete bdc; | |
293 | ||
294 | if ( image.HasMask() ) | |
295 | { | |
296 | wxImage mask_image = image.ConvertToMono(image.GetMaskRed(), | |
297 | image.GetMaskGreen(), | |
298 | image.GetMaskBlue()); | |
299 | mask_image.SetMask(FALSE); | |
300 | wxBitmap mask_bmp(mask_image, 1); | |
301 | SetMask(new wxMask(mask_bmp)); | |
302 | } | |
303 | } | |
304 | ||
305 | wxImage wxBitmap::ConvertToImage() const | |
306 | { | |
307 | wxCHECK_MSG( Ok(), FALSE, wxT("invalid bitmap") ); | |
308 | ||
a4bbc9f7 | 309 | int width, height; |
32b8ec41 VZ |
310 | width = GetWidth(); |
311 | height = GetHeight(); | |
312 | ||
313 | wxImage image(width, height); | |
314 | wxASSERT_MSG( image.Ok(), wxT("cannot create image") ); | |
315 | ||
316 | MGLMemoryDC idc(width, height, 24, &gs_pixel_format_24, | |
317 | width * 3, (void*)image.GetData(), NULL); | |
318 | wxASSERT_MSG( idc.isValid(), wxT("cannot create custom MGLDC") ); | |
319 | ||
320 | if ( M_BMPDATA->m_palette ) | |
321 | image.SetPalette(*(M_BMPDATA->m_palette)); | |
322 | ||
323 | if ( GetMask() ) | |
324 | { | |
325 | // in consistency with other ports, we convert parts covered | |
326 | // by the mask to <16,16,16> colour and set that colour to image's | |
327 | // mask. We do that by OR-blitting the mask over image with | |
328 | // bg colour set to black and fg colour to <16,16,16> | |
329 | ||
330 | image.SetMaskColour(16, 16, 16); | |
331 | image.SetMask(TRUE); | |
332 | ||
333 | wxDC tmpDC; | |
334 | tmpDC.SetMGLDC(&idc, FALSE); | |
335 | tmpDC.SetBackground(wxBrush(wxColour(16,16,16), wxSOLID)); | |
336 | tmpDC.Clear(); | |
337 | tmpDC.DrawBitmap(*this, 0, 0, TRUE); | |
338 | } | |
339 | else | |
340 | { | |
341 | image.SetMask(FALSE); | |
342 | idc.putBitmap(0, 0, M_BMPDATA->m_bitmap, MGL_REPLACE_MODE); | |
343 | } | |
344 | ||
345 | return image; | |
346 | } | |
347 | ||
348 | wxBitmap::wxBitmap(const wxBitmap& bmp) | |
349 | { | |
350 | Ref(bmp); | |
351 | ||
352 | if ( wxTheBitmapList ) wxTheBitmapList->AddBitmap(this); | |
353 | } | |
354 | ||
355 | wxBitmap::wxBitmap(const wxString &filename, wxBitmapType type) | |
356 | { | |
357 | LoadFile(filename, type); | |
358 | ||
359 | if ( wxTheBitmapList ) wxTheBitmapList->AddBitmap(this); | |
360 | } | |
361 | ||
362 | wxBitmap::wxBitmap(const char bits[], int width, int height, int depth) | |
363 | { | |
364 | wxCHECK_RET( depth == 1, wxT("can only create mono bitmap from XBM data") ); | |
365 | ||
366 | if ( !Create(width, height, 1) ) return; | |
367 | MGLDevCtx *bdc = CreateTmpDC(); | |
368 | wxCurrentDCSwitcher curDC(bdc); | |
369 | bdc->setColor(1); | |
370 | bdc->setBackColor(0); | |
371 | bdc->clearDevice(); | |
372 | bdc->putMonoImage(0, 0, width, (width + 7) / 8, height, (void*)bits); | |
373 | delete bdc; | |
374 | ||
375 | if ( wxTheBitmapList ) wxTheBitmapList->AddBitmap(this); | |
376 | } | |
377 | ||
378 | wxBitmap::~wxBitmap() | |
379 | { | |
380 | if ( wxTheBitmapList ) wxTheBitmapList->DeleteObject(this); | |
381 | } | |
382 | ||
383 | wxBitmap& wxBitmap::operator = (const wxBitmap& bmp) | |
384 | { | |
385 | if ( *this == bmp ) return (*this); | |
386 | Ref(bmp); | |
387 | return *this; | |
388 | } | |
389 | ||
390 | bool wxBitmap::operator == (const wxBitmap& bmp) const | |
391 | { | |
392 | return (m_refData == bmp.m_refData); | |
393 | } | |
394 | ||
395 | bool wxBitmap::operator != (const wxBitmap& bmp) const | |
396 | { | |
397 | return (m_refData != bmp.m_refData); | |
398 | } | |
399 | ||
400 | bool wxBitmap::Ok() const | |
401 | { | |
402 | return (m_refData != NULL && M_BMPDATA->m_bitmap != NULL); | |
403 | } | |
404 | ||
405 | int wxBitmap::GetHeight() const | |
406 | { | |
407 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); | |
408 | ||
409 | return M_BMPDATA->m_height; | |
410 | } | |
411 | ||
412 | int wxBitmap::GetWidth() const | |
413 | { | |
414 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); | |
415 | ||
416 | return M_BMPDATA->m_width; | |
417 | } | |
418 | ||
419 | int wxBitmap::GetDepth() const | |
420 | { | |
421 | wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") ); | |
422 | ||
423 | return M_BMPDATA->m_bpp; | |
424 | } | |
425 | ||
426 | wxMask *wxBitmap::GetMask() const | |
427 | { | |
428 | wxCHECK_MSG( Ok(), (wxMask *) NULL, wxT("invalid bitmap") ); | |
429 | ||
430 | return M_BMPDATA->m_mask; | |
431 | } | |
432 | ||
433 | void wxBitmap::SetMask(wxMask *mask) | |
434 | { | |
435 | wxCHECK_RET( Ok(), wxT("invalid bitmap") ); | |
436 | ||
437 | delete M_BMPDATA->m_mask; | |
438 | M_BMPDATA->m_mask = mask; | |
439 | } | |
440 | ||
441 | bool wxBitmap::CopyFromIcon(const wxIcon& icon) | |
442 | { | |
443 | wxBitmap *bmp = (wxBitmap*)(&icon); | |
444 | *this = *bmp; | |
445 | return TRUE; | |
446 | } | |
447 | ||
448 | wxBitmap wxBitmap::GetSubBitmap(const wxRect& rect) const | |
449 | { | |
450 | wxCHECK_MSG( Ok() && | |
451 | (rect.x >= 0) && (rect.y >= 0) && | |
452 | (rect.x+rect.width <= M_BMPDATA->m_width) && (rect.y+rect.height <= M_BMPDATA->m_height), | |
453 | wxNullBitmap, wxT("invalid bitmap or bitmap region") ); | |
454 | ||
455 | wxBitmap ret( rect.width, rect.height, M_BMPDATA->m_bpp ); | |
456 | wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") ); | |
457 | ||
458 | if ( GetPalette() ) | |
459 | ret.SetPalette(*GetPalette()); | |
460 | ||
461 | MGLDevCtx *tdc = ret.CreateTmpDC(); | |
462 | tdc->putBitmapSection(rect.x, rect.y, | |
463 | rect.x + rect.width, rect.y + rect.height, | |
464 | 0, 0, M_BMPDATA->m_bitmap, MGL_REPLACE_MODE); | |
465 | delete tdc; | |
466 | ||
467 | if ( GetMask() ) | |
468 | { | |
469 | wxBitmap submask = GetMask()->GetBitmap()->GetSubBitmap(rect); | |
470 | ret.SetMask(new wxMask(submask)); | |
471 | } | |
472 | ||
473 | return ret; | |
474 | } | |
475 | ||
476 | void wxBitmap::SetMonoPalette(const wxColour& fg, const wxColour& bg) | |
477 | { | |
478 | wxCHECK_RET( Ok(), wxT("invalid bitmap") ); | |
479 | ||
480 | palette_t *mono = M_BMPDATA->m_bitmap->pal; | |
481 | ||
482 | wxCHECK_RET( M_BMPDATA->m_bpp == 1, wxT("bitmap is not 1bpp") ); | |
483 | wxCHECK_RET( mono != NULL, wxT("bitmap w/o palette") ); | |
484 | ||
485 | mono[0].red = bg.Red(); | |
486 | mono[0].green = bg.Green(); | |
487 | mono[0].blue = bg.Blue(); | |
488 | mono[0].alpha = 0; | |
489 | for (size_t i = 1; i < 256; i++) | |
490 | { | |
491 | mono[i].red = fg.Red(); | |
492 | mono[i].green = fg.Green(); | |
493 | mono[i].blue = fg.Blue(); | |
494 | mono[i].alpha = 0; | |
495 | } | |
496 | } | |
497 | ||
498 | MGLDevCtx *wxBitmap::CreateTmpDC() const | |
499 | { | |
500 | wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") ); | |
501 | ||
502 | MGLDevCtx *tdc = new MGLMemoryDC(GetWidth(), GetHeight(), | |
503 | M_BMPDATA->m_bitmap->bitsPerPixel, | |
504 | M_BMPDATA->m_bitmap->pf, | |
505 | M_BMPDATA->m_bitmap->bytesPerLine, | |
506 | M_BMPDATA->m_bitmap->surface, | |
507 | NULL); | |
508 | wxCHECK_MSG( tdc->isValid(), NULL, wxT("cannot create temporary MGLDC") ); | |
509 | ||
510 | if ( M_BMPDATA->m_bitmap->pal != NULL ) | |
511 | { | |
512 | int cnt; | |
513 | ||
514 | switch (M_BMPDATA->m_bitmap->bitsPerPixel) | |
515 | { | |
516 | case 2: cnt = 2; break; | |
517 | case 4: cnt = 16; break; | |
518 | case 8: cnt = 256; break; | |
519 | default: | |
a4bbc9f7 | 520 | cnt = 0; |
32b8ec41 VZ |
521 | wxFAIL_MSG( wxT("bitmap with this depth cannot have palette") ); |
522 | break; | |
523 | } | |
524 | ||
525 | tdc->setPalette(M_BMPDATA->m_bitmap->pal, cnt, 0); | |
526 | tdc->realizePalette(cnt, 0, FALSE); | |
527 | } | |
528 | ||
529 | return tdc; | |
530 | } | |
531 | ||
532 | bool wxBitmap::LoadFile(const wxString &name, wxBitmapType type) | |
533 | { | |
534 | UnRef(); | |
535 | ||
536 | if ( type == wxBITMAP_TYPE_BMP || type == wxBITMAP_TYPE_PNG || | |
537 | type == wxBITMAP_TYPE_PCX || type == wxBITMAP_TYPE_JPEG ) | |
538 | { | |
539 | // prevent accidental loading of bitmap from $MGL_ROOT: | |
540 | if ( !wxFileExists(name) ) | |
541 | { | |
542 | wxLogError(_("File %s does not exist."), name.c_str()); | |
543 | return FALSE; | |
544 | } | |
545 | } | |
546 | ||
547 | wxBitmapHandler *handler = FindHandler(type); | |
548 | ||
549 | if ( handler == NULL ) | |
550 | { | |
551 | wxImage image; | |
552 | if ( !image.LoadFile(name, type) || !image.Ok() ) | |
553 | { | |
554 | wxLogError("no bitmap handler for type %d defined.", type); | |
555 | return FALSE; | |
556 | } | |
557 | else | |
558 | { | |
559 | *this = wxBitmap(image); | |
560 | return TRUE; | |
561 | } | |
562 | } | |
563 | ||
564 | m_refData = new wxBitmapRefData(); | |
565 | ||
566 | return handler->LoadFile(this, name, type, -1, -1); | |
567 | } | |
568 | ||
569 | bool wxBitmap::SaveFile(const wxString& filename, wxBitmapType type, const wxPalette *palette) const | |
570 | { | |
571 | wxCHECK_MSG( Ok(), FALSE, wxT("invalid bitmap") ); | |
572 | ||
573 | wxBitmapHandler *handler = FindHandler(type); | |
574 | ||
575 | if ( handler == NULL ) | |
576 | { | |
577 | wxImage image = ConvertToImage(); | |
578 | if ( palette ) | |
579 | image.SetPalette(*palette); | |
580 | ||
581 | if ( image.Ok() ) | |
582 | return image.SaveFile(filename, type); | |
583 | else | |
584 | { | |
585 | wxLogError("no bitmap handler for type %d defined.", type); | |
586 | return FALSE; | |
587 | } | |
588 | } | |
589 | ||
590 | return handler->SaveFile(this, filename, type, palette); | |
591 | } | |
592 | ||
593 | wxPalette *wxBitmap::GetPalette() const | |
594 | { | |
595 | wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") ); | |
596 | ||
597 | return M_BMPDATA->m_palette; | |
598 | } | |
599 | ||
600 | void wxBitmap::SetPalette(const wxPalette& palette) | |
601 | { | |
602 | wxCHECK_RET( Ok(), wxT("invalid bitmap") ); | |
603 | wxCHECK_RET( GetDepth() > 1 && GetDepth() <= 8, wxT("cannot set palette for bitmap of this depth") ); | |
604 | ||
605 | delete M_BMPDATA->m_palette; | |
606 | M_BMPDATA->m_palette = NULL; | |
607 | ||
608 | if ( !palette.Ok() ) return; | |
609 | ||
610 | M_BMPDATA->m_palette = new wxPalette(palette); | |
611 | ||
612 | int cnt = palette.GetColoursCount(); | |
613 | palette_t *pal = palette.GetMGLpalette_t(); | |
614 | memcpy(M_BMPDATA->m_bitmap->pal, pal, cnt * sizeof(palette_t)); | |
615 | } | |
616 | ||
617 | void wxBitmap::SetHeight(int height) | |
618 | { | |
619 | if (!m_refData) m_refData = new wxBitmapRefData(); | |
620 | ||
621 | M_BMPDATA->m_height = height; | |
622 | } | |
623 | ||
624 | void wxBitmap::SetWidth(int width) | |
625 | { | |
626 | if (!m_refData) m_refData = new wxBitmapRefData(); | |
627 | ||
628 | M_BMPDATA->m_width = width; | |
629 | } | |
630 | ||
631 | void wxBitmap::SetDepth(int depth) | |
632 | { | |
633 | if (!m_refData) m_refData = new wxBitmapRefData(); | |
634 | ||
635 | M_BMPDATA->m_bpp = depth; | |
636 | } | |
637 | ||
638 | bitmap_t *wxBitmap::GetMGLbitmap_t() const | |
639 | { | |
640 | return M_BMPDATA->m_bitmap; | |
641 | } | |
642 | ||
643 | ||
644 | ||
645 | //----------------------------------------------------------------------------- | |
646 | // wxBitmap I/O handlers | |
647 | //----------------------------------------------------------------------------- | |
648 | ||
649 | class wxMGLBitmapHandler: public wxBitmapHandler | |
650 | { | |
651 | public: | |
652 | wxMGLBitmapHandler(wxBitmapType type, | |
653 | const wxString& extension, const wxString& name); | |
654 | ||
655 | virtual bool Create(wxBitmap *bitmap, void *data, long flags, | |
656 | int width, int height, int depth = 1) | |
657 | { return FALSE; } | |
658 | ||
659 | virtual bool LoadFile(wxBitmap *bitmap, const wxString& name, long flags, | |
660 | int desiredWidth, int desiredHeight); | |
661 | virtual bool SaveFile(const wxBitmap *bitmap, const wxString& name, | |
662 | int type, const wxPalette *palette = NULL); | |
663 | }; | |
664 | ||
665 | wxMGLBitmapHandler::wxMGLBitmapHandler(wxBitmapType type, | |
666 | const wxString& extension, | |
667 | const wxString& name) | |
668 | : wxBitmapHandler() | |
669 | { | |
670 | SetType(type); | |
671 | SetName(name); | |
672 | SetExtension(extension); | |
673 | } | |
674 | ||
675 | bool wxMGLBitmapHandler::LoadFile(wxBitmap *bitmap, const wxString& name, | |
676 | long flags, | |
677 | int WXUNUSED(desiredWidth), | |
678 | int WXUNUSED(desiredHeight)) | |
679 | { | |
680 | int width, height, bpp; | |
681 | pixel_format_t pf; | |
682 | wxString fullname; | |
683 | wxMemoryDC dc; | |
684 | ||
685 | switch (flags) | |
686 | { | |
687 | case wxBITMAP_TYPE_BMP_RESOURCE: | |
688 | case wxBITMAP_TYPE_JPEG_RESOURCE: | |
689 | case wxBITMAP_TYPE_PNG_RESOURCE: | |
690 | case wxBITMAP_TYPE_PCX_RESOURCE: | |
691 | fullname = name + wxT(".bmp"); | |
692 | break; | |
693 | default: | |
694 | fullname= name; | |
695 | break; | |
696 | } | |
697 | ||
698 | switch (flags) | |
699 | { | |
700 | case wxBITMAP_TYPE_BMP: | |
701 | case wxBITMAP_TYPE_BMP_RESOURCE: | |
702 | if ( !MGL_getBitmapSize(fullname.mb_str(), &width, &height, &bpp, &pf) ) | |
703 | return FALSE; | |
704 | bitmap->Create(width, height, -1); | |
705 | if ( !bitmap->Ok() ) return FALSE; | |
706 | dc.SelectObject(*bitmap); | |
707 | if ( !dc.GetMGLDC()->loadBitmapIntoDC(fullname.mb_str(), 0, 0, TRUE) ) | |
708 | return FALSE; | |
709 | break; | |
710 | ||
711 | case wxBITMAP_TYPE_JPEG: | |
712 | case wxBITMAP_TYPE_JPEG_RESOURCE: | |
713 | if ( !MGL_getJPEGSize(fullname.mb_str(), &width, &height, &bpp, &pf) ) | |
714 | return FALSE; | |
715 | bitmap->Create(width, height, -1); | |
716 | if ( !bitmap->Ok() ) return FALSE; | |
717 | dc.SelectObject(*bitmap); | |
718 | if ( !dc.GetMGLDC()->loadJPEGIntoDC(fullname.mb_str(), 0, 0, TRUE) ) | |
719 | return FALSE; | |
720 | break; | |
721 | ||
722 | case wxBITMAP_TYPE_PNG: | |
723 | case wxBITMAP_TYPE_PNG_RESOURCE: | |
724 | if ( !MGL_getPNGSize(fullname.mb_str(), &width, &height, &bpp, &pf) ) | |
725 | return FALSE; | |
726 | bitmap->Create(width, height, -1); | |
727 | if ( !bitmap->Ok() ) return FALSE; | |
728 | dc.SelectObject(*bitmap); | |
729 | if ( !dc.GetMGLDC()->loadPNGIntoDC(fullname.mb_str(), 0, 0, TRUE) ) | |
730 | return FALSE; | |
731 | break; | |
732 | ||
733 | case wxBITMAP_TYPE_PCX: | |
734 | case wxBITMAP_TYPE_PCX_RESOURCE: | |
735 | if ( !MGL_getPCXSize(fullname.mb_str(), &width, &height, &bpp) ) | |
736 | return FALSE; | |
737 | bitmap->Create(width, height, -1); | |
738 | if ( !bitmap->Ok() ) return FALSE; | |
739 | dc.SelectObject(*bitmap); | |
740 | if ( !dc.GetMGLDC()->loadPCXIntoDC(fullname.mb_str(), 0, 0, TRUE) ) | |
741 | return FALSE; | |
742 | break; | |
743 | ||
744 | default: | |
745 | wxFAIL_MSG(wxT("Unsupported image format.")); | |
746 | break; | |
747 | } | |
748 | ||
749 | return TRUE; | |
750 | } | |
751 | ||
752 | bool wxMGLBitmapHandler::SaveFile(const wxBitmap *bitmap, const wxString& name, | |
753 | int type, const wxPalette * WXUNUSED(palette)) | |
754 | { | |
755 | wxMemoryDC mem; | |
756 | MGLDevCtx *tdc; | |
757 | int w = bitmap->GetWidth(), | |
758 | h = bitmap->GetHeight(); | |
759 | ||
760 | mem.SelectObject(*bitmap); | |
761 | tdc = mem.GetMGLDC(); | |
762 | ||
763 | switch (type) | |
764 | { | |
765 | case wxBITMAP_TYPE_BMP: | |
766 | return tdc->saveBitmapFromDC(name.mb_str(), 0, 0, w, h); | |
767 | break; | |
768 | case wxBITMAP_TYPE_JPEG: | |
769 | return tdc->saveJPEGFromDC(name.mb_str(), 0, 0, w, h, 75); | |
770 | break; | |
771 | case wxBITMAP_TYPE_PNG: | |
772 | return tdc->savePNGFromDC(name.mb_str(), 0, 0, w, h); | |
773 | break; | |
774 | case wxBITMAP_TYPE_PCX: | |
775 | return tdc->savePCXFromDC(name.mb_str(), 0, 0, w, h); | |
776 | break; | |
777 | default: | |
778 | return FALSE; | |
779 | break; | |
780 | } | |
781 | } | |
782 | ||
783 | ||
784 | ||
785 | // let's handle PNGs in special way because they have alpha channel | |
786 | // which we can access via bitmap_t most easily | |
787 | class wxPNGBitmapHandler: public wxMGLBitmapHandler | |
788 | { | |
789 | public: | |
790 | wxPNGBitmapHandler(wxBitmapType type, | |
791 | const wxString& extension, const wxString& name) | |
792 | : wxMGLBitmapHandler(type, extension, name) {} | |
793 | ||
794 | virtual bool LoadFile(wxBitmap *bitmap, const wxString& name, long flags, | |
795 | int desiredWidth, int desiredHeight); | |
796 | }; | |
797 | ||
798 | bool wxPNGBitmapHandler::LoadFile(wxBitmap *bitmap, const wxString& name, | |
799 | long flags, | |
800 | int desiredWidth, int desiredHeight) | |
801 | { | |
802 | int width, height, bpp; | |
803 | pixel_format_t pf; | |
804 | wxString fullname; | |
805 | ||
806 | if ( flags == wxBITMAP_TYPE_PNG_RESOURCE ) | |
807 | fullname = name + wxT(".png"); | |
808 | else | |
809 | fullname = name; | |
810 | ||
811 | if ( !MGL_getPNGSize(fullname.mb_str(), &width, &height, &bpp, &pf) ) | |
812 | return FALSE; | |
813 | ||
814 | if ( bpp != 32 ) | |
815 | { | |
816 | // We can load ordinary PNGs faster with 'normal' MGL handler. | |
817 | // Only RGBA PNGs need to be processed in special way because | |
818 | // we have to convert alpha channel to mask | |
819 | return wxMGLBitmapHandler::LoadFile(bitmap, name, flags, | |
820 | desiredWidth, desiredHeight); | |
821 | } | |
822 | ||
823 | bitmap_t *bmp = MGL_loadPNG(fullname.mb_str(), TRUE); | |
824 | ||
825 | if ( bmp == NULL ) return FALSE; | |
826 | ||
827 | bitmap->Create(bmp->width, bmp->height, -1); | |
828 | if ( !bitmap->Ok() ) return FALSE; | |
829 | ||
830 | // convert bmp to display's depth and write it to *bitmap: | |
831 | wxMemoryDC dc; | |
832 | dc.SelectObject(*bitmap); | |
833 | dc.GetMGLDC()->putBitmap(0, 0, bmp, MGL_REPLACE_MODE); | |
834 | dc.SelectObject(wxNullBitmap); | |
835 | ||
836 | // create mask, if bmp contains alpha channel (ARGB format): | |
837 | if ( bmp->bitsPerPixel == 32 ) | |
838 | { | |
839 | int x, y; | |
840 | wxUint32 *s = (wxUint32*)bmp->surface; | |
841 | for (y = 0; y < bmp->height; y++) | |
842 | { | |
843 | s = ((wxUint32*)bmp->surface) + y * bmp->bytesPerLine/4; | |
844 | for (x = 0; x < bmp->width; x++, s ++) | |
845 | { | |
846 | if ( ((((*s) >> bmp->pf->rsvdPos) & bmp->pf->rsvdMask) | |
847 | << bmp->pf->rsvdAdjust) < 128 ) | |
848 | *s = 0; | |
849 | else | |
850 | *s = 0x00FFFFFF; // white | |
851 | } | |
852 | } | |
853 | wxBitmap mask(bmp->width, bmp->height, 1); | |
854 | dc.SelectObject(mask); | |
855 | dc.GetMGLDC()->putBitmap(0, 0, bmp, MGL_REPLACE_MODE); | |
856 | dc.SelectObject(wxNullBitmap); | |
857 | bitmap->SetMask(new wxMask(mask)); | |
858 | } | |
859 | ||
860 | MGL_unloadBitmap(bmp); | |
861 | ||
862 | return TRUE; | |
863 | } | |
864 | ||
865 | ||
866 | ||
867 | ||
868 | class wxICOBitmapHandler: public wxBitmapHandler | |
869 | { | |
870 | public: | |
871 | wxICOBitmapHandler(wxBitmapType type, | |
872 | const wxString& extension, const wxString& name); | |
873 | ||
874 | virtual bool Create(wxBitmap *bitmap, void *data, long flags, | |
875 | int width, int height, int depth = 1) | |
876 | { return FALSE; } | |
877 | ||
878 | virtual bool LoadFile(wxBitmap *bitmap, const wxString& name, long flags, | |
879 | int desiredWidth, int desiredHeight); | |
880 | virtual bool SaveFile(const wxBitmap *bitmap, const wxString& name, | |
881 | int type, const wxPalette *palette = NULL); | |
882 | }; | |
883 | ||
884 | wxICOBitmapHandler::wxICOBitmapHandler(wxBitmapType type, | |
885 | const wxString& extension, | |
886 | const wxString& name) | |
887 | : wxBitmapHandler() | |
888 | { | |
889 | SetType(type); | |
890 | SetName(name); | |
891 | SetExtension(extension); | |
892 | } | |
893 | ||
894 | bool wxICOBitmapHandler::LoadFile(wxBitmap *bitmap, const wxString& name, | |
895 | long flags, | |
896 | int WXUNUSED(desiredWidth), | |
897 | int WXUNUSED(desiredHeight)) | |
898 | { | |
899 | icon_t *icon = NULL; | |
900 | MGLDevCtx *dc; | |
901 | ||
902 | if ( flags == wxBITMAP_TYPE_ICO_RESOURCE ) | |
903 | icon = MGL_loadIcon(wxString(name + wxT(".ico")).mb_str(), TRUE); | |
904 | else | |
905 | icon = MGL_loadIcon(name.mb_str(), TRUE); | |
906 | ||
907 | if ( icon == NULL ) return FALSE; | |
908 | ||
909 | bitmap->Create(icon->xorMask.width, icon->xorMask.height); | |
910 | ||
911 | wxMemoryDC mem; | |
912 | mem.SelectObject(*bitmap); | |
913 | dc = mem.GetMGLDC(); | |
914 | dc->putBitmap(0, 0, &(icon->xorMask), MGL_REPLACE_MODE); | |
915 | mem.SelectObject(wxNullBitmap); | |
916 | ||
917 | wxBitmap mask(icon->xorMask.width, icon->xorMask.height, 1); | |
918 | mem.SelectObject(mask); | |
919 | dc = mem.GetMGLDC(); | |
920 | ||
921 | wxCurrentDCSwitcher curDC(dc); | |
922 | dc->setColor(0); | |
923 | dc->setBackColor(1); | |
924 | dc->clearDevice(); | |
925 | dc->putMonoImage(0, 0, icon->xorMask.width, icon->byteWidth, | |
926 | icon->xorMask.height, (void*)icon->andMask); | |
927 | ||
928 | bitmap->SetMask(new wxMask(mask)); | |
929 | ||
930 | MGL_unloadIcon(icon); | |
931 | ||
932 | return TRUE; | |
933 | } | |
934 | ||
935 | bool wxICOBitmapHandler::SaveFile(const wxBitmap *bitmap, const wxString& name, | |
936 | int type, const wxPalette * WXUNUSED(palette)) | |
937 | { | |
938 | return FALSE; | |
939 | } | |
940 | ||
941 | ||
942 | ||
943 | ||
944 | /*static*/ void wxBitmap::InitStandardHandlers() | |
945 | { | |
946 | AddHandler(new wxMGLBitmapHandler(wxBITMAP_TYPE_BMP, wxT("bmp"), wxT("Windows bitmap"))); | |
947 | AddHandler(new wxMGLBitmapHandler(wxBITMAP_TYPE_BMP_RESOURCE, wxEmptyString, wxT("Windows bitmap resource"))); | |
948 | AddHandler(new wxMGLBitmapHandler(wxBITMAP_TYPE_JPEG, wxT("jpg"), wxT("JPEG image"))); | |
949 | AddHandler(new wxMGLBitmapHandler(wxBITMAP_TYPE_JPEG_RESOURCE, wxEmptyString, wxT("JPEG resource"))); | |
950 | AddHandler(new wxMGLBitmapHandler(wxBITMAP_TYPE_PCX, wxT("pcx"), wxT("PCX image"))); | |
951 | AddHandler(new wxMGLBitmapHandler(wxBITMAP_TYPE_PCX_RESOURCE, wxEmptyString, wxT("PCX resource"))); | |
952 | ||
953 | AddHandler(new wxPNGBitmapHandler(wxBITMAP_TYPE_PNG, wxT("png"), wxT("PNG image"))); | |
954 | AddHandler(new wxPNGBitmapHandler(wxBITMAP_TYPE_PNG_RESOURCE, wxEmptyString, wxT("PNG resource"))); | |
955 | ||
956 | AddHandler(new wxICOBitmapHandler(wxBITMAP_TYPE_ICO, wxT("ico"), wxT("Icon resource"))); | |
957 | AddHandler(new wxICOBitmapHandler(wxBITMAP_TYPE_ICO_RESOURCE, wxEmptyString, wxT("Icon resource"))); | |
958 | } |