无法使用CImg加载任何图像 [英] Cannot load any image with CImg
问题描述
每当我尝试加载图像时,都会出现错误消息CImg<unsigned char>::load(): Failed to recognize format of file
. jpg和png文件都会发生这种情况.
Any time I try to load an image I get an error saying CImg<unsigned char>::load(): Failed to recognize format of file
. This happens for both jpg and png files.
我发现了其他有关定义cimg_use_png
和cimg_use_jpeg
的说法,但是随后出现编译错误,告诉我需要png.n
和jpeglib.h
.不知道我应该从哪里得到这些.
I have found other posts about this saying to define cimg_use_png
and cimg_use_jpeg
, but then I get compilation errorstelling me I need png.n
and jpeglib.h
. Not sure where I'm supposed to get these from.
我不确定我哪里出了问题,所以我不知道该问些什么.怎么了?
I'm not sure where I've gone wrong, so I don't know what to ask specifically. What's gone wrong?
推荐答案
如果要打开JPEG
图像,则需要安装libjpeg
并对其进行编译和链接.
If you want to open JPEG
images, you need to install libjpeg
and compile and link against it.
如果要打开PNG
图像,则需要安装libpng
和libz
(用于压缩)并针对它们进行编译和链接.
If you want to open PNG
images, you need to install libpng
and libz
(for the compression) and compile and link against them.
目前,您应该能够使用NetPBM格式的图像-即PBM,PGM和PPM.
At the moment, you should be able to use NetPBM format images - i.e. PBM, PGM and PPM.
好吧,在经历了两天的痛苦工作之后,我们试图弄清 Visual Studio 2017 的工作原理,以及如何安装和集成libjpeg
,我现在可以解释如何安装 CImg 和Windows上的libjpeg.
Well, after two painful days of trying to work out how on Earth Visual Studio 2017 works, and how to install and integrate libjpeg
with it, I can now explain how to install CImg and libjpeg on Windows.
第1步
您需要安装 Visual Studio 2017 ,这意味着您需要带有SP1的Windows 7 或更高版本.安装时,请确保包含"Windows 8.1 SDK"
You need to have Visual Studio 2017 installed, which means you need Windows 7 with SP1 or better. When installing it, be sure to include "Windows 8.1 SDK"
第2步
从 IJG网站下载libjpeg
.我选择了jpegsr9b.zip
,因为它是最新版本.
Download libjpeg
from the IJG website. I took jpegsr9b.zip
as it is the latest.
第3步
解压缩文件并将其存储在HOME目录中名为libjpeg
的目录中.
Unzip the file and store it in a directory called libjpeg
in your HOME directory.
第4步
转到开始>所有程序> Microsoft Visual Studio 2017> Visual Studio工具> VS2017的开发人员命令提示符
导航到刚刚解压缩的目录.就像这样:
Navigate to the directory you just unzipped. That will be something like this:
cd libjpeg
cd jpeg-9b
第5步
现在,您需要找到一个名为win32.mak
的文件.我在C:\Program Files\Microsoft SDKs\Windows\v7.0\Include
中找到了我的.如果您拥有Windows 8.1 SDK,则可能在其他地方.无论如何,无论您身在何处,都需要将其包含目录添加到包含中.所以我做到了:
Now you are going to need to find a file called win32.mak
. I found mine in C:\Program Files\Microsoft SDKs\Windows\v7.0\Include
. Yours may be somewhere else if you have Windows 8.1 SDK. Anyway, wherever it is, you need to add its containing directory to your includes. So I did:
set INCLUDE=%INCLUDE%;C:\Program Files\Microsoft SDKs\Windows\v7.0\Include
第6步
现在运行nmake
以获得您的SLN-一些奇怪的Microsoft 解决方案" 文件.命令是:
Now run nmake
to get your SLN - some weird Microsoft "solution" file. The command is:
nmake -f makefile.vc setup-v10
您应该得到一个名为jpeg.sln
的文件-欢呼!
And you should get a file called jpeg.sln
- hurray!
第7步
现在启动 Visual Studio 2017 ,然后打开刚刚创建的jpeg.sln
文件并生成项目.它将在您的libjpeg
目录中创建一个Release
目录,在Release
目录中您将找到jpeg.lib
.您现在已经安装了libjpeg
.
Now start Visual Studio 2017, and open the jpeg.sln
file you just created and build the project. It will create a Release
directory in your libjpeg
directory and inside the Release
directory you will find jpeg.lib
. You have now installed libjpeg
.
第8步
关闭该项目,然后启动一个新的C ++命令行项目并编写基于CImg的程序.我写了有史以来最简单的书:
Close that project, and start a new C++ command-line project and write your CImg-based program. I wrote the simplest ever:
#define cimg_use_jpeg
#include "CImg.h"
using namespace cimg_library;
int main() {
CImg<unsigned char> img("C:\\Users\\Mark\\test.jpg");
img.display("Image");
return 0;
}
第9步
从 Github 下载CImg.h
,并将其保存在您计算机中名为CImg
的目录中HOME目录.
Download CImg.h
from Github, and save it in a directory called CImg
in your HOME directory.
第10步
现在告诉Visual Studio包含文件(对于CImg
和libjpeg
)在哪里以及库文件(对于libjpeg
)在哪里:
Now tell Visual Studio where the include files (for CImg
and libjpeg
) are and where the library files (for libjpeg
) are:
第11步
现在告诉 Visual Studio 2017 您要与libjpeg
链接:
第12步
现在,您可以编译,链接和运行您的 CImg 程序,并加载JPEG文件并将其显示在屏幕上!
Now you can compile, link and run your CImg program and load JPEG files and display them on the screen!
如果您在 Linux / macOS 上使用cmake
,请此答案为您展示了方式.
If you are using cmake
on Linux/macOS, this answer shows you the way.
如果要从命令行在 macOS 上进行编译,则要在屏幕上显示图像,则需要安装 XQuartz .像这样的东西:
If you are compiling on macOS from the command line, you'll need to have XQuartz installed if you are display images on the screen, and you'll want something like this:
g++ -std=c++11 sample.cpp -o sample -I /opt/X11/include -L /opt/X11/lib -lX11 -ljpeg -lpng -lz
这篇关于无法使用CImg加载任何图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!