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