1 /////////////////////////////////////////////////////////////////////////////
4 // Author: Julian Smart, Robert Roebling
8 // Copyright: (c) Julian Smart, Robert Roebling
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
13 #pragma implementation "bitmap.h"
16 #include "wx/bitmap.h"
22 #include "wx/dcmemory.h"
25 #include "wx/x11/private.h"
27 /* No point in using libXPM for NanoX */
30 #define wxHAVE_LIB_XPM 0
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
);
40 #include "wx/xpmdecod.h"
41 #include "wx/wfstream.h"
46 //-----------------------------------------------------------------------------
48 //-----------------------------------------------------------------------------
50 IMPLEMENT_DYNAMIC_CLASS(wxMask
,wxObject
)
58 wxMask
::wxMask( const wxBitmap
& bitmap
, const wxColour
& colour
)
61 Create( bitmap
, colour
);
64 wxMask
::wxMask( const wxBitmap
& bitmap
, int paletteIndex
)
67 Create( bitmap
, paletteIndex
);
70 wxMask
::wxMask( const wxBitmap
& bitmap
)
79 XFreePixmap( (Display
*) m_display
, (Pixmap
) m_bitmap
);
82 bool wxMask
::Create( const wxBitmap
& bitmap
,
83 const wxColour
& colour
)
88 XFreePixmap( (Display
*) m_display
, (Pixmap
) m_bitmap
);
92 m_display
= bitmap
.GetDisplay();
94 wxImage
image( bitmap
);
95 if (!image
.Ok()) return FALSE
;
97 m_display
= bitmap
.GetDisplay();
99 Display
*xdisplay
= (Display
*) m_display
;
101 int xscreen
= DefaultScreen( xdisplay
);
102 Window xroot
= RootWindow( xdisplay
, xscreen
);
103 Visual
* xvisual
= DefaultVisual( xdisplay
, xscreen
);
104 int bpp
= DefaultDepth( xdisplay
, xscreen
);
106 m_bitmap
= (WXPixmap
) XCreatePixmap( xdisplay
, xroot
, image
.GetWidth(), image
.GetHeight(), 1 );
107 GC gc
= XCreateGC( xdisplay
, (Pixmap
) m_bitmap
, 0, NULL
);
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() );
113 unsigned char *data
= image
.GetData();
116 unsigned char red
= colour
.Red();
117 unsigned char green
= colour
.Green();
118 unsigned char blue
= colour
.Blue();
120 XVisualInfo vinfo_template
;
123 vinfo_template
.visual
= xvisual
;
124 vinfo_template
.visualid
= XVisualIDFromVisual( xvisual
);
125 vinfo_template
.depth
= bpp
;
128 vi
= XGetVisualInfo( xdisplay
, VisualIDMask
|VisualDepthMask
, &vinfo_template
, &nitem
);
129 wxASSERT_MSG( vi
, wxT("No visual info") );
131 if ((bpp
== 16) && (vi
->red_mask
!= 0xf800)) bpp
= 15;
135 green
= green
& 0xf8;
141 green
= green
& 0xfc;
147 green
= green
& 0xf0;
151 XSetForeground( xdisplay
, gc
, BlackPixel(xdisplay
,xscreen
) );
153 for (int j
= 0; j
< image
.GetHeight(); j
++)
157 for (i
= 0; i
< image
.GetWidth(); i
++)
159 if ((data
[index
] == red
) &&
160 (data
[index
+1] == green
) &&
161 (data
[index
+2] == blue
))
170 XDrawLine( xdisplay
, (Pixmap
) m_bitmap
, gc
, start_x
, j
, i
-1, j
);
177 XDrawLine( xdisplay
, (Pixmap
) m_bitmap
, gc
, start_x
, j
, i
, j
);
180 XFreeGC( xdisplay
, gc
);
189 bool wxMask
::Create( const wxBitmap
& bitmap
, int paletteIndex
)
192 wxPalette
*pal
= bitmap
.GetPalette();
194 wxCHECK_MSG( pal
, FALSE
, wxT("Cannot create mask from bitmap without palette") );
196 pal
->GetRGB(paletteIndex
, &r
, &g
, &b
);
198 return Create(bitmap
, wxColour(r
, g
, b
));
201 bool wxMask
::Create( const wxBitmap
& bitmap
)
206 XFreePixmap( (Display
*) m_display
, (Pixmap
) m_bitmap
);
210 if (!bitmap
.Ok()) return FALSE
;
212 wxCHECK_MSG( bitmap
.GetBitmap(), FALSE
, wxT("Cannot create mask from colour bitmap") );
214 m_display
= bitmap
.GetDisplay();
216 int xscreen
= DefaultScreen( (Display
*) m_display
);
217 Window xroot
= RootWindow( (Display
*) m_display
, xscreen
);
219 m_bitmap
= (WXPixmap
) XCreatePixmap( (Display
*) m_display
, xroot
, bitmap
.GetWidth(), bitmap
.GetHeight(), 1 );
221 if (!m_bitmap
) return FALSE
;
223 GC gc
= XCreateGC( (Display
*) m_display
, (Pixmap
) m_bitmap
, 0, NULL
);
225 XCopyPlane( (Display
*) m_display
, (Pixmap
) bitmap
.GetBitmap(), (Pixmap
) m_bitmap
,
226 gc
, 0, 0, bitmap
.GetWidth(), bitmap
.GetHeight(), 0, 0, 1 );
228 XFreeGC( (Display
*) m_display
, gc
);
237 //-----------------------------------------------------------------------------
239 //-----------------------------------------------------------------------------
241 class wxBitmapRefData
: public wxObjectRefData
249 WXDisplay
*m_display
;
254 wxPalette
*m_palette
;
257 wxBitmapRefData
::wxBitmapRefData()
262 m_mask
= (wxMask
*) NULL
;
266 m_palette
= (wxPalette
*) NULL
;
269 wxBitmapRefData
::~wxBitmapRefData()
271 if (m_pixmap
) XFreePixmap( (Display
*) m_display
, (Pixmap
) m_pixmap
);
272 if (m_bitmap
) XFreePixmap( (Display
*) m_display
, (Pixmap
) m_bitmap
);
273 if (m_mask
) delete m_mask
;
274 if (m_palette
) delete m_palette
;
277 //-----------------------------------------------------------------------------
279 #define M_BMPDATA ((wxBitmapRefData *)m_refData)
281 IMPLEMENT_DYNAMIC_CLASS(wxBitmap
,wxGDIObject
)
287 wxBitmap
::wxBitmap( int width
, int height
, int depth
)
289 Create( width
, height
, depth
);
292 bool wxBitmap
::Create( int width
, int height
, int depth
)
296 wxCHECK_MSG( (width
> 0) && (height
> 0), FALSE
, wxT("invalid bitmap size") )
298 m_refData
= new wxBitmapRefData();
300 M_BMPDATA
->m_display
= wxGlobalDisplay();
302 wxASSERT_MSG( M_BMPDATA
->m_display
, wxT("No display") );
304 int xscreen
= DefaultScreen( (Display
*) M_BMPDATA
->m_display
);
305 Window xroot
= RootWindow( (Display
*) M_BMPDATA
->m_display
, xscreen
);
307 int bpp
= DefaultDepth( (Display
*) M_BMPDATA
->m_display
, xscreen
);
308 if (depth
== -1) depth
= bpp
;
310 wxCHECK_MSG( (depth
== bpp
) ||
311 (depth
== 1), FALSE
, wxT("invalid bitmap depth") )
313 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
314 M_BMPDATA
->m_width
= width
;
315 M_BMPDATA
->m_height
= height
;
318 M_BMPDATA
->m_pixmap
= (WXPixmap
) GrNewPixmap(width
, height
, NULL
);
319 M_BMPDATA
->m_bpp
= bpp
;
321 wxASSERT_MSG( M_BMPDATA
->m_pixmap
, wxT("Bitmap creation failed") );
325 M_BMPDATA
->m_bitmap
= (WXPixmap
) XCreatePixmap( (Display
*) M_BMPDATA
->m_display
, xroot
, width
, height
, 1 );
327 wxASSERT_MSG( M_BMPDATA
->m_bitmap
, wxT("Bitmap creation failed") );
329 M_BMPDATA
->m_bpp
= 1;
333 M_BMPDATA
->m_pixmap
= (WXPixmap
) XCreatePixmap( (Display
*) M_BMPDATA
->m_display
, xroot
, width
, height
, depth
);
335 wxASSERT_MSG( M_BMPDATA
->m_pixmap
, wxT("Pixmap creation failed") );
337 M_BMPDATA
->m_bpp
= depth
;
343 bool wxBitmap
::CreateFromXpm( const char **bits
)
349 wxCHECK_MSG( bits
!= NULL
, FALSE
, wxT("invalid bitmap data") )
351 m_refData
= new wxBitmapRefData();
353 M_BMPDATA
->m_display
= wxGlobalDisplay();
355 Display
*xdisplay
= (Display
*) M_BMPDATA
->m_display
;
357 int xscreen
= DefaultScreen( xdisplay
);
358 Window xroot
= RootWindow( xdisplay
, xscreen
);
360 int bpp
= DefaultDepth( xdisplay
, xscreen
);
362 XpmAttributes xpmAttr
;
363 xpmAttr
.valuemask
= XpmReturnInfos
; // nothing yet, but get infos back
368 int ErrorStatus
= XpmCreatePixmapFromData( xdisplay
, xroot
, (char**) bits
, &pixmap
, &mask
, &xpmAttr
);
370 if (ErrorStatus
== XpmSuccess
)
372 M_BMPDATA
->m_width
= xpmAttr
.width
;
373 M_BMPDATA
->m_height
= xpmAttr
.height
;
375 M_BMPDATA
->m_bpp
= bpp
; // mono as well?
378 unsigned int depthRet
;
380 unsigned int widthRet
, heightRet
, borderWidthRet
;
381 XGetGeometry( xdisplay
, pixmap
, &xroot
, &xRet
, &yRet
,
382 &widthRet
, &heightRet
, &borderWidthRet
, &depthRet
);
384 wxASSERT_MSG( bpp
== (int)depthRet
, wxT("colour depth mismatch") )
387 XpmFreeAttributes(&xpmAttr
);
389 M_BMPDATA
->m_pixmap
= (WXPixmap
) pixmap
;
393 M_BMPDATA
->m_mask
= new wxMask
;
394 M_BMPDATA
->m_mask
->SetBitmap( (WXPixmap
) mask
);
395 M_BMPDATA
->m_mask
->SetDisplay( xdisplay
);
406 wxXPMDecoder decoder
;
407 wxImage
image(decoder
.ReadData(bits
));
409 return CreateFromImage(image
);
417 bool wxBitmap
::CreateFromImage( const wxImage
& image
, int depth
)
422 wxASSERT_MSG(image
.Ok(), "Invalid wxImage passed to wxBitmap::CreateFromImage.");
426 int w
= image
.GetWidth();
427 int h
= image
.GetHeight();
429 if (!Create(w
, h
, depth
))
432 // Unfortunately the mask has to be screen-depth since
433 // 1-bpp bitmaps don't seem to be supported
434 // TODO: implement transparent drawing, presumably
435 // by doing several blits as per the Windows
436 // implementation because Nano-X doesn't support
439 bool hasMask
= image
.HasMask();
441 GC pixmapGC
= GrNewGC();
442 Pixmap pixmap
= (Pixmap
) GetPixmap();
445 Pixmap maskPixmap
= 0;
447 unsigned char maskR
= 0;
448 unsigned char maskG
= 0;
449 unsigned char maskB
= 0;
453 maskR
= image
.GetMaskRed();
454 maskG
= image
.GetMaskGreen();
455 maskB
= image
.GetMaskBlue();
458 maskPixmap
= GrNewPixmap(w
, h
, 0);
463 wxMask
* mask
= new wxMask
;
464 mask
->SetBitmap((WXPixmap
) maskPixmap
);
469 GR_COLOR lastPixmapColour
= 0;
470 GR_COLOR lastMaskColour
= 0;
473 for (i
= 0; i
< w
; i
++)
475 for (j
= 0; j
< h
; j
++)
477 unsigned char red
= image
.GetRed(i
, j
);
478 unsigned char green
= image
.GetGreen(i
, j
);
479 unsigned char blue
= image
.GetBlue(i
, j
);
481 GR_COLOR colour
= GR_RGB(red
, green
, blue
);
483 // Efficiency measure
484 if (colour
!= lastPixmapColour
|| (i
== 0 && j
== 0))
486 GrSetGCForeground(pixmapGC
, colour
);
487 lastPixmapColour
= colour
;
490 GrPoint(pixmap
, pixmapGC
, i
, j
);
494 // scan the bitmap for the transparent colour and set the corresponding
495 // pixels in the mask to BLACK and the rest to WHITE
496 if (maskR
== red
&& maskG
== green
&& maskB
== blue
)
498 colour
= GR_RGB(0, 0, 0);
502 colour
= GR_RGB(255, 255, 255);
504 if (colour
!= lastMaskColour
|| (i
== 0 && j
== 0))
506 GrSetGCForeground(maskGC
, colour
);
507 lastMaskColour
= colour
;
509 GrPoint(maskPixmap
, maskGC
, i
, j
);
514 GrDestroyGC(pixmapGC
);
524 wxCHECK_MSG( image
.Ok(), FALSE
, wxT("invalid image") )
525 wxCHECK_MSG( depth
== -1, FALSE
, wxT("invalid bitmap depth") )
527 m_refData
= new wxBitmapRefData();
529 M_BMPDATA
->m_display
= wxGlobalDisplay();
531 Display
*xdisplay
= (Display
*) M_BMPDATA
->m_display
;
533 int xscreen
= DefaultScreen( xdisplay
);
534 Window xroot
= RootWindow( xdisplay
, xscreen
);
535 Visual
* xvisual
= DefaultVisual( xdisplay
, xscreen
);
537 int bpp
= DefaultDepth( xdisplay
, xscreen
);
539 int width
= image
.GetWidth();
540 int height
= image
.GetHeight();
541 M_BMPDATA
->m_width
= width
;
542 M_BMPDATA
->m_height
= height
;
544 if (depth
!= 1) depth
= bpp
;
545 M_BMPDATA
->m_bpp
= depth
;
549 wxFAIL_MSG( "mono images later" );
555 XImage
*data_image
= XCreateImage( xdisplay
, xvisual
, bpp
, ZPixmap
, 0, 0, width
, height
, 32, 0 );
556 data_image
->data
= (char*) malloc( data_image
->bytes_per_line
* data_image
->height
);
558 if (data_image
->data
== NULL
)
560 wxLogError( wxT("Out of memory.") ); // TODO clean
564 M_BMPDATA
->m_pixmap
= (WXPixmap
) XCreatePixmap( xdisplay
, xroot
, width
, height
, depth
);
568 XImage
*mask_image
= (XImage
*) NULL
;
571 mask_image
= XCreateImage( xdisplay
, xvisual
, 1, ZPixmap
, 0, 0, width
, height
, 32, 0 );
572 mask_image
->data
= (char*) malloc( mask_image
->bytes_per_line
* mask_image
->height
);
574 if (mask_image
->data
== NULL
)
576 wxLogError( wxT("Out of memory.") ); // TODO clean
580 wxMask
*mask
= new wxMask();
581 mask
->SetDisplay( xdisplay
);
582 mask
->SetBitmap( (WXPixmap
) XCreatePixmap( xdisplay
, xroot
, width
, height
, 1 ) );
589 XVisualInfo vinfo_template
;
592 vinfo_template
.visual
= xvisual
;
593 vinfo_template
.visualid
= XVisualIDFromVisual( xvisual
);
594 vinfo_template
.depth
= bpp
;
597 vi
= XGetVisualInfo( xdisplay
, VisualIDMask
|VisualDepthMask
, &vinfo_template
, &nitem
);
598 wxASSERT_MSG( vi
, wxT("No visual info") );
600 if ((bpp
== 16) && (vi
->red_mask
!= 0xf800)) bpp
= 15;
601 if (bpp
< 8) bpp
= 8;
605 enum byte_order
{ RGB
, RBG
, BRG
, BGR
, GRB
, GBR
};
606 byte_order b_o
= RGB
;
610 if ((vi
->red_mask
> vi
->green_mask
) && (vi
->green_mask
> vi
->blue_mask
)) b_o
= RGB
;
611 else if ((vi
->red_mask
> vi
->blue_mask
) && (vi
->blue_mask
> vi
->green_mask
)) b_o
= RBG
;
612 else if ((vi
->blue_mask
> vi
->red_mask
) && (vi
->red_mask
> vi
->green_mask
)) b_o
= BRG
;
613 else if ((vi
->blue_mask
> vi
->green_mask
) && (vi
->green_mask
> vi
->red_mask
)) b_o
= BGR
;
614 else if ((vi
->green_mask
> vi
->red_mask
) && (vi
->red_mask
> vi
->blue_mask
)) b_o
= GRB
;
615 else if ((vi
->green_mask
> vi
->blue_mask
) && (vi
->blue_mask
> vi
->red_mask
)) b_o
= GBR
;
620 int r_mask
= image
.GetMaskRed();
621 int g_mask
= image
.GetMaskGreen();
622 int b_mask
= image
.GetMaskBlue();
624 unsigned char* data
= image
.GetData();
625 wxASSERT_MSG( data
, "No image data" );
627 bool hasMask
= image
.HasMask();
630 for (int y
= 0; y
< height
; y
++)
632 for (int x
= 0; x
< width
; x
++)
643 if ((r
== r_mask
) && (b
== b_mask
) && (g
== g_mask
))
644 XPutPixel( mask_image
, x
, y
, 0 );
646 XPutPixel( mask_image
, x
, y
, 1 );
655 if (wxTheApp
->m_colorCube
)
657 pixel
= wxTheApp
->m_colorCube
[ ((r
& 0xf8) << 7) + ((g
& 0xf8) << 2) + ((b
& 0xf8) >> 3) ];
661 GdkColormap
*cmap
= gtk_widget_get_default_colormap();
662 GdkColor
*colors
= cmap
->colors
;
663 int max
= 3 * (65536);
665 for (int i
= 0; i
< cmap
->size
; i
++)
667 int rdiff
= (r
<< 8) - colors
[i
].red
;
668 int gdiff
= (g
<< 8) - colors
[i
].green
;
669 int bdiff
= (b
<< 8) - colors
[i
].blue
;
670 int sum
= ABS (rdiff
) + ABS (gdiff
) + ABS (bdiff
);
671 if (sum
< max
) { pixel
= i
; max
= sum
; }
675 XPutPixel( data_image
, x
, y
, pixel
);
683 case RGB
: pixel
= ((r
& 0xf0) << 4) | (g
& 0xf0) | ((b
& 0xf0) >> 4); break;
684 case RBG
: pixel
= ((r
& 0xf0) << 4) | (b
& 0xf0) | ((g
& 0xf0) >> 4); break;
685 case GRB
: pixel
= ((g
& 0xf0) << 4) | (r
& 0xf0) | ((b
& 0xf0) >> 4); break;
686 case GBR
: pixel
= ((g
& 0xf0) << 4) | (b
& 0xf0) | ((r
& 0xf0) >> 4); break;
687 case BRG
: pixel
= ((b
& 0xf0) << 4) | (r
& 0xf0) | ((g
& 0xf0) >> 4); break;
688 case BGR
: pixel
= ((b
& 0xf0) << 4) | (g
& 0xf0) | ((r
& 0xf0) >> 4); break;
690 XPutPixel( data_image
, x
, y
, pixel
);
698 case RGB
: pixel
= ((r
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
699 case RBG
: pixel
= ((r
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
700 case GRB
: pixel
= ((g
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((b
& 0xf8) >> 3); break;
701 case GBR
: pixel
= ((g
& 0xf8) << 7) | ((b
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
702 case BRG
: pixel
= ((b
& 0xf8) << 7) | ((r
& 0xf8) << 2) | ((g
& 0xf8) >> 3); break;
703 case BGR
: pixel
= ((b
& 0xf8) << 7) | ((g
& 0xf8) << 2) | ((r
& 0xf8) >> 3); break;
705 XPutPixel( data_image
, x
, y
, pixel
);
710 // I actually don't know if for 16-bit displays, it is alway the green
711 // component or the second component which has 6 bits.
715 case RGB
: pixel
= ((r
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
716 case RBG
: pixel
= ((r
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
717 case GRB
: pixel
= ((g
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((b
& 0xf8) >> 3); break;
718 case GBR
: pixel
= ((g
& 0xf8) << 8) | ((b
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
719 case BRG
: pixel
= ((b
& 0xf8) << 8) | ((r
& 0xfc) << 3) | ((g
& 0xf8) >> 3); break;
720 case BGR
: pixel
= ((b
& 0xf8) << 8) | ((g
& 0xfc) << 3) | ((r
& 0xf8) >> 3); break;
722 XPutPixel( data_image
, x
, y
, pixel
);
731 case RGB
: pixel
= (r
<< 16) | (g
<< 8) | b
; break;
732 case RBG
: pixel
= (r
<< 16) | (b
<< 8) | g
; break;
733 case BRG
: pixel
= (b
<< 16) | (r
<< 8) | g
; break;
734 case BGR
: pixel
= (b
<< 16) | (g
<< 8) | r
; break;
735 case GRB
: pixel
= (g
<< 16) | (r
<< 8) | b
; break;
736 case GBR
: pixel
= (g
<< 16) | (b
<< 8) | r
; break;
738 XPutPixel( data_image
, x
, y
, pixel
);
747 GC gc
= XCreateGC( xdisplay
, (Pixmap
) M_BMPDATA
->m_pixmap
, 0, NULL
);
748 XPutImage( xdisplay
, (Pixmap
) M_BMPDATA
->m_pixmap
, gc
, data_image
, 0, 0, 0, 0, width
, height
);
750 XSync(wxGlobalDisplay(), False
);
753 XDestroyImage( data_image
);
754 XFreeGC( xdisplay
, gc
);
760 GC gc
= XCreateGC( xdisplay
, (Pixmap
) GetMask()->GetBitmap(), 0, NULL
);
761 XPutImage( xdisplay
, (Pixmap
) GetMask()->GetBitmap(), gc
, mask_image
, 0, 0, 0, 0, width
, height
);
763 XDestroyImage( mask_image
);
764 XFreeGC( xdisplay
, gc
);
773 static void wxCalcPrecAndShift( unsigned long mask
, int *shift
, int *prec
)
778 while (!(mask
& 0x1))
791 wxImage wxBitmap
::ConvertToImage() const
795 wxCHECK_MSG( Ok(), wxNullImage
, wxT("invalid bitmap") );
797 Display
*xdisplay
= (Display
*) M_BMPDATA
->m_display
;
798 wxASSERT_MSG( xdisplay
, wxT("No display") );
800 int xscreen
= DefaultScreen( xdisplay
);
801 Visual
* xvisual
= DefaultVisual( xdisplay
, xscreen
);
803 int bpp
= DefaultDepth( xdisplay
, xscreen
);
806 int w
= image
.GetWidth();
807 int h
= image
.GetHeight();
810 memDC
.SelectObject(*this);
814 // Warning: this is very inefficient.
815 // TODO: use GrReadArea to get an array of pixels
817 for (i
= 0; i
< w
; i
++)
819 for (j
= 0; j
< h
; j
++)
821 memDC
.GetPixel(i
, j
, & pixelCol
);
823 // TODO: make wxColour accessors more efficient
824 // by inlining, if possible
826 pixelCol
.Red(), pixelCol
.Green(),
830 memDC
.SelectObject(wxNullBitmap
);
836 XImage
*x_image
= NULL
;
839 x_image
= XGetImage( xdisplay
, (Pixmap
) GetPixmap(),
841 GetWidth(), GetHeight(),
842 AllPlanes
, ZPixmap
);
846 x_image
= XGetImage( xdisplay
, (Pixmap
) GetBitmap(),
848 GetWidth(), GetHeight(),
849 AllPlanes
, ZPixmap
);
852 wxFAIL_MSG( wxT("Ill-formed bitmap") );
855 wxCHECK_MSG( x_image
, wxNullImage
, wxT("couldn't create image") );
857 image
.Create( GetWidth(), GetHeight() );
858 char unsigned *data
= image
.GetData();
862 XDestroyImage( x_image
);
863 wxFAIL_MSG( wxT("couldn't create image") );
867 XImage
*x_image_mask
= NULL
;
870 x_image_mask
= XGetImage( xdisplay
, (Pixmap
) GetMask()->GetBitmap(),
872 GetWidth(), GetHeight(),
873 AllPlanes
, ZPixmap
);
875 image
.SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
878 int red_shift_right
= 0;
879 int green_shift_right
= 0;
880 int blue_shift_right
= 0;
881 int red_shift_left
= 0;
882 int green_shift_left
= 0;
883 int blue_shift_left
= 0;
884 bool use_shift
= FALSE
;
890 XVisualInfo vinfo_template
;
893 vinfo_template
.visual
= xvisual
;
894 vinfo_template
.visualid
= XVisualIDFromVisual( xvisual
);
895 vinfo_template
.depth
= bpp
;
898 vi
= XGetVisualInfo( xdisplay
, VisualIDMask
|VisualDepthMask
, &vinfo_template
, &nitem
);
899 wxASSERT_MSG( vi
, wxT("No visual info") );
901 int red_prec
,green_prec
,blue_prec
;
902 int red_shift
,green_shift
,blue_shift
;
903 wxCalcPrecAndShift( vi
->red_mask
, &red_shift
, &red_prec
);
904 wxCalcPrecAndShift( vi
->green_mask
, &green_shift
, &green_prec
);
905 wxCalcPrecAndShift( vi
->blue_mask
, &blue_shift
, &blue_prec
);
906 if (bpp
== 16) bpp
= red_prec
+ green_prec
+ blue_prec
;
908 red_shift_right
= red_shift
;
909 red_shift_left
= 8-red_prec
;
910 green_shift_right
= green_shift
;
911 green_shift_left
= 8-green_prec
;
912 blue_shift_right
= blue_shift
;
913 blue_shift_left
= 8-blue_prec
;
916 use_shift
= (vi
->visual
->c_class
== TrueColor
) || (vi
->visual
->c_class
== DirectColor
);
929 // GdkColormap *cmap = gtk_widget_get_default_colormap();
932 for (int j
= 0; j
< GetHeight(); j
++)
934 for (int i
= 0; i
< GetWidth(); i
++)
936 unsigned long pixel
= XGetPixel( x_image
, i
, j
);
954 data
[pos
] = (pixel
>> red_shift_right
) << red_shift_left
;
955 data
[pos
+1] = (pixel
>> green_shift_right
) << green_shift_left
;
956 data
[pos
+2] = (pixel
>> blue_shift_right
) << blue_shift_left
;
959 else if (cmap
->colors
)
961 data
[pos
] = cmap
->colors
[pixel
].red
>> 8;
962 data
[pos
+1] = cmap
->colors
[pixel
].green
>> 8;
963 data
[pos
+2] = cmap
->colors
[pixel
].blue
>> 8;
968 wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") );
973 int mask_pixel
= XGetPixel( x_image_mask
, i
, j
);
986 XDestroyImage( x_image
);
987 if (x_image_mask
) XDestroyImage( x_image_mask
);
993 wxBitmap
::wxBitmap( const wxBitmap
& bmp
)
998 wxBitmap
::wxBitmap( const wxString
&filename
, int type
)
1000 LoadFile( filename
, type
);
1003 wxBitmap
::wxBitmap( const char bits
[], int width
, int height
, int WXUNUSED(depth
) )
1006 m_refData
= new wxBitmapRefData();
1008 M_BMPDATA
->m_display
= wxGlobalDisplay();
1010 Display
*xdisplay
= (Display
*) M_BMPDATA
->m_display
;
1012 int xscreen
= DefaultScreen( xdisplay
);
1013 Window xroot
= RootWindow( xdisplay
, xscreen
);
1015 M_BMPDATA
->m_mask
= (wxMask
*) NULL
;
1016 M_BMPDATA
->m_bitmap
= (WXPixmap
) XCreateBitmapFromData( xdisplay
, xroot
, (char *) bits
, width
, height
);
1017 M_BMPDATA
->m_width
= width
;
1018 M_BMPDATA
->m_height
= height
;
1019 M_BMPDATA
->m_bpp
= 1;
1021 wxCHECK_RET( M_BMPDATA
->m_bitmap
, wxT("couldn't create bitmap") );
1024 wxBitmap
::~wxBitmap()
1028 wxBitmap
& wxBitmap
::operator = ( const wxBitmap
& bmp
)
1030 if ( m_refData
!= bmp
.m_refData
)
1036 bool wxBitmap
::operator == ( const wxBitmap
& bmp
) const
1038 return m_refData
== bmp
.m_refData
;
1041 bool wxBitmap
::operator != ( const wxBitmap
& bmp
) const
1043 return m_refData
!= bmp
.m_refData
;
1046 bool wxBitmap
::Ok() const
1048 return (m_refData
!= NULL
);
1051 int wxBitmap
::GetHeight() const
1053 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1055 return M_BMPDATA
->m_height
;
1058 int wxBitmap
::GetWidth() const
1060 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1062 return M_BMPDATA
->m_width
;
1065 int wxBitmap
::GetDepth() const
1067 wxCHECK_MSG( Ok(), -1, wxT("invalid bitmap") );
1069 return M_BMPDATA
->m_bpp
;
1072 wxMask
*wxBitmap
::GetMask() const
1074 wxCHECK_MSG( Ok(), (wxMask
*) NULL
, wxT("invalid bitmap") );
1076 return M_BMPDATA
->m_mask
;
1079 void wxBitmap
::SetMask( wxMask
*mask
)
1081 wxCHECK_RET( Ok(), wxT("invalid bitmap") );
1083 if (M_BMPDATA
->m_mask
) delete M_BMPDATA
->m_mask
;
1085 M_BMPDATA
->m_mask
= mask
;
1088 bool wxBitmap
::CopyFromIcon(const wxIcon
& icon
)
1094 wxBitmap wxBitmap
::GetSubBitmap( const wxRect
& rect
) const
1096 wxCHECK_MSG( Ok() &&
1097 (rect
.x
>= 0) && (rect
.y
>= 0) &&
1098 (rect
.x
+rect
.width
<= M_BMPDATA
->m_width
) && (rect
.y
+rect
.height
<= M_BMPDATA
->m_height
),
1099 wxNullBitmap
, wxT("invalid bitmap or bitmap region") );
1101 wxBitmap
ret( rect
.width
, rect
.height
, M_BMPDATA
->m_bpp
);
1102 wxASSERT_MSG( ret
.Ok(), wxT("GetSubBitmap error") );
1105 wxFAIL_MSG( "wxBitmap::GetSubBitmap not yet implemented" );
1108 if (ret
.GetPixmap())
1110 GdkGC
*gc
= gdk_gc_new( ret
.GetPixmap() );
1111 gdk_draw_pixmap( ret
.GetPixmap(), gc
, GetPixmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1112 gdk_gc_destroy( gc
);
1116 GdkGC
*gc
= gdk_gc_new( ret
.GetBitmap() );
1117 gdk_wx_draw_bitmap( ret
.GetBitmap(), gc
, GetBitmap(), rect
.x
, rect
.y
, 0, 0, rect
.width
, rect
.height
);
1118 gdk_gc_destroy( gc
);
1123 wxMask
*mask
= new wxMask
;
1124 mask
->m_bitmap
= gdk_pixmap_new( wxGetRootWindow()->window
, rect
.width
, rect
.height
, 1 );
1126 GdkGC
*gc
= gdk_gc_new( mask
->m_bitmap
);
1127 gdk_wx_draw_bitmap( mask
->m_bitmap
, gc
, M_BMPDATA
->m_mask
->m_bitmap
, 0, 0, rect
.x
, rect
.y
, rect
.width
, rect
.height
);
1128 gdk_gc_destroy( gc
);
1130 ret
.SetMask( mask
);
1137 bool wxBitmap
::SaveFile( const wxString
&name
, int type
, wxPalette
*WXUNUSED(palette
) )
1139 wxCHECK_MSG( Ok(), FALSE
, wxT("invalid bitmap") );
1141 // Try to save the bitmap via wxImage handlers:
1143 wxImage
image( *this );
1144 if (image
.Ok()) return image
.SaveFile( name
, type
);
1150 bool wxBitmap
::LoadFile( const wxString
&name
, int type
)
1154 if (!wxFileExists(name
)) return FALSE
;
1157 if (type
== wxBITMAP_TYPE_XPM
)
1161 m_refData
= new wxBitmapRefData();
1163 M_BMPDATA
->m_display
= wxGlobalDisplay();
1165 Display
*xdisplay
= (Display
*) M_BMPDATA
->m_display
;
1167 int xscreen
= DefaultScreen( xdisplay
);
1168 Window xroot
= RootWindow( xdisplay
, xscreen
);
1170 int bpp
= DefaultDepth( xdisplay
, xscreen
);
1172 XpmAttributes xpmAttr
;
1173 xpmAttr
.valuemask
= XpmReturnInfos
; // nothing yet, but get infos back
1178 int ErrorStatus
= XpmReadFileToPixmap( xdisplay
, xroot
, (char*) name
.c_str(), &pixmap
, &mask
, &xpmAttr
);
1180 if (ErrorStatus
== XpmSuccess
)
1182 M_BMPDATA
->m_width
= xpmAttr
.width
;
1183 M_BMPDATA
->m_height
= xpmAttr
.height
;
1185 M_BMPDATA
->m_bpp
= bpp
; // mono as well?
1187 XpmFreeAttributes(&xpmAttr
);
1189 M_BMPDATA
->m_bitmap
= (WXPixmap
) pixmap
;
1193 M_BMPDATA
->m_mask
= new wxMask
;
1194 M_BMPDATA
->m_mask
->SetBitmap( (WXPixmap
) mask
);
1195 M_BMPDATA
->m_mask
->SetDisplay( xdisplay
);
1206 wxXPMDecoder decoder
;
1207 wxFileInputStream
stream(name
);
1210 wxImage
image(decoder
.ReadFile(stream
));
1212 return CreateFromImage(image
);
1228 else // try if wxImage can load it
1231 if (!image
.LoadFile( name
, type
)) return FALSE
;
1232 if (image
.Ok()) *this = image
.ConvertToBitmap();
1239 wxPalette
*wxBitmap
::GetPalette() const
1241 if (!Ok()) return (wxPalette
*) NULL
;
1243 return M_BMPDATA
->m_palette
;
1246 void wxBitmap
::SetHeight( int height
)
1248 if (!m_refData
) m_refData
= new wxBitmapRefData();
1250 M_BMPDATA
->m_height
= height
;
1253 void wxBitmap
::SetWidth( int width
)
1255 if (!m_refData
) m_refData
= new wxBitmapRefData();
1257 M_BMPDATA
->m_width
= width
;
1260 void wxBitmap
::SetDepth( int depth
)
1262 if (!m_refData
) m_refData
= new wxBitmapRefData();
1264 M_BMPDATA
->m_bpp
= depth
;
1267 void wxBitmap
::SetPixmap( WXPixmap pixmap
)
1269 if (!m_refData
) m_refData
= new wxBitmapRefData();
1271 M_BMPDATA
->m_pixmap
= pixmap
;
1274 void wxBitmap
::SetBitmap( WXPixmap bitmap
)
1276 if (!m_refData
) m_refData
= new wxBitmapRefData();
1278 M_BMPDATA
->m_bitmap
= bitmap
;
1281 WXPixmap wxBitmap
::GetPixmap() const
1283 wxCHECK_MSG( Ok(), (WXPixmap
) NULL
, wxT("invalid bitmap") );
1285 return M_BMPDATA
->m_pixmap
;
1288 WXPixmap wxBitmap
::GetBitmap() const
1290 wxCHECK_MSG( Ok(), (WXPixmap
) NULL
, wxT("invalid bitmap") );
1292 return M_BMPDATA
->m_bitmap
;
1295 WXDisplay
*wxBitmap
::GetDisplay() const
1297 wxCHECK_MSG( Ok(), (WXDisplay
*) NULL
, wxT("invalid bitmap") );
1299 return M_BMPDATA
->m_display
;
1303 // Copy from the drawable to the wxImage
1304 bool wxGetImageFromDrawable(GR_DRAW_ID drawable
, int srcX
, int srcY
, int width
, int height
, wxImage
& image
)
1306 GR_SCREEN_INFO sinfo
;
1308 GR_PIXELVAL
*pixels
;
1311 GR_PALETTE
* palette
= NULL
;
1313 GrGetScreenInfo(&sinfo
);
1315 if (sinfo
.pixtype
== MWPF_PALETTE
) {
1316 if(!(palette
= malloc(sizeof(GR_PALETTE
)))) {
1319 GrGetSystemPalette(palette
);
1322 if(!(pixels
= (GR_PIXELVAL
*) malloc(sizeof(GR_PIXELVAL
) * width
* height
)))
1327 if (!image
.Create(width
, height
))
1333 GrReadArea(drawable
, srcX
, srcY
, width
, height
,
1337 for(x
= 0; x
< sinfo
.cols
; x
++) {
1339 pp
= (unsigned char *)pixels
+
1340 ((x
+ (y
* sinfo
.cols
)) *
1341 sizeof(GR_PIXELVAL
));
1343 switch(sinfo
.pixtype
) {
1344 /* FIXME: These may need modifying on big endian. */
1345 case MWPF_TRUECOLOR0888
:
1346 case MWPF_TRUECOLOR888
:
1352 rgb
[0] = palette
->palette
[pp
[0]].r
;
1353 rgb
[1] = palette
->palette
[pp
[0]].g
;
1354 rgb
[2] = palette
->palette
[pp
[0]].b
;
1356 case MWPF_TRUECOLOR565
:
1357 rgb
[0] = pp
[1] & 0xf8;
1358 rgb
[1] = ((pp
[1] & 0x07) << 5) |
1359 ((pp
[0] & 0xe0) >> 3);
1360 rgb
[2] = (pp
[0] & 0x1f) << 3;
1362 case MWPF_TRUECOLOR555
:
1363 rgb
[0] = (pp
[1] & 0x7c) << 1;
1364 rgb
[1] = ((pp
[1] & 0x03) << 6) |
1365 ((pp
[0] & 0xe0) >> 2);
1366 rgb
[2] = (pp
[0] & 0x1f) << 3;
1368 case MWPF_TRUECOLOR332
:
1369 rgb
[0] = pp
[0] & 0xe0;
1370 rgb
[1] = (pp
[0] & 0x1c) << 3;
1371 rgb
[2] = (pp
[0] & 0x03) << 6;
1374 fprintf(stderr
, "Unsupported pixel "
1379 image
.SetRGB(x
, y
, rgb
[0], rgb
[1], rgb
[2]);
1385 if(palette
) free(palette
);
1391 int GrGetPixelColor(GR_SCREEN_INFO
* sinfo
, GR_PALETTE
* palette
, GR_PIXELVAL pixel
,
1392 unsigned char* red
, unsigned char* green
, unsigned char* blue
)
1394 unsigned char rgb
[3], *pp
;
1396 pp
= (unsigned char*) & pixel
;
1398 switch (sinfo
.pixtype
)
1400 /* FIXME: These may need modifying on big endian. */
1401 case MWPF_TRUECOLOR0888
:
1402 case MWPF_TRUECOLOR888
:
1408 rgb
[0] = palette
->palette
[pp
[0]].r
;
1409 rgb
[1] = palette
->palette
[pp
[0]].g
;
1410 rgb
[2] = palette
->palette
[pp
[0]].b
;
1412 case MWPF_TRUECOLOR565
:
1413 rgb
[0] = pp
[1] & 0xf8;
1414 rgb
[1] = ((pp
[1] & 0x07) << 5) |
1415 ((pp
[0] & 0xe0) >> 3);
1416 rgb
[2] = (pp
[0] & 0x1f) << 3;
1418 case MWPF_TRUECOLOR555
:
1419 rgb
[0] = (pp
[1] & 0x7c) << 1;
1420 rgb
[1] = ((pp
[1] & 0x03) << 6) |
1421 ((pp
[0] & 0xe0) >> 2);
1422 rgb
[2] = (pp
[0] & 0x1f) << 3;
1424 case MWPF_TRUECOLOR332
:
1425 rgb
[0] = pp
[0] & 0xe0;
1426 rgb
[1] = (pp
[0] & 0x1c) << 3;
1427 rgb
[2] = (pp
[0] & 0x03) << 6;
1430 fprintf(stderr
, "Unsupported pixel format\n");