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