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

3
  • Have you looked at this answer here? It seems to be something of the same context. stackoverflow.com/a/5354657/1484168
    – iRebel_85
    Mar 25, 2014 at 15:16
  • @stefan yes it was meant to be a comment. thanks for the conversion
    – iRebel_85
    Mar 25, 2014 at 15:28
  • thanks @iRebel_85, this helped with the implementation
    – Lukas
    Mar 25, 2014 at 17:16

1 Answer 1

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.

1
  • I forgot to add the binary flag when opening the file. Corrected version is here pastebin.com/uR8T5wYf
    – Lukas
    Apr 4, 2014 at 9:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.