C++ 使用libpng将转换设置为灰度
我正在使用libpng读取图像。图像本身有4个通道(rgba),通道为8位。我想把它读成灰度图像,最好是黑白(每像素1位),但即使是每像素8位也是一种改进 为此,我编写了以下函数:C++ 使用libpng将转换设置为灰度,c++,libpng,C++,Libpng,我正在使用libpng读取图像。图像本身有4个通道(rgba),通道为8位。我想把它读成灰度图像,最好是黑白(每像素1位),但即使是每像素8位也是一种改进 为此,我编写了以下函数: extern "C" void cuttingStream::set_image_transformations_in_grayscale( pngImageInfo* info,pngDataStructures* png) { //alpha channel is removed if (info
extern "C" void cuttingStream::set_image_transformations_in_grayscale( pngImageInfo* info,pngDataStructures* png)
{
//alpha channel is removed
if (info->color_type & PNG_COLOR_MASK_ALPHA != PNG_COLOR_TYPE_GRAY)
png_set_strip_alpha(png->png_struct_field);
//switch to rgb is completed
if (info->color_type & PNG_COLOR_MASK_COLOR != PNG_COLOR_TYPE_GRAY)
png_set_rgb_to_gray(png->png_struct_field,1,NULL,NULL);
png_read_update_info(png->png_struct_field, png->png_info_field);
info -> rowbytes = png_get_rowbytes(png->png_struct_field, png->png_info_field);
//bits per CHANNEL! note: not per pixel!
info->bitdepth = png_get_bit_depth(png->png_struct_field, png->png_info_field);
//Number of channels
info->channels = png_get_channels(png->png_struct_field, png->png_info_field);
//Color type. (RGB, RGBA, Luminance, luminance alpha... palette... etc)
info->color_type = png_get_color_type(png->png_struct_field, png->png_info_field);
}
pngDataStructures是我自己的结构,它包含诸如png_structp、png_infop和指向自定义读取函数的指针等内容。pngImageInfo保存了一些我感兴趣的关于图像的信息,可以在代码中看到
问题是,在代码执行后,通道的数量不会改变。如何解决这个问题?运算符优先级问题: //阿尔法通道被移除 如果((info->color\u type&PNG\u color\u MASK\u ALPHA)!=PNG\u color\u type\u GRAY) 运算符和的优先级小于运算符=强>