C++ C++;在编译时将图像像素嵌入向量中?

C++ C++;在编译时将图像像素嵌入向量中?,c++,vector,C++,Vector,我通常使用极简图像加载库将图像读/写到像素数组中,通常这是一个无符号字符的std::向量,每个像素由4个无符号字符值(r0-255g0-255b0-255a1-255)表示 我想知道的是,如果我能在编译时嵌入一个图像(或更多)到我的C++程序中,这样在运行时,STD::向量已经被像素填充并准备好使用。这是因为我的应用程序有一堆图像,如果没有另外指定,它们将用作默认纹理 我的编译器是MingWGCC4.8.2 编辑: 根据目前发布的答案以及我在浏览时发现的其他答案,我想出了这个临时解决方案 一个将

我通常使用极简图像加载库将图像读/写到像素数组中,通常这是一个无符号字符的std::向量,每个像素由4个无符号字符值(r0-255g0-255b0-255a1-255)表示

我想知道的是,如果我能在编译时嵌入一个图像(或更多)到我的C++程序中,这样在运行时,STD::向量已经被像素填充并准备好使用。这是因为我的应用程序有一堆图像,如果没有另外指定,它们将用作默认纹理

我的编译器是MingWGCC4.8.2

编辑: 根据目前发布的答案以及我在浏览时发现的其他答案,我想出了这个临时解决方案

一个将图像转换为字节数组代码的小程序

将_文件_转换为_code.cpp

#include <cstdlib>
#include <cstdio>
#include <string>

int main(int argc, char** argv) {
    if (argc != 3)
    {
        printf("f2bytes row file\n");
        printf("row = after how many bytes to add a new line (0 for continuous)\n");
        printf("file = the name of the input file that should be converted\n");
        printf("note: a new file with the same name but .cpp appended will be create as output");
        return EXIT_FAILURE;
    }

    int rowl = atoi(argv[1]);

    std::string fsn(argv[2]);
    std::string fdn(argv[2]);
    fdn.append(".cpp");

    FILE* fs = fopen(fsn.c_str(), "rb");
    if (!fs)
        printf("Failed to open source file %s \n", fsn.c_str());

    FILE* fd = fopen(fdn.c_str(), "w");
    if (!fd)
        printf("Failed to open destination file %s \n", fdn.c_str());

    printf("transferring from %s to %s with row length of %d", fsn.c_str(), fdn.c_str(), rowl);

    fprintf(fd, "static const unsigned char file_data[] = {");

    if (rowl != 0) fprintf(fd, "\n");

    int n = 0;
    while(!feof(fs)) {
        unsigned char c;
        if(fread(&c, 1, 1, fs) == 0) break;
        fprintf(fd, "0x%.2X,", (int)c);
        ++n;
        if(rowl !=0 && n % rowl == 0) fprintf(fd, "\n");
    }

    fprintf(fd, "};\n");

    fclose(fs);
    fclose(fd);

    return EXIT_SUCCESS;
}
#包括
#包括
#包括
int main(int argc,字符**argv){
如果(argc!=3)
{
printf(“f2bytes行文件\n”);
printf(“行=添加新行的字节数之后(0表示连续)\n”);
printf(“file=应转换的输入文件的名称\n”);
printf(“注意:将创建一个名称相同但附加了.cpp的新文件作为输出”);
返回退出失败;
}
int rowl=atoi(argv[1]);
std::字符串fsn(argv[2]);
std::字符串fdn(argv[2]);
fdn.追加(“cpp”);
FILE*fs=fopen(fsn.c_str(),“rb”);
如果(!fs)
printf(“未能打开源文件%s\n”,fsn.c_str());
文件*fd=fopen(fdn.c_str(),“w”);
如果(!fd)
printf(“打开目标文件%s\n失败”,fdn.c_str());
printf(“从%s传输到%s,行长为%d”,fsn.c_str(),fdn.c_str(),rowl);
fprintf(fd,“静态常量无符号字符文件_data[]={”);
如果(rowl!=0)fprintf(fd,“\n”);
int n=0;
而(!feof(fs)){
无符号字符c;
如果(fread&c,1,1,fs)==0)中断;
fprintf(fd,“0x%.2X”,(int)c);
++n;
如果(rowl!=0&&n%rowl==0)fprintf(fd,“\n”);
}
fprintf(fd,“};\n”);
fclose(fs);
fclose(fd);
返回退出成功;
}
在运行时使用文件数据将字节数组转换为像素数组的程序

示例_program.cpp

#include <cstdlib>
#include <cstdio>
#include <string>
#include <vector>

#include "stb_image.h"

