Reputation: 285
I want to read a png image using the libpng and store the pixel values into a std::vectorstd::vector<uint8_t>.
But the compilator throw an error.
My C++ code is :
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";
I have compiled my code like this : (on Linux)
g++ -o yourBinary youFile.cc -lpng
Now I want to replace this :
// 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";
By this :
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";
The error is :
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);
But this doesn't work. Can someone help me ?
Upvotes: 0
Views: 1162
Reputation: 62686
You need to do more to pixels
to get something compatible with png_read_image
, and you need to fill pixels
before using it.
#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());
Upvotes: 1