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