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