0

I am looking for a way to get the width and height of an image on the disk without loading the whole image into memory.

I need to support BMP and PNG images; TGA would be nice too.

I am already using DirectX11, DirectX Tool Kit, CxImage and the Boost library, so a way to do it with one of these libraries would be great. However, I am also willing to use another library

Lukas
  • 886
  • 2
  • 7
  • 14

1 Answers1

5

For BMP, read in bytes 18 through 21 to get the width, and bytes 22 though 25 to get the height.

For PNG, read in the dimensions from the file's IHDR chunk, which looks to start after the first eight bytes of the file, and the eight bytes of the header of the IHDR chunk itself. Bytes 16 through 19 would give the width, and bytes 20 through 23 give the height.

Likewise, for TGA, you could read the first 18 bytes (with advised notes) into a TGA header struct and then dereference the struct instance's width and height properties.

Within C++, you can use standard C I/O (e.g., fopen() fseek() and fread()) to open, move and read bytes from a file pointer. You wouldn't have to read in the entire file: just move to the correct byte offset and then read in the correct number of bytes.

Alex Reynolds
  • 95,983
  • 54
  • 240
  • 345
  • I forgot to add the binary flag when opening the file. Corrected version is here http://pastebin.com/uR8T5wYf – Lukas Apr 04 '14 at 09:31