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