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