1

I'm trying to use the FreeImage.Net library to open the image and convert it to a standard bitmap, but either there must be a step I'm missing, or FreeImage simply can not convert floating point RGBA to 32BPP RGBA:

FreeImageBitmap fib = new FreeImageBitmap("C:\\test.exr");
Console.WriteLine(fib.ImageType); //FIT_RGBAF   
fib.ConvertType(FREE_IMAGE_TYPE.FIT_BITMAP, true); //returns False

Conversion fails:

Can not convert from type 12 to type 1; No such conversion exists.

I need this as a 32BPP RGBA Bitmap solely for the purpose of being able to convert it into a System.Drawing.Bitmap, so I may draw it using GDI+. I'm willing to use a different image library if necessary, or to consider WPF over Winforms if anyone thinks a solution will be found there.

I've uploaded an example floating point EXR for testing: http://dl.dropbox.com/u/2817180/test.exr

Rotem
  • 21,452
  • 6
  • 62
  • 109
  • 1
    well you could always just read data from one bitmap and write it to the other... – neeKo Jan 03 '12 at 20:12
  • FreeImage.Net doesn't support this conversion - see [documentation](http://ignum.dl.sourceforge.net/project/freeimage/Source%20Documentation/3.15.1/FreeImage3151.pdf) (page 36) – nirbruner Jan 03 '12 at 20:59
  • It would help if you uploaded an example of the bitmap file you need to parse. – Peter O. Jan 03 '12 at 23:13
  • @Niko : That's a possible last resort, but I feel there should be a faster way. – Rotem Jan 04 '12 at 08:50
  • @nirbruner : Yes I see, but I can't imagine why that would conversion would be so hard. – Rotem Jan 04 '12 at 08:50
  • @Peter : I've uploaded an example EXR file for testing. – Rotem Jan 04 '12 at 08:51

1 Answers1

2

AS Niko said, you should do it manually. Using unsafe code, and pointers that would PIN your memory buffers and make it unmovable, your routine will be both fast in execution and easy to write.

More so if you are having just one source and one target format.

Daniel Mošmondor
  • 19,718
  • 12
  • 58
  • 99
  • Would you say it is correct to assume that the unsafe pointer based code would run as fast as an unmanaged C++ implementation of the same procedure? – Rotem Jan 04 '12 at 09:15
  • 1
    Not as fast. If you are able to code it in c++ and call it via `static extern` that would be winner. – Daniel Mošmondor Jan 04 '12 at 09:17
  • 1
    I did just as you suggested, and considering it's my first C++ code, the result is decent. The C++ code executes roughly 40% faster than the equivalent unsafe C# code. I'll wait another day before awarding the answer just to give chance to other potential answers. – Rotem Jan 05 '12 at 12:30
  • Thank you, I'm glad it worked for you, and thanks for sharing the result. – Daniel Mošmondor Jan 05 '12 at 14:55
  • Not only is it working now, it's working great! parallelFor is a beast! :) I can convert a 800*600 image in ~5 milliseconds. Thanks for your help. – Rotem Jan 06 '12 at 07:30
  • BTW, if you want (or need) to squeeze even more from it, look into ASM and streaming instructions. – Daniel Mošmondor Jan 06 '12 at 11:18
  • Actually the only thing that's making it slow right now is calling std::pow on every float value (for gamma correction). It's taking up 75% of total execution time, but from what I understand, it's already implemented using SSE2 instructions. Is there anything I could do about it? Should I open a separate question? – Rotem Jan 06 '12 at 15:16
  • Get rid of the pow - use pre-calculated table for that! – Daniel Mošmondor Jan 06 '12 at 16:51
  • I've thought about that, but how does one generate a lookup table for an infinite range of values? – Rotem Jan 06 '12 at 18:22
  • You won't have lookup at FLOAT side, but on the destination, BYTE side... ;-) – Daniel Mošmondor Jan 06 '12 at 18:23
  • A byte side lookup won't help me much as I'd lose the floating point accuracy (and in this case it matters). I'm trying to use an interpolated lookup table up to a 'reasonable' input value, and the rare values outside that range will use std::pow. Thanks again for all your help. – Rotem Jan 06 '12 at 18:38
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/6488/discussion-between-rotem-and-daniel-mosmondor) – Rotem Jan 06 '12 at 18:45
  • @Rotem cuold you lease post the full answer for your question? I am having the same question here: http://stackoverflow.com/questions/8694739/convert-floating-point-rgba-bitmap-to-standard-dotnet-bitmap – olidev Jul 15 '12 at 11:03