Dave's Brain

Browse - Programming Tips - How can I get the width and height (dimensions) of a Targa (.TGA) file?

Date: 2009may14
Language: C/C++
Keywords: graphics

Q.  How can I get the width and height (dimensions) of a Targa (.TGA) file?

A.  Here's a simple routine that does it:

// Helper routine inline size_t Read(FILE *f, void *buf, const size_t size) { return fread(buf, 1, size, f); } // Helper routine inline BOOL GetChunk(FILE *f, void *buf, const size_t size) { return Read(f, buf, size) == size; } // Helper routine BOOL GetWordIntel(FILE *f, WORD &w) { BYTE buf[2]; if (!GetChunk(f, buf, sizeof(buf))) return FALSE; w = (buf[1]<<8) | buf[0]; return TRUE; } BOOL TargaDim(LPCSTR szFile, int *pWidth, int *pHeight) { FILE *f; WORD wWidth, wHeight; if (f = fopen(szFile, "rb")) == NULL) return FALSE; if (fseek(f, 12, SEEK_SET) != 0) { fclose(f); return FALSE; } if (!GetWordIntel(f, wWidth)) { fclose(f); return FALSE; } if (!GetWordIntel(f, wHeight)) { fclose(f); return FALSE; } fclose(f); if (pWidth != NULL) *pWidth = wWidth; if (pHeight != NULL) *pHeight = wHeight; return TRUE; } // Example Use main() { int width, height; TargaDim("myfile.tga", &width, &height); printf("width=%d, height=%d\n", width, height); }

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.