LibPng读入Struct

时间:2012-12-22 19:51:48

标签: c++ libpng

我似乎无法将libpng转储到我的struct中。我无法弄清楚我做错了什么。我试图翻转字节,因为PNG存储在顶部>向下,我需要数据底部 - >向上。

首先我的结构如下:

typedef union RGB
{
    uint32_t Color;
    struct
    {
        unsigned char B, G, R, A;
    } RGBA;
} *PRGB;

然后我创建了一个矢量:

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);

我做错了什么?如何获取PNG的像素并将其颠倒存储?我对位图使用了相同的技术,但PNG不起作用:l

1 个答案:

答案 0 :(得分:3)

不应该这样:

Pixels[(height - 1 - I) * width + J].RGBA.B = *(RowPointers[J]++);

通过RowPointers代替I