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