]> git.saurik.com Git - wxWidgets.git/blame - src/x11/bitmap.cpp
Fix command line parsing in media player sample.
[wxWidgets.git] / src / x11 / bitmap.cpp
CommitLineData
83df96d6 1/////////////////////////////////////////////////////////////////////////////
55034339 2// Name: src/x11/bitmap.cpp
83df96d6 3// Purpose: wxBitmap
a11672a4 4// Author: Julian Smart, Robert Roebling
83df96d6
JS
5// Modified by:
6// Created: 17/09/98
7// RCS-ID: $Id$
a11672a4 8// Copyright: (c) Julian Smart, Robert Roebling
65571936 9// Licence: wxWindows licence
83df96d6
JS
10/////////////////////////////////////////////////////////////////////////////
11
55034339
WS
12// for compilers that support precompilation, includes "wx.h".
13#include "wx/wxprec.h"
14
83df96d6 15#include "wx/bitmap.h"
e4db172a
WS
16
17#ifndef WX_PRECOMP
18 #include "wx/log.h"
670f9935 19 #include "wx/app.h"
f38924e8 20 #include "wx/dcmemory.h"
923d28da 21 #include "wx/icon.h"
18680f86 22 #include "wx/math.h"
155ecd4c 23 #include "wx/image.h"
e4db172a
WS
24#endif
25
bc797f4c 26#include "wx/x11/private.h"
83df96d6 27
c79a329d
JS
28/* No point in using libXPM for NanoX */
29#if wxUSE_NANOX
30#undef wxHAVE_LIB_XPM
31#define wxHAVE_LIB_XPM 0
a20c04ab
JS
32
33// Copy from the drawable to the wxImage
34bool wxGetImageFromDrawable(GR_DRAW_ID drawable, int srcX, int srcY, int width, int height, wxImage& image);
c79a329d
JS
35#endif
36
04a8da5f
VZ
37static WXPixmap wxGetSubPixmap( WXDisplay* xdisplay, WXPixmap xpixmap,
38 int x, int y, int width, int height,
39 int depth );
40
707440dc 41#if wxUSE_XPM
83df96d6 42#if wxHAVE_LIB_XPM
707440dc
JS
43#include <X11/xpm.h>
44#else
45#include "wx/xpmdecod.h"
46#include "wx/wfstream.h"
47#endif
83df96d6 48#endif
83df96d6 49
a11672a4
RR
50//-----------------------------------------------------------------------------
51// wxMask
52//-----------------------------------------------------------------------------
83df96d6 53
a11672a4 54IMPLEMENT_DYNAMIC_CLASS(wxMask,wxObject)
83df96d6 55
a11672a4 56wxMask::wxMask()
83df96d6 57{
a11672a4
RR
58 m_bitmap = NULL;
59 m_display = NULL;
83df96d6
JS
60}
61
04a8da5f
VZ
62wxMask::wxMask(const wxMask& mask)
63{
64 m_display = mask.m_display;
65 if ( !mask.m_bitmap )
66 {
67 m_bitmap = NULL;
68 return;
69 }
70
71 m_size = mask.m_size;
72
73 // Duplicate the mask bitmap using the existing wxGetSubPixmap() function.
74 // There are probably/surely better ways to do it.
75 m_bitmap = wxGetSubPixmap(m_display, mask.m_bitmap,
76 0, 0, m_size.x, m_size.y,
77 1);
78}
79
a11672a4 80wxMask::wxMask( const wxBitmap& bitmap, const wxColour& colour )
83df96d6 81{
a11672a4
RR
82 m_bitmap = NULL;
83 Create( bitmap, colour );
83df96d6
JS
84}
85
a11672a4 86wxMask::wxMask( const wxBitmap& bitmap, int paletteIndex )
83df96d6 87{
a11672a4
RR
88 m_bitmap = NULL;
89 Create( bitmap, paletteIndex );
83df96d6
JS
90}
91
a11672a4 92wxMask::wxMask( const wxBitmap& bitmap )
83df96d6 93{
a11672a4
RR
94 m_bitmap = NULL;
95 Create( bitmap );
83df96d6
JS
96}
97
a11672a4 98wxMask::~wxMask()
83df96d6 99{
a11672a4
RR
100 if (m_bitmap)
101 XFreePixmap( (Display*) m_display, (Pixmap) m_bitmap );
83df96d6
JS
102}
103
a11672a4
RR
104bool wxMask::Create( const wxBitmap& bitmap,
105 const wxColour& colour )
83df96d6 106{
04a8da5f
VZ
107 m_size = bitmap.GetSize();
108
c79a329d 109#if !wxUSE_NANOX
a11672a4
RR
110 if (m_bitmap)
111 {
112 XFreePixmap( (Display*) m_display, (Pixmap) m_bitmap );
113 m_bitmap = NULL;
114 }
83df96d6 115
a11672a4 116 m_display = bitmap.GetDisplay();
83df96d6 117
2b5f62a0 118 wxImage image = bitmap.ConvertToImage();
a1b806b9 119 if (!image.IsOk()) return false;
0cad49b4 120
a11672a4 121 m_display = bitmap.GetDisplay();
0cad49b4 122
a11672a4 123 Display *xdisplay = (Display*) m_display;
a11672a4
RR
124 int xscreen = DefaultScreen( xdisplay );
125 Window xroot = RootWindow( xdisplay, xscreen );
0cad49b4 126
a11672a4
RR
127 m_bitmap = (WXPixmap) XCreatePixmap( xdisplay, xroot, image.GetWidth(), image.GetHeight(), 1 );
128 GC gc = XCreateGC( xdisplay, (Pixmap) m_bitmap, 0, NULL );
83df96d6 129
a11672a4
RR
130 XSetForeground( xdisplay, gc, WhitePixel(xdisplay,xscreen) );
131 XSetFillStyle( xdisplay, gc, FillSolid );
132 XFillRectangle( xdisplay, (Pixmap) m_bitmap, gc, 0, 0, image.GetWidth(), image.GetHeight() );
83df96d6 133
a11672a4
RR
134 unsigned char *data = image.GetData();
135 int index = 0;
83df96d6 136
a11672a4
RR
137 unsigned char red = colour.Red();
138 unsigned char green = colour.Green();
139 unsigned char blue = colour.Blue();
83df96d6 140
9ce8d6a2 141 int bpp = wxTheApp->GetVisualInfo(m_display)->m_visualDepth;
0cad49b4 142
a11672a4
RR
143 if (bpp == 15)
144 {
55034339
WS
145 red &= 0xf8;
146 green &= 0xf8;
147 blue &= 0xf8;
a11672a4
RR
148 } else
149 if (bpp == 16)
150 {
55034339
WS
151 red &= 0xf8;
152 green &= 0xfc;
153 blue &= 0xf8;
a11672a4
RR
154 } else
155 if (bpp == 12)
156 {
55034339
WS
157 red &= 0xf0;
158 green &= 0xf0;
159 blue &= 0xf0;
a11672a4 160 }
83df96d6 161
a11672a4 162 XSetForeground( xdisplay, gc, BlackPixel(xdisplay,xscreen) );
83df96d6 163
288efe84
JS
164 int width = image.GetWidth();
165 int height = image.GetHeight();
166 for (int j = 0; j < height; j++)
a11672a4
RR
167 {
168 int start_x = -1;
169 int i;
288efe84 170 for (i = 0; i < width; i++)
83df96d6 171 {
a11672a4
RR
172 if ((data[index] == red) &&
173 (data[index+1] == green) &&
174 (data[index+2] == blue))
175 {
176 if (start_x == -1)
177 start_x = i;
178 }
179 else
180 {
181 if (start_x != -1)
182 {
183 XDrawLine( xdisplay, (Pixmap) m_bitmap, gc, start_x, j, i-1, j );
184 start_x = -1;
185 }
186 }
187 index += 3;
83df96d6 188 }
a11672a4
RR
189 if (start_x != -1)
190 XDrawLine( xdisplay, (Pixmap) m_bitmap, gc, start_x, j, i, j );
83df96d6 191 }
0cad49b4 192
a11672a4 193 XFreeGC( xdisplay, gc );
83df96d6 194
55034339 195 return true;
c79a329d 196#else
55034339 197 return false;
c79a329d
JS
198#endif
199 // wxUSE_NANOX
83df96d6
JS
200}
201
a11672a4 202bool wxMask::Create( const wxBitmap& bitmap, int paletteIndex )
83df96d6 203{
a11672a4
RR
204 unsigned char r,g,b;
205 wxPalette *pal = bitmap.GetPalette();
83df96d6 206
55034339 207 wxCHECK_MSG( pal, false, wxT("Cannot create mask from bitmap without palette") );
83df96d6 208
a11672a4 209 pal->GetRGB(paletteIndex, &r, &g, &b);
83df96d6 210
a11672a4 211 return Create(bitmap, wxColour(r, g, b));
83df96d6
JS
212}
213
a11672a4 214bool wxMask::Create( const wxBitmap& bitmap )
83df96d6 215{
c79a329d 216#if !wxUSE_NANOX
a11672a4
RR
217 if (m_bitmap)
218 {
219 XFreePixmap( (Display*) m_display, (Pixmap) m_bitmap );
220 m_bitmap = NULL;
83df96d6
JS
221 }
222
a1b806b9 223 if (!bitmap.IsOk()) return false;
83df96d6 224
55034339 225 wxCHECK_MSG( bitmap.GetBitmap(), false, wxT("Cannot create mask from colour bitmap") );
0cad49b4 226
a11672a4 227 m_display = bitmap.GetDisplay();
83df96d6 228
a11672a4
RR
229 int xscreen = DefaultScreen( (Display*) m_display );
230 Window xroot = RootWindow( (Display*) m_display, xscreen );
0cad49b4 231
a11672a4 232 m_bitmap = (WXPixmap) XCreatePixmap( (Display*) m_display, xroot, bitmap.GetWidth(), bitmap.GetHeight(), 1 );
83df96d6 233
55034339 234 if (!m_bitmap) return false;
0cad49b4 235
a11672a4 236 GC gc = XCreateGC( (Display*) m_display, (Pixmap) m_bitmap, 0, NULL );
83df96d6 237
a11672a4
RR
238 XCopyPlane( (Display*) m_display, (Pixmap) bitmap.GetBitmap(), (Pixmap) m_bitmap,
239 gc, 0, 0, bitmap.GetWidth(), bitmap.GetHeight(), 0, 0, 1 );
0cad49b4 240
a11672a4 241 XFreeGC( (Display*) m_display, gc );
83df96d6 242
55034339 243 return true;
c79a329d 244#else
55034339 245 return false;
c79a329d
JS
246#endif
247 // wxUSE_NANOX
83df96d6
JS
248}
249
a11672a4 250//-----------------------------------------------------------------------------
732d8c74 251// wxBitmapRefData
a11672a4 252//-----------------------------------------------------------------------------
83df96d6 253
8f884a0d 254class wxBitmapRefData : public wxGDIRefData
83df96d6 255{
a11672a4
RR
256public:
257 wxBitmapRefData();
dd21c826 258 wxBitmapRefData(const wxBitmapRefData& data);
d3c7fc99 259 virtual ~wxBitmapRefData();
a11672a4 260
dd21c826
VZ
261 // shouldn't be called more than once as it doesn't free the existing data
262 bool Create(int width, int height, int depth);
263
8f884a0d
VZ
264 virtual bool IsOk() const { return m_pixmap || m_bitmap; }
265
dd21c826
VZ
266 Pixmap m_pixmap;
267 Pixmap m_bitmap;
268 Display *m_display;
a11672a4
RR
269 wxMask *m_mask;
270 int m_width;
271 int m_height;
272 int m_bpp;
273 wxPalette *m_palette;
274};
83df96d6 275
a11672a4 276wxBitmapRefData::wxBitmapRefData()
83df96d6 277{
dd21c826
VZ
278 m_pixmap = 0;
279 m_bitmap = 0;
a11672a4 280 m_display = NULL;
dd21c826 281 m_mask = NULL;
a11672a4
RR
282 m_width = 0;
283 m_height = 0;
284 m_bpp = 0;
d3b9f782 285 m_palette = NULL;
83df96d6
JS
286}
287
dd21c826
VZ
288wxBitmapRefData::wxBitmapRefData(const wxBitmapRefData& data)
289{
290 m_pixmap = 0;
291 m_bitmap = 0;
292 m_display = data.m_display;
293 m_mask = NULL; // FIXME: should copy
294 m_palette = NULL; // FIXME: should copy
295
296 Create(data.m_width, data.m_height, data.m_bpp);
297}
298
299bool wxBitmapRefData::Create(int width, int height, int depth)
300{
301 m_width = width;
302 m_height = height;
303 m_bpp = depth;
304
305 m_display = wxGlobalDisplay();
306
307 wxCHECK_MSG( m_display, false, wxT("No display") );
308
309 int xscreen = DefaultScreen(m_display);
310 int bpp = DefaultDepth(m_display, xscreen);
311 if ( depth == -1 )
312 depth = bpp;
313
314 wxCHECK_MSG( (depth == bpp) || (depth == 1), false,
315 wxT("invalid bitmap depth") );
316
317#if wxUSE_NANOX
318 m_pixmap = (WXPixmap) GrNewPixmap(width, height, NULL);
319#else // !wxUSE_NANOX
320 Window xroot = RootWindow(m_display, xscreen);
321
03647350 322 *(depth == 1 ? &m_bitmap : &m_pixmap) =
dd21c826
VZ
323 XCreatePixmap(m_display, xroot, width, height, depth);
324#endif // wxUSE_NANOX/!wxUSE_NANOX
325
326 wxCHECK_MSG( m_pixmap || m_bitmap, false, wxT("Bitmap creation failed") );
327
328 return true;
329}
330
a11672a4 331wxBitmapRefData::~wxBitmapRefData()
83df96d6 332{
dd21c826
VZ
333 if (m_pixmap)
334 XFreePixmap(m_display, m_pixmap);
335 if (m_bitmap)
336 XFreePixmap(m_display, m_bitmap);
337 delete m_mask;
338 delete m_palette;
83df96d6
JS
339}
340
a11672a4 341//-----------------------------------------------------------------------------
83df96d6 342
62ad3cb3
MB
343// helper function
344
345static WXPixmap wxGetSubPixmap( WXDisplay* xdisplay, WXPixmap xpixmap,
346 int x, int y, int width, int height,
347 int depth )
348{
4c620d19 349 Display * const dpy = (Display *)xdisplay;
62ad3cb3 350
4c620d19
VZ
351 int xscreen = DefaultScreen( dpy );
352 Window xroot = RootWindow( dpy, xscreen );
353 Visual* xvisual = DefaultVisual( dpy, xscreen );
354
355 XImage* ximage = XCreateImage( dpy, xvisual, depth,
62ad3cb3
MB
356 ZPixmap, 0, 0, width, height, 32, 0 );
357 ximage->data = (char*)malloc( ximage->bytes_per_line * ximage->height );
4c620d19 358 ximage = XGetSubImage( dpy, (Pixmap)xpixmap,
0cad49b4 359 x, y, width, height,
62ad3cb3
MB
360 AllPlanes, ZPixmap, ximage, 0, 0 );
361
4c620d19
VZ
362 GC gc = XCreateGC( dpy, (Pixmap)xpixmap, 0, NULL );
363 Pixmap ret = XCreatePixmap( dpy, xroot,
62ad3cb3
MB
364 width, height, depth );
365
4c620d19 366 XPutImage( dpy, ret, gc, ximage,
62ad3cb3
MB
367 0, 0, 0, 0, width, height );
368 XDestroyImage( ximage );
4c620d19 369 XFreeGC( dpy, gc );
62ad3cb3
MB
370
371 return (WXPixmap)ret;
372}
373
83df96d6 374
732d8c74
FM
375//-----------------------------------------------------------------------------
376// wxBitmap
377//-----------------------------------------------------------------------------
83df96d6 378
732d8c74 379#define M_BMPDATA ((wxBitmapRefData *)m_refData)
83df96d6 380
732d8c74 381IMPLEMENT_DYNAMIC_CLASS(wxBitmap,wxGDIObject)
83df96d6 382
a11672a4 383bool wxBitmap::Create( int width, int height, int depth )
83df96d6 384{
a11672a4
RR
385 UnRef();
386
637b7e4f 387 wxCHECK_MSG( (width > 0) && (height > 0), false, wxT("invalid bitmap size") );
a11672a4
RR
388
389 m_refData = new wxBitmapRefData();
0cad49b4 390
dd21c826 391 return M_BMPDATA->Create(width, height, depth);
83df96d6
JS
392}
393
452418c4 394bool wxBitmap::Create(const void* data, wxBitmapType type,
62ad3cb3 395 int width, int height, int depth)
83df96d6 396{
a11672a4
RR
397 UnRef();
398
62ad3cb3 399 wxBitmapHandler *handler = FindHandler(type);
a11672a4 400
62ad3cb3 401 if ( handler == NULL ) {
54385bdb 402 wxLogWarning(wxT("no data bitmap handler for type %ld defined."),
62ad3cb3 403 (long)type);
83df96d6 404
55034339 405 return false;
62ad3cb3 406 }
83df96d6 407
62ad3cb3
MB
408 return handler->Create(this, data, type, width, height, depth);
409}
83df96d6 410
dd38c875
MB
411bool wxBitmap::Create(WXPixmap pixmap)
412{
413 UnRef();
414 Pixmap xpixmap = (Pixmap)pixmap;
415 Display* xdisplay = wxGlobalDisplay();
416 int xscreen = DefaultScreen( xdisplay );
417 Window xroot = RootWindow( xdisplay, xscreen );
418
419 // make a copy of the Pixmap
420 Window root;
dd38c875
MB
421 int x, y;
422 unsigned width, height, border, depth;
423
424 XGetGeometry( xdisplay, (Drawable)xpixmap, &root, &x, &y,
425 &width, &height, &border, &depth );
dd21c826 426 Pixmap copy = XCreatePixmap( xdisplay, xroot, width, height, depth );
dd38c875
MB
427
428 GC gc = XCreateGC( xdisplay, copy, 0, NULL );
429 XCopyArea( xdisplay, xpixmap, copy, gc, 0, 0, width, height, 0, 0 );
430 XFreeGC( xdisplay, gc );
431
432 // fill in ref data
433 wxBitmapRefData* ref = new wxBitmapRefData();
434
435 if( depth == 1 )
dd21c826 436 ref->m_bitmap = copy;
dd38c875 437 else
dd21c826 438 ref->m_pixmap = copy;
dd38c875 439
dd21c826 440 ref->m_display = xdisplay;
dd38c875
MB
441 ref->m_width = width;
442 ref->m_height = height;
443 ref->m_bpp = depth;
444
445 m_refData = ref;
446
447 return true;
448}
449
452418c4 450wxBitmap::wxBitmap(const char* const* bits)
62ad3cb3 451{
452418c4 452 Create(bits, wxBITMAP_TYPE_XPM_DATA, 0, 0, 0);
83df96d6
JS
453}
454
8f884a0d 455wxGDIRefData *wxBitmap::CreateGDIRefData() const
dd21c826
VZ
456{
457 return new wxBitmapRefData;
458}
459
8f884a0d 460wxGDIRefData *wxBitmap::CloneGDIRefData(const wxGDIRefData *data) const
dd21c826 461{
5c33522f 462 return new wxBitmapRefData(*static_cast<const wxBitmapRefData *>(data));
dd21c826
VZ
463}
464
a11672a4 465bool wxBitmap::CreateFromImage( const wxImage& image, int depth )
83df96d6 466{
c79a329d 467#if wxUSE_NANOX
a1b806b9 468 if (!image.IsOk())
c79a329d 469 {
a1b806b9 470 wxASSERT_MSG(image.IsOk(), wxT("Invalid wxImage passed to wxBitmap::CreateFromImage."));
55034339 471 return false;
c79a329d 472 }
0cad49b4 473
c79a329d
JS
474 int w = image.GetWidth();
475 int h = image.GetHeight();
0cad49b4 476
c79a329d 477 if (!Create(w, h, depth))
55034339 478 return false;
c79a329d 479
888d3c80
JS
480 // Unfortunately the mask has to be screen-depth since
481 // 1-bpp bitmaps don't seem to be supported
482 // TODO: implement transparent drawing, presumably
483 // by doing several blits as per the Windows
484 // implementation because Nano-X doesn't support
485 // XSetClipMask.
f22033e5
JS
486 // TODO: could perhaps speed this function up
487 // by making a buffer of pixel values,
488 // and then calling GrArea to write that to the
489 // pixmap. See demos/nxroach.c.
c79a329d 490
888d3c80 491 bool hasMask = image.HasMask();
0cad49b4 492
888d3c80
JS
493 GC pixmapGC = GrNewGC();
494 Pixmap pixmap = (Pixmap) GetPixmap();
495
496 GC maskGC = 0;
497 Pixmap maskPixmap = 0;
498
499 unsigned char maskR = 0;
500 unsigned char maskG = 0;
501 unsigned char maskB = 0;
502
503 if (hasMask)
504 {
505 maskR = image.GetMaskRed();
506 maskG = image.GetMaskGreen();
507 maskB = image.GetMaskBlue();
0cad49b4 508
888d3c80
JS
509 maskGC = GrNewGC();
510 maskPixmap = GrNewPixmap(w, h, 0);
511 if (!maskPixmap)
55034339 512 hasMask = false;
888d3c80
JS
513 else
514 {
515 wxMask* mask = new wxMask;
516 mask->SetBitmap((WXPixmap) maskPixmap);
517 SetMask(mask);
518 }
519 }
520
521 GR_COLOR lastPixmapColour = 0;
522 GR_COLOR lastMaskColour = 0;
0cad49b4 523
c79a329d
JS
524 int i, j;
525 for (i = 0; i < w; i++)
526 {
527 for (j = 0; j < h; j++)
528 {
529 unsigned char red = image.GetRed(i, j);
530 unsigned char green = image.GetGreen(i, j);
531 unsigned char blue = image.GetBlue(i, j);
c79a329d 532
888d3c80
JS
533 GR_COLOR colour = GR_RGB(red, green, blue);
534
535 // Efficiency measure
536 if (colour != lastPixmapColour || (i == 0 && j == 0))
537 {
538 GrSetGCForeground(pixmapGC, colour);
539 lastPixmapColour = colour;
540 }
0cad49b4 541
888d3c80 542 GrPoint(pixmap, pixmapGC, i, j);
0cad49b4 543
c79a329d
JS
544 if (hasMask)
545 {
546 // scan the bitmap for the transparent colour and set the corresponding
547 // pixels in the mask to BLACK and the rest to WHITE
548 if (maskR == red && maskG == green && maskB == blue)
888d3c80
JS
549 {
550 colour = GR_RGB(0, 0, 0);
551 }
c79a329d 552 else
888d3c80
JS
553 {
554 colour = GR_RGB(255, 255, 255);
555 }
556 if (colour != lastMaskColour || (i == 0 && j == 0))
557 {
558 GrSetGCForeground(maskGC, colour);
559 lastMaskColour = colour;
560 }
561 GrPoint(maskPixmap, maskGC, i, j);
c79a329d 562 }
c79a329d
JS
563 }
564 }
888d3c80
JS
565
566 GrDestroyGC(pixmapGC);
567 if (hasMask)
568 GrDestroyGC(maskGC);
0cad49b4 569
55034339 570 return true;
c79a329d
JS
571#else
572 // !wxUSE_NANOX
0cad49b4 573
a11672a4 574 UnRef();
83df96d6 575
a1b806b9 576 wxCHECK_MSG( image.IsOk(), false, wxT("invalid image") );
637b7e4f 577 wxCHECK_MSG( depth == -1, false, wxT("invalid bitmap depth") );
83df96d6 578
a11672a4 579 m_refData = new wxBitmapRefData();
83df96d6 580
a11672a4 581 M_BMPDATA->m_display = wxGlobalDisplay();
0cad49b4 582
a11672a4 583 Display *xdisplay = (Display*) M_BMPDATA->m_display;
0cad49b4 584
a11672a4
RR
585 int xscreen = DefaultScreen( xdisplay );
586 Window xroot = RootWindow( xdisplay, xscreen );
587 Visual* xvisual = DefaultVisual( xdisplay, xscreen );
0cad49b4 588
9ce8d6a2 589 int bpp = wxTheApp->GetVisualInfo(M_BMPDATA->m_display)->m_visualDepth;
0cad49b4 590
a11672a4
RR
591 int width = image.GetWidth();
592 int height = image.GetHeight();
593 M_BMPDATA->m_width = width;
594 M_BMPDATA->m_height = height;
83df96d6 595
a11672a4
RR
596 if (depth != 1) depth = bpp;
597 M_BMPDATA->m_bpp = depth;
0cad49b4 598
a11672a4
RR
599 if (depth == 1)
600 {
54385bdb 601 wxFAIL_MSG( wxT("mono images later") );
a11672a4
RR
602 }
603 else
604 {
605 // Create image
0cad49b4 606
a11672a4
RR
607 XImage *data_image = XCreateImage( xdisplay, xvisual, bpp, ZPixmap, 0, 0, width, height, 32, 0 );
608 data_image->data = (char*) malloc( data_image->bytes_per_line * data_image->height );
0cad49b4 609
a11672a4
RR
610 if (data_image->data == NULL)
611 {
612 wxLogError( wxT("Out of memory.") ); // TODO clean
55034339 613 return false;
a11672a4 614 }
83df96d6 615
dd21c826 616 M_BMPDATA->m_pixmap = XCreatePixmap( xdisplay, xroot, width, height, depth );
83df96d6 617
16fc7c45
VZ
618 // Create mask if necessary
619 const bool hasMask = image.HasMask();
83df96d6 620
d3b9f782 621 XImage *mask_image = NULL;
16fc7c45 622 if ( hasMask )
0cad49b4 623 {
a11672a4
RR
624 mask_image = XCreateImage( xdisplay, xvisual, 1, ZPixmap, 0, 0, width, height, 32, 0 );
625 mask_image->data = (char*) malloc( mask_image->bytes_per_line * mask_image->height );
0cad49b4 626
a11672a4
RR
627 if (mask_image->data == NULL)
628 {
629 wxLogError( wxT("Out of memory.") ); // TODO clean
55034339 630 return false;
a11672a4 631 }
0cad49b4 632
a11672a4
RR
633 wxMask *mask = new wxMask();
634 mask->SetDisplay( xdisplay );
635 mask->SetBitmap( (WXPixmap) XCreatePixmap( xdisplay, xroot, width, height, 1 ) );
83df96d6 636
a11672a4
RR
637 SetMask( mask );
638 }
83df96d6 639
a11672a4 640 if (bpp < 8) bpp = 8;
83df96d6 641
a11672a4 642 // Render
83df96d6 643
a11672a4
RR
644 enum byte_order { RGB, RBG, BRG, BGR, GRB, GBR };
645 byte_order b_o = RGB;
83df96d6 646
9ce8d6a2
MB
647 wxXVisualInfo* vi = wxTheApp->GetVisualInfo(M_BMPDATA->m_display);
648 unsigned long greenMask = vi->m_visualGreenMask,
649 redMask = vi->m_visualRedMask,
650 blueMask = vi->m_visualBlueMask;
651
a11672a4
RR
652 if (bpp > 8)
653 {
9ce8d6a2
MB
654 if ((redMask > greenMask) && (greenMask > blueMask)) b_o = RGB;
655 else if ((redMask > blueMask) && (blueMask > greenMask)) b_o = RBG;
656 else if ((blueMask > redMask) && (redMask > greenMask)) b_o = BRG;
657 else if ((blueMask > greenMask) && (greenMask > redMask))b_o = BGR;
658 else if ((greenMask > redMask) && (redMask > blueMask)) b_o = GRB;
659 else if ((greenMask > blueMask) && (blueMask > redMask)) b_o = GBR;
a11672a4 660 }
83df96d6 661
a11672a4
RR
662 int r_mask = image.GetMaskRed();
663 int g_mask = image.GetMaskGreen();
664 int b_mask = image.GetMaskBlue();
83df96d6 665
a11672a4 666 unsigned char* data = image.GetData();
54385bdb 667 wxASSERT_MSG( data, wxT("No image data") );
0cad49b4 668
9ce8d6a2
MB
669 unsigned char *colorCube =
670 wxTheApp->GetVisualInfo(M_BMPDATA->m_display)->m_colorCube;
83df96d6 671
a11672a4
RR
672 int index = 0;
673 for (int y = 0; y < height; y++)
674 {
675 for (int x = 0; x < width; x++)
676 {
677 int r = data[index];
678 index++;
679 int g = data[index];
680 index++;
681 int b = data[index];
682 index++;
683
684 if (hasMask)
685 {
686 if ((r == r_mask) && (b == b_mask) && (g == g_mask))
687 XPutPixel( mask_image, x, y, 0 );
688 else
689 XPutPixel( mask_image, x, y, 1 );
690 }
83df96d6 691
a11672a4
RR
692 switch (bpp)
693 {
694 case 8:
695 {
696 int pixel = 0;
dc4025af 697 pixel = colorCube[ ((r & 0xf8) << 7) + ((g & 0xf8) << 2) + ((b & 0xf8) >> 3) ];
a11672a4
RR
698 XPutPixel( data_image, x, y, pixel );
699 break;
700 }
701 case 12: // SGI only
702 {
703 int pixel = 0;
704 switch (b_o)
705 {
706 case RGB: pixel = ((r & 0xf0) << 4) | (g & 0xf0) | ((b & 0xf0) >> 4); break;
707 case RBG: pixel = ((r & 0xf0) << 4) | (b & 0xf0) | ((g & 0xf0) >> 4); break;
708 case GRB: pixel = ((g & 0xf0) << 4) | (r & 0xf0) | ((b & 0xf0) >> 4); break;
709 case GBR: pixel = ((g & 0xf0) << 4) | (b & 0xf0) | ((r & 0xf0) >> 4); break;
710 case BRG: pixel = ((b & 0xf0) << 4) | (r & 0xf0) | ((g & 0xf0) >> 4); break;
711 case BGR: pixel = ((b & 0xf0) << 4) | (g & 0xf0) | ((r & 0xf0) >> 4); break;
712 }
713 XPutPixel( data_image, x, y, pixel );
714 break;
715 }
716 case 15:
717 {
718 int pixel = 0;
719 switch (b_o)
720 {
721 case RGB: pixel = ((r & 0xf8) << 7) | ((g & 0xf8) << 2) | ((b & 0xf8) >> 3); break;
722 case RBG: pixel = ((r & 0xf8) << 7) | ((b & 0xf8) << 2) | ((g & 0xf8) >> 3); break;
723 case GRB: pixel = ((g & 0xf8) << 7) | ((r & 0xf8) << 2) | ((b & 0xf8) >> 3); break;
724 case GBR: pixel = ((g & 0xf8) << 7) | ((b & 0xf8) << 2) | ((r & 0xf8) >> 3); break;
725 case BRG: pixel = ((b & 0xf8) << 7) | ((r & 0xf8) << 2) | ((g & 0xf8) >> 3); break;
726 case BGR: pixel = ((b & 0xf8) << 7) | ((g & 0xf8) << 2) | ((r & 0xf8) >> 3); break;
727 }
728 XPutPixel( data_image, x, y, pixel );
729 break;
730 }
731 case 16:
732 {
733 // I actually don't know if for 16-bit displays, it is alway the green
734 // component or the second component which has 6 bits.
735 int pixel = 0;
736 switch (b_o)
737 {
738 case RGB: pixel = ((r & 0xf8) << 8) | ((g & 0xfc) << 3) | ((b & 0xf8) >> 3); break;
739 case RBG: pixel = ((r & 0xf8) << 8) | ((b & 0xfc) << 3) | ((g & 0xf8) >> 3); break;
740 case GRB: pixel = ((g & 0xf8) << 8) | ((r & 0xfc) << 3) | ((b & 0xf8) >> 3); break;
741 case GBR: pixel = ((g & 0xf8) << 8) | ((b & 0xfc) << 3) | ((r & 0xf8) >> 3); break;
742 case BRG: pixel = ((b & 0xf8) << 8) | ((r & 0xfc) << 3) | ((g & 0xf8) >> 3); break;
743 case BGR: pixel = ((b & 0xf8) << 8) | ((g & 0xfc) << 3) | ((r & 0xf8) >> 3); break;
744 }
745 XPutPixel( data_image, x, y, pixel );
746 break;
747 }
748 case 32:
749 case 24:
750 {
751 int pixel = 0;
752 switch (b_o)
753 {
754 case RGB: pixel = (r << 16) | (g << 8) | b; break;
755 case RBG: pixel = (r << 16) | (b << 8) | g; break;
756 case BRG: pixel = (b << 16) | (r << 8) | g; break;
757 case BGR: pixel = (b << 16) | (g << 8) | r; break;
758 case GRB: pixel = (g << 16) | (r << 8) | b; break;
759 case GBR: pixel = (g << 16) | (b << 8) | r; break;
760 }
761 XPutPixel( data_image, x, y, pixel );
762 }
763 default: break;
764 }
765 } // for
766 } // for
83df96d6 767
a11672a4 768 // Blit picture
83df96d6 769
a11672a4
RR
770 GC gc = XCreateGC( xdisplay, (Pixmap) M_BMPDATA->m_pixmap, 0, NULL );
771 XPutImage( xdisplay, (Pixmap) M_BMPDATA->m_pixmap, gc, data_image, 0, 0, 0, 0, width, height );
a11672a4
RR
772 XDestroyImage( data_image );
773 XFreeGC( xdisplay, gc );
83df96d6 774
a11672a4 775 // Blit mask
0cad49b4 776
a11672a4
RR
777 if (image.HasMask())
778 {
779 GC gc = XCreateGC( xdisplay, (Pixmap) GetMask()->GetBitmap(), 0, NULL );
011c1748 780 XPutImage( xdisplay, (Pixmap) GetMask()->GetBitmap(), gc, mask_image, 0, 0, 0, 0, width, height );
a11672a4
RR
781
782 XDestroyImage( mask_image );
783 XFreeGC( xdisplay, gc );
784 }
785 }
83df96d6 786
55034339 787 return true;
c79a329d
JS
788#endif
789 // wxUSE_NANOX
83df96d6
JS
790}
791
a11672a4 792wxImage wxBitmap::ConvertToImage() const
83df96d6 793{
a11672a4 794 wxImage image;
83df96d6 795
a1b806b9 796 wxCHECK_MSG( IsOk(), wxNullImage, wxT("invalid bitmap") );
83df96d6 797
a11672a4
RR
798 Display *xdisplay = (Display*) M_BMPDATA->m_display;
799 wxASSERT_MSG( xdisplay, wxT("No display") );
c79a329d
JS
800
801#if wxUSE_NANOX
8601b2e1 802 //int bpp = DefaultDepth(xdisplay, xscreen);
f22033e5 803 wxGetImageFromDrawable((Pixmap) GetPixmap(), 0, 0, GetWidth(), GetHeight(), image);
c79a329d 804 return image;
c79a329d
JS
805#else
806 // !wxUSE_NANOX
9ce8d6a2 807 int bpp = wxTheApp->GetVisualInfo(M_BMPDATA->m_display)->m_visualDepth;
a11672a4
RR
808 XImage *x_image = NULL;
809 if (GetPixmap())
810 {
811 x_image = XGetImage( xdisplay, (Pixmap) GetPixmap(),
812 0, 0,
813 GetWidth(), GetHeight(),
814 AllPlanes, ZPixmap );
815 } else
816 if (GetBitmap())
817 {
818 x_image = XGetImage( xdisplay, (Pixmap) GetBitmap(),
819 0, 0,
820 GetWidth(), GetHeight(),
821 AllPlanes, ZPixmap );
822 } else
823 {
824 wxFAIL_MSG( wxT("Ill-formed bitmap") );
825 }
83df96d6 826
a11672a4 827 wxCHECK_MSG( x_image, wxNullImage, wxT("couldn't create image") );
83df96d6 828
a11672a4
RR
829 image.Create( GetWidth(), GetHeight() );
830 char unsigned *data = image.GetData();
83df96d6 831
a11672a4
RR
832 if (!data)
833 {
834 XDestroyImage( x_image );
835 wxFAIL_MSG( wxT("couldn't create image") );
836 return wxNullImage;
837 }
83df96d6 838
a11672a4
RR
839 XImage *x_image_mask = NULL;
840 if (GetMask())
841 {
842 x_image_mask = XGetImage( xdisplay, (Pixmap) GetMask()->GetBitmap(),
843 0, 0,
0cad49b4 844 GetWidth(), GetHeight(),
a11672a4 845 AllPlanes, ZPixmap );
83df96d6 846
a11672a4
RR
847 image.SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
848 }
83df96d6 849
a11672a4
RR
850 int red_shift_right = 0;
851 int green_shift_right = 0;
852 int blue_shift_right = 0;
853 int red_shift_left = 0;
854 int green_shift_left = 0;
855 int blue_shift_left = 0;
55034339 856 bool use_shift = false;
83df96d6 857
a11672a4
RR
858 if (GetPixmap())
859 {
9ce8d6a2
MB
860 wxXVisualInfo* vi = wxTheApp->GetVisualInfo(M_BMPDATA->m_display);
861
862 red_shift_right = vi->m_visualRedShift;
863 red_shift_left = 8 - vi->m_visualRedPrec;
864 green_shift_right = vi->m_visualGreenShift;
865 green_shift_left = 8 - vi->m_visualGreenPrec;
866 blue_shift_right = vi->m_visualBlueShift;
867 blue_shift_left = 8 - vi->m_visualBluePrec;
0cad49b4 868
9ce8d6a2
MB
869 use_shift = (vi->m_visualType == GrayScale) ||
870 (vi->m_visualType != PseudoColor);
a11672a4 871 }
0cad49b4 872
a11672a4
RR
873 if (GetBitmap())
874 {
875 bpp = 1;
876 }
83df96d6 877
9ce8d6a2
MB
878 XColor *colors = (XColor*)wxTheApp->
879 GetVisualInfo(M_BMPDATA->m_display)->m_visualColormap;
83df96d6 880
288efe84
JS
881 int width = GetWidth();
882 int height = GetHeight();
a11672a4 883 long pos = 0;
288efe84 884 for (int j = 0; j < height; j++)
a11672a4 885 {
288efe84 886 for (int i = 0; i < width; i++)
a11672a4
RR
887 {
888 unsigned long pixel = XGetPixel( x_image, i, j );
889 if (bpp == 1)
890 {
891 if (pixel == 0)
892 {
893 data[pos] = 0;
894 data[pos+1] = 0;
895 data[pos+2] = 0;
896 }
897 else
898 {
899 data[pos] = 255;
900 data[pos+1] = 255;
901 data[pos+2] = 255;
902 }
903 }
904 else if (use_shift)
905 {
55034339
WS
906 data[pos] = (unsigned char)((pixel >> red_shift_right) << red_shift_left);
907 data[pos+1] = (unsigned char)((pixel >> green_shift_right) << green_shift_left);
908 data[pos+2] = (unsigned char)((pixel >> blue_shift_right) << blue_shift_left);
a11672a4 909 }
dc4025af 910 else if (colors)
a11672a4 911 {
55034339
WS
912 data[pos] = (unsigned char)(colors[pixel].red >> 8);
913 data[pos+1] = (unsigned char)(colors[pixel].green >> 8);
914 data[pos+2] = (unsigned char)(colors[pixel].blue >> 8);
a11672a4 915 }
a11672a4
RR
916 else
917 {
918 wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") );
919 }
83df96d6 920
a11672a4
RR
921 if (x_image_mask)
922 {
923 int mask_pixel = XGetPixel( x_image_mask, i, j );
924 if (mask_pixel == 0)
925 {
926 data[pos] = 16;
927 data[pos+1] = 16;
928 data[pos+2] = 16;
929 }
930 }
83df96d6 931
a11672a4
RR
932 pos += 3;
933 }
934 }
83df96d6 935
a11672a4
RR
936 XDestroyImage( x_image );
937 if (x_image_mask) XDestroyImage( x_image_mask );
a11672a4 938 return image;
c79a329d
JS
939#endif
940 // wxUSE_NANOX
83df96d6
JS
941}
942
62ad3cb3 943wxBitmap::wxBitmap( const wxString &filename, wxBitmapType type )
83df96d6 944{
a11672a4 945 LoadFile( filename, type );
83df96d6
JS
946}
947
62ad3cb3 948wxBitmap::wxBitmap( const char bits[], int width, int height, int depth )
83df96d6 949{
62ad3cb3 950 m_refData = new wxBitmapRefData;
83df96d6 951
452418c4 952 (void) Create(bits, wxBITMAP_TYPE_XBM_DATA, width, height, depth);
83df96d6
JS
953}
954
a11672a4 955wxBitmap::~wxBitmap()
83df96d6 956{
83df96d6
JS
957}
958
a11672a4
RR
959int wxBitmap::GetHeight() const
960{
a1b806b9 961 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
83df96d6 962
a11672a4
RR
963 return M_BMPDATA->m_height;
964}
83df96d6 965
a11672a4
RR
966int wxBitmap::GetWidth() const
967{
a1b806b9 968 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
83df96d6 969
a11672a4
RR
970 return M_BMPDATA->m_width;
971}
83df96d6 972
a11672a4
RR
973int wxBitmap::GetDepth() const
974{
a1b806b9 975 wxCHECK_MSG( IsOk(), -1, wxT("invalid bitmap") );
83df96d6 976
a11672a4
RR
977 return M_BMPDATA->m_bpp;
978}
83df96d6 979
a11672a4
RR
980wxMask *wxBitmap::GetMask() const
981{
a1b806b9 982 wxCHECK_MSG( IsOk(), NULL, wxT("invalid bitmap") );
83df96d6 983
a11672a4
RR
984 return M_BMPDATA->m_mask;
985}
83df96d6 986
a11672a4
RR
987void wxBitmap::SetMask( wxMask *mask )
988{
a1b806b9 989 wxCHECK_RET( IsOk(), wxT("invalid bitmap") );
83df96d6 990
55ccdb93 991 AllocExclusive();
a11672a4 992 if (M_BMPDATA->m_mask) delete M_BMPDATA->m_mask;
83df96d6 993
a11672a4
RR
994 M_BMPDATA->m_mask = mask;
995}
83df96d6 996
a11672a4
RR
997bool wxBitmap::CopyFromIcon(const wxIcon& icon)
998{
999 *this = icon;
55034339 1000 return true;
a11672a4 1001}
83df96d6 1002
a11672a4
RR
1003wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const
1004{
a1b806b9 1005 wxCHECK_MSG( IsOk() &&
a11672a4 1006 (rect.x >= 0) && (rect.y >= 0) &&
62ad3cb3
MB
1007 (rect.x+rect.width <= M_BMPDATA->m_width ) &&
1008 (rect.y+rect.height <= M_BMPDATA->m_height),
a11672a4 1009 wxNullBitmap, wxT("invalid bitmap or bitmap region") );
83df96d6 1010
a11672a4 1011 wxBitmap ret( rect.width, rect.height, M_BMPDATA->m_bpp );
a1b806b9 1012 wxASSERT_MSG( ret.IsOk(), wxT("GetSubBitmap error") );
83df96d6 1013
62ad3cb3 1014 if( GetMask() )
83df96d6 1015 {
62ad3cb3
MB
1016 wxMask* mask = new wxMask();
1017 mask->SetDisplay( GetMask()->GetDisplay() );
1018 mask->SetBitmap( wxGetSubPixmap( GetMask()->GetDisplay(),
1019 GetMask()->GetBitmap(),
1020 rect.x, rect.y,
1021 rect.width, rect.height,
1022 1 ) );
1023
1024 ret.SetMask( mask );
a11672a4 1025 }
62ad3cb3
MB
1026
1027 if( GetPixmap() )
a11672a4 1028 {
62ad3cb3
MB
1029 ret.SetPixmap( wxGetSubPixmap( GetDisplay(),
1030 GetPixmap(),
1031 rect.x, rect.y,
1032 rect.width, rect.height,
1033 M_BMPDATA->m_bpp ) );
83df96d6
JS
1034 }
1035
62ad3cb3 1036 if( GetBitmap() )
83df96d6 1037 {
62ad3cb3
MB
1038 ret.SetBitmap( wxGetSubPixmap( GetDisplay(),
1039 GetBitmap(),
1040 rect.x, rect.y,
1041 rect.width, rect.height,
1042 1 ) );
83df96d6
JS
1043 }
1044
a11672a4
RR
1045 return ret;
1046}
83df96d6 1047
62ad3cb3
MB
1048bool wxBitmap::SaveFile( const wxString &name, wxBitmapType type,
1049 const wxPalette *palette ) const
a11672a4 1050{
a1b806b9 1051 wxCHECK_MSG( IsOk(), false, wxT("invalid bitmap") );
83df96d6 1052
62ad3cb3
MB
1053 wxBitmapHandler *handler = FindHandler(type);
1054
a11672a4 1055 // Try to save the bitmap via wxImage handlers:
62ad3cb3 1056 if (handler == NULL)
0cad49b4 1057 {
62ad3cb3 1058 wxImage image(this->ConvertToImage());
a1b806b9 1059 if (image.IsOk()) return image.SaveFile( name, type );
62ad3cb3 1060
55034339 1061 return false;
a11672a4 1062 }
83df96d6 1063
62ad3cb3 1064 return handler->SaveFile(this, name, type, palette);
a11672a4 1065}
83df96d6 1066
62ad3cb3 1067bool wxBitmap::LoadFile( const wxString &name, wxBitmapType type )
a11672a4
RR
1068{
1069 UnRef();
83df96d6 1070
55034339 1071 if (!wxFileExists(name)) return false;
83df96d6 1072
62ad3cb3 1073 wxBitmapHandler *handler = FindHandler(type);
83df96d6 1074
62ad3cb3 1075 if (handler == NULL)
83df96d6 1076 {
62ad3cb3
MB
1077 wxImage image;
1078 if (!image.LoadFile( name, type ))
55034339 1079 return false;
83df96d6 1080
a1b806b9 1081 if (image.IsOk())
a11672a4 1082 {
62ad3cb3 1083 *this = wxBitmap(image);
55034339 1084 return true;
a11672a4 1085 }
55034339 1086 else return false;
83df96d6 1087 }
83df96d6 1088
62ad3cb3
MB
1089 return handler->LoadFile(this, name, type, -1, -1);
1090}
1091
1092void wxBitmap::SetPalette(const wxPalette& palette)
1093{
a1b806b9 1094 wxCHECK_RET(IsOk(), wxT("invalid bitmap"));
62ad3cb3
MB
1095 wxCHECK_RET(GetDepth() > 1 && GetDepth() <= 8,
1096 wxT("cannot set palette for bitmap of this depth"));
1097
55ccdb93 1098 AllocExclusive();
5276b0a5 1099 wxDELETE(M_BMPDATA->m_palette);
62ad3cb3 1100
a1b806b9 1101 if (!palette.IsOk()) return;
0cad49b4 1102
62ad3cb3 1103 M_BMPDATA->m_palette = new wxPalette(palette);
83df96d6
JS
1104}
1105
a11672a4 1106wxPalette *wxBitmap::GetPalette() const
83df96d6 1107{
a1b806b9 1108 if (!IsOk()) return NULL;
83df96d6 1109
a11672a4
RR
1110 return M_BMPDATA->m_palette;
1111}
83df96d6 1112
a11672a4
RR
1113void wxBitmap::SetHeight( int height )
1114{
55ccdb93 1115 AllocExclusive();
83df96d6 1116
a11672a4
RR
1117 M_BMPDATA->m_height = height;
1118}
83df96d6 1119
a11672a4
RR
1120void wxBitmap::SetWidth( int width )
1121{
55ccdb93 1122 AllocExclusive();
83df96d6 1123
a11672a4
RR
1124 M_BMPDATA->m_width = width;
1125}
83df96d6 1126
a11672a4
RR
1127void wxBitmap::SetDepth( int depth )
1128{
55ccdb93 1129 AllocExclusive();
83df96d6 1130
a11672a4
RR
1131 M_BMPDATA->m_bpp = depth;
1132}
83df96d6 1133
a11672a4
RR
1134void wxBitmap::SetPixmap( WXPixmap pixmap )
1135{
1136 if (!m_refData) m_refData = new wxBitmapRefData();
83df96d6 1137
dd21c826 1138 M_BMPDATA->m_pixmap = (Pixmap)pixmap;
a11672a4 1139}
83df96d6 1140
a11672a4
RR
1141void wxBitmap::SetBitmap( WXPixmap bitmap )
1142{
1143 if (!m_refData) m_refData = new wxBitmapRefData();
83df96d6 1144
dd21c826 1145 M_BMPDATA->m_bitmap = (Pixmap)bitmap;
a11672a4 1146}
83df96d6 1147
a11672a4
RR
1148WXPixmap wxBitmap::GetPixmap() const
1149{
a1b806b9 1150 wxCHECK_MSG( IsOk(), (WXPixmap) NULL, wxT("invalid bitmap") );
83df96d6 1151
dd21c826 1152 return (WXPixmap)M_BMPDATA->m_pixmap;
a11672a4 1153}
83df96d6 1154
a11672a4
RR
1155WXPixmap wxBitmap::GetBitmap() const
1156{
a1b806b9 1157 wxCHECK_MSG( IsOk(), (WXPixmap) NULL, wxT("invalid bitmap") );
83df96d6 1158
dd21c826 1159 return (WXPixmap)M_BMPDATA->m_bitmap;
83df96d6 1160}
7266b672 1161
a29c6824
MB
1162WXPixmap wxBitmap::GetDrawable() const
1163{
a1b806b9 1164 wxCHECK_MSG( IsOk(), (WXPixmap) NULL, wxT("invalid bitmap") );
a29c6824 1165
dd21c826
VZ
1166 return (WXPixmap)(M_BMPDATA->m_bpp == 1 ? M_BMPDATA->m_bitmap
1167 : M_BMPDATA->m_pixmap);
a29c6824
MB
1168}
1169
a11672a4 1170WXDisplay *wxBitmap::GetDisplay() const
7266b672 1171{
a1b806b9 1172 wxCHECK_MSG( IsOk(), NULL, wxT("invalid bitmap") );
b6ed4565 1173
a11672a4 1174 return M_BMPDATA->m_display;
7266b672 1175}
a11672a4 1176
a20c04ab
JS
1177#if wxUSE_NANOX
1178// Copy from the drawable to the wxImage
1179bool wxGetImageFromDrawable(GR_DRAW_ID drawable, int srcX, int srcY, int width, int height, wxImage& image)
1180{
f22033e5 1181 GR_SCREEN_INFO sinfo;
a20c04ab
JS
1182 int x, y;
1183 GR_PIXELVAL *pixels;
a20c04ab 1184 GR_PALETTE* palette = NULL;
f22033e5 1185 unsigned char rgb[3], *pp;
a20c04ab 1186
f22033e5 1187 GrGetScreenInfo(&sinfo);
a20c04ab 1188
f22033e5 1189 if (sinfo.pixtype == MWPF_PALETTE) {
0cad49b4 1190 if(!(palette = (GR_PALETTE*) malloc(sizeof(GR_PALETTE)))) {
55034339 1191 return false;
0cad49b4
VZ
1192 }
1193 GrGetSystemPalette(palette);
1194 }
a20c04ab 1195
f22033e5 1196 if(!(pixels = (GR_PIXELVAL*) malloc(sizeof(GR_PIXELVAL) * width * height)))
a20c04ab 1197 {
55034339 1198 return false;
a20c04ab
JS
1199 }
1200
f22033e5 1201 image.Create(width, height);
a20c04ab 1202
0cad49b4
VZ
1203 GrReadArea(drawable, srcX, srcY, width, height,
1204 pixels);
1205
1206
1207 for(x = 0; x < sinfo.cols; x++) {
1208
1209 pp = (unsigned char *)pixels +
1210 ((x + (y * sinfo.cols)) *
1211 sizeof(GR_PIXELVAL));
1212
1213 switch(sinfo.pixtype) {
1214 /* FIXME: These may need modifying on big endian. */
1215 case MWPF_TRUECOLOR0888:
1216 case MWPF_TRUECOLOR888:
1217 rgb[0] = pp[2];
1218 rgb[1] = pp[1];
1219 rgb[2] = pp[0];
1220 break;
1221 case MWPF_PALETTE:
1222 rgb[0] = palette->palette[pp[0]].r;
1223 rgb[1] = palette->palette[pp[0]].g;
1224 rgb[2] = palette->palette[pp[0]].b;
1225 break;
1226 case MWPF_TRUECOLOR565:
1227 rgb[0] = pp[1] & 0xf8;
1228 rgb[1] = ((pp[1] & 0x07) << 5) |
1229 ((pp[0] & 0xe0) >> 3);
1230 rgb[2] = (pp[0] & 0x1f) << 3;
1231 break;
1232 case MWPF_TRUECOLOR555:
1233 rgb[0] = (pp[1] & 0x7c) << 1;
1234 rgb[1] = ((pp[1] & 0x03) << 6) |
1235 ((pp[0] & 0xe0) >> 2);
1236 rgb[2] = (pp[0] & 0x1f) << 3;
1237 break;
1238 case MWPF_TRUECOLOR332:
1239 rgb[0] = pp[0] & 0xe0;
1240 rgb[1] = (pp[0] & 0x1c) << 3;
1241 rgb[2] = (pp[0] & 0x03) << 6;
1242 break;
1243 default:
1244 fprintf(stderr, "Unsupported pixel "
1245 "format\n");
1246 return 1;
1247 }
1248
1249 image.SetRGB(x, y, rgb[0], rgb[1], rgb[2]);
1250
1251 }
a20c04ab
JS
1252
1253 free(pixels);
f22033e5 1254 if(palette) free(palette);
a20c04ab 1255
55034339 1256 return true;
a20c04ab
JS
1257}
1258
1259#if 0
1260int GrGetPixelColor(GR_SCREEN_INFO* sinfo, GR_PALETTE* palette, GR_PIXELVAL pixel,
1261 unsigned char* red, unsigned char* green, unsigned char* blue)
1262{
0cad49b4 1263 unsigned char rgb[3], *pp;
a20c04ab
JS
1264
1265 pp = (unsigned char*) & pixel ;
1266
1267 switch (sinfo.pixtype)
1268 {
0cad49b4
VZ
1269 /* FIXME: These may need modifying on big endian. */
1270 case MWPF_TRUECOLOR0888:
1271 case MWPF_TRUECOLOR888:
1272 rgb[0] = pp[2];
1273 rgb[1] = pp[1];
1274 rgb[2] = pp[0];
1275 break;
1276 case MWPF_PALETTE:
1277 rgb[0] = palette->palette[pp[0]].r;
1278 rgb[1] = palette->palette[pp[0]].g;
1279 rgb[2] = palette->palette[pp[0]].b;
1280 break;
1281 case MWPF_TRUECOLOR565:
1282 rgb[0] = pp[1] & 0xf8;
1283 rgb[1] = ((pp[1] & 0x07) << 5) |
1284 ((pp[0] & 0xe0) >> 3);
1285 rgb[2] = (pp[0] & 0x1f) << 3;
1286 break;
1287 case MWPF_TRUECOLOR555:
1288 rgb[0] = (pp[1] & 0x7c) << 1;
1289 rgb[1] = ((pp[1] & 0x03) << 6) |
1290 ((pp[0] & 0xe0) >> 2);
1291 rgb[2] = (pp[0] & 0x1f) << 3;
1292 break;
1293 case MWPF_TRUECOLOR332:
1294 rgb[0] = pp[0] & 0xe0;
1295 rgb[1] = (pp[0] & 0x1c) << 3;
1296 rgb[2] = (pp[0] & 0x03) << 6;
1297 break;
1298 default:
1299 fprintf(stderr, "Unsupported pixel format\n");
1300 return 0;
1301 }
a20c04ab
JS
1302
1303
1304 *(red) = rgb[0];
1305 *(green) = rgb[1];
1306 *(blue) = rgb[2];
1307 return 1;
1308}
1309#endif
c978d361 1310
a20c04ab
JS
1311#endif
1312 // wxUSE_NANOX
1313
62ad3cb3
MB
1314// ============================================================================
1315// Bitmap handlers
1316// ============================================================================
1317
62ad3cb3
MB
1318#define M_BMPHANDLERDATA ((wxBitmapRefData *)bitmap->GetRefData())
1319
1320#if wxUSE_XPM
1321
1322#if wxHAVE_LIB_XPM || wxUSE_STREAMS
1323
1324// ----------------------------------------------------------------------------
1325// wxXPMFileHandler
1326// ----------------------------------------------------------------------------
1327
1328class wxXPMFileHandler : public wxBitmapHandler
1329{
62ad3cb3
MB
1330public:
1331 wxXPMFileHandler()
1332 {
2fd1cf7d
MB
1333 SetName( wxT("XPM file") );
1334 SetExtension( wxT("xpm") );
1335 SetType( wxBITMAP_TYPE_XPM );
62ad3cb3
MB
1336 };
1337
545cb3fc
VZ
1338 virtual bool LoadFile(wxBitmap *bitmap, const wxString& name,
1339 wxBitmapType flags,
62ad3cb3
MB
1340 int desiredWidth, int desiredHeight);
1341
1342 virtual bool SaveFile(const wxBitmap *bitmap, const wxString& name,
545cb3fc
VZ
1343 wxBitmapType type,
1344 const wxPalette *palette = NULL) const;
1345
1346 virtual bool Create(wxBitmap *WXUNUSED(bitmap),
1347 const void* WXUNUSED(data),
1348 wxBitmapType WXUNUSED(flags),
1349 int WXUNUSED(width),
1350 int WXUNUSED(height),
1351 int WXUNUSED(depth) = 1)
55034339 1352 { return false; }
545cb3fc
VZ
1353
1354 DECLARE_DYNAMIC_CLASS(wxXPMFileHandler)
62ad3cb3
MB
1355};
1356
e933b5bc 1357IMPLEMENT_DYNAMIC_CLASS(wxXPMFileHandler, wxBitmapHandler)
62ad3cb3 1358
545cb3fc
VZ
1359bool wxXPMFileHandler::LoadFile(wxBitmap *bitmap,
1360 const wxString& name,
1361 wxBitmapType WXUNUSED(flags),
1362 int WXUNUSED(desiredWidth),
55034339 1363 int WXUNUSED(desiredHeight))
62ad3cb3
MB
1364{
1365#if wxHAVE_LIB_XPM
1366 if (!bitmap->GetRefData())
1367 bitmap->SetRefData( new wxBitmapRefData() );
1368
1369 M_BMPHANDLERDATA->m_display = wxGlobalDisplay();
0cad49b4 1370
62ad3cb3 1371 Display *xdisplay = (Display*) M_BMPHANDLERDATA->m_display;
0cad49b4 1372
62ad3cb3
MB
1373 int xscreen = DefaultScreen( xdisplay );
1374 Window xroot = RootWindow( xdisplay, xscreen );
0cad49b4 1375
62ad3cb3
MB
1376 int bpp = DefaultDepth( xdisplay, xscreen );
1377
1378 XpmAttributes xpmAttr;
1379 xpmAttr.valuemask = XpmReturnInfos; // nothing yet, but get infos back
1380
1381 Pixmap pixmap;
1382 Pixmap mask = 0;
0cad49b4 1383
62ad3cb3 1384 int ErrorStatus = XpmReadFileToPixmap( xdisplay, xroot,
0f0223d3 1385 (char*) ((const char*) name.c_str()),
62ad3cb3 1386 &pixmap, &mask, &xpmAttr);
0cad49b4 1387
62ad3cb3
MB
1388 if (ErrorStatus == XpmSuccess)
1389 {
1390 M_BMPHANDLERDATA->m_width = xpmAttr.width;
1391 M_BMPHANDLERDATA->m_height = xpmAttr.height;
1392
1393 M_BMPHANDLERDATA->m_bpp = bpp; // mono as well?
1394
1395 XpmFreeAttributes(&xpmAttr);
1396
b29aaf4a 1397 M_BMPHANDLERDATA->m_bitmap = (Pixmap) pixmap;
0cad49b4 1398
62ad3cb3
MB
1399 if (mask)
1400 {
1401 M_BMPHANDLERDATA->m_mask = new wxMask;
1402 M_BMPHANDLERDATA->m_mask->SetBitmap( (WXPixmap) mask );
1403 M_BMPHANDLERDATA->m_mask->SetDisplay( xdisplay );
1404 }
1405 }
1406 else
1407 {
1408 UnRef();
0cad49b4 1409
55034339 1410 return false;
62ad3cb3
MB
1411 }
1412
55034339 1413 return true;
62ad3cb3
MB
1414#elif wxUSE_STREAMS
1415 wxXPMDecoder decoder;
1416 wxFileInputStream stream(name);
9690b006 1417 if (stream.IsOk())
62ad3cb3
MB
1418 {
1419 wxImage image(decoder.ReadFile(stream));
9690b006 1420 return image.IsOk() && bitmap->CreateFromImage(image);
62ad3cb3 1421 }
6cee3292 1422
55034339 1423 return false;
0cad49b4 1424#else // !wxHAVE_LIB_XPM && !wxUSE_STREAMS
55034339 1425 return false;
62ad3cb3
MB
1426#endif // wxHAVE_LIB_XPM / wxUSE_STREAMS
1427}
1428
545cb3fc
VZ
1429bool wxXPMFileHandler::SaveFile(const wxBitmap *bitmap,
1430 const wxString& name,
1431 wxBitmapType type,
1432 const wxPalette *WXUNUSED(palette)) const
62ad3cb3
MB
1433{
1434 wxImage image(bitmap->ConvertToImage());
a1b806b9 1435 if (image.IsOk())
545cb3fc 1436 return image.SaveFile( name, type );
62ad3cb3 1437
55034339 1438 return false;
62ad3cb3
MB
1439}
1440
1441#endif // wxHAVE_LIB_XPM || wxUSE_STREAMS
1442
1443// ----------------------------------------------------------------------------
1444// wxXPMDataHandler
1445// ----------------------------------------------------------------------------
1446
1447class wxXPMDataHandler : public wxBitmapHandler
1448{
bc21780e 1449 DECLARE_DYNAMIC_CLASS(wxXPMDataHandler)
62ad3cb3
MB
1450public:
1451 wxXPMDataHandler()
1452 {
2fd1cf7d
MB
1453 SetName( wxT("XPM data") );
1454 SetExtension( wxT("xpm") );
1455 SetType( wxBITMAP_TYPE_XPM_DATA );
62ad3cb3
MB
1456 };
1457
55034339
WS
1458 virtual bool LoadFile(wxBitmap *WXUNUSED(bitmap),
1459 const wxString& WXUNUSED(name),
85d98dfe 1460 wxBitmapType WXUNUSED(flags),
55034339
WS
1461 int WXUNUSED(desiredWidth),
1462 int WXUNUSED(desiredHeight))
1463 { return false; }
62ad3cb3 1464
55034339
WS
1465 virtual bool SaveFile(const wxBitmap *WXUNUSED(bitmap),
1466 const wxString& WXUNUSED(name),
85d98dfe 1467 wxBitmapType WXUNUSED(type),
545cb3fc 1468 const wxPalette *WXUNUSED(palette) = NULL) const
55034339 1469 { return false; }
62ad3cb3 1470
85d98dfe 1471 virtual bool Create(wxBitmap *bitmap, const void* data, wxBitmapType flags,
62ad3cb3
MB
1472 int width, int height, int depth = 1);
1473};
1474
e933b5bc 1475IMPLEMENT_DYNAMIC_CLASS(wxXPMDataHandler, wxBitmapHandler)
62ad3cb3 1476
452418c4 1477bool wxXPMDataHandler::Create(wxBitmap *bitmap, const void* bits,
85d98dfe 1478 wxBitmapType WXUNUSED(flags),
55034339 1479 int WXUNUSED(width), int WXUNUSED(height), int WXUNUSED(depth))
62ad3cb3
MB
1480{
1481#if wxHAVE_LIB_XPM
637b7e4f 1482 wxCHECK_MSG( bits != NULL, false, wxT("invalid bitmap data") );
62ad3cb3
MB
1483
1484 if (!bitmap->GetRefData())
1485 bitmap->SetRefData( new wxBitmapRefData() );
1486
1487 M_BMPHANDLERDATA->m_display = wxGlobalDisplay();
0cad49b4 1488
62ad3cb3 1489 Display *xdisplay = (Display*) M_BMPHANDLERDATA->m_display;
0cad49b4 1490
62ad3cb3
MB
1491 int xscreen = DefaultScreen( xdisplay );
1492 Window xroot = RootWindow( xdisplay, xscreen );
0cad49b4 1493
62ad3cb3
MB
1494 int bpp = DefaultDepth( xdisplay, xscreen );
1495
1496 XpmAttributes xpmAttr;
1497 xpmAttr.valuemask = XpmReturnInfos; // nothing yet, but get infos back
1498
1499 Pixmap pixmap = 0;
1500 Pixmap mask = 0;
0cad49b4 1501
62ad3cb3
MB
1502 int ErrorStatus = XpmCreatePixmapFromData( xdisplay, xroot, (char**) bits,
1503 &pixmap, &mask, &xpmAttr );
0cad49b4 1504
62ad3cb3
MB
1505 if (ErrorStatus == XpmSuccess)
1506 {
1507 M_BMPHANDLERDATA->m_width = xpmAttr.width;
1508 M_BMPHANDLERDATA->m_height = xpmAttr.height;
1509
1510 M_BMPHANDLERDATA->m_bpp = bpp; // mono as well?
1511
4b6a582b 1512#if wxDEBUG_LEVEL
62ad3cb3
MB
1513 unsigned int depthRet;
1514 int xRet, yRet;
1515 unsigned int widthRet, heightRet, borderWidthRet;
1516 XGetGeometry( xdisplay, pixmap, &xroot, &xRet, &yRet,
1517 &widthRet, &heightRet, &borderWidthRet, &depthRet);
1518
1519 wxASSERT_MSG( bpp == (int)depthRet, wxT("colour depth mismatch") );
4b6a582b 1520#endif // wxDEBUG_LEVEL
62ad3cb3
MB
1521
1522 XpmFreeAttributes(&xpmAttr);
0cad49b4 1523
b29aaf4a 1524 M_BMPHANDLERDATA->m_pixmap = (Pixmap) pixmap;
0cad49b4 1525
62ad3cb3
MB
1526 if (mask)
1527 {
1528 M_BMPHANDLERDATA->m_mask = new wxMask;
1529 M_BMPHANDLERDATA->m_mask->SetBitmap( (WXPixmap) mask );
1530 M_BMPHANDLERDATA->m_mask->SetDisplay( xdisplay );
1531 }
55034339 1532 return true;
62ad3cb3
MB
1533 }
1534 else
1535 {
1536 bitmap->UnRef();
0cad49b4 1537
55034339 1538 return false;
62ad3cb3 1539 }
0cad49b4 1540#else // !wxHAVE_LIB_XPM
62ad3cb3 1541 wxXPMDecoder decoder;
0cad49b4 1542 wxImage image(decoder.ReadData((const char **)bits));
a1b806b9 1543 return image.IsOk() && bitmap->CreateFromImage(image);
0cad49b4 1544#endif // wxHAVE_LIB_XPM/!wxHAVE_LIB_XPM
62ad3cb3
MB
1545}
1546
1547#endif // wxUSE_XPM
1548
1549// ----------------------------------------------------------------------------
1550// wxXBMDataHandler
1551// ----------------------------------------------------------------------------
1552
1553class WXDLLEXPORT wxXBMDataHandler: public wxBitmapHandler
1554{
1555 DECLARE_DYNAMIC_CLASS(wxXBMDataHandler)
1556public:
1557 inline wxXBMDataHandler()
1558 {
2fd1cf7d
MB
1559 SetName( wxT("XBM data") );
1560 SetExtension( wxT("xbm") );
1561 SetType( wxBITMAP_TYPE_XBM_DATA );
62ad3cb3
MB
1562 };
1563
55034339
WS
1564 virtual bool LoadFile(wxBitmap *WXUNUSED(bitmap),
1565 const wxString& WXUNUSED(name),
545cb3fc 1566 wxBitmapType WXUNUSED(flags),
55034339
WS
1567 int WXUNUSED(desiredWidth),
1568 int WXUNUSED(desiredHeight))
1569 { return false; }
62ad3cb3 1570
55034339
WS
1571 virtual bool SaveFile(const wxBitmap *WXUNUSED(bitmap),
1572 const wxString& WXUNUSED(name),
545cb3fc
VZ
1573 wxBitmapType WXUNUSED(type),
1574 const wxPalette *WXUNUSED(palette) = NULL) const
55034339 1575 { return false; }
62ad3cb3 1576
545cb3fc 1577 virtual bool Create(wxBitmap *bitmap, const void* data, wxBitmapType type,
62ad3cb3
MB
1578 int width, int height, int depth = 1);
1579};
1580
e933b5bc 1581IMPLEMENT_DYNAMIC_CLASS(wxXBMDataHandler, wxBitmapHandler)
62ad3cb3 1582
452418c4 1583bool wxXBMDataHandler::Create( wxBitmap *bitmap, const void* bits,
545cb3fc 1584 wxBitmapType WXUNUSED(type),
7fc65a03 1585 int width, int height, int WXUNUSED(depth))
62ad3cb3
MB
1586{
1587#if !wxUSE_NANOX
1588 if (!bitmap->GetRefData())
1589 bitmap->SetRefData( new wxBitmapRefData() );
1590
1591 M_BMPHANDLERDATA->m_display = wxGlobalDisplay();
1592
1593 Display *xdisplay = (Display*) M_BMPHANDLERDATA->m_display;
0cad49b4 1594
62ad3cb3
MB
1595 int xscreen = DefaultScreen( xdisplay );
1596 Window xroot = RootWindow( xdisplay, xscreen );
0cad49b4 1597
d3b9f782 1598 M_BMPHANDLERDATA->m_mask = NULL;
62ad3cb3 1599 M_BMPHANDLERDATA->m_bitmap =
dd21c826
VZ
1600 XCreateBitmapFromData(xdisplay, xroot,
1601 (char *) bits, width, height );
62ad3cb3
MB
1602 M_BMPHANDLERDATA->m_width = width;
1603 M_BMPHANDLERDATA->m_height = height;
1604 M_BMPHANDLERDATA->m_bpp = 1;
1605
55034339 1606 return true;
d171743e 1607#else
55034339 1608 wxCHECK_MSG( M_BMPHANDLERDATA->m_bitmap, false,
62ad3cb3 1609 wxT("couldn't create bitmap") );
d171743e 1610#endif
62ad3cb3
MB
1611}
1612
1613void wxBitmap::InitStandardHandlers()
1614{
1615 AddHandler(new wxXBMDataHandler);
1616#if wxUSE_XPM
1617#if wxHAVE_LIB_XPM || wxUSE_STREAMS
1618 AddHandler(new wxXPMFileHandler);
1619#endif
1620 AddHandler(new wxXPMDataHandler);
1621#endif
1622}