99 lines
2.5 KiB
C++
99 lines
2.5 KiB
C++
/*
|
|
Nome: Mario
|
|
Cognome: Montanari
|
|
Classe: 3AIN
|
|
Data: 02/05/2025
|
|
|
|
Metodo 2 per la generazione di un immagine .bmp (specifico le caratteristiche
|
|
dell'immagine .bmp).
|
|
*/
|
|
|
|
/*
|
|
Nome: Mario
|
|
Cognome: Montanari
|
|
Classe: 3AIN
|
|
Data: 02/05/2025
|
|
|
|
Metodo 2 per la generazione di un'immagine .bmp (specifico le caratteristiche
|
|
dell'immagine .bmp).
|
|
*/
|
|
|
|
#include <iostream>
|
|
|
|
using namespace std;
|
|
|
|
#pragma pack(1)
|
|
|
|
typedef struct {
|
|
char bfType[2] = {'B', 'M'};
|
|
int bfSize;
|
|
int bfReserved = 0;
|
|
int bfOffBits = 54;
|
|
} BITMAPFILEHEADER;
|
|
|
|
typedef struct {
|
|
unsigned char b;
|
|
unsigned char g;
|
|
unsigned char r;
|
|
} BGR;
|
|
|
|
int main(void) {
|
|
cout << "Size of BITMAPFILEHEADER: " << sizeof(BITMAPFILEHEADER) << endl;
|
|
|
|
FILE *file = fopen("immagine_generata_metodo_2.bmp", "wb");
|
|
|
|
if (file != NULL) {
|
|
BITMAPFILEHEADER bf;
|
|
|
|
int altezza = 20;
|
|
int lunghezza = 20;
|
|
|
|
int headerSize = 54; // 14 bytes di file header + 40 bytes di info header
|
|
int pixelDataSize = altezza * lunghezza * sizeof(BGR);
|
|
|
|
bf.bfSize = headerSize + pixelDataSize; // Dimensione totale del file
|
|
|
|
cout << "bfType: " << bf.bfType[0] << bf.bfType[1] << endl;
|
|
cout << "bfSize: " << bf.bfSize << endl;
|
|
cout << "bfReserved: " << bf.bfReserved << endl;
|
|
cout << "bfOffBits: " << bf.bfOffBits << endl;
|
|
|
|
// Scrittura del BITMAPFILEHEADER
|
|
if (fwrite(&bf, sizeof(bf), 1, file) < 1) {
|
|
cout << "Errore di scrittura." << endl;
|
|
fclose(file);
|
|
return 1;
|
|
}
|
|
|
|
// Scrittura corretta del BITMAPINFOHEADER
|
|
int infoHeader[10] = {
|
|
40, lunghezza, altezza, 1 | (24 << 16), 0, pixelDataSize, 0, 0, 0, 0
|
|
};
|
|
|
|
if (fwrite(&infoHeader, sizeof(infoHeader), 1, file) < 1) {
|
|
cout << "Errore di scrittura." << endl;
|
|
fclose(file);
|
|
return 1;
|
|
}
|
|
|
|
// Creazione e scrittura dei dati pixel (immagine bianca)
|
|
BGR bgr[altezza * lunghezza];
|
|
for (int i = 0; i < altezza * lunghezza; i++) {
|
|
bgr[i].b = 50;
|
|
bgr[i].g = 25;
|
|
bgr[i].r = 100;
|
|
}
|
|
|
|
if (fwrite(&bgr, sizeof(bgr), 1, file) < 1) {
|
|
cout << "Errore di scrittura." << endl;
|
|
fclose(file);
|
|
return 1;
|
|
}
|
|
|
|
fclose(file);
|
|
} else {
|
|
perror("Errore nell'apertura del file");
|
|
}
|
|
|
|
return 0;
|
|
} |