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