//*************************************//
//******** created by mamo139 *********//
//*** http://mamo139.altervista.org ***//
//*************************************//
#include <stdio.h>
#include <windows.h>
#include <math.h>
unsigned char* dammibit(unsigned char numero);
unsigned char dammichar(unsigned char *bits);
int main (void){
char nomefileb[20];
printf("nome del file base:"); scanf("%s",nomefileb);
FILE * pFile;
long lSize;
pFile = fopen ( nomefileb , "rb" );
fseek (pFile , 0 , SEEK_END);
lSize = ftell (pFile);
fclose (pFile);
long lbuffer = lSize; //byte letti alla volta nella base
printf("buffer impostato a %d bytes\n",lbuffer
);
unsigned char *buf; //buffer di lettura
buf = new unsigned char [(lbuffer)+100]; //grandezza del buffer della base
FILE *file, *f_out;
if ((file = fopen(nomefileb, "rb")) && (f_out = fopen("cript.bmp", "wb"))){
printf("files aperti con successo!!\n");
long b; //byte letti
b = fread( buf, 1, 54, file);
unsigned int coordx = buf[18]+buf[19]*256;
unsigned int coordy = buf[22]+buf[23]*256;
printf("caratteristiche immagine: %d x %d\n",coordx
,coordy
);
fwrite( buf, 1, b, f_out);
printf("header scritto...\n");
//******************************INIZIO LETTURA DEL FILE*********************************//
b = fread( buf, 1, lbuffer, file);
printf("file caricato nel buffer...\n");
long x,y,z=0;
//caricamento in array bidimensionale...
//unsigned char mappa[coordx][coordy][4];
int x1=coordx, y1=coordy, z1=3; // Le dimensioni
unsigned char ***mappa;
int i, j;
mappa=(unsigned char***)malloc(x1 * sizeof(unsigned char *));
for(i=0; i<x1; i++) {
mappa[i]=(unsigned char**)malloc(y1 * sizeof(unsigned char *));
for (j=0; j<y1; j++)
mappa[i][j]=(unsigned char*)malloc(z1 * sizeof(unsigned char *));
}
printf("mappa a due dimensioni creata...\n");
for(y=0; y < coordy ;y++){
for(x=0;x < coordx;x++){
mappa[x][y][0] = buf[z];
mappa[x][y][1] = buf[(z+1)];
mappa[x][y][2] = buf[(z+2)];
z=z+3;
}}
printf("byte immagazzinati nella mappa: %d \n",z
);
//*****************************INIZIO MODE DEL FILE**********************************//
//operatore di sobel
double kernel[3][3]; //kernel
kernel[0][0]= 1; kernel[0][1]= 0; kernel[0][2]= -1;
kernel[1][0]= 2; kernel[1][1]= 0; kernel[1][2]= -2;
kernel[2][0]= 1; kernel[2][1]= 0; kernel[2][2]= -1;
double kernel2[3][3]; //kernel
kernel2[0][0]= 1; kernel2[0][1]= 2; kernel2[0][2]= 1;
kernel2[1][0]= 0; kernel2[1][1]= 0; kernel2[1][2]= 0;
kernel2[2][0]= -1; kernel2[2][1]= -2; kernel2[2][2]= -1;
/*
//filtro blurr
double kernel[3][3]; //kernel
kernel[0][0]= 0.1111; kernel[0][1]= 0.1111; kernel[0][2]= 0.1111;
kernel[1][0]= 0.1111; kernel[1][1]= 0.1111; kernel[1][2]= 0.1111;
kernel[2][0]= 0.1111; kernel[2][1]= 0.1111; kernel[2][2]= 0.1111;
*/
double grigio,grigio2;
for(y=0; y < coordy ; y++){
for(x=0; x < coordx ; x++){
/*
unsigned char *wbuf;
wbuf = new unsigned char [4];
wbuf[0]= mappa[x][y][0];
wbuf[1]= mappa[x][y][1];
wbuf[2]= mappa[x][y][2];
*/
grigio = (mappa[x][y][0] + mappa[x][y][1] + mappa[x][y][2])/3;
grigio2 = (mappa[x][y][0] + mappa[x][y][1] + mappa[x][y][2])/3;
//printf("%f",grigio);
if(x>=1 && y >=1 && x<coordx-1 && y<coordy-1){
//convoluzione
grigio =
((mappa[x-1][y-1][0]+mappa[x-1][y-1][1]+mappa[x-1][y-1][2])/3) * kernel[0][0] +
((mappa[x][y-1][0]+mappa[x][y-1][1]+mappa[x][y-1][2])/3) * kernel[0][1] +
((mappa[x+1][y-1][0]+mappa[x+1][y-1][1]+mappa[x+1][y-1][2])/3) * kernel[0][2] +
((mappa[x-1][y][0]+mappa[x-1][y][1]+mappa[x-1][y][2])/3) * kernel[1][0] +
((mappa[x][y][0]+mappa[x][y][1]+mappa[x][y][2])/3) * kernel[1][1] +
((mappa[x+1][y][0]+mappa[x+1][y][1]+mappa[x+1][y][2])/3) * kernel[1][2] +
((mappa[x-1][y+1][0]+mappa[x-1][y+1][1]+mappa[x-1][y+1][2])/3) * kernel[2][0] +
((mappa[x][y+1][0]+mappa[x][y+1][1]+mappa[x][y+1][2])/3) * kernel[2][1] +
((mappa[x+1][y+1][0]+mappa[x+1][y+1][1]+mappa[x+1][y+1][2])/3) * kernel[2][2] ;
grigio2 =
((mappa[x-1][y-1][0]+mappa[x-1][y-1][1]+mappa[x-1][y-1][2])/3) * kernel2[0][0] +
((mappa[x][y-1][0]+mappa[x][y-1][1]+mappa[x][y-1][2])/3) * kernel2[0][1] +
((mappa[x+1][y-1][0]+mappa[x+1][y-1][1]+mappa[x+1][y-1][2])/3) * kernel2[0][2] +
((mappa[x-1][y][0]+mappa[x-1][y][1]+mappa[x-1][y][2])/3) * kernel2[1][0] +
((mappa[x][y][0]+mappa[x][y][1]+mappa[x][y][2])/3) * kernel2[1][1] +
((mappa[x+1][y][0]+mappa[x+1][y][1]+mappa[x+1][y][2])/3) * kernel2[1][2] +
((mappa[x-1][y+1][0]+mappa[x-1][y+1][1]+mappa[x-1][y+1][2])/3) * kernel2[2][0] +
((mappa[x][y+1][0]+mappa[x][y+1][1]+mappa[x][y+1][2])/3) * kernel2[2][1] +
((mappa[x+1][y+1][0]+mappa[x+1][y+1][1]+mappa[x+1][y+1][2])/3) * kernel2[2][2] ;
}
//printf(" - %f\n",grigio);
unsigned char *wbuf;
wbuf = new unsigned char [3];
wbuf[0]= 255 - (char) sqrt(grigio*grigio+grigio2*grigio2);
wbuf[1]= wbuf[0];
wbuf[2]= wbuf[0];
fwrite( wbuf, 1, 3, f_out);
}}
//fwrite( buf, 1, b, f_out);
}
fclose(file);
fclose(f_out);
system("PAUSE");
return 1;
}