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