2011-07-03 79 views
3

我正在玩弄隱寫術。我正試圖從圖像中拉出一個文本文件。我能夠讀取文件,獲取這些位,但是我在提取這些位時遇到了問題。位提取和隱寫術

int getbits(pixel p) { 
    return p & 0x03; 
} 

char extract (pixel* image) { 
    static int postion; 
    postion = 0; 

    postion = *image; 

    postion++; 

    char curChar; 
    curChar = '\0'; 
    for(int i = 0; i<4; ++i) { 
     curChar = curChar << 2; 
     curChar = curChar | getbits(postion); 
    } 
    return curChar; 
} 

Pixel是一個無符號字符。我有循環調用extract()fputc(3)的返回值。我覺得自己正在從這些位上得到垃圾。這導致我有大量(1.5演出)的txt文件作爲回報。

void decode(PgmType* pgm, char output[80]) 
{ 
FILE*outstream; 
int i, length; 

outstream = fopen(output, "w"); 

if(!outstream) 
{ 
    fatal("Could not open"); 
} 
for(i=0; i < 16; ++i) 
{ 
    length = length << 2; 
    length = length | getbits(*pgm->image); 
} 
if ((length* 4) < (pgm->width * pgm->height)) 
{ 
    fatal("File Too Big"); 
} 
for (i = 0 ;i<length; ++i) 
{ 
    fputc(extract(pgm->image), outstream); 

} 
fclose(outstream); 

} 
+5

速記或**隱寫術**? –

+2

顯示調用提取的循環 - 因爲您應該證明您正在循環顯示圖像。 – borrible

+0

@borrible我包含了循環 –

回答

2

你只實際讀取圖像中的第一像素 - [編輯]因爲當你試圖使用一個靜態無功保持計數,如奧利指出你立即覆蓋它。

改爲使用位置來跟蹤您的計數;但在另一個變種保存數據:

相反extract()應該是這個樣子:

char extract (pixel* image) 
{ 
    static int postion = 0; 

    pixel data = image[position]; 

    postion++; 

    // use 'data' for processing 
} 
+0

這是一條紅鯡魚; OP立即用'* image'覆蓋'position' ... –

+0

@Oli:確實;根據您的評論更新。 – DaveR

+0

@Dave我得到一個內存訪問錯誤。 –

2

戴夫·裏格比的excellent diagnosis是正確的,但經過position作爲參數(和這裏增加的話)會導致更容易理解和更靈活的套路:

char extract (pixel* image, int position) { 
    char curChar = '\0'; 
    for(int i = 0; i<4; ++i) { 
     curChar = curChar << 2; 
     curChar = curChar | getbits(postion); 
    } 
    return curChar; 
} 

char *build_string(pixel *image) { 
    int i; 
    char *ret = malloc(SECRET_SIZE); 
    for (i=0; i<SECRET_SIZE; i++) { 
     ret[i]=extract(image, i); 
    } 
    ret[i] = '\0'; 
    return ret; 
} 

然後,當你意識到,改變所有像素的行使得相當明顯,你寧願使用位於斐波納契值的像素,變化很容易使:

char *build_string_via_fib(pixel *image) { 
    int i; 
    char *ret = malloc(SECRET_SIZE); 

    for (i=0; i<SECRET_SIZE; i++) { 
     ret[i]=extract(image, fib(i)); 
    } 
    ret[i]='\0'; 
    return ret; 
} 

可能東西斐波那契數計算到你的日常extract()過,但功能分解成最小的,最有用的,件,爲您提供出色的可讀性,出色的可測試性以及未來代碼重用的最佳機會。