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