#define STB_IMAGE_WRITE_IMPLEMENTATION

#include "stb_image_write.h"

static const unsigned char file_data[] = {0x89,0x50,0x4E,0x47,0x0D,0x0A,0x1A /* ... more data */};

int width, height, channel;
std::vector<unsigned char> pixel_data;

void read_data()
{
    // I know the image is a png data
    unsigned char* ptr = stbi_load_from_memory(&file_data[0], sizeof(file_data), &width, &height, &channel, STBI_rgb_alpha);

    if (ptr && width && height)
    {
        // Copy the loaded pixels to the pixel buffer
        pixel_data.resize(width * height * 4 /* R+G+B+A */ );
        memcpy(&pixel_data[0], ptr, pixel_data.size());

        // Free the loaded pixels
        stbi_image_free(ptr);

        printf("Loaded: %d x %d image \n", width, height);
    }
    else
    {
        printf("Failed: %s\n", stbi_failure_reason());
    }
}

void write_data(const std::string& file)
{
    std::string filename(file);
    filename.append(".png");
    if (!stbi_write_png(filename.c_str(), width, height, 4, &pixel_data[0], 0))
        printf("Could not save %d x %d image to file %s\n", width, height, filename.c_str());

}

int main(void)
{
    read_data();
    write_data("myimage");

    return EXIT_SUCCESS;
}
#包括
#包括
#包括
#包括
#包括“stb_image.h”
#定义机顶盒映像写入实现
#包括“stb_image_write.h”
静态常量无符号字符文件_data[]={0x89,0x50,0x4E,0x47,0x0D,0x0A,0x1A/*…更多数据*/};
int宽度、高度、通道;
std::矢量像素_数据;
无效读取数据()
{
//我知道图像是png数据
unsigned char*ptr=stbi_load_from_memory(&file_data[0])、sizeof(file_data)、&width、&height、&channel、stbi_rgb_alpha);
if(ptr&宽度和高度)
{
//将加载的像素复制到像素缓冲区
像素数据。调整大小(宽度*高度*4/*R+G+B+A*/);
memcpy(&pixel_data[0],ptr,pixel_data.size());
//释放加载的像素
stbi_无图像(ptr);
printf(“已加载:%d x%d个图像\n”,宽度、高度);
}
其他的
{
printf(“失败:%s\n”,stbi_failure_reason());
}
}
无效写入数据(常量标准::字符串和文件)
{
std::字符串文件名(文件);
filename.append(“.png”);
如果(!stbi_write_png(filename.c_str()、宽度、高度、4和像素数据[0],0))
printf(“无法将%d x%d图像保存到文件%s\n”,宽度、高度、文件名.c_str());
}
内部主(空)
{
读取数据();
写入_数据(“myimage”);
返回退出成功;
}
这就是我到目前为止得到的。顺便说一句,图像不需要在编译时被替换,所以这个解决方案到目前为止仍然有效


我猜测我在读取像素之后保持FielyDATA [],但我会在读取像素后找到一个垃圾的方法。< / P > < P>如果我是你,我会写一些C++代码生成器代码,把图像读入你的数据存储,并扩展你的现有源代码。


该程序将作为构建过程的一部分进行预编译和运行。当然,C++也可以做到这一点,但这些东西通常都是用脚本语言编写的,比如Python,因为性能通常不是主要的问题。

谢谢这些信息,但是编译时不需要替换图像:<代码>我想知道的是,我是否可以嵌入图像(或更多)。在编译时,我的C++程序在运行时,STD::向量已经被像素填充并且准备使用。< /代码>坏表达式:我不希望图像是外部文件,而我的程序依赖于一个有时会丢失的图像,如果删除或损坏的话。- 1,因为它是另一个OP改变了意思是(虽然只是在回答注释中!)在原始(有效)问题得到回答后。我只是希望图像数据在编译时嵌入到我的可执行代码中,而不是在运行时从外部文件加载它。并且(仅限于)如果可能,它已经存储在std::vector中,而不是从字节数组中读取。但最后一部分只有在可能的情况下才有可能。那么我的答案有什么错呢?我想我是想报道那个案子的。除了确切的代码外,你还缺少什么?诚然,我不是在为您编写代码。:-)我可能会被处罚,因为这是离题的,但你的答案没有错。我只是给它时间看看是否还有其他答案。这与拍卖不同,我必须批准第一个答案。但我猜时间已经足够长了,所以我会接受你的答案。好的,但我不是有意让你接受我的答案。我只是想知道你看到了,我没有得到我能理解的批评。我的理解可能不正确,但我需要理解它,以便进一步帮助或从中学习。:-)