C/C++ 图像加载
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3284498/
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/C++ Image Loading
提问by Jookia
I'm working on a game engine and I'm too much of a wuss to write image loaders for multiple formats, so my question is this: Is there an abstracted image loading library to load image files? I just need to load files then splat them on to the screen using an array of pixels.
我正在开发一个游戏引擎,我太笨了,无法为多种格式编写图像加载器,所以我的问题是:是否有一个抽象的图像加载库来加载图像文件?我只需要加载文件,然后使用像素数组将它们放到屏幕上。
回答by NG.
I'm always a fan of CImg. It's very easy to use. Another user liked the answeras well. I'll post the same example I posted in the answer so you can see how easy it is to access pixels and dimension info.
我一直是CImg的粉丝。它非常容易使用。另一位用户也喜欢这个答案。我将发布我在答案中发布的相同示例,以便您了解访问像素和尺寸信息是多么容易。
CImg<unsigned char> src("image.jpg");
int width = src.width();
int height = src.height();
unsigned char* ptr = src.data(10,10); // get pointer to pixel @ 10,10
unsigned char pixel = *ptr;
回答by Tomaka17
FreeImageis a good open source library
FreeImage是一个很好的开源库
Here is an example code, data is accessible with "out.data()"
这是一个示例代码,数据可以通过“out.data()”访问
FREE_IMAGE_FORMAT format = FreeImage_GetFileTypeU(filename.c_str());
if (format == FIF_UNKNOWN) format = FreeImage_GetFIFFromFilenameU(filename.c_str());
if (format == FIF_UNKNOWN) throw(std::runtime_error("File format not supported"));
FIBITMAP* bitmap = FreeImage_LoadU(format, filename.c_str());
FIBITMAP* bitmap2 = FreeImage_ConvertTo32Bits(bitmap);
FreeImage_Unload(bitmap);
std::vector<char> out(FreeImage_GetWidth(bitmap2) * FreeImage_GetHeight(bitmap2) * 4);
FreeImage_ConvertToRawBits((BYTE*)out.data(), bitmap2, FreeImage_GetWidth(bitmap2) * 4, 32, FI_RGBA_RED_MASK, FI_RGBA_GREEN_MASK, FI_RGBA_BLUE_MASK, true);
FreeImage_Unload(bitmap2);
回答by Gianni
There is Boost's GILwhich was originally developed by Adobe. It might not be the most intuitive one, but it certainly is one of the most complete and powerful libs.
有Boost的GIL它最初是由Adobe开发。它可能不是最直观的,但它肯定是最完整和最强大的库之一。
回答by Zoli
If you're using OpenGL, then DevILis a good choice since its style and conventions adhere to OpenGL more than any other library. It's relatively easy to set up and also has great support for multiple formats.
如果您使用 OpenGL,那么DevIL是一个不错的选择,因为它的风格和约定比任何其他库都更符合 OpenGL。它相对容易设置,并且对多种格式也有很好的支持。
One thing about the wuss thing. While it's nice to set up working 3rd party code that has been well tested and saves you time, there's something to be also said about learning how image loading works and why it works the way it does. If you really want to get good at something, I believe that you need to actually learn how to do it from scratch. Even if you end up using 3rd party code in the end.
关于wuss的一件事。虽然设置经过良好测试并节省时间的有效 3rd 方代码很好,但关于了解图像加载的工作原理以及为什么它会以这种方式工作,还有一些话要说。如果你真的想擅长某件事,我相信你需要真正从头开始学习如何去做。即使您最终使用了第 3 方代码。
回答by Michael Kristofik
You could have a look at SDL_Image, a sub-project of the Simple DirectMedia Layer. It serves as an easy abstraction for loading several different image formats. SDL is written in C but is easy to use with either C or C++ code.
您可以查看SDL_Image,它是Simple DirectMedia Layer的子项目。它用作加载几种不同图像格式的简单抽象。SDL 是用 C 编写的,但很容易与 C 或 C++ 代码一起使用。
回答by Jerry Coffin
回答by Vertexwahn
OpenImageIOsupports many different file formats among them TIFF, JPEG/JFIF, OpenEXR, PNG, HDR/RGBE, ICO, BMP, Targa, JPEG-2000, RMan Zfile, FITS, DDS, Softimage PIC, PNM, DPX, Cineon, IFF, Field3D, Ptex, Photoshop PSD, Wavefront RLA, SGI, WebP, and GIF
OpenImageIO支持许多不同的文件格式,其中包括 TIFF、JPEG/JFIF、OpenEXR、PNG、HDR/RGBE、ICO、BMP、Targa、JPEG-2000、RMan Zfile、FITS、DDS、Softimage PIC、PNM、DPX、Cineon、IFF、 Field3D、Ptex、Photoshop PSD、Wavefront RLA、SGI、WebP 和 GIF