C++ 创建图像
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1863344/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++ creating image
提问by Adam
I haven't been programming in C++ for a while, and now I have to write a simple thing, but it's driving me nuts.
我有一段时间没有用 C++ 编程,现在我必须写一个简单的东西,但这让我发疯。
I need to create a bitmap from a table of colors:
char image[200][200][3];
我需要从颜色表创建位图:
char image[200][200][3];
First coordinate is width, second height, third colors: RGB. How to do it?
第一个坐标是宽度,第二个高度,第三个颜色:RGB。怎么做?
Thanks for any help. Adam
谢谢你的帮助。亚当
回答by Ron Warholic
I'm sure you've already checked http://en.wikipedia.org/wiki/BMP_file_format.
我确定您已经检查过http://en.wikipedia.org/wiki/BMP_file_format。
With that information in hand we can write a quick BMP with:
有了这些信息,我们可以编写一个快速的 BMP:
// setup header structs bmpfile_header and bmp_dib_v3_header before this (see wiki)
// * note for a windows bitmap you want a negative height if you're starting from the top *
// * otherwise the image data is expected to go from bottom to top *
FILE * fp = fopen ("file.bmp", "wb");
fwrite(bmpfile_header, sizeof(bmpfile_header), 1, fp);
fwrite(bmp_dib_v3_header, sizeof(bmp_dib_v3_header_t), 1, fp);
for (int i = 0; i < 200; i++) {
for (int j = 0; j < 200; j++) {
fwrite(&image[j][i][2], 1, 1, fp);
fwrite(&image[j][i][1], 1, 1, fp);
fwrite(&image[j][i][0], 1, 1, fp);
}
}
fclose(fp);
If setting up the headers is a problem let us know.
如果设置标题有问题,请告诉我们。
Edit: I forgot, BMP files expect BGR instead of RGB, I've updated the code (surprised nobody caught it).
编辑:我忘了,BMP 文件需要 BGR 而不是 RGB,我已经更新了代码(很惊讶没有人发现它)。
回答by dagoof
I'd suggest ImageMagick, comprehensive library etc.
我建议使用ImageMagick、综合库等。
回答by Jaime Ivan Cervantes
For simple image operations I highly recommend Cimg. This library works like a charm, and is extremely easy to use. You just have to include a header file in your code. It literally took me less than 10 minutes to compile and test.
对于简单的图像操作,我强烈推荐Cimg。这个库就像一个魅力,非常容易使用。您只需要在代码中包含一个头文件。从字面上看,我花了不到 10 分钟的时间来编译和测试。
If you want to do more complicated image operations however, I would go with Magick++as suggested by dagoof.
但是,如果您想做更复杂的图像操作,我会按照 dagoof 的建议使用Magick++。
回答by Igor Oks
I would first try to find out, how the BMP file format(that's what you mean by a bitmap, right?) is defined. Then I would convert the array to that format and print it to the file.
我将首先尝试找出BMP 文件格式(这就是位图的意思,对吧?)是如何定义的。然后我会将数组转换为该格式并将其打印到文件中。
If that's an option, I would also consider trying to find an existing library for BMP files creation, and just use it.
如果这是一个选项,我还会考虑尝试查找用于创建 BMP 文件的现有库,然后使用它。
Sorry if what I said is already obvious for you, but I don't know on which stage of the process you are stuck.
对不起,如果我说的对你来说已经很明显了,但我不知道你卡在了流程的哪个阶段。
回答by Goz
It would be advisable to initialise the function as a simple 1 dimensional array.
建议将函数初始化为简单的一维数组。
ie (Where bytes is the number of bytes per pixel)
即(其中字节是每个像素的字节数)
char image[width * height * bytes];
You can then access the relevant position in the array as follows
然后可以按如下方式访问数组中的相关位置
char byte1 = image[(x * 3) + (y * (width * bytes)) + 0];
char byte2 = image[(x * 3) + (y * (width * bytes)) + 1];
char byte3 = image[(x * 3) + (y * (width * bytes)) + 2];