【发布时间】:2020-11-19 23:25:48
【问题描述】:
我想使用 libpng 读取 png 图像并将像素值存储到 std::vectorstd::vector
但是编译器会报错。
我的 C++ 代码是:
char fileName[] = "test.png";
// We try to open the image file
FILE * inputImageFile;
if((inputImageFile = fopen(fileName, "rb")) == NULL) {
throw std::runtime_error("Error : can't open this file");
}
// We start the decompression
png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
png_infop imageInfo = png_create_info_struct(png);
png_init_io(png, inputImageFile);
png_read_info(png, imageInfo);
// We store the image informations into privates variables
unsigned int width = png_get_image_width(png, imageInfo);
unsigned int height = png_get_image_height(png, imageInfo);
unsigned int colorType = png_get_color_type(png, imageInfo);
unsigned int bitDepth = png_get_bit_depth(png, imageInfo);
// We continue to read the image
png_read_update_info(png, imageInfo);
// We create a table to store the pixels values
png_bytep * rowPointers;
rowPointers = (png_bytep*)malloc(sizeof(png_bytep) * height);
// We allocate memory
for(unsigned int i = 0; i < height; i++) {
rowPointers[i] = (png_byte*)malloc(png_get_rowbytes(png, imageInfo));
}
// We finish the decompression
png_read_image(png, rowPointers);
png_destroy_read_struct(&png, &imageInfo, NULL);
std::cout << "Image Dimensions : " << width << "x" << height <<"\n";
// Now you can get the rgb values like this :
int x = 4;
int y = 7;
png_bytep pixel = &(rowPointers[y][x * 4]); // 4 for R, G, B, and the alpha value (the transparance)
// The "+" is here to print the value as a number
std::cout << "Image Pixel (x = 4, y = 7) : RGB(" << +pixel[0] << ", " << +pixel[1] << ", " << +pixel[2] << ")" << "\n";
std::cout << "Image Pixel (x = 4, y = 7) : transparance : " << +pixel[3] << "\n";
我的代码是这样编译的:(在 Linux 上)g++ -o yourBinary youFile.cc -lpng
现在我想替换这个:
// We create a table to store the pixels values
png_bytep * rowPointers;
rowPointers = (png_bytep*)malloc(sizeof(png_bytep) * height);
// We allocate memory
for(unsigned int i = 0; i < height; i++) {
rowPointers[i] = (png_byte*)malloc(png_get_rowbytes(png, imageInfo));
}
// We finish the decompression
png_read_image(png, rowPointers);
png_destroy_read_struct(&png, &imageInfo, NULL);
// Now you can get the rgb values like this :
int x = 4;
int y = 7;
png_bytep pixel = &(rowPointers[y][x * 4]); // 4 for R, G, B, and the alpha value (the transparance)
std::cout << "Image Dimensions : " << width << "x" << height <<"\n";
std::cout << "Image Pixel (x = 4, y = 7) : RGB(" << +pixel[0] << ", " << +pixel[1] << ", " << +pixel[2] << ")" << "\n";
std::cout << "Image Pixel (x = 4, y = 7) : transparance : " << +pixel[3] << "\n";
通过这个:
std::vector<std::vector<uint8_t>> pixels;
pixels.reserve(height);
// We finish the decompression
png_read_image(png, pixels);
png_destroy_read_struct(&png, &imageInfo, NULL);
// Now you can get the rgb values like this :
int x = 4;
int y = 7;
std::cout << "Image Dimensions : " << width << "x" << height <<"\n";
std::cout << "Image Pixel (x = 4, y = 7) : RGB(" << +pixels[y][x * 4] << ", " << +pixels[y][x * 4 + 1] << ", " << +pixels[y][x * 4 + 2] << ")" << "\n";
std::cout << "Image Pixel (x = 4, y = 7) : transparance : " << +pixels[y][x * 4 + 3] << "\n";
错误是:
test.cc: In function ‘int main()’:
test.cc:60:29: error: cannot convert ‘std::vector<std::vector<unsigned char> >’ to ‘png_bytepp {aka unsigned char**}’ for argument ‘2’ to ‘void png_read_image(png_structrp, png_bytepp)’
png_read_image(png, pixels);
但这不起作用。 有人可以帮我吗?
【问题讨论】:
-
你能告诉我们错误吗?
-
当然可以。该帖子已被编辑。
-
看函数定义
void png_read_image(png_structp png_ptr, png_bytepp image);第二个参数的类型是png_bytepp。查看定义:typedef png_byte **png_bytepp,typedef unsigned char png_byte;。换句话说,第二个参数必须是unsigned char **。但是你给出的是向量的向量,这是完全不同的类型。