PaletteMap is fast

August 13th, 2007

This is one of the tricks to my Doom engine. Doom has a 256 color palette, and all colors in the game come from this palette. When an index is found for a pixel, instead of converting it to an rgb value using the palette, write it directly to the image itself. In other words, the last 8 bits (the “blue” section) of every color on the bitmapData should have an index, rather than an actual blue value.

bitmapData.setPixel(x, y, index);

After all processing is complete, you can use paletteMap to convert these indexes for you. PaletteMap uses the value of each byte of an image color (four total) to come up with a new color. See the documentation for paletteMap for more details. Provide an array that converts indexes to rgb values for blue and Flash will do the work for you. If you pass arrays full of zeros for red, green, and alpha, then these values of the associated bytes will have no effect on the resulting color.

bitmapData.paletteMap(bitmapData, bitmapData.rect, new Point(0,0), _blank, _blank, _index2rgb, _blank);

PaletteMap is very fast, which is why this offers a nice improvement in speed. Passing null to a section instead of an array makes it slightly slower on my computer, so that should probably be avoided.

2 Responses to “PaletteMap is fast”

  1. admin Says:

    Sweet, any idea how much faster it is ?

  2. Max Says:

    Hard to tell, but despite adding features that slowed down the engine, adding palettemap saved me about 5ms per frame. In an engine like this, that’s a lifetime.

    This fellow claims it doubled the speed of his engine.

Leave a Reply