Cześć, mam problem z błędnym działaniem programu. Program ma za zadanie wyświetlać dane nagłówka pliku i nagłówka obrazu bitmapy (BITMAPINFOHEADER). Nagłowek pliku odczytuje dobrze ale problem jest z odczytaniem nagłówka obrazu.
#include <iostream>
#include <fstream>
using namespace std;
struct BITMAPFILEHEADER{
char bfType[3];
unsigned int bfSize;
unsigned short int bfReserved1;
unsigned short int bfReserved2;
unsigned long bfOffBits;
};
void odczytajBFH(ifstream & ifs, BITMAPFILEHEADER &bfh)
{
ifs.read(reinterpret_cast<char*> (& bfh.bfType), 2);
ifs.read(reinterpret_cast<char*> (& bfh.bfSize), 4);
ifs.read(reinterpret_cast<char*> (& bfh.bfReserved1), 2);
ifs.read(reinterpret_cast<char*> (& bfh.bfReserved2), 2);
ifs.read(reinterpret_cast<char*> (& bfh.bfOffBits), 2);
cout << "bfType :" << bfh.bfType << endl;
cout << "bfSize (Dlugosc calego pliku w bajtach): " << bfh.bfSize << endl;
cout << "bfReserved1 (Pole zarezerwowane (zwykle zero)): " << bfh.bfReserved1 << endl;
cout << "bfReserved2 (Pole zarezerwowane (zwykle zero)): " << bfh.bfReserved2 << endl;
cout << "bfOffBits (Pozycja danych obrazowych w pliku): " << bfh.bfOffBits << endl;
}
struct BITMAPINFOHEADER{
unsigned int biSize;
long biWidth;
long biHeight;
unsigned short biPlanes;
unsigned short biBitCount;
unsigned int biCompression;
unsigned int biSizeImage;
long biXpelsPerMeter;
long biYpelsPerMeter;
unsigned long biCrlUses;
unsigned long biCrlImportant;
};
void odczytajBIH(ifstream & ifs, BITMAPINFOHEADER &bih)
{
ifs.read(reinterpret_cast<char *> (& bih.biSize), 4);
ifs.read(reinterpret_cast<char *> (& bih.biWidth), 4);
ifs.read(reinterpret_cast<char *> (& bih.biHeight), 4);
ifs.read(reinterpret_cast<char *> (& bih.biPlanes), 2);
ifs.read(reinterpret_cast<char *> (& bih.biBitCount), 2);
ifs.read(reinterpret_cast<char *> (& bih.biCompression), 4);
ifs.read(reinterpret_cast<char *> (& bih.biSizeImage), 4);
ifs.read(reinterpret_cast<char *> (& bih.biXpelsPerMeter), 4);
ifs.read(reinterpret_cast<char *> (& bih.biYpelsPerMeter), 4);
ifs.read(reinterpret_cast<char *> (& bih.biCrlUses), 4 );
ifs.read(reinterpret_cast<char *> (& bih.biCrlImportant), 4);
cout << endl << "BITMAPINFOHEADER: " << endl << endl;
cout << "biSize (Rozmiar naglowka informacyjnego): " << bih.biSize << endl;
cout << "biWidth (Szerokosc obrazu w pikselach): " << bih.biWidth << endl;
cout << "biHeight (Wysokosc obrazu w pikselach): " << bih.biHeight << endl;
cout << "biPlanes (Liczba platow (musi byc 1)): " << bih.biPlanes << endl;
cout << "biBitCount (Liczba bitow na piksel: 1, 4, 8, 16, 24 lub 32): " << bih.biBitCount << endl;
cout << "biCompression (Algorytm kompresji: BI_RGB=0, BI_RLE8=1, BI_RLE4=2, BI_BITFIELDS=3): " << bih.biCompression << endl;
cout << "biSizeImage (Rozmiar rysunku): " << bih.biSizeImage << endl;
cout << "biXpelsPerMeter (Rozdzielczosc pozioma): " << bih.biXpelsPerMeter << endl;
cout << "biYpelsPerMeter (Rozdzielczosc pionowa): " << bih.biYpelsPerMeter << endl;
cout << "biCrlUses (Liczba kolorow w palecie): " << bih.biCrlUses << endl;
cout << "biCrlImportant (Liczba waznych kolorow w palecie): " << bih.biCrlImportant << endl;
}
int main()
{
BITMAPFILEHEADER bfh;
BITMAPINFOHEADER bih;
ifstream ifs("YEL.bmp", ios::binary);
if(!ifs) cout << "Nie wczytano pliku!";
if(ifs)
{
odczytajBFH(ifs, bfh);
odczytajBIH(ifs, bih);
}
return 0;
}