为什么我的位图图像在将32位转换为8位后具有另一个颜色覆盖层

发布于 2025-01-30 15:48:49 字数 2065 浏览 3 评论 0原文

IM正在努力调整位图图像和将位图图像转换为8位(灰度)。但是我有一个问题是,当我将32位图像转换为8位图像时,结果在24位完美工作时具有另一种颜色覆盖层。我想原因是α颜色。但是我不知道问题到底在哪里。

这是我的代码生成8位调色板颜色,并在 dib 部分之后写:

char* palette = new char[1024];
for (int i = 0; i < 256; i++) {
    palette[i * 4] = palette[i * 4 + 1] = palette[i * 4 + 2] = (char)i;
    palette[i * 4 + 3] = 255;
}
fout.write(palette, 1024);
delete[] palette;

正如我所说,我的代码在24位上完美地工作。在32位调整大小后,颜色仍然保留,但是当转换为8位时,它看起来像这样: 预期图像(从24位转换时) // 意外图像(从32位转换时)

这就是我获得颜色并保存它的方式到srcpixel []

int i = 0;
for (int y = 0; y < height; y++) {
    for (int x = 0; x < width; x++) {
        int index = getIndex(width, x, y);
        srcPixel[index].A = srcBMP.pImageData[i];
        i += alpha;
        srcPixel[index].B = srcBMP.pImageData[i++];
        srcPixel[index].G = srcBMP.pImageData[i++];
        srcPixel[index].R = srcBMP.pImageData[i++];
    }
    i += padding;
}

这是我通过从该srcpixel [] 的平均获得4种颜色A,B,G和R进行转换的代码:

int i = 0;
for (int y = 0; y < dstHeight; y++) {
    for (int x = 0; x < dstWidth; x++) {
        int index = getIndex(dstWidth, x, y);
        dstBMP.pImageData[i++] = (srcPixel[index].A + srcPixel[index].B + srcPixel[index].G + srcPixel[index].R) / 4;
    }
    i += dstPadding;
}

如果我删除并删除并且在我的代码中跳过所有alpha字节,当转换我的图像时仍然是这样的,我将遇到另一个问题,当调整大小时,我的图像将在转换为8位时具有另一个颜色覆盖层:不带alpha Channel的大小

如果我在平均值时跳过alpha频道(更改为dstbmp.pimagedata [i ++] =(srcpixel [index] .b + srcpixel [index] .g + srcpixel [index] .r).r) / 3 < / code>> < / code>> ,几乎没有什么不同,

如果我删除调色板[I * 4 + 3] = 255;或对此进行任何操作,结果仍然是不受影响

Im working on resizing bitmap image and converting bitmap image to 8-bit (grayscale). But I have the problem that when I convert 32-bit image to 8-bit image, the result has another color overlay while it works perfectly on 24-bit. I guess the cause is in the alpha color. but I dont know where the problem exactly is.

This is my code to generate 8-bit palette color and write it after DIB part:

char* palette = new char[1024];
for (int i = 0; i < 256; i++) {
    palette[i * 4] = palette[i * 4 + 1] = palette[i * 4 + 2] = (char)i;
    palette[i * 4 + 3] = 255;
}
fout.write(palette, 1024);
delete[] palette;

As I said, my code works perfectly on 24-bit. In 32-bit the color is still kept after resizing, but when converting to 8-bit, it will look like this:
expected image (when converted from 24-bit) //
unexpected image (when converted from 32-bit)

This is how I get the colors and save it to srcPixel[]:

int i = 0;
for (int y = 0; y < height; y++) {
    for (int x = 0; x < width; x++) {
        int index = getIndex(width, x, y);
        srcPixel[index].A = srcBMP.pImageData[i];
        i += alpha;
        srcPixel[index].B = srcBMP.pImageData[i++];
        srcPixel[index].G = srcBMP.pImageData[i++];
        srcPixel[index].R = srcBMP.pImageData[i++];
    }
    i += padding;
}

And this is the code I converted it by getting average of 4 colors A, B, G and R from that srcPixel[]:

int i = 0;
for (int y = 0; y < dstHeight; y++) {
    for (int x = 0; x < dstWidth; x++) {
        int index = getIndex(dstWidth, x, y);
        dstBMP.pImageData[i++] = (srcPixel[index].A + srcPixel[index].B + srcPixel[index].G + srcPixel[index].R) / 4;
    }
    i += dstPadding;
}

If I remove and skip all alpha bytes in my code, when converting my image is still like that and I will have another problem is when resizing, my image will have another color overlay like the problem when converting to 8-bit: resizing without alpha channel.

If I skip the alpha channel while getting average (change into dstBMP.pImageData[i++] = (srcPixel[index].B + srcPixel[index].G + srcPixel[index].R) / 3, there is almost nothing different, the overlay still exists.

If I remove palette[i * 4 + 3] = 255; or doing anything with it, the result is still not affected.

Thank you very much.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风渺 2025-02-06 15:48:49

您将alpha通道添加到颜色上,这就是为什么它变得更明亮的原因。来自在这里0-因此,您将另一个设置为“白色”的通道添加到您的结果中。

从您的方程式中删除Alpha通道,看看我是否对。

You add alpha channel to the color and that's why it becomes brighter. From here I found that opaque is 255 and transparent 0 - therefore you add another channel which is set to 'white' to your result.

Remove alpha channel from your equation and see if I'm right.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文