]> git.saurik.com Git - wxWidgets.git/blame - src/x11/bitmap.cpp
compilation fix
[wxWidgets.git] / src / x11 / bitmap.cpp
CommitLineData
83df96d6
JS
1/////////////////////////////////////////////////////////////////////////////
2// Name: bitmap.cpp
3// Purpose: wxBitmap
a11672a4 4// Author: Julian Smart, Robert Roebling
83df96d6
JS
5// Modified by:
6// Created: 17/09/98
7// RCS-ID: $Id$
a11672a4 8// Copyright: (c) Julian Smart, Robert Roebling
83df96d6
JS
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12#ifdef __GNUG__
13#pragma implementation "bitmap.h"
14#endif
15
83df96d6
JS
16#include "wx/bitmap.h"
17#include "wx/icon.h"
18#include "wx/log.h"
83df96d6
JS
19#include "wx/image.h"
20#include "wx/app.h"
21
bc797f4c 22#include "wx/x11/private.h"
83df96d6 23
707440dc 24#if wxUSE_XPM
83df96d6 25#if wxHAVE_LIB_XPM
707440dc
JS
26#include <X11/xpm.h>
27#else
28#include "wx/xpmdecod.h"
29#include "wx/wfstream.h"
30#endif
83df96d6
JS
31#endif
32#include <math.h>
33
a11672a4
RR
34//-----------------------------------------------------------------------------
35// wxMask
36//-----------------------------------------------------------------------------
83df96d6 37
a11672a4 38IMPLEMENT_DYNAMIC_CLASS(wxMask,wxObject)
83df96d6 39
a11672a4 40wxMask::wxMask()
83df96d6 41{
a11672a4
RR
42 m_bitmap = NULL;
43 m_display = NULL;
83df96d6
JS
44}
45
a11672a4 46wxMask::wxMask( const wxBitmap& bitmap, const wxColour& colour )
83df96d6 47{
a11672a4
RR
48 m_bitmap = NULL;
49 Create( bitmap, colour );
83df96d6
JS
50}
51
a11672a4 52wxMask::wxMask( const wxBitmap& bitmap, int paletteIndex )
83df96d6 53{
a11672a4
RR
54 m_bitmap = NULL;
55 Create( bitmap, paletteIndex );
83df96d6
JS
56}
57
a11672a4 58wxMask::wxMask( const wxBitmap& bitmap )
83df96d6 59{
a11672a4
RR
60 m_bitmap = NULL;
61 Create( bitmap );
83df96d6
JS
62}
63
a11672a4 64wxMask::~wxMask()
83df96d6 65{
a11672a4
RR
66 if (m_bitmap)
67 XFreePixmap( (Display*) m_display, (Pixmap) m_bitmap );
83df96d6
JS
68}
69
a11672a4
RR
70bool wxMask::Create( const wxBitmap& bitmap,
71 const wxColour& colour )
83df96d6 72{
a11672a4
RR
73 if (m_bitmap)
74 {
75 XFreePixmap( (Display*) m_display, (Pixmap) m_bitmap );
76 m_bitmap = NULL;
77 }
83df96d6 78
a11672a4 79 m_display = bitmap.GetDisplay();
83df96d6 80
a11672a4
RR
81 wxImage image( bitmap );
82 if (!image.Ok()) return FALSE;
83
84 m_display = bitmap.GetDisplay();
85
86 Display *xdisplay = (Display*) m_display;
83df96d6 87
a11672a4
RR
88 int xscreen = DefaultScreen( xdisplay );
89 Window xroot = RootWindow( xdisplay, xscreen );
90 Visual* xvisual = DefaultVisual( xdisplay, xscreen );
91 int bpp = DefaultDepth( xdisplay, xscreen );
92
93 m_bitmap = (WXPixmap) XCreatePixmap( xdisplay, xroot, image.GetWidth(), image.GetHeight(), 1 );
94 GC gc = XCreateGC( xdisplay, (Pixmap) m_bitmap, 0, NULL );
83df96d6 95
a11672a4
RR
96 XSetForeground( xdisplay, gc, WhitePixel(xdisplay,xscreen) );
97 XSetFillStyle( xdisplay, gc, FillSolid );
98 XFillRectangle( xdisplay, (Pixmap) m_bitmap, gc, 0, 0, image.GetWidth(), image.GetHeight() );
83df96d6 99
a11672a4
RR
100 unsigned char *data = image.GetData();
101 int index = 0;
83df96d6 102
a11672a4
RR
103 unsigned char red = colour.Red();
104 unsigned char green = colour.Green();
105 unsigned char blue = colour.Blue();
83df96d6 106
a11672a4
RR
107 XVisualInfo vinfo_template;
108 XVisualInfo *vi;
83df96d6 109
a11672a4
RR
110 vinfo_template.visual = xvisual;
111 vinfo_template.visualid = XVisualIDFromVisual( xvisual );
112 vinfo_template.depth = bpp;
113 int nitem = 0;
83df96d6 114
a11672a4
RR
115 vi = XGetVisualInfo( xdisplay, VisualIDMask|VisualDepthMask, &vinfo_template, &nitem );
116 wxASSERT_MSG( vi, wxT("No visual info") );
117
118 if ((bpp == 16) && (vi->red_mask != 0xf800)) bpp = 15;
119 if (bpp == 15)
120 {
121 red = red & 0xf8;
122 green = green & 0xf8;
123 blue = blue & 0xf8;
124 } else
125 if (bpp == 16)
126 {
127 red = red & 0xf8;
128 green = green & 0xfc;
129 blue = blue & 0xf8;
130 } else
131 if (bpp == 12)
132 {
133 red = red & 0xf0;
134 green = green & 0xf0;
135 blue = blue & 0xf0;
136 }
83df96d6 137
a11672a4 138 XSetForeground( xdisplay, gc, BlackPixel(xdisplay,xscreen) );
83df96d6 139
a11672a4
RR
140 for (int j = 0; j < image.GetHeight(); j++)
141 {
142 int start_x = -1;
143 int i;
144 for (i = 0; i < image.GetWidth(); i++)
83df96d6 145 {
a11672a4
RR
146 if ((data[index] == red) &&
147 (data[index+1] == green) &&
148 (data[index+2] == blue))
149 {
150 if (start_x == -1)
151 start_x = i;
152 }
153 else
154 {
155 if (start_x != -1)
156 {
157 XDrawLine( xdisplay, (Pixmap) m_bitmap, gc, start_x, j, i-1, j );
158 start_x = -1;
159 }
160 }
161 index += 3;
83df96d6 162 }
a11672a4
RR
163 if (start_x != -1)
164 XDrawLine( xdisplay, (Pixmap) m_bitmap, gc, start_x, j, i, j );
83df96d6 165 }
a11672a4
RR
166
167 XFreeGC( xdisplay, gc );
83df96d6 168
a11672a4 169 return TRUE;
83df96d6
JS
170}
171
a11672a4 172bool wxMask::Create( const wxBitmap& bitmap, int paletteIndex )
83df96d6 173{
a11672a4
RR
174 unsigned char r,g,b;
175 wxPalette *pal = bitmap.GetPalette();
83df96d6 176
a11672a4 177 wxCHECK_MSG( pal, FALSE, wxT("Cannot create mask from bitmap without palette") );
83df96d6 178
a11672a4 179 pal->GetRGB(paletteIndex, &r, &g, &b);
83df96d6 180
a11672a4 181 return Create(bitmap, wxColour(r, g, b));
83df96d6
JS
182}
183
a11672a4 184bool wxMask::Create( const wxBitmap& bitmap )
83df96d6 185{
a11672a4
RR
186 if (m_bitmap)
187 {
188 XFreePixmap( (Display*) m_display, (Pixmap) m_bitmap );
189 m_bitmap = NULL;
83df96d6
JS
190 }
191
a11672a4 192 if (!bitmap.Ok()) return FALSE;
83df96d6 193
a11672a4
RR
194 wxCHECK_MSG( bitmap.GetBitmap(), FALSE, wxT("Cannot create mask from colour bitmap") );
195
196 m_display = bitmap.GetDisplay();
83df96d6 197
a11672a4
RR
198 int xscreen = DefaultScreen( (Display*) m_display );
199 Window xroot = RootWindow( (Display*) m_display, xscreen );
200
201 m_bitmap = (WXPixmap) XCreatePixmap( (Display*) m_display, xroot, bitmap.GetWidth(), bitmap.GetHeight(), 1 );
83df96d6 202
a11672a4
RR
203 if (!m_bitmap) return FALSE;
204
205 GC gc = XCreateGC( (Display*) m_display, (Pixmap) m_bitmap, 0, NULL );
83df96d6 206
a11672a4
RR
207 XCopyPlane( (Display*) m_display, (Pixmap) bitmap.GetBitmap(), (Pixmap) m_bitmap,
208 gc, 0, 0, bitmap.GetWidth(), bitmap.GetHeight(), 0, 0, 1 );
209
210 XFreeGC( (Display*) m_display, gc );
83df96d6 211
a11672a4 212 return TRUE;
83df96d6
JS
213}
214
a11672a4
RR
215//-----------------------------------------------------------------------------
216// wxBitmap
217//-----------------------------------------------------------------------------
83df96d6 218
a11672a4 219class wxBitmapRefData: public wxObjectRefData
83df96d6 220{
a11672a4
RR
221public:
222 wxBitmapRefData();
223 ~wxBitmapRefData();
224
225 WXPixmap m_pixmap;
226 WXPixmap m_bitmap;
227 WXDisplay *m_display;
228 wxMask *m_mask;
229 int m_width;
230 int m_height;
231 int m_bpp;
232 wxPalette *m_palette;
233};
83df96d6 234
a11672a4 235wxBitmapRefData::wxBitmapRefData()
83df96d6 236{
a11672a4
RR
237 m_pixmap = NULL;
238 m_bitmap = NULL;
239 m_display = NULL;
240 m_mask = (wxMask *) NULL;
241 m_width = 0;
242 m_height = 0;
243 m_bpp = 0;
244 m_palette = (wxPalette *) NULL;
83df96d6
JS
245}
246
a11672a4 247wxBitmapRefData::~wxBitmapRefData()
83df96d6 248{
a11672a4
RR
249 if (m_pixmap) XFreePixmap( (Display*) m_display, (Pixmap) m_pixmap );
250 if (m_bitmap) XFreePixmap( (Display*) m_display, (Pixmap) m_bitmap );
251 if (m_mask) delete m_mask;
252 if (m_palette) delete m_palette;
83df96d6
JS
253}
254
a11672a4 255//-----------------------------------------------------------------------------
83df96d6 256
a11672a4 257#define M_BMPDATA ((wxBitmapRefData *)m_refData)
83df96d6 258
a11672a4 259IMPLEMENT_DYNAMIC_CLASS(wxBitmap,wxGDIObject)
83df96d6 260
a11672a4 261wxBitmap::wxBitmap()
83df96d6 262{
83df96d6
JS
263}
264
a11672a4 265wxBitmap::wxBitmap( int width, int height, int depth )
83df96d6 266{
a11672a4 267 Create( width, height, depth );
83df96d6
JS
268}
269
a11672a4 270bool wxBitmap::Create( int width, int height, int depth )
83df96d6 271{
a11672a4
RR
272 UnRef();
273
274 wxCHECK_MSG( (width > 0) && (height > 0), FALSE, wxT("invalid bitmap size") )
275
276 m_refData = new wxBitmapRefData();
277
278 M_BMPDATA->m_display = wxGlobalDisplay();
279
280 wxASSERT_MSG( M_BMPDATA->m_display, wxT("No display") );
281
282 int xscreen = DefaultScreen( (Display*) M_BMPDATA->m_display );
283 Window xroot = RootWindow( (Display*) M_BMPDATA->m_display, xscreen );
284
285 int bpp = DefaultDepth( (Display*) M_BMPDATA->m_display, xscreen );
286 if (depth == -1) depth = bpp;
287
288 wxCHECK_MSG( (depth == bpp) ||
289 (depth == 1), FALSE, wxT("invalid bitmap depth") )
290
291 M_BMPDATA->m_mask = (wxMask *) NULL;
292 M_BMPDATA->m_width = width;
293 M_BMPDATA->m_height = height;
294 if (depth == 1)
83df96d6 295 {
a11672a4
RR
296 M_BMPDATA->m_bitmap = (WXPixmap) XCreatePixmap( (Display*) M_BMPDATA->m_display, xroot, width, height, 1 );
297
298 wxASSERT_MSG( M_BMPDATA->m_bitmap, wxT("Bitmap creation failed") );
299
300 M_BMPDATA->m_bpp = 1;
83df96d6
JS
301 }
302 else
83df96d6 303 {
a11672a4
RR
304 M_BMPDATA->m_pixmap = (WXPixmap) XCreatePixmap( (Display*) M_BMPDATA->m_display, xroot, width, height, depth );
305
306 wxASSERT_MSG( M_BMPDATA->m_pixmap, wxT("Pixmap creation failed") );
307
308 M_BMPDATA->m_bpp = depth;
83df96d6 309 }
83df96d6 310
a11672a4 311 return Ok();
83df96d6
JS
312}
313
a11672a4 314bool wxBitmap::CreateFromXpm( const char **bits )
83df96d6 315{
707440dc
JS
316#if wxUSE_XPM
317#if wxHAVE_LIB_XPM
a11672a4
RR
318 UnRef();
319
320 wxCHECK_MSG( bits != NULL, FALSE, wxT("invalid bitmap data") )
321
322 m_refData = new wxBitmapRefData();
323
324 M_BMPDATA->m_display = wxGlobalDisplay();
325
326 Display *xdisplay = (Display*) M_BMPDATA->m_display;
327
328 int xscreen = DefaultScreen( xdisplay );
329 Window xroot = RootWindow( xdisplay, xscreen );
330
331 int bpp = DefaultDepth( xdisplay, xscreen );
332
333 XpmAttributes xpmAttr;
334 xpmAttr.valuemask = XpmReturnInfos; // nothing yet, but get infos back
335
336 Pixmap pixmap;
337 Pixmap mask = 0;
338
339 int ErrorStatus = XpmCreatePixmapFromData( xdisplay, xroot, (char**) bits, &pixmap, &mask, &xpmAttr );
340
341 if (ErrorStatus == XpmSuccess)
83df96d6 342 {
a11672a4
RR
343 M_BMPDATA->m_width = xpmAttr.width;
344 M_BMPDATA->m_height = xpmAttr.height;
83df96d6 345
a11672a4 346 M_BMPDATA->m_bpp = bpp; // mono as well?
83df96d6 347
a11672a4
RR
348#if __WXDEBUG__
349 unsigned int depthRet;
350 int xRet, yRet;
351 unsigned int widthRet, heightRet, borderWidthRet;
352 XGetGeometry( xdisplay, pixmap, &xroot, &xRet, &yRet,
353 &widthRet, &heightRet, &borderWidthRet, &depthRet);
83df96d6 354
a11672a4
RR
355 wxASSERT_MSG( bpp == (int)depthRet, wxT("colour depth mismatch") )
356#endif
357
358 XpmFreeAttributes(&xpmAttr);
359
360 M_BMPDATA->m_pixmap = (WXPixmap) pixmap;
361
362 if (mask)
363 {
364 M_BMPDATA->m_mask = new wxMask;
365 M_BMPDATA->m_mask->SetBitmap( (WXPixmap) mask );
366 M_BMPDATA->m_mask->SetDisplay( xdisplay );
367 }
707440dc 368 return TRUE;
a11672a4
RR
369 }
370 else
371 {
372 UnRef();
373
374 return FALSE;
375 }
707440dc
JS
376#else
377 wxXPMDecoder decoder;
378 wxImage image(decoder.ReadData(bits));
379 if (image.Ok())
380 return CreateFromImage(image);
381 else
382 return FALSE;
383#endif
384#endif
385 return FALSE;
83df96d6
JS
386}
387
a11672a4 388bool wxBitmap::CreateFromImage( const wxImage& image, int depth )
83df96d6 389{
a11672a4 390 UnRef();
83df96d6 391
a11672a4
RR
392 wxCHECK_MSG( image.Ok(), FALSE, wxT("invalid image") )
393 wxCHECK_MSG( depth == -1, FALSE, wxT("invalid bitmap depth") )
83df96d6 394
a11672a4 395 m_refData = new wxBitmapRefData();
83df96d6 396
a11672a4
RR
397 M_BMPDATA->m_display = wxGlobalDisplay();
398
399 Display *xdisplay = (Display*) M_BMPDATA->m_display;
400
401 int xscreen = DefaultScreen( xdisplay );
402 Window xroot = RootWindow( xdisplay, xscreen );
403 Visual* xvisual = DefaultVisual( xdisplay, xscreen );
404
405 int bpp = DefaultDepth( xdisplay, xscreen );
406
407 int width = image.GetWidth();
408 int height = image.GetHeight();
409 M_BMPDATA->m_width = width;
410 M_BMPDATA->m_height = height;
83df96d6 411
a11672a4
RR
412 if (depth != 1) depth = bpp;
413 M_BMPDATA->m_bpp = depth;
414
415 if (depth == 1)
416 {
417 wxFAIL_MSG( "mono images later" );
418 }
419 else
420 {
421 // Create image
422
423 XImage *data_image = XCreateImage( xdisplay, xvisual, bpp, ZPixmap, 0, 0, width, height, 32, 0 );
424 data_image->data = (char*) malloc( data_image->bytes_per_line * data_image->height );
425
426 if (data_image->data == NULL)
427 {
428 wxLogError( wxT("Out of memory.") ); // TODO clean
429 return FALSE;
430 }
83df96d6 431
a11672a4 432 M_BMPDATA->m_pixmap = (WXPixmap) XCreatePixmap( xdisplay, xroot, width, height, depth );
83df96d6 433
a11672a4 434 // Create mask
83df96d6 435
a11672a4
RR
436 XImage *mask_image = (XImage*) NULL;
437 if (image.HasMask())
438 {
439 mask_image = XCreateImage( xdisplay, xvisual, 1, ZPixmap, 0, 0, width, height, 32, 0 );
440 mask_image->data = (char*) malloc( mask_image->bytes_per_line * mask_image->height );
441
442 if (mask_image->data == NULL)
443 {
444 wxLogError( wxT("Out of memory.") ); // TODO clean
445 return FALSE;
446 }
447
448 wxMask *mask = new wxMask();
449 mask->SetDisplay( xdisplay );
450 mask->SetBitmap( (WXPixmap) XCreatePixmap( xdisplay, xroot, width, height, 1 ) );
83df96d6 451
a11672a4
RR
452 SetMask( mask );
453 }
83df96d6 454
a11672a4 455 // Retrieve info
83df96d6 456
a11672a4
RR
457 XVisualInfo vinfo_template;
458 XVisualInfo *vi;
83df96d6 459
a11672a4
RR
460 vinfo_template.visual = xvisual;
461 vinfo_template.visualid = XVisualIDFromVisual( xvisual );
462 vinfo_template.depth = bpp;
463 int nitem = 0;
83df96d6 464
a11672a4
RR
465 vi = XGetVisualInfo( xdisplay, VisualIDMask|VisualDepthMask, &vinfo_template, &nitem );
466 wxASSERT_MSG( vi, wxT("No visual info") );
83df96d6 467
a11672a4
RR
468 if ((bpp == 16) && (vi->red_mask != 0xf800)) bpp = 15;
469 if (bpp < 8) bpp = 8;
83df96d6 470
a11672a4 471 // Render
83df96d6 472
a11672a4
RR
473 enum byte_order { RGB, RBG, BRG, BGR, GRB, GBR };
474 byte_order b_o = RGB;
83df96d6 475
a11672a4
RR
476 if (bpp > 8)
477 {
478 if ((vi->red_mask > vi->green_mask) && (vi->green_mask > vi->blue_mask)) b_o = RGB;
479 else if ((vi->red_mask > vi->blue_mask) && (vi->blue_mask > vi->green_mask)) b_o = RBG;
480 else if ((vi->blue_mask > vi->red_mask) && (vi->red_mask > vi->green_mask)) b_o = BRG;
481 else if ((vi->blue_mask > vi->green_mask) && (vi->green_mask > vi->red_mask)) b_o = BGR;
482 else if ((vi->green_mask > vi->red_mask) && (vi->red_mask > vi->blue_mask)) b_o = GRB;
483 else if ((vi->green_mask > vi->blue_mask) && (vi->blue_mask > vi->red_mask)) b_o = GBR;
484 }
83df96d6 485
a11672a4 486 XFree( vi );
83df96d6 487
a11672a4
RR
488 int r_mask = image.GetMaskRed();
489 int g_mask = image.GetMaskGreen();
490 int b_mask = image.GetMaskBlue();
83df96d6 491
a11672a4
RR
492 unsigned char* data = image.GetData();
493 wxASSERT_MSG( data, "No image data" );
83df96d6 494
a11672a4 495 bool hasMask = image.HasMask();
83df96d6 496
a11672a4
RR
497 int index = 0;
498 for (int y = 0; y < height; y++)
499 {
500 for (int x = 0; x < width; x++)
501 {
502 int r = data[index];
503 index++;
504 int g = data[index];
505 index++;
506 int b = data[index];
507 index++;
508
509 if (hasMask)
510 {
511 if ((r == r_mask) && (b == b_mask) && (g == g_mask))
512 XPutPixel( mask_image, x, y, 0 );
513 else
514 XPutPixel( mask_image, x, y, 1 );
515 }
83df96d6 516
a11672a4
RR
517 switch (bpp)
518 {
519 case 8:
520 {
521 int pixel = 0;
522#if 0
523 if (wxTheApp->m_colorCube)
524 {
525 pixel = wxTheApp->m_colorCube[ ((r & 0xf8) << 7) + ((g & 0xf8) << 2) + ((b & 0xf8) >> 3) ];
526 }
527 else
528 {
529 GdkColormap *cmap = gtk_widget_get_default_colormap();
530 GdkColor *colors = cmap->colors;
531 int max = 3 * (65536);
532
533 for (int i = 0; i < cmap->size; i++)
534 {
535 int rdiff = (r << 8) - colors[i].red;
536 int gdiff = (g << 8) - colors[i].green;
537 int bdiff = (b << 8) - colors[i].blue;
538 int sum = ABS (rdiff) + ABS (gdiff) + ABS (bdiff);
539 if (sum < max) { pixel = i; max = sum; }
540 }
541 }
542#endif
543 XPutPixel( data_image, x, y, pixel );
544 break;
545 }
546 case 12: // SGI only
547 {
548 int pixel = 0;
549 switch (b_o)
550 {
551 case RGB: pixel = ((r & 0xf0) << 4) | (g & 0xf0) | ((b & 0xf0) >> 4); break;
552 case RBG: pixel = ((r & 0xf0) << 4) | (b & 0xf0) | ((g & 0xf0) >> 4); break;
553 case GRB: pixel = ((g & 0xf0) << 4) | (r & 0xf0) | ((b & 0xf0) >> 4); break;
554 case GBR: pixel = ((g & 0xf0) << 4) | (b & 0xf0) | ((r & 0xf0) >> 4); break;
555 case BRG: pixel = ((b & 0xf0) << 4) | (r & 0xf0) | ((g & 0xf0) >> 4); break;
556 case BGR: pixel = ((b & 0xf0) << 4) | (g & 0xf0) | ((r & 0xf0) >> 4); break;
557 }
558 XPutPixel( data_image, x, y, pixel );
559 break;
560 }
561 case 15:
562 {
563 int pixel = 0;
564 switch (b_o)
565 {
566 case RGB: pixel = ((r & 0xf8) << 7) | ((g & 0xf8) << 2) | ((b & 0xf8) >> 3); break;
567 case RBG: pixel = ((r & 0xf8) << 7) | ((b & 0xf8) << 2) | ((g & 0xf8) >> 3); break;
568 case GRB: pixel = ((g & 0xf8) << 7) | ((r & 0xf8) << 2) | ((b & 0xf8) >> 3); break;
569 case GBR: pixel = ((g & 0xf8) << 7) | ((b & 0xf8) << 2) | ((r & 0xf8) >> 3); break;
570 case BRG: pixel = ((b & 0xf8) << 7) | ((r & 0xf8) << 2) | ((g & 0xf8) >> 3); break;
571 case BGR: pixel = ((b & 0xf8) << 7) | ((g & 0xf8) << 2) | ((r & 0xf8) >> 3); break;
572 }
573 XPutPixel( data_image, x, y, pixel );
574 break;
575 }
576 case 16:
577 {
578 // I actually don't know if for 16-bit displays, it is alway the green
579 // component or the second component which has 6 bits.
580 int pixel = 0;
581 switch (b_o)
582 {
583 case RGB: pixel = ((r & 0xf8) << 8) | ((g & 0xfc) << 3) | ((b & 0xf8) >> 3); break;
584 case RBG: pixel = ((r & 0xf8) << 8) | ((b & 0xfc) << 3) | ((g & 0xf8) >> 3); break;
585 case GRB: pixel = ((g & 0xf8) << 8) | ((r & 0xfc) << 3) | ((b & 0xf8) >> 3); break;
586 case GBR: pixel = ((g & 0xf8) << 8) | ((b & 0xfc) << 3) | ((r & 0xf8) >> 3); break;
587 case BRG: pixel = ((b & 0xf8) << 8) | ((r & 0xfc) << 3) | ((g & 0xf8) >> 3); break;
588 case BGR: pixel = ((b & 0xf8) << 8) | ((g & 0xfc) << 3) | ((r & 0xf8) >> 3); break;
589 }
590 XPutPixel( data_image, x, y, pixel );
591 break;
592 }
593 case 32:
594 case 24:
595 {
596 int pixel = 0;
597 switch (b_o)
598 {
599 case RGB: pixel = (r << 16) | (g << 8) | b; break;
600 case RBG: pixel = (r << 16) | (b << 8) | g; break;
601 case BRG: pixel = (b << 16) | (r << 8) | g; break;
602 case BGR: pixel = (b << 16) | (g << 8) | r; break;
603 case GRB: pixel = (g << 16) | (r << 8) | b; break;
604 case GBR: pixel = (g << 16) | (b << 8) | r; break;
605 }
606 XPutPixel( data_image, x, y, pixel );
607 }
608 default: break;
609 }
610 } // for
611 } // for
83df96d6 612
a11672a4 613 // Blit picture
83df96d6 614
a11672a4
RR
615 GC gc = XCreateGC( xdisplay, (Pixmap) M_BMPDATA->m_pixmap, 0, NULL );
616 XPutImage( xdisplay, (Pixmap) M_BMPDATA->m_pixmap, gc, data_image, 0, 0, 0, 0, width, height );
e334d0ea
JS
617#ifdef __WXDEBUG__
618 XSync(wxGlobalDisplay(), False);
619#endif
83df96d6 620
a11672a4
RR
621 XDestroyImage( data_image );
622 XFreeGC( xdisplay, gc );
83df96d6 623
a11672a4
RR
624 // Blit mask
625
626 if (image.HasMask())
627 {
628 GC gc = XCreateGC( xdisplay, (Pixmap) GetMask()->GetBitmap(), 0, NULL );
629 XPutImage( xdisplay, (Pixmap) GetMask()->GetBitmap(), gc, data_image, 0, 0, 0, 0, width, height );
630
631 XDestroyImage( mask_image );
632 XFreeGC( xdisplay, gc );
633 }
634 }
83df96d6 635
83df96d6
JS
636 return TRUE;
637}
638
a11672a4 639static void wxCalcPrecAndShift( unsigned long mask, int *shift, int *prec )
83df96d6 640{
a11672a4
RR
641 *shift = 0;
642 *prec = 0;
643
644 while (!(mask & 0x1))
83df96d6 645 {
a11672a4
RR
646 (*shift)++;
647 mask >>= 1;
83df96d6 648 }
83df96d6 649
a11672a4
RR
650 while (mask & 0x1)
651 {
652 (*prec)++;
653 mask >>= 1;
654 }
83df96d6
JS
655}
656
a11672a4 657wxImage wxBitmap::ConvertToImage() const
83df96d6 658{
a11672a4 659 wxImage image;
83df96d6 660
a11672a4 661 wxCHECK_MSG( Ok(), wxNullImage, wxT("invalid bitmap") );
83df96d6 662
a11672a4
RR
663 Display *xdisplay = (Display*) M_BMPDATA->m_display;
664 wxASSERT_MSG( xdisplay, wxT("No display") );
665
666 int xscreen = DefaultScreen( xdisplay );
667 Visual* xvisual = DefaultVisual( xdisplay, xscreen );
668
669 int bpp = DefaultDepth( xdisplay, xscreen );
670
671 XImage *x_image = NULL;
672 if (GetPixmap())
673 {
674 x_image = XGetImage( xdisplay, (Pixmap) GetPixmap(),
675 0, 0,
676 GetWidth(), GetHeight(),
677 AllPlanes, ZPixmap );
678 } else
679 if (GetBitmap())
680 {
681 x_image = XGetImage( xdisplay, (Pixmap) GetBitmap(),
682 0, 0,
683 GetWidth(), GetHeight(),
684 AllPlanes, ZPixmap );
685 } else
686 {
687 wxFAIL_MSG( wxT("Ill-formed bitmap") );
688 }
83df96d6 689
a11672a4 690 wxCHECK_MSG( x_image, wxNullImage, wxT("couldn't create image") );
83df96d6 691
a11672a4
RR
692 image.Create( GetWidth(), GetHeight() );
693 char unsigned *data = image.GetData();
83df96d6 694
a11672a4
RR
695 if (!data)
696 {
697 XDestroyImage( x_image );
698 wxFAIL_MSG( wxT("couldn't create image") );
699 return wxNullImage;
700 }
83df96d6 701
a11672a4
RR
702 XImage *x_image_mask = NULL;
703 if (GetMask())
704 {
705 x_image_mask = XGetImage( xdisplay, (Pixmap) GetMask()->GetBitmap(),
706 0, 0,
707 GetWidth(), GetHeight(),
708 AllPlanes, ZPixmap );
83df96d6 709
a11672a4
RR
710 image.SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
711 }
83df96d6 712
a11672a4
RR
713 int red_shift_right = 0;
714 int green_shift_right = 0;
715 int blue_shift_right = 0;
716 int red_shift_left = 0;
717 int green_shift_left = 0;
718 int blue_shift_left = 0;
719 bool use_shift = FALSE;
83df96d6 720
a11672a4
RR
721 if (GetPixmap())
722 {
723 // Retrieve info
724
725 XVisualInfo vinfo_template;
726 XVisualInfo *vi;
727
728 vinfo_template.visual = xvisual;
729 vinfo_template.visualid = XVisualIDFromVisual( xvisual );
730 vinfo_template.depth = bpp;
731 int nitem = 0;
732
733 vi = XGetVisualInfo( xdisplay, VisualIDMask|VisualDepthMask, &vinfo_template, &nitem );
734 wxASSERT_MSG( vi, wxT("No visual info") );
735
736 int red_prec,green_prec,blue_prec;
737 int red_shift,green_shift,blue_shift;
738 wxCalcPrecAndShift( vi->red_mask, &red_shift, &red_prec );
739 wxCalcPrecAndShift( vi->green_mask, &green_shift, &green_prec );
740 wxCalcPrecAndShift( vi->blue_mask, &blue_shift, &blue_prec );
741 if (bpp == 16) bpp = red_prec + green_prec + blue_prec;
742
743 red_shift_right = red_shift;
744 red_shift_left = 8-red_prec;
745 green_shift_right = green_shift;
746 green_shift_left = 8-green_prec;
747 blue_shift_right = blue_shift;
748 blue_shift_left = 8-blue_prec;
749
750#if 0
751 use_shift = (vi->visual->c_class == TrueColor) || (vi->visual->c_class == DirectColor);
752#else
753 use_shift = TRUE;
754#endif
755
756 XFree( vi );
757 }
758 if (GetBitmap())
759 {
760 bpp = 1;
761 }
83df96d6 762
83df96d6 763
a11672a4 764// GdkColormap *cmap = gtk_widget_get_default_colormap();
83df96d6 765
a11672a4
RR
766 long pos = 0;
767 for (int j = 0; j < GetHeight(); j++)
768 {
769 for (int i = 0; i < GetWidth(); i++)
770 {
771 unsigned long pixel = XGetPixel( x_image, i, j );
772 if (bpp == 1)
773 {
774 if (pixel == 0)
775 {
776 data[pos] = 0;
777 data[pos+1] = 0;
778 data[pos+2] = 0;
779 }
780 else
781 {
782 data[pos] = 255;
783 data[pos+1] = 255;
784 data[pos+2] = 255;
785 }
786 }
787 else if (use_shift)
788 {
789 data[pos] = (pixel >> red_shift_right) << red_shift_left;
790 data[pos+1] = (pixel >> green_shift_right) << green_shift_left;
791 data[pos+2] = (pixel >> blue_shift_right) << blue_shift_left;
792 }
793#if 0
794 else if (cmap->colors)
795 {
796 data[pos] = cmap->colors[pixel].red >> 8;
797 data[pos+1] = cmap->colors[pixel].green >> 8;
798 data[pos+2] = cmap->colors[pixel].blue >> 8;
799 }
800#endif
801 else
802 {
803 wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") );
804 }
83df96d6 805
a11672a4
RR
806 if (x_image_mask)
807 {
808 int mask_pixel = XGetPixel( x_image_mask, i, j );
809 if (mask_pixel == 0)
810 {
811 data[pos] = 16;
812 data[pos+1] = 16;
813 data[pos+2] = 16;
814 }
815 }
83df96d6 816
a11672a4
RR
817 pos += 3;
818 }
819 }
83df96d6 820
a11672a4
RR
821 XDestroyImage( x_image );
822 if (x_image_mask) XDestroyImage( x_image_mask );
83df96d6 823
a11672a4 824 return image;
83df96d6
JS
825}
826
a11672a4 827wxBitmap::wxBitmap( const wxBitmap& bmp )
83df96d6 828{
a11672a4 829 Ref( bmp );
83df96d6
JS
830}
831
a11672a4 832wxBitmap::wxBitmap( const wxString &filename, int type )
83df96d6 833{
a11672a4 834 LoadFile( filename, type );
83df96d6
JS
835}
836
a11672a4 837wxBitmap::wxBitmap( const char bits[], int width, int height, int WXUNUSED(depth) )
83df96d6 838{
a11672a4 839 m_refData = new wxBitmapRefData();
83df96d6 840
a11672a4 841 M_BMPDATA->m_display = wxGlobalDisplay();
83df96d6 842
a11672a4
RR
843 Display *xdisplay = (Display*) M_BMPDATA->m_display;
844
845 int xscreen = DefaultScreen( xdisplay );
846 Window xroot = RootWindow( xdisplay, xscreen );
847
848 M_BMPDATA->m_mask = (wxMask *) NULL;
849 M_BMPDATA->m_bitmap = (WXPixmap) XCreateBitmapFromData( xdisplay, xroot, (char *) bits, width, height );
850 M_BMPDATA->m_width = width;
851 M_BMPDATA->m_height = height;
852 M_BMPDATA->m_bpp = 1;
83df96d6 853
a11672a4 854 wxCHECK_RET( M_BMPDATA->m_bitmap, wxT("couldn't create bitmap") );
83df96d6
JS
855}
856
a11672a4 857wxBitmap::~wxBitmap()
83df96d6 858{
83df96d6
JS
859}
860
a11672a4 861wxBitmap& wxBitmap::operator = ( const wxBitmap& bmp )
83df96d6 862{
a11672a4
RR
863 if ( m_refData != bmp.m_refData )
864 Ref( bmp );
83df96d6 865
a11672a4
RR
866 return *this;
867}
83df96d6 868
a11672a4
RR
869bool wxBitmap::operator == ( const wxBitmap& bmp ) const
870{
871 return m_refData == bmp.m_refData;
872}
83df96d6 873
a11672a4
RR
874bool wxBitmap::operator != ( const wxBitmap& bmp ) const
875{
876 return m_refData != bmp.m_refData;
877}
83df96d6 878
a11672a4
RR
879bool wxBitmap::Ok() const
880{
881 return (m_refData != NULL);
882}
83df96d6 883
a11672a4
RR
884int wxBitmap::GetHeight() const
885{
886 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
83df96d6 887
a11672a4
RR
888 return M_BMPDATA->m_height;
889}
83df96d6 890
a11672a4
RR
891int wxBitmap::GetWidth() const
892{
893 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
83df96d6 894
a11672a4
RR
895 return M_BMPDATA->m_width;
896}
83df96d6 897
a11672a4
RR
898int wxBitmap::GetDepth() const
899{
900 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
83df96d6 901
a11672a4
RR
902 return M_BMPDATA->m_bpp;
903}
83df96d6 904
a11672a4
RR
905wxMask *wxBitmap::GetMask() const
906{
907 wxCHECK_MSG( Ok(), (wxMask *) NULL, wxT("invalid bitmap") );
83df96d6 908
a11672a4
RR
909 return M_BMPDATA->m_mask;
910}
83df96d6 911
a11672a4
RR
912void wxBitmap::SetMask( wxMask *mask )
913{
914 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
83df96d6 915
a11672a4 916 if (M_BMPDATA->m_mask) delete M_BMPDATA->m_mask;
83df96d6 917
a11672a4
RR
918 M_BMPDATA->m_mask = mask;
919}
83df96d6 920
a11672a4
RR
921bool wxBitmap::CopyFromIcon(const wxIcon& icon)
922{
923 *this = icon;
924 return TRUE;
925}
83df96d6 926
a11672a4
RR
927wxBitmap wxBitmap::GetSubBitmap( const wxRect& rect) const
928{
929 wxCHECK_MSG( Ok() &&
930 (rect.x >= 0) && (rect.y >= 0) &&
931 (rect.x+rect.width <= M_BMPDATA->m_width) && (rect.y+rect.height <= M_BMPDATA->m_height),
932 wxNullBitmap, wxT("invalid bitmap or bitmap region") );
83df96d6 933
a11672a4
RR
934 wxBitmap ret( rect.width, rect.height, M_BMPDATA->m_bpp );
935 wxASSERT_MSG( ret.Ok(), wxT("GetSubBitmap error") );
83df96d6 936
a11672a4
RR
937
938 wxFAIL_MSG( "wxBitmap::GetSubBitmap not yet implemented" );
939
940#if 0
941 if (ret.GetPixmap())
83df96d6 942 {
a11672a4
RR
943 GdkGC *gc = gdk_gc_new( ret.GetPixmap() );
944 gdk_draw_pixmap( ret.GetPixmap(), gc, GetPixmap(), rect.x, rect.y, 0, 0, rect.width, rect.height );
945 gdk_gc_destroy( gc );
946 }
947 else
948 {
949 GdkGC *gc = gdk_gc_new( ret.GetBitmap() );
950 gdk_wx_draw_bitmap( ret.GetBitmap(), gc, GetBitmap(), rect.x, rect.y, 0, 0, rect.width, rect.height );
951 gdk_gc_destroy( gc );
83df96d6
JS
952 }
953
a11672a4 954 if (GetMask())
83df96d6 955 {
a11672a4
RR
956 wxMask *mask = new wxMask;
957 mask->m_bitmap = gdk_pixmap_new( wxGetRootWindow()->window, rect.width, rect.height, 1 );
958
959 GdkGC *gc = gdk_gc_new( mask->m_bitmap );
960 gdk_wx_draw_bitmap( mask->m_bitmap, gc, M_BMPDATA->m_mask->m_bitmap, 0, 0, rect.x, rect.y, rect.width, rect.height );
961 gdk_gc_destroy( gc );
83df96d6 962
a11672a4 963 ret.SetMask( mask );
83df96d6 964 }
a11672a4 965#endif
83df96d6 966
a11672a4
RR
967 return ret;
968}
83df96d6 969
a11672a4
RR
970bool wxBitmap::SaveFile( const wxString &name, int type, wxPalette *WXUNUSED(palette) )
971{
972 wxCHECK_MSG( Ok(), FALSE, wxT("invalid bitmap") );
83df96d6 973
a11672a4 974 // Try to save the bitmap via wxImage handlers:
83df96d6 975 {
a11672a4
RR
976 wxImage image( *this );
977 if (image.Ok()) return image.SaveFile( name, type );
978 }
83df96d6 979
a11672a4
RR
980 return FALSE;
981}
83df96d6 982
a11672a4
RR
983bool wxBitmap::LoadFile( const wxString &name, int type )
984{
985 UnRef();
83df96d6 986
a11672a4 987 if (!wxFileExists(name)) return FALSE;
83df96d6 988
83df96d6 989
a11672a4 990 if (type == wxBITMAP_TYPE_XPM)
83df96d6 991 {
707440dc
JS
992#if wxUSE_XPM
993#if wxHAVE_LIB_XPM
a11672a4 994 m_refData = new wxBitmapRefData();
83df96d6 995
a11672a4
RR
996 M_BMPDATA->m_display = wxGlobalDisplay();
997
998 Display *xdisplay = (Display*) M_BMPDATA->m_display;
999
1000 int xscreen = DefaultScreen( xdisplay );
1001 Window xroot = RootWindow( xdisplay, xscreen );
1002
1003 int bpp = DefaultDepth( xdisplay, xscreen );
83df96d6 1004
a11672a4
RR
1005 XpmAttributes xpmAttr;
1006 xpmAttr.valuemask = XpmReturnInfos; // nothing yet, but get infos back
83df96d6 1007
a11672a4
RR
1008 Pixmap pixmap;
1009 Pixmap mask = 0;
1010
1011 int ErrorStatus = XpmReadFileToPixmap( xdisplay, xroot, (char*) name.c_str(), &pixmap, &mask, &xpmAttr);
1012
1013 if (ErrorStatus == XpmSuccess)
1014 {
1015 M_BMPDATA->m_width = xpmAttr.width;
1016 M_BMPDATA->m_height = xpmAttr.height;
83df96d6 1017
a11672a4 1018 M_BMPDATA->m_bpp = bpp; // mono as well?
83df96d6 1019
a11672a4
RR
1020 XpmFreeAttributes(&xpmAttr);
1021
1022 M_BMPDATA->m_bitmap = (WXPixmap) pixmap;
1023
1024 if (mask)
1025 {
1026 M_BMPDATA->m_mask = new wxMask;
1027 M_BMPDATA->m_mask->SetBitmap( (WXPixmap) mask );
1028 M_BMPDATA->m_mask->SetDisplay( xdisplay );
1029 }
1030 }
1031 else
1032 {
1033 UnRef();
1034
1035 return FALSE;
1036 }
707440dc
JS
1037#else
1038#if wxUSE_STREAMS
1039 wxXPMDecoder decoder;
1040 wxFileInputStream stream(name);
1041 if (stream.Ok())
1042 {
1043 wxImage image(decoder.Read(stream));
1044 if (image.Ok())
1045 return CreateFromImage(image);
1046 else
1047 return FALSE;
1048 }
1049 else
1050 return FALSE;
1051#else
1052 return FALSE;
1053#endif
1054 // wxUSE_STREAMS
1055#endif
1056 // wxHAVE_LIB_XPM
1057#endif
1058 // wxUSE_XPM
1059 return FALSE;
a11672a4
RR
1060 }
1061 else // try if wxImage can load it
1062 {
1063 wxImage image;
1064 if (!image.LoadFile( name, type )) return FALSE;
1065 if (image.Ok()) *this = image.ConvertToBitmap();
1066 else return FALSE;
83df96d6 1067 }
83df96d6
JS
1068
1069 return TRUE;
1070}
1071
a11672a4 1072wxPalette *wxBitmap::GetPalette() const
83df96d6 1073{
a11672a4 1074 if (!Ok()) return (wxPalette *) NULL;
83df96d6 1075
a11672a4
RR
1076 return M_BMPDATA->m_palette;
1077}
83df96d6 1078
a11672a4
RR
1079void wxBitmap::SetHeight( int height )
1080{
1081 if (!m_refData) m_refData = new wxBitmapRefData();
83df96d6 1082
a11672a4
RR
1083 M_BMPDATA->m_height = height;
1084}
83df96d6 1085
a11672a4
RR
1086void wxBitmap::SetWidth( int width )
1087{
1088 if (!m_refData) m_refData = new wxBitmapRefData();
83df96d6 1089
a11672a4
RR
1090 M_BMPDATA->m_width = width;
1091}
83df96d6 1092
a11672a4
RR
1093void wxBitmap::SetDepth( int depth )
1094{
1095 if (!m_refData) m_refData = new wxBitmapRefData();
83df96d6 1096
a11672a4
RR
1097 M_BMPDATA->m_bpp = depth;
1098}
83df96d6 1099
a11672a4
RR
1100void wxBitmap::SetPixmap( WXPixmap pixmap )
1101{
1102 if (!m_refData) m_refData = new wxBitmapRefData();
83df96d6 1103
a11672a4
RR
1104 M_BMPDATA->m_pixmap = pixmap;
1105}
83df96d6 1106
a11672a4
RR
1107void wxBitmap::SetBitmap( WXPixmap bitmap )
1108{
1109 if (!m_refData) m_refData = new wxBitmapRefData();
83df96d6 1110
a11672a4
RR
1111 M_BMPDATA->m_bitmap = bitmap;
1112}
83df96d6 1113
a11672a4
RR
1114WXPixmap wxBitmap::GetPixmap() const
1115{
1116 wxCHECK_MSG( Ok(), (WXPixmap) NULL, wxT("invalid bitmap") );
83df96d6 1117
a11672a4
RR
1118 return M_BMPDATA->m_pixmap;
1119}
83df96d6 1120
a11672a4
RR
1121WXPixmap wxBitmap::GetBitmap() const
1122{
1123 wxCHECK_MSG( Ok(), (WXPixmap) NULL, wxT("invalid bitmap") );
83df96d6 1124
a11672a4 1125 return M_BMPDATA->m_bitmap;
83df96d6 1126}
7266b672 1127
a11672a4 1128WXDisplay *wxBitmap::GetDisplay() const
7266b672 1129{
a11672a4 1130 wxCHECK_MSG( Ok(), (WXDisplay*) NULL, wxT("invalid bitmap") );
b6ed4565 1131
a11672a4 1132 return M_BMPDATA->m_display;
7266b672 1133}
a11672a4 1134