//*************************************//
//******** created by mamo139 *********//
//*** http://mamo139.altervista.org ***//
//*************************************//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
//*** strumenti gestione file bmp ***//
struct bmpfile {
long x;
long y;
char *** matrice;
};
char *** crea_bmp(char *nome, long x, long y);
struct bmpfile carica_bmp(char *nome);
void disegna_bmp(char *nome, char *** array, long x, long y);
//*** MAIN ***//
int main (void){
struct bmpfile car = carica_bmp("a.bmp");
char *** mappanf = crea_bmp("out.bmp",car.x,car.y);
disegna_bmp("out.bmp",car.matrice,car.x,car.y);
}
//****** FUNZIONI *******//
char *** crea_bmp(char *nome, long x, long y){
char buffer [55] = "\x42\x4d\x36\x39\xb1\x00\x00\x00\x00\x00" \
"\x36\x00\x00\x00\x28\x00\x00\x00\xe0\x08\x00\x00\xa8\x06" \
"\x00\x00\x01\x00\x18\x00\x00\x00\x00\x00\x00\x00\x00\x00" \
"\xc4\x0e\x00\x00\xc4\x0e\x00\x00\x00\x00\x00\x00\x00\x00" \
"\x00\x00";
buffer[18] = x%256;//asse x
buffer[19] = x/256;
buffer[22] = y%256;//asse y
buffer[23] = y/256;
FILE *stream;
stream = fopen(nome,"wb");
fwrite(buffer, 1, 54, stream);
fclose(stream);
char *** array;
int i, j, h;
array = (char***)malloc(x * sizeof(char *));
for(i=0; i<x; i++) {
array[i] = (char**)malloc(y * sizeof(char *));
for (j=0; j<y; j++)
array[i][j] = (char*)malloc(3 * sizeof(char *));
}
int i1,i2,i3;
for(i1=0; i1<x; i1++)
for (i2=0; i2<y; i2++)
for (i3=0; i3<3; i3++)
array[i1][i2][i3] = 0;
return array;
}
struct bmpfile carica_bmp(char *nome){
struct bmpfile dati;
long filesize,b,i,j,z=0;
char *** array;
char * buffer;
FILE * file;
file = fopen(nome,"rb");
fseek (file , 0 , SEEK_END);
filesize = ftell(file);
rewind(file);
buffer = (char *) malloc(filesize * sizeof(char *));
b = fread( buffer, 1, 54, file);
dati.x = ((unsigned char)buffer[18])+((unsigned char)buffer[19])*256;
dati.y = ((unsigned char)buffer[22])+((unsigned char)buffer[23])*256;
b = fread( buffer, 1, filesize, file);
array = (char***)malloc(dati.x * sizeof(char *));
for(i=0; i<dati.x; i++) {
array[i] = (char**)malloc(dati.y * sizeof(char *));
for (j=0; j<dati.y; j++)
array[i][j] = (char*)malloc(3 * sizeof(char *));
}
for(i=0; i<dati.x; i++)
for (j=0; j<dati.y; j++){
array[i][j][0]=buffer[z];
array[i][j][1]=buffer[z+1];
array[i][j][2]=buffer[z+2];
z=z+3;
}
dati.matrice = array;
return dati;
}
void disegna_bmp(char *nome, char *** array, long x, long y){
FILE *stream;
stream = fopen(nome,"ab");
int i1,i2;
for(i1=0; i1<x; i1++)
for (i2=0; i2<y; i2++)
fwrite(array[i1][i2], 1, 3, stream);
fclose(stream);
}