Create CImg<uint8_t> from byte array?

1.6k Views Asked by At

I'm trying to modify a C++ library that has a function that creates a CImg instance from an image file, to use a byte array instead. Is this possible? I found one method that appears to allow it...

CImg  ( const t *const  values,  
  const unsigned int  size_x,  
  const unsigned int  size_y = 1,  
  const unsigned int  size_z = 1,  
  const unsigned int  size_c = 1,  
  const bool  is_shared = false  
) 

...but since all I have is the byte array, I don't have the dimensions of the source image.

UPDATED TO ADDRESS COMMENTS This is an attempt to make a modification to the pHash library, which uses the CImg class as defined here http://cimg.sourceforge.net/reference/structcimg__library_1_1CImg.html

The byte array is populated through an http request for the source image.

1

There are 1 best solutions below

0
On

If your byte array contains a copy of the entire image file (not just the bitmap portion), then you can read the dimensions from the header.

See BITMAPFILEHEADER and BITMAPINFOHEADER (of course, for other formats such as PNG or JPEG, you'll need the corresponding headers).

For example, this will let you view an image from the network or your application resource segment, without first writing it to disk.