简体   繁体   English

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

[英]libpng, c code, how to convert RGB value to pixel value?

I am using libpng for C, I am new to image processing. 我正在为C使用libpng,我是图像处理的新手。

I use png_get_IHDR() to get the width, height, color_type and bit_depth, the values I got for an image are: 我使用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

My aim is to convert the RGB values to pixel value, that's all ! 我的目标是将RGB值转换为像素值,仅此而已! I know the R, G, B and Alpha values of each pixel are stored orderly in the image data buffer, seems the conversion methods are different for different bit_depth. 我知道每个像素的R,G,B和Alpha值有序地存储在图像数据缓冲区中,对于不同的bit_depth,似乎转换方法是不同的。 Also can I ignore Alpha value, only use R, G, B values when doing the conversion? 还可以忽略Alpha值,在进行转换时仅使用R,G,B值吗? Anyone can help? 有人可以帮忙吗? Thanks! 谢谢!

If you don't want to worry about differences due to color_type and bit_depth, and always want to ignore the alpha channel, use 如果您不想担心由于color_type和bit_depth而引起的差异,并且始终想忽略Alpha通道,请使用

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

Then your rows will always contain 8-bit R,G,B, R,G,B ... samples, one group of three bytes per pixel, "width" groups per row. 然后,您的行将始终包含8位R,G,B,R,G,B ...样本,一组每像素3个字节,每行“宽度”组。

If you are building with libpng-1.5.6 or later, you can use "png_set_scale_16(png_ptr)" instead; 如果您使用的是libpng-1.5.6或更高版本,则可以使用“ png_set_scale_16(png_ptr)”代替; it scales 16-bit samples down to 8-bit more accurately. 它可以将16位样本更精确地缩减为8位。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM