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