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