School-Coding-Cpp/sfusi/file_bmp.cpp

57 lines
1.1 KiB
C++

/*
Nome: Mario
Cognome: Montanari
Classe: 3AIN
Data: 28/04/2025
*/
#include <iostream>
#pragma pack(1)
using namespace std;
typedef struct {
char bfType[2];
int bfSize;
int bfReserved;
int bfOffBits;
} BITMAPFILEHEADER;
int main(void) {
cout << "sizeof(BITMAPFILEHEADER): " << sizeof(BITMAPFILEHEADER) << endl;
FILE * file = fopen("carpet.bmp", "rb");
if (file != NULL) {
int bfOffBits;
BITMAPFILEHEADER bf;
fread(&bf, sizeof(bf), 1, file);
// VANNO BENE QUESTI DUE METODI:
// METODO 1
cout << "bfType: " << bf.bfType[0] << bf.bfType[1] << endl;
cout << "bfSize: " << bf.bfSize << endl;
cout << "bfReserved: " << bf.bfReserved << endl;
cout << "bfOffBits: " << bf.bfOffBits << endl;
// METODO 2
// fseek(file, 10, SEEK_SET);
// fread(&bfOffBits, sizeof(bfOffBits), 1, file);
unsigned char rgb[3];
fseek(file, bfOffBits, SEEK_SET);
fread(&rgb, sizeof(rgb), 1, file);
cout << "B: " << (int)rgb[0] << " G: " << (int)rgb[1] << " R: " << (int)rgb[2] << endl;
fclose(file);
} else {
perror("Error (source)");
}
return 0;
}