目的:簡單的二元圖資料隱藏,將lenna二元圖藏入到boat中,然後再把它分出來
1 #include <stdio.h> 2 #include <stdlib.h> 3 4 #define sizein 512 5 unsigned char boat[sizein][sizein]; 6 unsigned char lena[sizein][sizein]; 7 unsigned char hboat[sizein][sizein]; 8 9 FILE *ifile1,*ifile2,*ofile,*hfile; 10 11 12 13 int main(int argc,char **argv) 14 { 15 16 int i,j,k,z; 17 18 if ((ifile1=fopen("512Fishing_Boat.raw","rb"))==NULL) { printf("Unable to open input graph file\n",argv[1]); exit(1); } 19 if ((ifile2=fopen("512Lenna_error.raw","rb"))==NULL) { printf("Unable to open input graph file\n",argv[1]); exit(1); } 20 if ((ofile=fopen("combine_finish.raw","wb"))==NULL) { printf("Unable to open output graph file\n","file1"); exit(1); } 21 if ((hfile=fopen("divide_finish.raw","wb"))==NULL) { printf("Unable to open output graph file\n","file1"); exit(1); } 22 23 for(i=0;i<sizein;i++) 24 { 25 for(j=0;j<sizein;j++) 26 { 27 // 輸入資料 28 boat[i][j]=fgetc(ifile1); 29 lena[i][j]=fgetc(ifile2); // 0 255 30 31 // 用 +1 -1 的方式藏入最後一個 bit 32 if(boat[i][j]%2==lena[i][j]%2); 33 else if(lena[i][j]%2==1) // 兩者不相等 又 lena[i][j] 為 1 34 boat[i][j]=boat[i][j]+1; 35 else // 兩者不相等 又 lena[i][j] 為 0 36 boat[i][j]=boat[i][j]-1; 37 38 39 // 將藏好的圖還原 40 int tmp=boat[i][j]%2; 41 if(tmp==1) 42 hboat[i][j]=255; 43 else 44 hboat[i][j]=0; 45 } 46 } 47 48 49 fwrite(boat, sizeof(unsigned char), sizein*sizein, ofile); // 藏好圖的船 50 fwrite(hboat, sizeof(unsigned char), sizein*sizein, hfile); // 還原的被隱藏的lena圖 51 52 fclose(ifile1); 53 fclose(ifile2); 54 fclose(ofile); 55 fclose(hfile); 56 57 system("pause"); 58 return 0; 59 }
輸入 : 漁船、lenna
輸出 : 藏入後的漁船、解出來的lenna
程式解說:
1.先將漁船、lenna資料輸入
27 // 輸入資料 28 boat[i][j]=fgetc(ifile1); 29 lena[i][j]=fgetc(ifile2); // 0 255
2.要將二元圖藏入灰階圖,意思就是藏到最後一個bit裡面去,在這邊可以用十進位的加減1完成這個動作,目前我把它加到原圖boat去
31 // 用 +1 -1 的方式藏入最後一個 bit 32 if(boat[i][j]%2==lena[i][j]%2); 33 else if(lena[i][j]%2==1) // 兩者不相等 又 lena[i][j] 為 1 34 boat[i][j]=boat[i][j]+1; 35 else // 兩者不相等 又 lena[i][j] 為 0 36 boat[i][j]=boat[i][j]-1;
3.最後將圖還原出來放到hboat中,因為1在MinGw代表255,所以需要用if判斷式判斷
39 // 將藏好的圖還原 40 int tmp=boat[i][j]%2; 41 if(tmp==1) 42 hboat[i][j]=255; 43 else 44 hboat[i][j]=0;
4.完成輸出藏入後的漁船、解出來的lenna
49 fwrite(boat, sizeof(unsigned char), sizein*sizein, ofile); // 藏好圖的船 50 fwrite(hboat, sizeof(unsigned char), sizein*sizein, hfile); // 還原的被隱藏的lena圖