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