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