Reputation: 23498
I cannot seem to get libpng to dump its data into my struct. I can't figure out what I'm doing wrong. I'm trying to flip the bytes because PNG's are stored top->down and I need the data bottom->up.
First my struct looks like:
typedef union RGB
{
uint32_t Color;
struct
{
unsigned char B, G, R, A;
} RGBA;
} *PRGB;
Then I created a vector as such:
png_init_io(PngPointer, hFile);
png_set_sig_bytes(PngPointer, 8);
png_read_info(PngPointer, InfoPointer);
uint32_t width, height;
int bitdepth, colortype, interlacetype, channels;
png_set_strip_16(PngPointer);
channels = png_get_channels(PngPointer, InfoPointer);
png_get_IHDR(PngPointer, InfoPointer, &width, &height, &bitdepth, &colortype, &interlacetype, nullptr, nullptr);
uint32_t RowBytes = png_get_rowbytes(PngPointer, InfoPointer);
unsigned char** RowPointers = png_get_rows(PngPointer, InfoPointer);
std::vector<RGB> Pixels(RowBytes * height); //Amount of bytes in one row * height of image.
//Crashes in the for loop below :S
for (int I = 0; I < height; I++)
{
for (int J = 0; J < width; J++)
{
Pixels[(height - 1 - I) * width + J].RGBA.B = *(RowPointers[J]++);
Pixels[(height - 1 - I) * width + J].RGBA.G = *(RowPointers[J]++);
Pixels[(height - 1 - I) * width + J].RGBA.R = *(RowPointers[J]++);
}
}
std::fclose(hFile);
png_destroy_read_struct(&PngPointer, &InfoPointer, nullptr);
What did I do wrong? How can I get the pixels of the PNG and store them upside down? I used the same technique for bitmaps but PNG just isn't working :l
Upvotes: 1
Views: 538
Reputation: 16582
Shouldn't this:
Pixels[(height - 1 - I) * width + J].RGBA.B = *(RowPointers[J]++);
Index RowPointers
via I
instead?
Upvotes: 3