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