//*************************************//
//******** 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);
unsigned int * rgb_hsv(unsigned int *);
unsigned int * hsv_rgb(unsigned int *);
/*
char *bits;
bits = new char [8];
bits = dammibit(154);
*/
int main (void){
char nomefileb[20];
printf("nome del file base:"); scanf("%s",nomefileb);
//peso supportabile dal file base con steganografia 3-3-2bit
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
);
free(buf);
//*****************************INIZIO MODE DEL FILE**********************************//
for(y=0; y < coordy ; y++){
for(x=0; x < coordx ; x++){
unsigned int *wbuf;
wbuf = new unsigned int [3];
unsigned int *wbuf3;
unsigned char *wbuf2;
wbuf2 = new unsigned char [3];
wbuf[0]=mappa[x][y][0];
wbuf[1]=mappa[x][y][1];
wbuf[2]=mappa[x][y][2];
wbuf3 = rgb_hsv(wbuf);
wbuf3[0]=360 - (wbuf3[2]*256/360);
wbuf3[1]=200;
wbuf3[2]=200;
wbuf = hsv_rgb(wbuf3);
wbuf2[0]=wbuf[0];
wbuf2[1]=wbuf[1];
wbuf2[2]=wbuf[2];
fwrite( wbuf2, 1, 3, f_out);
delete [] wbuf2;
delete [] wbuf3;
delete [] wbuf;
} printf("%d%%\n",(y
*100
/coordy
)); }
//fwrite( buf, 1, b, f_out);
}
fclose(file);
fclose(f_out);
system("PAUSE");
return 1;
}
unsigned int * hsv_rgb(unsigned int hsv[3]){
//printf("REVERSE:\nH:%d\nS:%d\nV:%d\n",hsv[0],hsv[1],hsv[2]);
double *dRGB;
dRGB = new double[3];
double H = ((double)hsv[0]);
double S = ((double)hsv[1])/255;
double V = ((double)hsv[2])/255;
double hi = ((int)(H/60))%6;
double f = (H/60) - (double)hi;
double p = V*(1-S);
double q = V*(1-(f*S));
double t = V*(1-(1-f)*S);
if (hi == 0){ dRGB[0]=V;dRGB[1]=t;dRGB[2]=p;}
else if (hi == 1){ dRGB[0]=q;dRGB[1]=V;dRGB[2]=p;}
else if (hi == 2){ dRGB[0]=p;dRGB[1]=V;dRGB[2]=t;}
else if (hi == 3){ dRGB[0]=p;dRGB[1]=q;dRGB[2]=V;}
else if (hi == 4){ dRGB[0]=t;dRGB[1]=p;dRGB[2]=V;}
else if (hi == 5){ dRGB[0]=V;dRGB[1]=p;dRGB[2]=q;}
//printf("R:%f\nG:%f\nB:%f\n",dRGB[0],dRGB[1],dRGB[2]);
unsigned int *RGB;
RGB = new unsigned int[3];
RGB[0] = (int)(dRGB[2]*255);
RGB[1] = (int)(dRGB[1]*255);
RGB[2] = (int)(dRGB[0]*255);
free(dRGB);
//printf("R:%d\nG:%d\nB:%d\n",RGB[0],RGB[1],RGB[2]);
return RGB;
}
unsigned int * rgb_hsv(unsigned int rgb[3]){
//colore rgb e hsv
unsigned int *RGB;
RGB = new unsigned int[3];
unsigned int *HSV;
HSV = new unsigned int[3];
//definizione del colore
RGB[0]=rgb[2];
RGB[1]=rgb[1];
RGB[2]=rgb[0];
double *dRGB;
dRGB = new double[3];
double *dHSV;
dHSV = new double[3];
//(0,1)
dRGB[0]= ((double)RGB[0])/255;
dRGB[1]= ((double)RGB[1])/255;
dRGB[2]= ((double)RGB[2])/255;
//printf("R:%f\nG:%f\nB:%f\n",dRGB[0],dRGB[1],dRGB[2]);
double gradi = 0;
double MAX= 0, MIN=0, numeratore=0;
double H = 0; //tonalità
if( dRGB[0] == dRGB[1] && dRGB[1] == dRGB[2]) { H=0; MAX = dRGB[0]; MIN = dRGB[0]; } //tonalità trovata *********
else{
if( dRGB[0] >= dRGB[1] && dRGB[0] >= dRGB[2] && dRGB[1] >= dRGB[2]){ //rosso maggiore blu minore
MAX = dRGB[0];
MIN = dRGB[2];
gradi = 0;
numeratore = dRGB[1]-dRGB[2];
//printf("a");
}
else if( dRGB[0] >= dRGB[1] && dRGB[0] >= dRGB[2] && dRGB[1] < dRGB[2]){ //rosso maggiore verde minore
MAX = dRGB[0];
MIN = dRGB[1];
gradi = 360;
numeratore = dRGB[1]-dRGB[2];
//printf("b");
}
else if( dRGB[1] >= dRGB[0] && dRGB[1] >= dRGB[2] && dRGB[0] <= dRGB[2]){ //verde maggiore, rosso minore
MAX = dRGB[1];
MIN = dRGB[0];
gradi = 120;
numeratore = dRGB[2]-dRGB[0];
//printf("c");
}
else if( dRGB[1] >= dRGB[0] && dRGB[1] >= dRGB[2] && dRGB[2] < dRGB[0]){ //verde maggiore, blu minore
MAX = dRGB[1];
MIN = dRGB[2];
gradi = 120;
numeratore = dRGB[2]-dRGB[0];
//printf("d");
}
else if( dRGB[2] >= dRGB[0] && dRGB[2] >= dRGB[1] && dRGB[0] <= dRGB[1]){ //blu maggiore, rosso minore
MAX = dRGB[2];
MIN = dRGB[0];
gradi = 240;
numeratore = dRGB[0]-dRGB[1];
//printf("e");
}
else if( dRGB[2] >= dRGB[0] && dRGB[2] >= dRGB[1] && dRGB[1] < dRGB[0]){ //blu maggiore, verde minore
MAX = dRGB[2];
MIN = dRGB[1];
gradi = 240;
numeratore = dRGB[0]-dRGB[1];
//printf("f");
}
H = ( (int) (60*(numeratore/(MAX-MIN)) + gradi) ) % 360;//tonalità trovata ***********
}
double S = 0;//saturazione
if(MAX == 0) S = 0; //saturazione trovata **********
else S = (MAX - MIN) / MAX; //saturazione trovata **********
double V = MAX; //luminosità/valore trovata **********
dHSV[0]=H;
dHSV[1]=S;
dHSV[2]=V;
HSV[0]= (int)(dHSV[0]);
HSV[1]= (int)(dHSV[1] * 255);
HSV[2]= (int)(dHSV[2] * 255);
//printf("H:%f\nS:%f\nV:%f\n",dHSV[0],dHSV[1],dHSV[2]);
//printf("H:%d\nS:%d\nV:%d\n",HSV[0],HSV[1],HSV[2]);
free(dHSV);
free(RGB);
free(dRGB);
return HSV;
}