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