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