]> git.saurik.com Git - wxWidgets.git/blame - src/mgl/bitmap.cpp
Left in debug printf.
[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
VZ
62
63 int m_width;
64 int m_height;
65 int m_bpp;
66 wxPalette *m_palette;
67 wxMask *m_mask;
68 bitmap_t *m_bitmap;
69};
70
71wxBitmapRefData::wxBitmapRefData()
72{
73 m_mask = NULL;
74 m_width = 0;
75 m_height = 0;
76 m_bpp = 0;
77 m_palette = NULL;
78 m_bitmap = NULL;
79}
80
81wxBitmapRefData::~wxBitmapRefData()
82{
83 if ( m_bitmap )
84 MGL_unloadBitmap(m_bitmap);
85 delete m_mask;
86 delete m_palette;
87}
88
89//-----------------------------------------------------------------------------
90
91#define M_BMPDATA ((wxBitmapRefData *)m_refData)
92
93
452418c4 94IMPLEMENT_ABSTRACT_CLASS(wxBitmapHandler, wxBitmapHandlerBase)
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
270bool wxBitmap::operator == (const wxBitmap& bmp) const
271{
272 return (m_refData == bmp.m_refData);
273}
274
275bool wxBitmap::operator != (const wxBitmap& bmp) const
276{
277 return (m_refData != bmp.m_refData);
278}
279
b7cacb43 280bool wxBitmap::IsOk() const
32b8ec41
VZ
281{
282 return (m_refData != NULL && M_BMPDATA->m_bitmap != NULL);
283}
284
285int wxBitmap::GetHeight() const
286{
287 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
288
289 return M_BMPDATA->m_height;
290}
291
292int wxBitmap::GetWidth() const
293{
294 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
295
296 return M_BMPDATA->m_width;
297}
298
299int wxBitmap::GetDepth() const
300{
301 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
302
303 return M_BMPDATA->m_bpp;
304}
305
306wxMask *wxBitmap::GetMask() const
307{
308 wxCHECK_MSG( Ok(), (wxMask *) NULL, wxT("invalid bitmap") );
309
310 return M_BMPDATA->m_mask;
311}
312
313void wxBitmap::SetMask(wxMask *mask)
314{
315 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
316
317 delete M_BMPDATA->m_mask;
318 M_BMPDATA->m_mask = mask;
319}
320
321bool wxBitmap::CopyFromIcon(const wxIcon& icon)
322{
323 wxBitmap *bmp = (wxBitmap*)(&icon);
324 *this = *bmp;
127eab18 325 return true;
32b8ec41
VZ
326}
327
328wxBitmap wxBitmap::GetSubBitmap(const wxRect& rect) const
329{
330 wxCHECK_MSG( Ok() &&
331 (rect.x >= 0) && (rect.y >= 0) &&
332 (rect.x+rect.width <= M_BMPDATA->m_width) && (rect.y+rect.height <= M_BMPDATA->m_height),
333 wxNullBitmap, wxT("invalid bitmap or bitmap region") );
334
335 wxBitmap ret( rect.width, rect.height, M_BMPDATA->m_bpp );
336 wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") );
337
338 if ( GetPalette() )
339 ret.SetPalette(*GetPalette());
340
341 MGLDevCtx *tdc = ret.CreateTmpDC();
127eab18 342 tdc->putBitmapSection(rect.x, rect.y,
32b8ec41
VZ
343 rect.x + rect.width, rect.y + rect.height,
344 0, 0, M_BMPDATA->m_bitmap, MGL_REPLACE_MODE);
345 delete tdc;
346
347 if ( GetMask() )
348 {
87f83ac8 349 wxBitmap submask = GetMask()->GetBitmap().GetSubBitmap(rect);
32b8ec41
VZ
350 ret.SetMask(new wxMask(submask));
351 }
352
353 return ret;
354}
355
356void wxBitmap::SetMonoPalette(const wxColour& fg, const wxColour& bg)
357{
358 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
359
360 palette_t *mono = M_BMPDATA->m_bitmap->pal;
361
362 wxCHECK_RET( M_BMPDATA->m_bpp == 1, wxT("bitmap is not 1bpp") );
363 wxCHECK_RET( mono != NULL, wxT("bitmap w/o palette") );
364
365 mono[0].red = bg.Red();
366 mono[0].green = bg.Green();
367 mono[0].blue = bg.Blue();
368 mono[0].alpha = 0;
369 for (size_t i = 1; i < 256; i++)
370 {
371 mono[i].red = fg.Red();
372 mono[i].green = fg.Green();
373 mono[i].blue = fg.Blue();
374 mono[i].alpha = 0;
375 }
376}
377
378MGLDevCtx *wxBitmap::CreateTmpDC() const
379{
380 wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") );
381
382 MGLDevCtx *tdc = new MGLMemoryDC(GetWidth(), GetHeight(),
383 M_BMPDATA->m_bitmap->bitsPerPixel,
384 M_BMPDATA->m_bitmap->pf,
385 M_BMPDATA->m_bitmap->bytesPerLine,
127eab18 386 M_BMPDATA->m_bitmap->surface,
32b8ec41
VZ
387 NULL);
388 wxCHECK_MSG( tdc->isValid(), NULL, wxT("cannot create temporary MGLDC") );
389
390 if ( M_BMPDATA->m_bitmap->pal != NULL )
391 {
392 int cnt;
127eab18 393
32b8ec41
VZ
394 switch (M_BMPDATA->m_bitmap->bitsPerPixel)
395 {
396 case 2: cnt = 2; break;
397 case 4: cnt = 16; break;
398 case 8: cnt = 256; break;
399 default:
a4bbc9f7 400 cnt = 0;
32b8ec41
VZ
401 wxFAIL_MSG( wxT("bitmap with this depth cannot have palette") );
402 break;
403 }
127eab18 404
32b8ec41
VZ
405 tdc->setPalette(M_BMPDATA->m_bitmap->pal, cnt, 0);
406 tdc->realizePalette(cnt, 0, FALSE);
407 }
127eab18 408
32b8ec41
VZ
409 return tdc;
410}
411
412bool wxBitmap::LoadFile(const wxString &name, wxBitmapType type)
413{
414 UnRef();
127eab18
WS
415
416 if ( type == wxBITMAP_TYPE_BMP || type == wxBITMAP_TYPE_PNG ||
32b8ec41
VZ
417 type == wxBITMAP_TYPE_PCX || type == wxBITMAP_TYPE_JPEG )
418 {
419 // prevent accidental loading of bitmap from $MGL_ROOT:
420 if ( !wxFileExists(name) )
421 {
422 wxLogError(_("File %s does not exist."), name.c_str());
127eab18 423 return false;
32b8ec41
VZ
424 }
425 }
127eab18 426
32b8ec41
VZ
427 wxBitmapHandler *handler = FindHandler(type);
428
127eab18 429 if ( handler == NULL )
32b8ec41
VZ
430 {
431 wxImage image;
432 if ( !image.LoadFile(name, type) || !image.Ok() )
433 {
127eab18
WS
434 wxLogError("no bitmap handler for type %d defined.", type);
435 return false;
32b8ec41
VZ
436 }
437 else
438 {
439 *this = wxBitmap(image);
127eab18 440 return true;
32b8ec41
VZ
441 }
442 }
443
444 m_refData = new wxBitmapRefData();
445
446 return handler->LoadFile(this, name, type, -1, -1);
447}
448
449bool wxBitmap::SaveFile(const wxString& filename, wxBitmapType type, const wxPalette *palette) const
450{
127eab18 451 wxCHECK_MSG( Ok(), false, wxT("invalid bitmap") );
32b8ec41
VZ
452
453 wxBitmapHandler *handler = FindHandler(type);
454
127eab18 455 if ( handler == NULL )
32b8ec41
VZ
456 {
457 wxImage image = ConvertToImage();
458 if ( palette )
459 image.SetPalette(*palette);
460
461 if ( image.Ok() )
462 return image.SaveFile(filename, type);
463 else
464 {
465 wxLogError("no bitmap handler for type %d defined.", type);
127eab18 466 return false;
32b8ec41
VZ
467 }
468 }
469
470 return handler->SaveFile(this, filename, type, palette);
471}
472
473wxPalette *wxBitmap::GetPalette() const
474{
475 wxCHECK_MSG( Ok(), NULL, wxT("invalid bitmap") );
476
477 return M_BMPDATA->m_palette;
478}
479
480void wxBitmap::SetPalette(const wxPalette& palette)
481{
482 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
483 wxCHECK_RET( GetDepth() > 1 && GetDepth() <= 8, wxT("cannot set palette for bitmap of this depth") );
484
485 delete M_BMPDATA->m_palette;
486 M_BMPDATA->m_palette = NULL;
487
488 if ( !palette.Ok() ) return;
127eab18 489
32b8ec41
VZ
490 M_BMPDATA->m_palette = new wxPalette(palette);
491
492 int cnt = palette.GetColoursCount();
493 palette_t *pal = palette.GetMGLpalette_t();
494 memcpy(M_BMPDATA->m_bitmap->pal, pal, cnt * sizeof(palette_t));
495}
496
497void wxBitmap::SetHeight(int height)
498{
499 if (!m_refData) m_refData = new wxBitmapRefData();
500
501 M_BMPDATA->m_height = height;
502}
503
504void wxBitmap::SetWidth(int width)
505{
506 if (!m_refData) m_refData = new wxBitmapRefData();
507
508 M_BMPDATA->m_width = width;
509}
510
511void wxBitmap::SetDepth(int depth)
512{
513 if (!m_refData) m_refData = new wxBitmapRefData();
514
515 M_BMPDATA->m_bpp = depth;
516}
517
518bitmap_t *wxBitmap::GetMGLbitmap_t() const
519{
520 return M_BMPDATA->m_bitmap;
521}
522
87f83ac8
VZ
523// Convert wxColour into it's quantized value in lower-precision
524// pixel format (needed for masking by colour).
3d5f724f 525wxColour wxBitmap::QuantizeColour(const wxColour& clr) const
87f83ac8
VZ
526{
527 pixel_format_t *pf = GetMGLbitmap_t()->pf;
528
529 if ( pf->redAdjust == 0 && pf->greenAdjust == 0 && pf->blueAdjust == 0 )
530 return clr;
531 else
532 return wxColour((unsigned char)((clr.Red() >> pf->redAdjust) << pf->redAdjust),
533 (unsigned char)((clr.Green() >> pf->greenAdjust) << pf->greenAdjust),
534 (unsigned char)((clr.Blue() >> pf->blueAdjust) << pf->blueAdjust));
535}
32b8ec41
VZ
536
537
538//-----------------------------------------------------------------------------
539// wxBitmap I/O handlers
540//-----------------------------------------------------------------------------
541
542class wxMGLBitmapHandler: public wxBitmapHandler
543{
544public:
545 wxMGLBitmapHandler(wxBitmapType type,
546 const wxString& extension, const wxString& name);
547
127eab18 548 virtual bool Create(wxBitmap *WXUNUSED(bitmap),
452418c4 549 const void* WXUNUSED(data),
127eab18
WS
550 long WXUNUSED(flags),
551 int WXUNUSED(width),
552 int WXUNUSED(height),
553 int WXUNUSED(depth) = 1)
554 { return false; }
32b8ec41
VZ
555
556 virtual bool LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
557 int desiredWidth, int desiredHeight);
127eab18 558 virtual bool SaveFile(const wxBitmap *bitmap, const wxString& name,
32b8ec41
VZ
559 int type, const wxPalette *palette = NULL);
560};
561
127eab18 562wxMGLBitmapHandler::wxMGLBitmapHandler(wxBitmapType type,
32b8ec41
VZ
563 const wxString& extension,
564 const wxString& name)
565 : wxBitmapHandler()
566{
567 SetType(type);
568 SetName(name);
569 SetExtension(extension);
570}
571
127eab18
WS
572bool wxMGLBitmapHandler::LoadFile(wxBitmap *bitmap, const wxString& name,
573 long flags,
574 int WXUNUSED(desiredWidth),
32b8ec41
VZ
575 int WXUNUSED(desiredHeight))
576{
577 int width, height, bpp;
578 pixel_format_t pf;
579 wxString fullname;
580 wxMemoryDC dc;
127eab18 581
32b8ec41
VZ
582 switch (flags)
583 {
584 case wxBITMAP_TYPE_BMP_RESOURCE:
585 case wxBITMAP_TYPE_JPEG_RESOURCE:
586 case wxBITMAP_TYPE_PNG_RESOURCE:
587 case wxBITMAP_TYPE_PCX_RESOURCE:
588 fullname = name + wxT(".bmp");
589 break;
590 default:
591 fullname= name;
592 break;
127eab18 593 }
32b8ec41
VZ
594
595 switch (flags)
596 {
597 case wxBITMAP_TYPE_BMP:
598 case wxBITMAP_TYPE_BMP_RESOURCE:
599 if ( !MGL_getBitmapSize(fullname.mb_str(), &width, &height, &bpp, &pf) )
127eab18 600 return false;
32b8ec41 601 bitmap->Create(width, height, -1);
127eab18 602 if ( !bitmap->Ok() ) return false;
32b8ec41
VZ
603 dc.SelectObject(*bitmap);
604 if ( !dc.GetMGLDC()->loadBitmapIntoDC(fullname.mb_str(), 0, 0, TRUE) )
127eab18 605 return false;
32b8ec41
VZ
606 break;
607
608 case wxBITMAP_TYPE_JPEG:
609 case wxBITMAP_TYPE_JPEG_RESOURCE:
610 if ( !MGL_getJPEGSize(fullname.mb_str(), &width, &height, &bpp, &pf) )
127eab18 611 return false;
32b8ec41 612 bitmap->Create(width, height, -1);
127eab18 613 if ( !bitmap->Ok() ) return false;
32b8ec41
VZ
614 dc.SelectObject(*bitmap);
615 if ( !dc.GetMGLDC()->loadJPEGIntoDC(fullname.mb_str(), 0, 0, TRUE) )
127eab18 616 return false;
32b8ec41
VZ
617 break;
618
619 case wxBITMAP_TYPE_PNG:
620 case wxBITMAP_TYPE_PNG_RESOURCE:
621 if ( !MGL_getPNGSize(fullname.mb_str(), &width, &height, &bpp, &pf) )
127eab18 622 return false;
32b8ec41 623 bitmap->Create(width, height, -1);
127eab18 624 if ( !bitmap->Ok() ) return false;
32b8ec41
VZ
625 dc.SelectObject(*bitmap);
626 if ( !dc.GetMGLDC()->loadPNGIntoDC(fullname.mb_str(), 0, 0, TRUE) )
127eab18 627 return false;
32b8ec41
VZ
628 break;
629
630 case wxBITMAP_TYPE_PCX:
631 case wxBITMAP_TYPE_PCX_RESOURCE:
632 if ( !MGL_getPCXSize(fullname.mb_str(), &width, &height, &bpp) )
127eab18 633 return false;
32b8ec41 634 bitmap->Create(width, height, -1);
127eab18 635 if ( !bitmap->Ok() ) return false;
32b8ec41
VZ
636 dc.SelectObject(*bitmap);
637 if ( !dc.GetMGLDC()->loadPCXIntoDC(fullname.mb_str(), 0, 0, TRUE) )
127eab18 638 return false;
32b8ec41
VZ
639 break;
640
641 default:
642 wxFAIL_MSG(wxT("Unsupported image format."));
643 break;
644 }
645
127eab18 646 return true;
32b8ec41
VZ
647}
648
127eab18 649bool wxMGLBitmapHandler::SaveFile(const wxBitmap *bitmap, const wxString& name,
32b8ec41
VZ
650 int type, const wxPalette * WXUNUSED(palette))
651{
652 wxMemoryDC mem;
653 MGLDevCtx *tdc;
654 int w = bitmap->GetWidth(),
655 h = bitmap->GetHeight();
656
657 mem.SelectObject(*bitmap);
658 tdc = mem.GetMGLDC();
659
660 switch (type)
661 {
662 case wxBITMAP_TYPE_BMP:
127eab18 663 return (bool)tdc->saveBitmapFromDC(name.mb_str(), 0, 0, w, h);
32b8ec41 664 case wxBITMAP_TYPE_JPEG:
127eab18 665 return (bool)tdc->saveJPEGFromDC(name.mb_str(), 0, 0, w, h, 75);
32b8ec41 666 case wxBITMAP_TYPE_PNG:
127eab18 667 return (bool)tdc->savePNGFromDC(name.mb_str(), 0, 0, w, h);
32b8ec41 668 case wxBITMAP_TYPE_PCX:
127eab18 669 return (bool)tdc->savePCXFromDC(name.mb_str(), 0, 0, w, h);
32b8ec41 670 }
127eab18
WS
671
672 return false;
32b8ec41
VZ
673}
674
675
676
127eab18 677// let's handle PNGs in special way because they have alpha channel
32b8ec41
VZ
678// which we can access via bitmap_t most easily
679class wxPNGBitmapHandler: public wxMGLBitmapHandler
680{
681public:
682 wxPNGBitmapHandler(wxBitmapType type,
683 const wxString& extension, const wxString& name)
684 : wxMGLBitmapHandler(type, extension, name) {}
685
686 virtual bool LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
687 int desiredWidth, int desiredHeight);
688};
689
127eab18
WS
690bool wxPNGBitmapHandler::LoadFile(wxBitmap *bitmap, const wxString& name,
691 long flags,
32b8ec41
VZ
692 int desiredWidth, int desiredHeight)
693{
694 int width, height, bpp;
695 pixel_format_t pf;
696 wxString fullname;
697
698 if ( flags == wxBITMAP_TYPE_PNG_RESOURCE )
699 fullname = name + wxT(".png");
700 else
701 fullname = name;
702
703 if ( !MGL_getPNGSize(fullname.mb_str(), &width, &height, &bpp, &pf) )
127eab18 704 return false;
32b8ec41
VZ
705
706 if ( bpp != 32 )
707 {
708 // We can load ordinary PNGs faster with 'normal' MGL handler.
709 // Only RGBA PNGs need to be processed in special way because
710 // we have to convert alpha channel to mask
127eab18 711 return wxMGLBitmapHandler::LoadFile(bitmap, name, flags,
32b8ec41
VZ
712 desiredWidth, desiredHeight);
713 }
127eab18 714
32b8ec41 715 bitmap_t *bmp = MGL_loadPNG(fullname.mb_str(), TRUE);
127eab18
WS
716
717 if ( bmp == NULL ) return false;
32b8ec41
VZ
718
719 bitmap->Create(bmp->width, bmp->height, -1);
127eab18
WS
720 if ( !bitmap->Ok() ) return false;
721
32b8ec41
VZ
722 // convert bmp to display's depth and write it to *bitmap:
723 wxMemoryDC dc;
724 dc.SelectObject(*bitmap);
725 dc.GetMGLDC()->putBitmap(0, 0, bmp, MGL_REPLACE_MODE);
726 dc.SelectObject(wxNullBitmap);
127eab18 727
32b8ec41
VZ
728 // create mask, if bmp contains alpha channel (ARGB format):
729 if ( bmp->bitsPerPixel == 32 )
730 {
731 int x, y;
732 wxUint32 *s = (wxUint32*)bmp->surface;
733 for (y = 0; y < bmp->height; y++)
734 {
735 s = ((wxUint32*)bmp->surface) + y * bmp->bytesPerLine/4;
736 for (x = 0; x < bmp->width; x++, s ++)
737 {
127eab18 738 if ( ((((*s) >> bmp->pf->alphaPos) & bmp->pf->alphaMask)
c0f02dbc 739 << bmp->pf->alphaAdjust) < 128 )
32b8ec41
VZ
740 *s = 0;
741 else
742 *s = 0x00FFFFFF; // white
743 }
744 }
745 wxBitmap mask(bmp->width, bmp->height, 1);
746 dc.SelectObject(mask);
747 dc.GetMGLDC()->putBitmap(0, 0, bmp, MGL_REPLACE_MODE);
748 dc.SelectObject(wxNullBitmap);
749 bitmap->SetMask(new wxMask(mask));
750 }
127eab18 751
32b8ec41 752 MGL_unloadBitmap(bmp);
127eab18
WS
753
754 return true;
32b8ec41
VZ
755}
756
757
758
759
760class wxICOBitmapHandler: public wxBitmapHandler
761{
762 public:
763 wxICOBitmapHandler(wxBitmapType type,
764 const wxString& extension, const wxString& name);
127eab18
WS
765
766 virtual bool Create(wxBitmap *WXUNUSED(bitmap),
452418c4 767 const void* WXUNUSED(data),
127eab18
WS
768 long WXUNUSED(flags),
769 int WXUNUSED(width),
770 int WXUNUSED(height),
771 int WXUNUSED(depth) = 1)
772 { return false; }
773
32b8ec41
VZ
774 virtual bool LoadFile(wxBitmap *bitmap, const wxString& name, long flags,
775 int desiredWidth, int desiredHeight);
127eab18 776 virtual bool SaveFile(const wxBitmap *bitmap, const wxString& name,
32b8ec41
VZ
777 int type, const wxPalette *palette = NULL);
778};
779
127eab18 780wxICOBitmapHandler::wxICOBitmapHandler(wxBitmapType type,
32b8ec41
VZ
781 const wxString& extension,
782 const wxString& name)
783 : wxBitmapHandler()
784{
785 SetType(type);
786 SetName(name);
787 SetExtension(extension);
788}
789
127eab18
WS
790bool wxICOBitmapHandler::LoadFile(wxBitmap *bitmap, const wxString& name,
791 long flags,
792 int WXUNUSED(desiredWidth),
32b8ec41
VZ
793 int WXUNUSED(desiredHeight))
794{
795 icon_t *icon = NULL;
796 MGLDevCtx *dc;
797
798 if ( flags == wxBITMAP_TYPE_ICO_RESOURCE )
799 icon = MGL_loadIcon(wxString(name + wxT(".ico")).mb_str(), TRUE);
127eab18 800 else
32b8ec41
VZ
801 icon = MGL_loadIcon(name.mb_str(), TRUE);
802
127eab18 803 if ( icon == NULL ) return false;
32b8ec41
VZ
804
805 bitmap->Create(icon->xorMask.width, icon->xorMask.height);
806
807 wxMemoryDC mem;
808 mem.SelectObject(*bitmap);
809 dc = mem.GetMGLDC();
810 dc->putBitmap(0, 0, &(icon->xorMask), MGL_REPLACE_MODE);
811 mem.SelectObject(wxNullBitmap);
812
813 wxBitmap mask(icon->xorMask.width, icon->xorMask.height, 1);
814 mem.SelectObject(mask);
815 dc = mem.GetMGLDC();
816
817 wxCurrentDCSwitcher curDC(dc);
818 dc->setColor(0);
819 dc->setBackColor(1);
820 dc->clearDevice();
821 dc->putMonoImage(0, 0, icon->xorMask.width, icon->byteWidth,
822 icon->xorMask.height, (void*)icon->andMask);
127eab18 823
32b8ec41
VZ
824 bitmap->SetMask(new wxMask(mask));
825
826 MGL_unloadIcon(icon);
127eab18
WS
827
828 return true;
32b8ec41
VZ
829}
830
127eab18
WS
831bool wxICOBitmapHandler::SaveFile(const wxBitmap *WXUNUSED(bitmap),
832 const wxString& WXUNUSED(name),
833 int WXUNUSED(type),
834 const wxPalette * WXUNUSED(palette))
32b8ec41 835{
127eab18 836 return false;
32b8ec41
VZ
837}
838
839
840
841
842/*static*/ void wxBitmap::InitStandardHandlers()
843{
844 AddHandler(new wxMGLBitmapHandler(wxBITMAP_TYPE_BMP, wxT("bmp"), wxT("Windows bitmap")));
845 AddHandler(new wxMGLBitmapHandler(wxBITMAP_TYPE_BMP_RESOURCE, wxEmptyString, wxT("Windows bitmap resource")));
846 AddHandler(new wxMGLBitmapHandler(wxBITMAP_TYPE_JPEG, wxT("jpg"), wxT("JPEG image")));
847 AddHandler(new wxMGLBitmapHandler(wxBITMAP_TYPE_JPEG_RESOURCE, wxEmptyString, wxT("JPEG resource")));
848 AddHandler(new wxMGLBitmapHandler(wxBITMAP_TYPE_PCX, wxT("pcx"), wxT("PCX image")));
849 AddHandler(new wxMGLBitmapHandler(wxBITMAP_TYPE_PCX_RESOURCE, wxEmptyString, wxT("PCX resource")));
850
851 AddHandler(new wxPNGBitmapHandler(wxBITMAP_TYPE_PNG, wxT("png"), wxT("PNG image")));
852 AddHandler(new wxPNGBitmapHandler(wxBITMAP_TYPE_PNG_RESOURCE, wxEmptyString, wxT("PNG resource")));
853
854 AddHandler(new wxICOBitmapHandler(wxBITMAP_TYPE_ICO, wxT("ico"), wxT("Icon resource")));
855 AddHandler(new wxICOBitmapHandler(wxBITMAP_TYPE_ICO_RESOURCE, wxEmptyString, wxT("Icon resource")));
856}