首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >LibPng读入结构

LibPng读入结构
EN

Stack Overflow用户
提问于 2012-12-23 03:51:48
回答 1查看 406关注 0票数 1

我似乎无法让libpng将其数据转储到我的结构中。我不知道我做错了什么。我试图翻转字节,因为PNG是自上而下存储的,而我需要数据自下而上。

首先,我的struct看起来像:

代码语言:javascript
复制
typedef union RGB
{
    uint32_t Color;
    struct
    {
        unsigned char B, G, R, A;
    } RGBA;
} *PRGB;

然后我创建了一个向量:

代码语言:javascript
复制
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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-12-23 03:56:14

不是应该这样吗:

代码语言:javascript
复制
Pixels[(height - 1 - I) * width + J].RGBA.B = *(RowPointers[J]++);

而是通过I索引RowPointers

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14006066

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档