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