如何将读取的图像(libpng)存储在std::vector<std::vector<uint8_t>>中?

How to store the readed image (libpng) in a std::vector<std::vector<uint8_t>>?

我想使用 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);

但这不起作用。 有人可以帮助我吗?

您需要对 pixels 进行更多操作才能获得与 png_read_image 兼容的内容,并且您需要在使用前填写 pixels

#include <algorithm>

size_t row_bytes = png_get_rowbytes(png, imageInfo);
std::vector<std::vector<png_byte>> pixels (height, std::vector<png_byte>(row_bytes));
std::vector<png_byte *> ppixels(height);
std::transform(pixels.begin(), pixels.end(), ppixels.begin(), [](auto & vec){ return vec.data(); });

png_read_image(png, ppixels.data());