School-Coding-Cpp/sfusi/tuttaBianca.cpp

80 lines
1.8 KiB
C++

/*
Nome: Mario
Cognome: Montanari
Classe: 3AIN
Data: 30/04/2025
Leggere:
- 2 caratteri all'offset 0
- dimensione_del_file = 1 int a partire dall'offset 2
- start = 1 int a partire dall'offset 0Ah o 10d (start sarà 54)
- 400 pixel costituiti da 3 byte ciascuno (BGR) a partire da start
- creare una struct di 3 unsigned char BGR per contenere i singoli pixel
- memorizzare i pixel in un array e stamparlo
Scrivere un file .bmp di 400 pixel (20x20) casuali
Usare hexed.it per visualizzare l'immagine .bmp
1) Apri la Calcolatrice di Windows
2) Visualizza
3) Programmatore
*/
#include <iostream>
#include <iomanip>
#pragma pack(1)
using namespace std;
typedef struct {
char bfType[2];
int bfSize;
int bfReserved;
int bfOffBits;
} BITMAPFILEHEADER;
typedef struct {
unsigned char b;
unsigned char g;
unsigned char r;
} BGR;
int main(void) {
cout << "sizeof(BITMAPFILEHEADER): " << sizeof(BITMAPFILEHEADER) << endl;
FILE * file = fopen("tuttaBianca.bmp", "rb");
if (file != NULL) {
int bfOffBits;
BITMAPFILEHEADER bf;
fseek(file, 0, SEEK_SET);
fread(&bf, sizeof(bf), 1, file);
cout << "bfType: " << bf.bfType[0] << bf.bfType[1] << endl;
fseek(file, 2, SEEK_SET);
cout << "bfSize: " << bf.bfSize << endl;
fseek(file, 10, SEEK_SET);
cout << "bfOffBits: " << bf.bfOffBits << endl << endl;
BGR bgr[400];
fseek(file, bf.bfOffBits, SEEK_SET);
for (int i = 0; i < 400; i++) {
fread(&bgr[i], sizeof(BGR), 1, file);
cout << "Pixel: " << setw(3) << i + 1 <<
" - B: " << setw(3) << (int)bgr[i].b <<
" G: " << setw(3) << (int)bgr[i].g <<
" R: " << (int)bgr[i].r << endl;
}
fclose(file);
} else {
perror("Error (source)");
}
return 0;
}