tags:

views:

417

answers:

3

C# question (.net 3.5). I have a class, ImageData, that has a field ushort[,] pixels. I am dealing with proprietary image formats. The ImageData class takes a file location in the constructor, then switches on file extension to determine how to decode. In several of the image files, there is a "bit depth" field in the header. After I decode the header I read the pixel values into the "pixels" array. So far I have not had more than 16bpp, so I'm okay. But what if I have 32bpp?

What I want to do is have the type of pixels be determined at runtime. I want to do this after I read the bit depth out of the header and before I copy the pixel data into memory. Any ideas?

+2  A: 

I would say not to do that work in the construtor - A constructor should not do so much work, in my opinion. Use a factory method that reads the file to determine the bit depth, then have it construct the correct generic variant of the class and return it.

Colin Mackay
A: 

Have your decode function return an object of type Array, which is the base class of all arrays. Then people who care about the type can do "if (a is ushort[,])" and so on if they want to go through the pixels. If you do it this way, you need to allocate the array in ImageData, not the other way around.

Alternatively, the caller probably knows what kind of pixel array they want you to use. Even if it's an 8bpp or 16bpp image, if you're decoding it to a 32bpp screen, you need to use uint instead of ushort. So you could write an ImageData function that will decode into integers of whatever type T is.

The root of your problem is that you don't know how to decide what kind of output format you want. You need to figure that out first, and the program syntax comes second.

apenwarr
+1  A: 

To boil down your problem, you want to be able to have a class that has a ushort[,] pixels field (16-bits per pixel) sometimes and a uint32[,] pixels field (32-bits per pixel) some other times. There are a couple different ways to achieve this.

You could create replacements for ushort / uint32 by making a Pixel class with 32-bit and 16-bit sub-classes, overriding various operators up the wazoo, but this incurs a lot of overhead, is tricky to get right and even trickier to determine if its right. Alternately you could create proxy classes for your pixel data (which would contain the ushort[,] or uint32[,] arrays and would have all the necessary accessors to be useful). The downside there is that you would likely end up with a lot of special case code in the ImageData class which executed one way or the other depending on some 16-bit/32-bit mode flag.

The better solution, I think, would be to sub-class ImageData into 16-bit and 32-bit classes, and use a factory method to create instances. E.g. ImageData is the base class, ImageData16bpp and ImageData32bpp are sub-classes, static method ImageData.Create(string imageFilename) is the factory method which creates either ImageData16bpp or ImageData32bpp depending on the header data. For example:

public static ImageData Create(string imageFilename)
{
   // ...
   ImageDataHeader imageHeader = ParseHeader(imageFilename);
   ImageData newImageData;
   if (imageHeader.bpp == 32)
   {
      newImageData = new ImageData32(imageFilename, imageHeader);
   }
   else
   {
      newImageData = new ImageData16(imageFilename, imageHeader);
   }
   // ...
   return newImageData;
}
Wedge