Filed in: Resources.ProgrammingCodeSnippletsConvertAPandaTextureToAWx-image · Modified on : Fri, 31 Jul 09
# written by pro-rsoft
THUMBNAIL_SIZE = 64
THUMBNAIL_MAX_WIDTH = 64
PREVIEW_SIZE = (128, 128)
PREVIEW_ENABLED_DEFAULT = False
def makeBitmap(tex, alpha = True, thumb = False):
"""Returns a wx.Bitmap. 'tex' should be a Panda Texture object."""
assert isinstance(tex, Texture)
xsize, ysize = tex.getXSize(), tex.getYSize()
data, adata = None, None
# Only available in 1.6.0 and higher
if hasattr(Texture, "getRamImageAs"):
data = tex.getRamImageAs("RGB")
if alpha and tex.getNumComponents() in [2, 4]:
adata = tex.getRamImageAs("A")
else:
# Grab the RGB data
ttex = tex.makeCopy()
ttex.setFormat(Texture.FRgb)
if hasattr(ttex, "getUncompressedRamImage"):
data = ttex.getUncompressedRamImage()
else:
data = ttex.getRamImage()
# If we have an alpha channel, grab it as well.
if alpha and tex.getNumComponents() in [2, 4]:
ttex = tex.makeCopy()
ttex.setFormat(Texture.FAlpha)
if hasattr(ttex, "getUncompressedRamImage"):
adata = ttex.getUncompressedRamImage()
else:
adata = ttex.getRamImage()
# Now for the conversion to wx.
assert not data.isNull()
if adata == None:
img = wx.ImageFromData(xsize, ysize, data.getData())
else:
assert not adata.isNull()
img = wx.ImageFromDataWithAlpha(xsize, ysize, data.getData(), adata.getData())
if thumb:
# Resize it not to be bigger than the THUMBNAIL_SIZE.
if xsize == ysize:
xsize, ysize = THUMBNAIL_SIZE, THUMBNAIL_SIZE
else:
factor = ysize / float(THUMBNAIL_SIZE)
xsize, ysize = int(xsize / factor), int(ysize / factor)
if xsize > THUMBNAIL_MAX_WIDTH:
factor = xsize / float(THUMBNAIL_MAX_WIDTH)
xsize, ysize = int(xsize / factor), int(ysize / factor)
img.Rescale(xsize, ysize, wx.IMAGE_QUALITY_HIGH)
# Swap red and blue channels, if we aren't using 1.6.0 or higher.
if not hasattr(Texture, "getRamImageAs"):
for x in xrange(xsize):
for y in xrange(ysize):
img.SetRGB(x, y, img.GetBlue(x, y), img.GetGreen(x, y), img.GetRed(x, y))
img = img.Mirror(False) # Downside up.
return wx.BitmapFromImage(img)