How would you implement / design a class which has to represent a bitmap? I'm stuck at handling the different possible color modes and I keep thinking that this should be somehow implementable using templates / design patterns in an elegant AND efficient way:
Suppose you have a file.bmp. You then want to open that file to use it in your application:
Bitmap myBitmap();
myBitmap.open("file.bmp");
The bitmap you just opened could have RGB colors, or it could be a grayscale image, or even a 1 bit per pixel image (i.e. black and white). In my application, I want to operate on the image (pixel) data. For example, I need some sort of brightness filter where I black out pixels whose (Red + Blue + Green) / 3 is below some filter value (i.e. 100).
So I need access to the pixels:
Pixel somePixel = myBitmap.pixelAt(10,30);
But now I face the problem of NOT knowing what type of pixel this is. Of course, I could provide subclasses of Pixel
(which then is an abstract base class) which ALL implement methods like getRGB()
, getGrayScale()
, ... where each subclass gives a way of converting between those colormodes (i.e. RGB to grayscale)
But this feels ... hm, "wrong".
Do you know some good way to implement this?
A nice feature I was thinking about is something like:
myBitmap.getMode(); // A RGBMode object, RGBMode being a subclass of Mode
myBitmap.setMode(new BlackWhiteMode()); // Converts the bitmap to a 1bpp image
Any ideas, recommendations, improvements?
So the really hard part is to represent the different color modes, like RGB (8bit and 4bit), HSV, Grayscale, BlackWhite, ... without messing the applications code.