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;
}

像素是无符号字符。我有调用extract()fputc(3)返回值的循环。我觉得我从这些碎片中得到了垃圾。这导致我有大量 (1.5 gig) 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);

}
4

2 回答 2

2

Dave Rigby 的出色诊断是正确的,但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()例程中,但是将函数分解为最小、最有用的部分,可以为您提供出色的易读性、出色的可测试性以及未来代码重用的最佳机会。

于 2011-07-03T21:18:54.557 回答
2

您实际上只是在读取图像中的第一个像素 - [编辑],因为当您尝试使用静态变量来保持计数时,正如 Oli 指出的那样,您会立即覆盖它。

而是使用位置来跟踪您的计数;但将数据保存在另一个变量中:

相反extract()应该看起来像:

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

   pixel data = image[position];

   postion++;

   // use 'data' for processing
}
于 2011-07-03T20:59:26.953 回答