为BMP彩色反相功能不起作用 [英] Function for BMP color inverting doesn't work

查看:135
本文介绍了为BMP彩色反相功能不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一些快速code是应该反转一个BMP图像的颜色。
我和一个40x40的二维BMP图像,通过油漆创建的测试。
然而,功能似乎与白色像素,而不是entirelly填充它。

I wrote some quick code that is supposed to invert colors of a BMP image. I test with a 40x40 dimensional BMP image, created by paint. However the function seem to fill it entirelly with white pixels instead.

void
negate
(char *FILE_NAME)
{
    FILE* fp = fopen(FILE_NAME, "r+b");
    uint_64 raw_data, i;

    fseek(fp, 35, SEEK_SET);
    //raw_data = fgetc(fp) + fgetc(fp)*256;
    raw_data = 4800; // calculated

    fseek(fp, 54, SEEK_SET);
    for(i = 54; i != 54 + raw_data; i++) // <=
    {
        int old = fgetc(fp);
        fseek(fp, i, SEEK_SET);
        fputc(255 - old, fp);
    }

    fclose(fp);
}

我在哪里错了?

推荐答案

添加到fseek的循环修复它,但我不明白为什么:

Adding an fseek into the loop fixes it, although I don't understand why:

for (i = 54; i != 54 + raw_data; i++) // <=
{
    fseek(fp, i, SEEK_SET);
    int old = fgetc(fp);

    fseek(fp, i, SEEK_SET);
    fputc(255 - old, fp);
}

在试图找出这个问题,我这样做,看是否流中的位置是正确的:

In trying to figure out the problem, I did this, to see if the position in the stream was correct:

for (i = 54; i != 54 + raw_data; i++) // <=
{
    long x = ftell(fp);
    printf("%d,", x);

    int old = fgetc(fp);

    fseek(fp, i, SEEK_SET);
    fputc(255 - old, fp);
}

和的确是。它。OUPUTS 54,55,56,57,......所以fseek的()不应该是必要的!但是,如果你看一下龟etc()的结果,老字号的价值似乎总是在阅读54我觉得@RSahu是正确的像素:不读,写这样的一个文件。更好地将数据读取到缓冲器,则否定缓冲器,然后将其写入到磁盘。或写入到完全不同的文件。

And indeed it is. It ouputs 54,55,56,57,... so the fseek() should not be necessary! But if you look at the result of the fgetc(), the "old" value always seems to be reading the pixel at 54. I think @RSahu was correct: don't read and write like that to one file. Better to read the data into a buffer, then negate the buffer, then write it to disk. Or write to a different file entirely.

也许这与缓冲办?

这篇关于为BMP彩色反相功能不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