gpt4 book ai didi

c - libpng,c代码,如何将RGB值转换为像素值?

转载 作者:太空宇宙 更新时间:2023-11-04 03:48:55 25 4
gpt4 key购买 nike

我正在为 C 使用 libpng,我是图像处理的新手。

我使用 png_get_IHDR() 获取宽度、高度、color_type 和 bit_depth,我得到的图像值是:

color_type = 6 (PNG_COLOR_TYPE_RGB_ALPHA)
bit_depth = 8
width = 1850
height = 2048

I use png_get_channels() to get the channels which is 4
I use png_get_rowbytes() to get the bytes per row which is 7400 (1850*4)
I use png_read_image() to get all the image data

我的目标是将 RGB 值转换为像素值,仅此而已!我知道每个像素的 R、G、B 和 Alpha 值都按顺序存储在图像数据缓冲区中,似乎不同的 bit_depth 转换方法不同。我也可以忽略 Alpha 值,在进行转换时只使用 R、G、B 值吗?任何人都可以帮忙吗?谢谢!

最佳答案

如果你不想担心 color_type 和 bit_depth 的差异,并且总是想忽略 alpha channel ,使用

png_set_expand(png_ptr);
png_set_strip_16(png_ptr);
if (color_type & PNG_COLOR_MASK_ALPHA)
png_set_strip_alpha(png_ptr);

那么您的行将始终包含 8 位 R,G,B,R,G,B ... 样本,每个像素一组三个字节,每行“宽度”组。

如果您使用 libpng-1.5.6 或更高版本构建,您可以使用“png_set_scale_16(png_ptr)”代替;它缩放 16 位样本精确到 8 位。

关于c - libpng,c代码,如何将RGB值转换为像素值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22185800/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com