views:

694

answers:

4

I'm loading a Bitmap from a jpg file. If the image is not 24bit RGB, I'd like to convert it. The conversion should be fairly fast. The images I'm loading are up to huge (9000*9000 pixel with a compressed size of 40-50MB). How can this be done?

Btw: I don't want to use any external libraries if possible. But if you know of an open source utility class performing the most common imaging tasks, I'd be happy to hear about it. Thanks in advance.

A: 

This isn't something I've tried myself, but I think you might need to acccess the picture's EXIF information as a start.

Check out Scott Hanselman's blog-entry on accessing EXIF information from pictures.

pavsaund
Exif-Data is just plain optional meta-data associated with an image. Modifying Exif-Data doesn't change anything on the image. But anyway thanks for your answer.
Matthias
A: 

Standard .NET System.Drawing namespace should have all that you need, but it probably won't be very efficient. It'll load the whole thing into RAM, uncompress it, convert it (probably by making a copy) and then re-compress and save it. If you aim for high performance, I'm afraid you might need to look into C/C++ libraries and make .NET wrappers for them.

Vilx-
A: 

As far as I know jpg is always 24 bpp. The only thing that could change would be that it's CMY(K?) rather then RGB. That information would be stored in the header. Unfortunately I don't have any means of creating a CMYK image to test whether loading into a Bitmap will convert it automatically.

The following line will read the file into memory:

Bitmap image = Image.FromFile(fileName);

image.PixelFormat will tell you the image format. However, I can't test what the file load does with files other than 24bpp RGB jpgs. I can only recommend that you try it out.

ChrisF
I believe, that jpg can come in 24 and 8 bit depth, which is a grayscale image then.
Matthias
Ah - didn't remember about the 8 bit depth, but I should have done as I had to deal with texture images in all sorts of formats in a previous job. Again though, won't the Bitmap load from file do the conversion for you?
ChrisF
I don't know how to test whether it is 24 bit or not ;) I hoped to have asked this implicitly with my question: How do I detect whether the image is 24bit?
Matthias
+2  A: 

The jpeg should start with 0xFF 0xD8. After that you will find various fields in the format:

  1. Field identifier 2 bytes
  2. Field length, excluding field identifier. 2 bytes.
  3. Variable data.

Parse through the fields. The identifier you will be looking for is 0xFF 0xC0. This is called SOF0, and contains height, width, bit depth, etc. 0xFF 0xC0 will be followed by two bytes for the field length. Immediately following that will be a single byte showing the bit depth, which will usually be 8. Then there will be two bytes for height, two for width, and a single byte for the number of components; this will usually be 1 (for greyscale) or 3. (for color)

R Ubben