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