How to convert jpg image to raw 32 bit float

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Post Reply
russianbear

How to convert jpg image to raw 32 bit float

Post by russianbear »

Hello.
I am trying to convert a jpg image to raw format, specifically RGBA 32 bit float.
I am using Magick++ btw...

Here is what I am doing so far...

Code: Select all

int _tmain(int argc, _TCHAR* argv[]) {

   Geometry g(800, 533);
   Image theI;
   try {
      theI.read(g, "./bluejay.jpg");
      theI.depth(32);
      theI.write("./bluejay32int.procd.rgba");
   }//end try block
   catch(Exception& ex) {
      cout << "imagicktstr,error," << ex.what() << endl;
   }//end catch block
   return 0;
}//end main
This code compiles and successfully outputs a raw image; however, it is still integer!

How do represent the pixels in "float" format????
I noticed that using the command line interface you can use the convert and define options...but I don't see the corresponding APIs for Magick++. :?

I've been looking at the Image.write method, specifically where I can specify a pixel buffer as the fifth paramter and the type...but I'm still not sure if that is proper way to do this.

Any sort of help would be much appreciated! :)
russianbear

Re: How to convert jpg image to raw 32 bit float

Post by russianbear »

Okay! I got it working, so I'm answering my own question.
My original bluejay.jpg file that I am reading in is 1,666KB big and I know that if I do this right, my file should be 4x as big (RGBA).
So....

Code: Select all

int _tmain(int argc, _TCHAR* argv[]) {
   Geometry g(800, 533);
   Image theI;
   FILE* fh;
   float *floatPixelMap;
   try {
      theI.read(g, "./bluejay.jpg");
      int height = theI.rows();
      int width = theI.columns();
      int size = height * width * 4;

      PixelPacket* myMap = theI.getPixels(0, 0, width, height);
      floatPixelMap = new float[size];

      for (int counter = 0, k=0; counter < size; counter++, ++k) {
         floatPixelMap[counter++]  = (float)myMap[k].red      / 255.0f;
         floatPixelMap[counter++]  = (float)myMap[k].green    / 255.0f;
         floatPixelMap[counter++]  = (float)myMap[k].blue     / 255.0f;
         floatPixelMap[counter]    = (float)myMap[k].opacity  / 255.0f;
      }//end for loop
      fh = fopen("./bluejay32float.procd.rgba", "wb");
      fwrite((void *)floatPixelMap, sizeof(float), size, fh);
   }//end try block

   catch(Exception& ex) {
      cout << "imagicktstr,error," << ex.what() << endl;
   }//end catch block
   return 0;
}//end main
This works...bluejay32float.procd.rgba is 6,663KB big
Just in case anybody needs to do something like this, I don't want them to have to waste an entire day like I did. :lol:
Post Reply