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