【问题标题】:How to store the readed image (libpng) in a std::vector<std::vector<uint8_t>>?如何将读取的图像(libpng)存储在 std::vector<std::vector<uint8_t>> 中?
【发布时间】: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_bytepptypedef unsigned char png_byte;。换句话说,第二个参数必须是unsigned char **。但是你给出的是向量的向量,这是完全不同的类型。

标签: c++ stdvector libpng


【解决方案1】:

您需要对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());

【讨论】:

  • 谢谢,如何使用 uint8_t ? (std::vector<:vector>>) 我也使用适用于此的 libjpeg。
  • @PacCol uint8_t 是与 png_byte (unsigned char) 相同类型的别名,在这种情况下你已经是,或者你不应该在这里使用它。
  • @PacCol 它在&lt;algorithm&gt;
  • 编译器不会抛出任何错误,但是当我尝试运行程序时,终端会打印: *** Error in `./main': double free or corruption (out): 0x0068c6d8 * **
  • widthpng_get_rowbytes(png, imageInfo)是什么关系?我可能有错误大小的内部向量
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-09
  • 1970-01-01
  • 2015-09-15
  • 1970-01-01
  • 1970-01-01
  • 2019-10-12
  • 2018-12-13
相关资源
最近更新 更多