Home > Net >  Working with palette / indexed images in ImageSharp
Working with palette / indexed images in ImageSharp

Time:11-29

for a legacy game I need to load an image in BMP (or PNG) paletted format and change certain well-known indices on the color palette (replace e.g. a red gradient using indices 0-8 with shades of green, to change the colors of a unit). It is not sufficient to do a color replacement alone, as the same color might by used by more than one unit feature.

Is it therfore possible to load the paletted image and keep the color indices in ImageSharp? When I load my paletted unit image with Image.Load("FileName"), it is always loaded in Argb32 mode, and the palette is lost. In Photoshop, the same image is correctly loaded in indexed mode. Also, I am not sure which properties I might use to see/edit the palette.

I did not find much information on that topic online. I see that there is a type IndexedImageFrame<TPixel> in the documentation, but I am not sure if/how that type would help with the above issue.

Edit: As James South correctly pointed out, the PNG is loaded in Rgba32 mode by default by ImageSharp.

CodePudding user response:

When I load my paletted unit image with Image.Load("FileName"), it is always loaded in Argb32 mode

Actually that'll be Rgba32 if loading a png (though this is subject to future change). To guarantee a pixel format you should use the generic variant.

In Photoshop, the same image is correctly loaded in indexed mode.

That's not what PhotoShop is doing. They're capturing the palette and displaying that to you. The actual canvas format will be the same for any pixel format.

ImageSharp does the same. The decoded image is a 2D buffer of TPixel but we capture metadata upon decode and use that to inform the encoder what pixel format to save encode the image with.

That said... Currently I'm afraid there is no support for capturing the current palette upon decode. If you want to raise an issue in the GitHub repository it is something we could potentially consider for V2.

If you were had that information to capture then adjusting/saving it would be possible using a PaletteQuantizer with the updated palette during encode of the image.

  • Related