views:

288

answers:

3

Ive got some in memory images in various simple formats, which I need to quickly convert to another format. In cases where the target format contains an alpha channel but the source does not, alpha should be taken as its full value (eg 0xFF for an 8bit target alpha channel).

I need to be able to deal with various formats, such as A8R8G8B8, R8G8B8A8, A1R4G4B4, R5G6B5, etc.

Conversion of pixels between any of these formats is simple, however I don't want to have to manually code every single combination, I also don't want to have a 2 pass solution of converting to a common format (eg A8R8G8B8) before converting again to the final format both for performance reasons, and that if I want to use a higher definition format, say A16B16G16R16 or a floating point I'd either loose some data converting to the intermediate format, or have to rewrite everything to use a different higher definition format...

Ideally id like some sort of enum with values for each format, and then a single method to convert, eg say

enum ImageFormat
{
    FMT_A8R8G8B8,//32bit
    FMT_A1R4G4B4,//16bit
    FMT_R5G6B5,//16bit
    FMT_A32R32G32B32F,//128bit floating point format
    ...
};
struct Image
{
    ImageFormat Format;
    size_t Width, Height;
    size_t Pitch;
    void *Data;
};
Image *ConvertImage(ImageFormat *targetFormat, const Image *sourceImage);
+3  A: 

Take a look at FreeImage; it's a library that will convert between various images.

You can also try ImageMagick to just convert back and forth, if you don't want to do anything to them.

mmr
Definitely agree about FreeImage - great library for most formats
zebrabox
I use FreeImage for all offline pre-processing, though I don't think it supports all the formats you want as well as 1-pass only. I actually think you will have to do it by hand, especially if performance matters (which it usually does).
Jim Buck
A: 

10 years ago I used the hermes pixel conversion library, which was very fast. It was possible to convert 640x480 32bit images to 15 or 16 bit images with at least 30 pics per second. We used this for a demo engine. Unfortunately I cannot find a link at the moment. On debian the package is orphaned..

But this is exactly what you want to use for real time usage..

Peter Parker
+3  A: 

You might want boost::gil.

TokenMacGuy