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