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