codice:
// crea da zero una bitmap 24 bit
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int multiplo4(int i)
{
while(i%4!=0)
i++;
return i;
}
unsigned long size_bmp(unsigned int w, unsigned int h)
{
unsigned long sc, tb;
sc=w*3;
sc=multiplo4(sc);
tb=(sc*h)+54;
return tb;
}
void header2s(FILE *p, unsigned char x, unsigned char y)
{
unsigned int b1, b2;
b1=x;
b2=y;
fwrite(&b1, sizeof(char), 1, p);
fwrite(&b2, sizeof(char), 1, p);
}
void header2d(FILE *p, unsigned int x)
{
unsigned int b1=0, b2=0;
unsigned int i=0;
while(i<x)
{
i++;
b1++;
if(b1>255){
b1=0;
b2++;
}
}
fwrite(&b1, sizeof(char), 1, p);
fwrite(&b2, sizeof(char), 1, p);
}
void header4s(FILE *p, unsigned char x, unsigned char y, unsigned char z, unsigned char u)
{
fwrite(&x, sizeof(char), 1, p);
fwrite(&y, sizeof(char), 1, p);
fwrite(&z, sizeof(char), 1, p);
fwrite(&u, sizeof(char), 1, p);
}
void header4d(FILE *p, unsigned long x)
{
unsigned int b1=0, b2=0, b3=0, b4=0;
unsigned long i=0;
while(i<x)
{
i++;
b1++;
if(b1>255){
b1=0;
b2++;
}
if(b2>255){
b2=0;
b3++;
}
if(b3>255){
b3=0;
b4++;
}
}
fwrite(&b1, sizeof(char), 1, p);
fwrite(&b2, sizeof(char), 1, p);
fwrite(&b3, sizeof(char), 1, p);
fwrite(&b4, sizeof(char), 1, p);
}
void data(FILE *p, unsigned long tb, unsigned int iw)
{
unsigned int sc=0, df=0;
unsigned int dr=0, dg=0, db=0, n=0, j=0, y=0, z=0;
unsigned long i=55;
iw=iw*3;
sc=multiplo4(iw);
df=sc-iw;
printf("Inserisci ROSSO [min.0 - max.255]: ");
scanf("%d", &dr);
printf("Inserisci VERDE [min.0 - max.255]: ");
scanf("%d", &dg);
printf("Inserisci BLU [min.0 - max.255]: ");
scanf("%d", &db);
printf("Larghezza x 3: %d\n", iw);
printf("Byte mancanti: %d\n", df);
printf("Scan-line : %d\n", sc);
printf("Sto scrivendo i dati...\n");
for(i=55; i<=tb; )
{
if(j==iw && df>0)
{
y=0;
while(y<df)
{
fwrite(&n, sizeof(char), 1, p);
i++;
y++;
}
j=0;
}
else
{
if(z==0)
fwrite(&db, sizeof(char), 1, p);
else if(z==1)
fwrite(&dg, sizeof(char), 1, p);
else if(z==2)
fwrite(&dr, sizeof(char), 1, p);
z++;
if(z>2)
z=0;
i++;
j++;
}
}
}
int main()
{
FILE *p;
unsigned char i=0, w[5]="\0", h[5]="\0", s[20]= "\0";
unsigned long iw, ih, tb, sc;
system("mode con: cols=80 lines=20");
printf("Inserisci Larghezza: ");
while((w[i]=getchar()) != '\n')
{
if(i>3)break;
i++;
}
w[i]='\0';
i=0;
printf("Inserisci Altezza : ");
while((h[i]=getchar()) != '\n')
{
if(i>3)break;
i++;
}
h[i]='\0';
iw = (long) atoi(w);
ih = (long) atoi(h);
if((iw>1600 || ih>1200) || (iw<1 || ih<1))
{
printf("Dimensione non supportata! [min.1x1 - max.1600x1200]\n");
system("pause");
return 1;
}
tb = size_bmp(iw, ih);
printf("Totale Byte da creare: %d\n", tb);
strcat(s, w);
strcat(s, "x");
strcat(s, h);
strcat(s, "b.bmp");
printf("Nome file da creare : %s\n", s);
p = fopen(s, "wb");
header2s(p, 'B', 'M'); // 1,2 2byte * intestazione Windows Bitmap 24 bit (BM)
header4d(p, tb); // 3,4,5,6 4byte * dimensione totale file BMP
header4s(p, 0, 0, 0, 0); // 7,8,9,10 4byte * riservati
header4s(p, 54, 0, 0, 0); // 11,12,13,14 4byte * lunghezza intestazione (54)
header4s(p, 40, 0, 0, 0); // 15,16,17,18 4byte * ???
header4d(p, iw); // 19,20,21,22 4byte * larghezza in pixel Bitmap
header2d(p, ih); // 23,24 2byte * altezza in pixel Bitmap
header2s(p, 0, 0); // 25,26 2byte * numero di piani
header4s(p, 1, 0, 24, 0); // 27,28,29,30 4byte * numero di bit per pixel usati
header4s(p, 0, 0, 0, 0); // 31,32,33,34 4byte * tipo di compressione
header4d(p, tb-54); // 35,36,37,38 4byte * lunghezza dei DATI della bitmap in byte (dal byte 55 in poi)
header4s(p, 0, 0, 0, 0); // 39,40,41,42 4byte * risoluzione orizzontale
header4s(p, 0, 0, 0, 0); // 43,44,45,46 4byte * risoluzione verticale
header4s(p, 0, 0, 0, 0); // 47,48,49,50 4byte * numero colori usati
header4s(p, 0, 0, 0, 0); // 51,52,53,54 4byte * numero colori importati
data(p, tb, iw); // creo la bitmap (inserendo i dati relativi al colore RGB)
fclose(p);
printf("Operazione terminata.\n");
system("pause");
return 0;
}