+ wxCHECK_MSG( bits, false, wxT("NULL pointer in wxBitmap::CreateFromXpm") );
+
+ return Create(bits, wxBITMAP_TYPE_XPM_DATA, 0, 0, 0);
+}
+
+bool wxBitmap::CreateFromImage( const wxImage& image, int depth )
+{
+#if wxUSE_NANOX
+ if (!image.Ok())
+ {
+ wxASSERT_MSG(image.Ok(), wxT("Invalid wxImage passed to wxBitmap::CreateFromImage."));
+ return false;
+ }
+
+ int w = image.GetWidth();
+ int h = image.GetHeight();
+
+ if (!Create(w, h, depth))
+ return false;
+
+ // Unfortunately the mask has to be screen-depth since
+ // 1-bpp bitmaps don't seem to be supported
+ // TODO: implement transparent drawing, presumably
+ // by doing several blits as per the Windows
+ // implementation because Nano-X doesn't support
+ // XSetClipMask.
+ // TODO: could perhaps speed this function up
+ // by making a buffer of pixel values,
+ // and then calling GrArea to write that to the
+ // pixmap. See demos/nxroach.c.
+
+ bool hasMask = image.HasMask();
+
+ GC pixmapGC = GrNewGC();
+ Pixmap pixmap = (Pixmap) GetPixmap();
+
+ GC maskGC = 0;
+ Pixmap maskPixmap = 0;
+
+ unsigned char maskR = 0;
+ unsigned char maskG = 0;
+ unsigned char maskB = 0;
+
+ if (hasMask)
+ {
+ maskR = image.GetMaskRed();
+ maskG = image.GetMaskGreen();
+ maskB = image.GetMaskBlue();
+
+ maskGC = GrNewGC();
+ maskPixmap = GrNewPixmap(w, h, 0);
+ if (!maskPixmap)
+ hasMask = false;
+ else
+ {
+ wxMask* mask = new wxMask;
+ mask->SetBitmap((WXPixmap) maskPixmap);
+ SetMask(mask);
+ }
+ }
+
+ GR_COLOR lastPixmapColour = 0;
+ GR_COLOR lastMaskColour = 0;
+
+ int i, j;
+ for (i = 0; i < w; i++)
+ {
+ for (j = 0; j < h; j++)
+ {
+ unsigned char red = image.GetRed(i, j);
+ unsigned char green = image.GetGreen(i, j);
+ unsigned char blue = image.GetBlue(i, j);
+
+ GR_COLOR colour = GR_RGB(red, green, blue);
+
+ // Efficiency measure
+ if (colour != lastPixmapColour || (i == 0 && j == 0))
+ {
+ GrSetGCForeground(pixmapGC, colour);
+ lastPixmapColour = colour;
+ }
+
+ GrPoint(pixmap, pixmapGC, i, j);
+
+ if (hasMask)
+ {
+ // scan the bitmap for the transparent colour and set the corresponding
+ // pixels in the mask to BLACK and the rest to WHITE
+ if (maskR == red && maskG == green && maskB == blue)
+ {
+ colour = GR_RGB(0, 0, 0);
+ }
+ else
+ {
+ colour = GR_RGB(255, 255, 255);
+ }
+ if (colour != lastMaskColour || (i == 0 && j == 0))
+ {
+ GrSetGCForeground(maskGC, colour);
+ lastMaskColour = colour;
+ }
+ GrPoint(maskPixmap, maskGC, i, j);
+ }
+ }
+ }
+
+ GrDestroyGC(pixmapGC);
+ if (hasMask)
+ GrDestroyGC(maskGC);
+
+ return true;
+#else
+ // !wxUSE_NANOX
+
+ UnRef();
+
+ wxCHECK_MSG( image.Ok(), false, wxT("invalid image") )
+ wxCHECK_MSG( depth == -1, false, wxT("invalid bitmap depth") )
+
+ m_refData = new wxBitmapRefData();
+
+ M_BMPDATA->m_display = wxGlobalDisplay();
+
+ Display *xdisplay = (Display*) M_BMPDATA->m_display;
+
+ int xscreen = DefaultScreen( xdisplay );
+ Window xroot = RootWindow( xdisplay, xscreen );
+ Visual* xvisual = DefaultVisual( xdisplay, xscreen );
+
+ int bpp = wxTheApp->GetVisualInfo(M_BMPDATA->m_display)->m_visualDepth;
+
+ int width = image.GetWidth();
+ int height = image.GetHeight();
+ M_BMPDATA->m_width = width;
+ M_BMPDATA->m_height = height;
+
+ if (depth != 1) depth = bpp;
+ M_BMPDATA->m_bpp = depth;
+
+ if (depth == 1)
+ {
+ wxFAIL_MSG( wxT("mono images later") );
+ }
+ else
+ {
+ // Create image
+
+ XImage *data_image = XCreateImage( xdisplay, xvisual, bpp, ZPixmap, 0, 0, width, height, 32, 0 );
+ data_image->data = (char*) malloc( data_image->bytes_per_line * data_image->height );
+
+ if (data_image->data == NULL)
+ {
+ wxLogError( wxT("Out of memory.") ); // TODO clean
+ return false;
+ }
+
+ M_BMPDATA->m_pixmap = (WXPixmap) XCreatePixmap( xdisplay, xroot, width, height, depth );
+
+ // Create mask
+
+ XImage *mask_image = (XImage*) NULL;
+ if (image.HasMask())
+ {
+ mask_image = XCreateImage( xdisplay, xvisual, 1, ZPixmap, 0, 0, width, height, 32, 0 );
+ mask_image->data = (char*) malloc( mask_image->bytes_per_line * mask_image->height );
+
+ if (mask_image->data == NULL)
+ {
+ wxLogError( wxT("Out of memory.") ); // TODO clean
+ return false;
+ }
+
+ wxMask *mask = new wxMask();
+ mask->SetDisplay( xdisplay );
+ mask->SetBitmap( (WXPixmap) XCreatePixmap( xdisplay, xroot, width, height, 1 ) );
+
+ SetMask( mask );
+ }
+
+ if (bpp < 8) bpp = 8;
+
+ // Render
+
+ enum byte_order { RGB, RBG, BRG, BGR, GRB, GBR };
+ byte_order b_o = RGB;
+
+ wxXVisualInfo* vi = wxTheApp->GetVisualInfo(M_BMPDATA->m_display);
+ unsigned long greenMask = vi->m_visualGreenMask,
+ redMask = vi->m_visualRedMask,
+ blueMask = vi->m_visualBlueMask;
+
+ if (bpp > 8)
+ {
+ if ((redMask > greenMask) && (greenMask > blueMask)) b_o = RGB;
+ else if ((redMask > blueMask) && (blueMask > greenMask)) b_o = RBG;
+ else if ((blueMask > redMask) && (redMask > greenMask)) b_o = BRG;
+ else if ((blueMask > greenMask) && (greenMask > redMask))b_o = BGR;
+ else if ((greenMask > redMask) && (redMask > blueMask)) b_o = GRB;
+ else if ((greenMask > blueMask) && (blueMask > redMask)) b_o = GBR;
+ }
+
+ int r_mask = image.GetMaskRed();
+ int g_mask = image.GetMaskGreen();
+ int b_mask = image.GetMaskBlue();
+
+ unsigned char* data = image.GetData();
+ wxASSERT_MSG( data, wxT("No image data") );
+
+ unsigned char *colorCube =
+ wxTheApp->GetVisualInfo(M_BMPDATA->m_display)->m_colorCube;
+
+ bool hasMask = image.HasMask();
+
+ int index = 0;
+ for (int y = 0; y < height; y++)
+ {
+ for (int x = 0; x < width; x++)
+ {
+ int r = data[index];
+ index++;
+ int g = data[index];
+ index++;
+ int b = data[index];
+ index++;
+
+ if (hasMask)
+ {
+ if ((r == r_mask) && (b == b_mask) && (g == g_mask))
+ XPutPixel( mask_image, x, y, 0 );
+ else
+ XPutPixel( mask_image, x, y, 1 );
+ }