Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Raw pixel storage 1

Status
Not open for further replies.

fabien

Technical User
Sep 25, 2001
299
AU
Hi!

Image files in raw format can have different pixel storage length (1bit 8bit 16bit, unsigned 8bit etc) WHat is the best way to determine in C++ the type from the file?

Thanks!
 
That depends on the file type. You would have to consult the file format specification to find out where the colour-depth information is stored.
 
After some searches I found out I could use the formula: filesize = ((xdim*ydim*zdim)*(datatype/8))+ offset Now I need a way to determine the filesize: what is the best way?

THanks!
 
man 2 stat
stat returns a structure containing lots of useful information about a file.

Or if you want something really portable, then
fopen(), fseek() to end of file, ftell() to get the offset (or file size) and fclose() also works.

--
 
THanks Salem,

Something like:
Code:
// obtaining file size
#include <iostream.h>
#include <fstream.h>

const char * filename = "example.txt";

int main () {
  long l,m;
  ifstream file (filename, ios::in|ios::binary);
  l = file.tellg();
  file.seekg (0, ios::end);
  m = file.tellg();
  file.close();
  cout << "size of " << filename;
  cout << " is " << (m-l) << " bytes.\n";
  return 0;
}
 
There are various encoding schemes used to save on storage. For instance, in 16-colour bmp format, they have the width, height and colour encodings at the start of the file and then only use 4 bits per pixel for the picture. Similar scheme is used for 256 colour.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top