void zen::convertToVanillaImage(wxImage& img) { if (!img.HasAlpha()) { const int width = img.GetWidth (); const int height = img.GetHeight(); if (width <= 0 || height <= 0) return; unsigned char mask_r = 0; unsigned char mask_g = 0; unsigned char mask_b = 0; const bool haveMask = img.HasMask() && img.GetOrFindMaskColour(&mask_r, &mask_g, &mask_b); //check for mask before calling wxImage::GetOrFindMaskColour() to skip needlessly searching for new mask color img.SetAlpha(); ::memset(img.GetAlpha(), wxIMAGE_ALPHA_OPAQUE, width * height); //wxWidgets, as always, tries to be more clever than it really is and f***s up wxStaticBitmap if wxBitmap is fully opaque: img.GetAlpha()[width * height - 1] = 254; if (haveMask) { img.SetMask(false); unsigned char* alphaPtr = img.GetAlpha(); const unsigned char* dataPtr = img.GetData(); const int pixelCount = width * height; for (int i = 0; i < pixelCount; ++ i) { const unsigned char r = *dataPtr++; const unsigned char g = *dataPtr++; const unsigned char b = *dataPtr++; if (r == mask_r && g == mask_g && b == mask_b) alphaPtr[i] = wxIMAGE_ALPHA_TRANSPARENT; } } } else { assert(!img.HasMask()); } }
void Map::AssignImgData(ImgData *imgdata, wxImage &img) { if(imgdata == NULL) return; if(img.IsOk()) { unsigned char *pixdata, *alphadata; int width, height; long datasize; if(!img.HasAlpha()) img.SetAlpha(); width = img.GetWidth(); height = img.GetHeight(); pixdata = img.GetData(); alphadata = img.GetAlpha(); datasize = (long) 4 * width * height; (*imgdata).width = (long)width; (*imgdata).height = (long)height; (*imgdata).data = (unsigned char*)malloc(datasize); if((*imgdata).data == NULL) return; int pi = 0, ai = 0; for(int di = 0; di < datasize;) { (*imgdata).data[di++] = pixdata[pi++]; (*imgdata).data[di++] = pixdata[pi++]; (*imgdata).data[di++] = pixdata[pi++]; (*imgdata).data[di++] = alphadata[ai++]; } } else { (*imgdata).width = 0; (*imgdata).height = 0; (*imgdata).data = NULL; } }