Per i BMP è facile...
Ecco un esempio che visualizza l'header di un file .bmp :
codice:
#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
using namespace std;
void showchars(long int dati);
void main()
{
// Chiede il nome del file
string fname;
cout << "Nome del file .bmp da aprire? ";
cin >> fname;
// Apre il file e controlla se è valido
ifstream fin;
fin.open(fname.c_str(),ios::in | ios::binary);
if(!fin)
{
cout << "Mi dispiace, ma il file non sembra essere valido!"<<endl;
return;
}
BITMAPFILEHEADER bmfh;
BITMAPINFOHEADER bmih;
fin.read((char *) &bmfh, sizeof(BITMAPFILEHEADER));
fin.read((char *) &bmih, sizeof(BITMAPINFOHEADER));
// Dice cosa fa
cout << "Informazioni sul file BMP:" << endl;
// Lunghezza header file
cout << "Lunghezza header file:\t" << sizeof(BITMAPFILEHEADER) << endl;
// Tipo file
cout << "Tipo:\t"; showchars(bmfh.bfType); cout << endl;
// Dimensioni file
cout << "Dimensioni:\t" << bmfh.bfSize << endl;
// Informazioni riservate
cout << "Informazioni riservate 1:\t" << bmfh.bfReserved1 << endl;
cout << "Informazioni riservate 2:\t" << bmfh.bfReserved2 << endl;
// Bytes di offset
cout << "Bytes di offset:\t" << bmfh.bfOffBits << endl;
cout << endl;
// Lunchezza header informazioni
cout << "Lunghezza header informazioni:\t" << sizeof(BITMAPINFOHEADER) << " = " << bmih.biSize << endl;
// Larghezza e altezza immagine
cout << "Larghezza file in pixel:\t" << bmih.biWidth << endl;
cout << "Altezza file in pixel:\t" << bmih.biHeight << endl;
// Piani
cout << "Piani:\t" << bmih.biPlanes << endl;
// Bit per pixel
cout << "Bit per pixel:\t" << bmih.biBitCount << endl;
// Compressione
switch(bmih.biCompression) {
case BI_RGB: cout << "Non ha compressione." << endl; break;
case BI_RLE8: cout << "Compressione per file a 8 bit per pixel" << endl; break;
case BI_RLE4: cout << "Compressione per file a 4 bit per pixel" << endl; break;
};
// Grandezza compressa facoltativa
if(bmih.biSizeImage != 0) cout << "Grandezza immagine compressa:\t" << bmih.biSizeImage << endl;
// Risoluzione
cout << "Risoluzione orrizzontale (pixel per metro):\t" << bmih.biXPelsPerMeter << endl;
cout << "Risoluzione verticale (pixel per metro):\t" << bmih.biYPelsPerMeter << endl;
cout << "Colori palette:\t" << bmih.biClrUsed << endl;
cout << "Colori palette importanti:\t" << bmih.biClrImportant << endl;
}
void showchars(long int dati)
{
int i;
for(i=0; i<4; i++)
{
cout << char(dati);
dati >>= 8;
}
}
Per ulteriori info sul formato, se macini l'inglese:
http://www.wotsit.org/