PNM Image: use palettized images

Hi,

Is there a way to use images with indexed colors (palettes) with Panda3D?

To be more precise, here’s what I want to do:
I have a texture composition pipeline, where multiple sub images are blended together (on the CPU) to create a single RGBA image, which then is used as texture for the GPU.
Currently I do this using PNM images. The code is roughly like this:

# self.baseImage is the target PNM
# blendInfos holds tuples of (blendImage, region)
# region describes the area of the blend
# blendImage is a PNM I want to blend in

for blendImage, region in blendInfos:
    self.baseImage.blendSubImage(blendImage, region.xPos, region.yPos, 0,0, region.xSize, region.ySize)

# move final image into a gpu Texture
self.baseTexture.load(self.baseImage)

Now, I’d like some of the source images to be palette images. I’d like to have multiple palettes which should be used during image blending to turn the index image into RGB or RGBA.
Something like this (pseudo-code):

# self.baseImage is the target PNM (using full RGBA color resolution)
# blendInfos holds tuples of (blendImage, palette, region), palette may be None
# region describes the area of the blend
# blendImage is a PNM I want to blend in

for blendImage, palette, region in blendInfos:
    if palette:
        # blend, using the palette
        self.baseImage.blendPalettizedSubImage(blendImage, palette, region.xPos, region.yPos, 0,0, region.xSize, region.ySize)
    else:
        # do a "regular" blend
        self.baseImage.blendSubImage(blendImage, region.xPos, region.yPos, 0,0, region.xSize, region.ySize)

# move final image into a gpu Texture
self.baseTexture.load(self.baseImage)

Is there a way to achieve this behavior with PNM Images or some other part of Panda3D?
Of course I could use the PIL for the paletted images, but I’d rather not bring in another library.
If worse comes to worse I’m prepared to modify the PNM library to include what I need.

Cneers,

Erik

Sorry, the PNMImage code doesn’t support palettized images. It will automatically inflate palettized images into RGBA on read, and reverse the process on write. I’m not 100% convinced we want it to be otherwise; one of the advantages of the PNMImage class is that you don’t have to know much about the structure of the image you have just read; you simply start examining pixels. If it made a distinction between palettized and unpalettized images, then you would have to know what kind of image you were traversing.

David

Erik, i do a similar thing in my mmorts game. I have ships and each ship has its texture and then its stripe/team color area. Then i just color it appropriately in the shader with shaders you can do all kinds of blending - it comes out a win because i have sooo many textures all the combinations that i will use will be prohibitive - also very low start up time and limited impact on speed.

Hi,

Yes of course to use a shader would be the obvious solution to the problem, but because I’m targeting a really low-spec platform we don’t use any shaders.

Also, I’m deliberately doing this texture combining on the CPU because it’s a unique texture per player consisting of very many layers that only needs to be calculated once.

hmm… I still have to figure out how to solve this problem most elegantly…

Erik

maybe you can use offscreen buffers to create the neccessary images. even very very old graficcards can handle at lest 2 texture laysers. so you can piecewise puzzle them together one by one and save it out again. check the texture-blend-modes in the panda manual.

in case you start modifiing the pnmimage lib… can you add a simple add/substract operation,too?

If you have to do that i recommend using some thing like PIL and just creating your images in that it has a nice API and can do lots of things like working with indexed color images. Graphics cards them selfs cant draw indexed color images so they always will be converted.