4

I'm using the FreeImage Library to handle loading images and passing them to OpenGL. Currently, I have support for reading an image from a file. However, I'd like to extend this to being able to read from a variable for the purpose of game content packages. Basically, in short, I have the entire file, header and all, written to an unsigned char *. I want to take this buffer and do something similar to this with it (assume variable fif is declared):

  FIBITMAP * dib = FreeImage_Load(fif, "someImage.png");

but instead of someImage.png, I want to specify the unsigned char * (lets call it buffer for the sake of the question). Is there a method in the library that can handle such a thing?

EDIT: I should be a bit more specific considering "someImage.png" can be considered an unsigned char *.

To clear confusion up, if any, the value of unsigned char * buffer would be determined by something like this psuedo code:

  fileStream = openFile "someImage.png"
  unsigned char * buffer = fileStream.ReadToEnd
MGZero
  • 5,812
  • 5
  • 29
  • 46

2 Answers2

6

It sounds like you want to use FreeImage_ConvertToRawBits. Here is an example from the documentation:

   // this code assumes there is a bitmap loaded and
   // present in a variable called ‘dib’
   // convert a bitmap to a 32-bit raw buffer (top-left pixel first)
   // --------------------------------------------------------------
   FIBITMAP *src = FreeImage_ConvertTo32Bits(dib);
   FreeImage_Unload(dib);
   // Allocate a raw buffer
   int width = FreeImage_GetWidth(src);
   int height = FreeImage_GetHeight(src);
   int scan_width = FreeImage_GetPitch(src);
   BYTE *bits = (BYTE*)malloc(height * scan_width);
   // convert the bitmap to raw bits (top-left pixel first)
   FreeImage_ConvertToRawBits(bits, src, scan_width, 32, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK, TRUE);
   FreeImage_Unload(src);
   // convert a 32-bit raw buffer (top-left pixel first) to a FIBITMAP
   // ----------------------------------------------------------------
   FIBITMAP *dst = FreeImage_ConvertFromRawBits(bits, width, height, scan_width, 32, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK, FALSE);
Mikola
  • 9,176
  • 2
  • 34
  • 41
  • This might be what I need, I'm going to give a try. +1! – MGZero Jun 24 '11 at 03:19
  • Sorry, but could you explain what the pitch parameter does exactly? I don't quite understand it from the documentation, probably because I don't really know what a scanline is. – MGZero Jun 24 '11 at 13:04
  • Pitch is the number of bytes per row of the image. It is typically padded to be a bit larger than the width of the image for alignment and performance reasons. If your image is tightly packed, it will just be width*bytes per pixel. – Mikola Jun 24 '11 at 15:44
0

Use FreeImage_LoadFromHandle. You'll need to create 3 functions that can access the buffer, and put the addresses of those functions in a FreeImageIO structure.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622