圖片隱藏

摘要 : 將一張圖片藏入另一張圖片裡面,被藏入的圖片大小必須是原圖片的一半以下

 

  1 /*
  2 (c++) mysterywho
  3 compiler    : VC2008
  4 description : 將圖片藏入另一張圖片中
  5 date        : 15/1/2008
  6 */
  7 
  8 #include <iostream>
  9 #include <fstream>
 10 #include <math.h>
 11 //cover圖大小
 12 #define SIZE1 512 
 13 #define SIZE2 512
 14 //secret圖大小
 15 #define SIZE3 256
 16 #define SIZE4 512
 17 
 18 using namespace std;
 19 
 20 
 21 unsigned char header[54], palette[1024];
 22 unsigned char raw_image[SIZE1*SIZE2*3];
 23 unsigned char raw_image1[SIZE3*SIZE4*3];
 24 unsigned char image[SIZE1/2][SIZE2];
 25 unsigned char image1[SIZE3][SIZE4];
 26 unsigned char coverdifferent[SIZE1/2*SIZE2];
 27 unsigned char initial[SIZE1/2*SIZE2];
 28 unsigned char secretdifferent[SIZE3*SIZE4];
 29 unsigned char table[SIZE1/2*SIZE2];
 30 unsigned char table1[SIZE3*SIZE4];
 31 unsigned char stego[SIZE1*SIZE2];
 32 
 33 void readfile();
 34 void writetofile();
 35 void readfile1();
 36 void writetofile1();
 37 void relativepositions(); 
 38 void readfile2();
 39 void writetofile2(); 
 40 void inverse();
 41 void readfile3();
 42 void writetofile3();
 43 void readfile4();
 44 void readfile41();
 45 void writetofile4();
 46 
 47 
 48 int main()
 49 {
 50 
 51     
 52     // 讀cover image
 53     readfile();
 54     writetofile();
 55 
 56     // 讀secret image
 57     readfile1();
 58     writetofile1();
 59 
 60     //將secretimage的點藏入coverimage
 61     relativepositions(); 
 62      
 63 
 64     //寫embedding result    
 65     readfile2();
 66     writetofile2(); 
 67     //將藏進去後所得的圖計算出來
 68     inverse();
 69 
 70     //得到stego-image
 71     readfile3();
 72     writetofile3(); 
 73 
 74     //抽出藏入圖片
 75     if(SIZE3*SIZE4==256*256){
 76         readfile4();
 77     }
 78     else
 79         readfile41();
 80 
 81     writetofile4();
 82 
 83     system("pause");
 84 
 85     return 0;    
 86 }
 87 
 88 void readfile()
 89 {
 90     FILE *fin  = fopen("10.bmp""rb");
 91     int i, j, t;
 92 
 93 
 94     if (fin == NULL) {
 95         printf("Error: The file doesn't exist !\n");
 96         exit(0);
 97     }
 98 
 99     // 讀 bmp 灰階標頭檔
100     fread(header, 541, fin);   
101     fread(palette, 10241, fin);
102     // 讀 bmp 影像資料
103     fread(raw_image, 1, SIZE1*SIZE2*3, fin);
104 
105 
106 
107 
108     for ( t=0,i=0; i< SIZE1/2; i++) {    
109         for (j=0; j< SIZE2; j++) {      
110 
111             image[i][j] = raw_image[t + 3- raw_image[t] + 128;
112             t = t + 6;           
113         }
114     }    
115 
116     fclose(fin);   
117 }
118 
119 
120 
121 
122 void writetofile()
123 {
124     int i, j,t;
125 
126     FILE *fout = fopen("coverdiff.bmp""wb");
127 
128     long file_size = (long)SIZE1/2 *SIZE2 * 3 + 54;
129     header[2= (unsigned char)(file_size &0x000000ff);
130     header[3= (file_size >> 8& 0x000000ff;
131     header[4= (file_size >> 16& 0x000000ff;
132     header[5= (file_size >> 24& 0x000000ff;
133 
134     long width = SIZE1/2;
135     header[18= width & 0x000000ff;
136     header[19= (width >> 8&0x000000ff;
137     header[20= (width >> 16&0x000000ff;
138     header[21= (width >> 24&0x000000ff;
139 
140     long height = SIZE2;
141     header[22= height &0x000000ff;
142     header[23= (height >> 8&0x000000ff;
143     header[24= (height >> 16&0x000000ff;
144     header[25= (height >> 24&0x000000ff;
145 
146     fwrite(header, 541, fout);   
147     fwrite(palette, 10241, fout);
148 
149 
150     for (t=0,i=0; i< SIZE1/2; i++) {
151         for (j=0; j< SIZE2; j++) {
152             //將coverimage different點的值存入
153             coverdifferent[t] = image[i][j];
154             //將coverimage different點的值存入
155             initial[t] = image[i][j];
156 
157             fwrite(&image[i][j], 11, fout);   
158             fwrite(&image[i][j], 11, fout); 
159             fwrite(&image[i][j], 11, fout);           
160             t++;
161 
162         }
163     }
164 
165     fclose(fout);
166 }
167 
168 
169 
170 
171 
172 void readfile1()
173 {
174     FILE *fin  = fopen("07.bmp""rb");
175     int i, j, t;
176 
177 
178     if (fin == NULL) {
179         printf("Error: The file doesn't exist !\n");
180         exit(0);
181     }
182 
183     // 讀 bmp 灰階標頭檔
184     fread(header, 541, fin);   
185     fread(palette, 10241, fin);
186     // 讀 bmp 影像資料
187     fread(raw_image1, 1, SIZE3*SIZE4*3, fin);
188 
189 
190 
191     for (t=0, i=0; i< SIZE3; i++) {    
192         for (j=0; j< SIZE4; j++) {      
193 
194             image1[i][j] = raw_image1[t+3- raw_image1[t] + 128;
195             t=t+3;           
196         }
197     }    
198 
199     fclose(fin);   
200 }
201 
202 void writetofile1()
203 {
204 
205     int i, j, t;
206 
207     FILE *fout = fopen("secretdiff.bmp""wb");
208 
209 
210     fwrite(header, 541, fout);   
211     fwrite(palette, 10241, fout);
212 
213     for (t=0,i=0; i< SIZE3; i++)   {
214         for (j=0; j< SIZE4; j++)    {
215             //將secretdifferent值存入
216             secretdifferent[t] = image1[i][j];
217             fwrite(&image1[i][j], 11, fout);   
218             fwrite(&image1[i][j], 11, fout); 
219             fwrite(&image1[i][j], 11, fout); 
220             t++;
221         }
222     }
223 
224     fclose(fout);
225 }
226 
227 
228 void relativepositions()
229 {
230     int i,j;
231     int m,count;
232 
233 
234     for(i=0;i<SIZE1/2*SIZE2;i++){
235         //紀錄coverdiff的點是否被替換過
236         table[i]=0;
237     }
238 
239 
240     for(i=0;i<SIZE3*SIZE4;i++){
241         //紀錄secretdiff的點是否已經藏進去
242         table1[i]=0;
243     }
244 
245 
246     for(count=0,i=0;i< SIZE3*SIZE4;i++ ){
247         for(j=0;j<SIZE1/2*SIZE2;j++){
248             if(table[j]==0){
249                 if( abs(secretdifferent[i] - coverdifferent[j]) < 5 ){
250                     coverdifferent[j] = secretdifferent[i];
251                     table[j] = 1;
252                     table1[i] = 1;
253                     count=count+1;
254                     break;
255                 }
256             }
257 
258         }
259 
260     }
261 
262 
263 
264     m=0;
265     while(count< SIZE3*SIZE4){
266         for(i=0;i< SIZE3*SIZE4;i++ ){
267             if(table1[i]==0){
268                 for(j=0;j<SIZE1/2*SIZE2;j++){
269                     if(table[j]==0){
270                         if( abs(secretdifferent[i] - coverdifferent[j]) < (m*5+10) ){
271                             coverdifferent[j] = secretdifferent[i];
272                             table[j] = 1;
273                             count=count+1;
274                             break;
275                         }
276                     }
277 
278                 }
279             }
280         }
281 
282         m=m+1;
283 
284     }
285 
286     printf("%d",count);
287 
288 
289 }
290 
291 
292 
293 void readfile2()
294 {
295     FILE *fin  = fopen("256512.bmp""rb");
296     int i, j, t;
297 
298 
299     if (fin == NULL) {
300         printf("Error: The file doesn't exist !\n");
301         exit(0);
302     }
303 
304     // 讀 bmp 灰階標頭檔
305     fread(header, 541, fin);   
306     fread(palette, 10241, fin);
307     // 讀 bmp 影像資料
308     //  fread(raw_image, 1, SIZE1*SIZE2*3, fin);
309 
310 
311     fclose(fin);   
312 }
313 
314 
315 void writetofile2()
316 {
317 
318 
319     int i,j;
320 
321     FILE *fout = fopen("embedding.bmp""wb");
322 
323 
324     fwrite(header, 541, fout);   
325     fwrite(palette, 10241, fout);
326 
327 
328     // embedding result
329     for (i=0; i< SIZE1/2*SIZE2; i++) {
330 
331         fwrite(&coverdifferent[i], 11, fout);   
332         fwrite(&coverdifferent[i], 11, fout); 
333         fwrite(&coverdifferent[i], 11, fout); 
334 
335 
336     }
337 
338 
339 
340     fclose(fout);
341 }
342 
343 
344 
345 
346 
347 void inverse()
348 {
349     int i,k,t;
350 
351     for(k=0,t=0;t< SIZE1*SIZE2; t=t+2 ) {
352 
353         //G'i-1 = Gi-1 - 取 下整數 (d'-d)/2
354         stego[t] = raw_image[3*t] - floor( ( double(coverdifferent[k]) - double(initial[k]) )/2 ) ;
355         //G'i = Gi - 取 上整數 (d'-d)/2
356         stego[t+1= raw_image[3*(t+1)] - ceil( ( double(coverdifferent[k]) - double(initial[k]) )/2 ) ;
357         k++;
358 
359 
360     }
361 
362     /*for(i=0;i<SIZE1*SIZE2;i++){
363 
364     if(i%10 == 0){
365     printf("\n");
366     }
367 
368     printf("%d  \b",stego[i]);
369     }*/
370 
371 
372 }
373 
374 
375 
376 void readfile3()
377 {
378     FILE *fin  = fopen("03.bmp""rb");
379 
380 
381 
382     if (fin == NULL) {
383         printf("Error: The file doesn't exist !\n");
384         exit(0);
385     }
386 
387     // 讀 bmp 灰階標頭檔
388     fread(header, 541, fin);   
389     fread(palette, 10241, fin);
390     // 讀 bmp 影像資料
391     //  fread(raw_image, 1, SIZE1*SIZE2*3, fin);
392 
393 
394     fclose(fin);   
395 }
396 
397 
398 void writetofile3()
399 {
400 
401 
402     int i;
403 
404     FILE *fout = fopen("stego.bmp""wb");
405 
406 
407     fwrite(header, 541, fout);   
408     fwrite(palette, 10241, fout);
409 
410 
411     // embedding result
412     for (i=0; i< SIZE1*SIZE2; i++) {
413 
414         fwrite(&stego[i], 11, fout);   
415         fwrite(&stego[i], 11, fout); 
416         fwrite(&stego[i], 11, fout); 
417 
418 
419     }
420 
421 
422 
423     fclose(fout);
424 }
425 
426 
427 
428 void readfile4()
429 {
430     FILE *fin  = fopen("256256.bmp""rb");
431     int i, j, t;
432 
433 
434     if (fin == NULL) {
435         printf("Error: The file doesn't exist !\n");
436         exit(0);
437     }
438 
439     // 讀 bmp 灰階標頭檔
440     fread(header, 541, fin);   
441     fread(palette, 10241, fin);
442     // 讀 bmp 影像資料
443     //  fread(raw_image, 1, SIZE1*SIZE2*3, fin);
444 
445 
446     fclose(fin);   
447 }
448 
449 
450 
451 
452 void readfile41()
453 {
454     FILE *fin  = fopen("256512.bmp""rb");
455     int i, j, t;
456 
457 
458     if (fin == NULL) {
459         printf("Error: The file doesn't exist !\n");
460         exit(0);
461     }
462 
463     // 讀 bmp 灰階標頭檔
464     fread(header, 541, fin);   
465     fread(palette, 10241, fin);
466     // 讀 bmp 影像資料
467     //  fread(raw_image, 1, SIZE1*SIZE2*3, fin);
468 
469 
470     fclose(fin);   
471 }
472 void writetofile4()
473 {
474 
475 
476     int i;
477 
478     FILE *fout = fopen("drawout.bmp""wb");
479 
480 
481     fwrite(header, 541, fout);   
482     fwrite(palette, 10241, fout);
483 
484 
485 
486     /*for (i=0; i< 256*512; i++) {
487 
488     fwrite(&secretdifferent[i], 1, 1, fout);   
489     fwrite(&secretdifferent[i], 1, 1, fout); 
490     fwrite(&secretdifferent[i], 1, 1, fout); 
491 
492 
493     }*/
494     for(i=0;i<SIZE3*SIZE4*3;i=i+3){
495 
496         fwrite(&raw_image1[i], 11, fout);
497         fwrite(&raw_image1[i], 11, fout);
498         fwrite(&raw_image1[i], 11, fout);
499 
500 
501     }
502 
503 
504 
505     fclose(fout);
506 }
posted @ 2009-03-10 17:59  春天播種,秋天才會收穫  阅读(180)  评论(0编辑  收藏  举报