Ciao. Ho studiato qualcosa riguardo alle strutture BITMAPFILEHEADER e BITMAPINFOHEADER e su come siano fatti i bmp. Io devo creare un file BMP con colori a 24 bit.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define bitForPixel 24
#define pixelsX 640
#define pixelsY 480
#define numColors 512
/*provvisoriamente usiamo 512 colori anche se 24 bit ne permettono molti di +*/

struct RGB {
unsigned char R;
unsigned char G;
unsigned char B;
};

void createShading(int*,int,int,int,int,int,int,struct RGB*);

typedef unsigned short WORD;
typedef unsigned long DWORD;
typedef unsigned char BYTE;
typedef int LONG; /* 32-bit signed integer */

typedef struct tagBITMAPFILEHEADER {
WORD bfType;
DWORD bfSize;
WORD bfReserved1;
WORD bfReserved2;
DWORD bfOffBits;
} BMFH;

typedef struct tagBITMAPINFOHEADER {
DWORD biSize;
LONG biWidth;
LONG biHeight;
WORD biPlanes;
WORD biBitCount;
DWORD biCompression;
DWORD biSizeImage;
LONG biXPelsPerMeter;
LONG biYPelsPerMeter;
DWORD biClrUsed;
DWORD biClrImportant;
} BMIH;

BMFH bmfh;
BMIH bmih;
struct RGB palettes[numColors];
struct RGB image[pixelsX*pixelsY];

void createShading(int *i,int r1,int g1,int b1,int r2,int g2,int b2,struct RGB *data)
{
int k;
int numColorsForShading = numColors / 4;
for(k=1; k<=numColorsForShading; k++){
data[*i].R = (r1*(numColorsForShading-k)+r2*k)/numColorsForShading;
data[*i].G = (g1*(numColorsForShading-k)+g2*k)/numColorsForShading;
data[*i].B = (b1*(numColorsForShading-k)+b2*k)/numColorsForShading;
(*i)++;
}
}

int main(){
bmih.biSize = sizeof(BMIH);
bmih.biWidth = pixelsX;
bmih.biHeight = pixelsY;
bmih.biPlanes = 1;
bmih.biBitCount = bitForPixel;
bmih.biCompression = 0L;
bmih.biSizeImage = pixelsX*pixelsY * bitForPixel/8;
bmih.biXPelsPerMeter = 0;
bmih.biYPelsPerMeter = 0;
bmih.biClrUsed = 0;
bmih.biClrImportant = 0;

bmfh.bfType = 0x4D42;
bmfh.bfOffBits = sizeof(BMFH) + sizeof(BMIH);
/*mi è stato detto di moltiplicare per 4 perchè anche se sono 24 bit devo scrivere su 32bit e l'ultimo byte è 0. Non so se devo moltiplicare per 4 pure in bmih.biSizeImage, eppure in molti sorgenti hanno utilizzato bitmapForPixel/8*/
bmfh.bfSize = bmfh.bfOffBits+ pixelsX*pixelsY * 4;
bmfh.bfReserved1 = 0;
bmfh.bfReserved2 = 0;

int Rblue = 000;
int Gblue = 000;
int Bblue = 255;
int Rred = 255;
int Gred = 000;
int Bred = 000;
int Rjellow = 255;
int Gjellow = 255;
int Bjellow = 000;
int Rcyan = 000;
int Gcyan = 255;
int Bcyan = 255;

int i = 0;

createShading(&i,Rblue,Gblue,Bblue,Rred,Gred,Bred, palettes);
createShading(&i,Rred,Gred,Bred,Rjellow,Gjellow,Bj ellow,palettes);
createShading(&i,Rjellow,Gjellow,Bjellow,Rcyan,Gcy an,Bcyan,palettes);
createShading(&i,Rcyan,Gcyan,Bcyan,Rblue,Gblue,Bbl ue,palettes);
int colum, row;
int x=0; int y=0;
int color = 0;
for (colum=0; colum<pixelsX; colum++)
for (row=0; row<pixelsY; row++) { /* create shading diagonal*/
i = y*pixelsX+x;
memcpy(&image[i], &palettes[color], sizeof(struct RGB));
x++;
y++;
color++;
}
FILE *fp;
fp* fopen("file.bmp", "wb");
fwrite (void *buf, int size, int count, *fp); /*non so che mettere nei primi 3 argomenti*/
}

Volevo sapere se ce qualcosa da sistemare e assegnazioni da correggere prima di scrivere il file e vorrei sapere come scrivere questo file.

Grazie. CIAO

)