在C ++中将PNG图像嵌入为矢量或数组

时间:2013-06-09 19:40:24

标签: c++ image-processing stl libpng

任何人都可以指向一个方向,这样我就可以获取我拥有的PNG图像并将其读入,然后将PNG图像的数据存储到数组或矢量(或其他一些数据结构)中,这样我就可以使用了在我的代码中而不是必须读入PNG等?

我知道我可以使用libPNG来读取图像但是一旦读入,我就会对如何读取内容并将其转换为我可以在游戏中使用的数据结构感到有点困惑。

所以我的想法是我可以写一个简单的控制台程序,我提供一个PNG列表,它读取它们并向我发送数据文件,以便我在我的实际游戏中硬编码成数据结构。

2 个答案:

答案 0 :(得分:0)

加载图片;取其宽度,高度和钻头深度(如果需要,可能还有更多信息);读取其颜色数据,并保存您提取的信息。

数据结构的示例:

int width;
int height;
int bits;
std::vector<int> data[width*height*bits];

使用该数据结构时:

/*
   Before these lines, you must get image dimensions and save them to width, height, depth variables. (either read them from somewhere or simply assign e.g. width=1024 if you know the width (I assume you do, as you're going to hardcode the image)).
*/
std::vector<int> sprite(width*height*depth);
for (int i = 0; i < width*height*depth; i++)
   // sprite[i] = (read pixel i from somewhere...);

答案 1 :(得分:0)

在您阅读了数据后,Jason表示您可以使用结构来包含每个像素的数据。

struct RGBAQUAD
{
     int Red;
     int Green;
     int Blue;
     int Alpha;
}

然后你可以创建一个像这样的2D数组,将所有像素结构表示为一个连续的图像。但缺点是必须管理内存。

RGBQUAD **arr = 0;
arr = new RGBQUAD *[y];
for(int i  = 0; i < y ; i++)
    arr[i] = new RGBQUAD[x];

或者你可以将一个像素打包成一个int以节省ram空间。