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