[英]Cannot load any image with CImg
Any time I try to load an image I get an error saying CImg<unsigned char>::load(): Failed to recognize format of file
. 每当我尝试加载图像时,都会收到一条错误
CImg<unsigned char>::load(): Failed to recognize format of file
。 This happens for both jpg and png files. jpg和png文件都会发生这种情况。
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
. 我发现了其他有关定义
cimg_use_png
和cimg_use_jpeg
,但是随后出现编译错误,告诉我需要png.n
和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?
怎么了?
If you want to open JPEG
images, you need to install libjpeg
and compile and link against it. 如果要打开
JPEG
图像,则需要安装libjpeg
并对其进行编译和链接。
If you want to open PNG
images, you need to install libpng
and libz
(for the compression) and compile and link against them. 如果要打开
PNG
图像,则需要安装libpng
和libz
(用于压缩)并针对它们进行编译和链接。
At the moment, you should be able to use NetPBM format images - ie PBM, PGM and PPM. 目前,您应该能够使用NetPBM格式的图像-即PBM,PGM和PPM。
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. 好了,经过两天的痛苦工作,试图弄清楚Earth Visual Studio 2017的工作原理以及如何安装
libjpeg
并将其集成后,我现在可以解释如何在Windows上安装CImg和libjpeg。
Step 1 第1步
You need to have Visual Studio 2017 installed, which means you need Windows 7 with SP1 or better. 您需要安装Visual Studio 2017 ,这意味着您需要Windows 7 SP1或更高版本。 When installing it, be sure to include "Windows 8.1 SDK"
安装时,请确保包括“ Windows 8.1 SDK”
Step 2 第2步
Download libjpeg
from the IJG website . 从IJG网站下载
libjpeg
。 I took jpegsr9b.zip
as it is the latest. 我选了
jpegsr9b.zip
因为它是最新的。
Step 3 第三步
Unzip the file and store it in a directory called libjpeg
in your HOME directory. 解压缩文件并将其存储在HOME目录中的
libjpeg
目录中。
Step 4 第四步
Go to Start>All Programs>Microsoft Visual Studio 2017>Visual Studio Tools > Developer Command Prompt for VS2017 转到开始>所有程序> 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
Step 5 第5步
Now you are going to need to find a file called win32.mak
. 现在,您将需要找到一个名为
win32.mak
的文件。 I found mine in C:\\Program Files\\Microsoft SDKs\\Windows\\v7.0\\Include
. 我在
C:\\Program Files\\Microsoft SDKs\\Windows\\v7.0\\Include
找到了我的。 Yours may be somewhere else if you have Windows 8.1 SDK. 如果您拥有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
Step 6 第6步
Now run nmake
to get your SLN - some weird Microsoft "solution" file. 现在运行
nmake
以获得您的SLN-一些奇怪的Microsoft “解决方案”文件。 The command is: 该命令是:
nmake -f makefile.vc setup-v10
And you should get a file called jpeg.sln
- hurray! 而且您应该得到一个名为
jpeg.sln
的文件-欢呼!
Step 7 步骤7
Now start Visual Studio 2017 , and open the jpeg.sln
file you just created and build the project. 现在启动Visual Studio 2017 ,然后打开刚刚创建的
jpeg.sln
文件并生成项目。 It will create a Release
directory in your libjpeg
directory and inside the Release
directory you will find jpeg.lib
. 它将在您的
libjpeg
目录中创建一个Release
目录,并在Release
目录中找到jpeg.lib
。 You have now installed libjpeg
. 您现在已经安装了
libjpeg
。
Step 8 步骤8
Close that project, and start a new C++ command-line project and write your CImg-based program. 关闭该项目,然后启动一个新的C ++命令行项目,并编写基于CImg的程序。 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;
}
Step 9 步骤9
Download CImg.h
from Github , and save it in a directory called CImg
in your HOME directory. 从Github下载
CImg.h
,并将其保存在HOME目录中名为CImg
的目录中。
Step 10 第10步
Now tell Visual Studio where the include files (for CImg
and libjpeg
) are and where the library files (for libjpeg
) are: 现在告诉Visual Studio包含文件(对于
CImg
和libjpeg
)在哪里,以及库文件(对于libjpeg
)在哪里:
Step 11 步骤11
Now tell Visual Studio 2017 that you want to link with libjpeg
: 现在告诉Visual Studio 2017您要与
libjpeg
链接:
Step 12 步骤12
Now you can compile, link and run your CImg program and load JPEG files and display them on the screen! 现在,您可以编译,链接和运行CImg程序,并加载JPEG文件并将其显示在屏幕上!
If you are using cmake
on Linux / macOS , this answer shows you the way. 如果您在Linux / macOS上使用
cmake
,则此答案将向您展示方法。
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: 如果要从命令行在macOS上进行编译,则在屏幕上显示图像时需要安装XQuartz ,并且需要以下内容:
g++ -std=c++11 sample.cpp -o sample -I /opt/X11/include -L /opt/X11/lib -lX11 -ljpeg -lpng -lz
If you want CImg to work with jpeg or png images, you need to supply appropriate library. 如果要CImg处理jpeg或png图像,则需要提供适当的库。
Here's a tutorial how to install libjpeg to Visual Studio project. 这是一个如何将libjpeg安装到Visual Studio项目的教程。 It's basically a 2019 update to Mark Setchell's answer.
这基本上是Mark Setchell的答案的2019年更新。
How to install libjpeg to your project 如何将libjpeg安装到您的项目
Download jpegsr9c.zip (or newer) zip with source code 下载带有源代码的jpegsr9c.zip (或更高版本)zip
Extract the archive to a folder. 将存档解压缩到文件夹。
Copy the address of the folder extracted from archive 复制从存档中提取的文件夹的地址
Search for Developer Command Prompt in Start menu 在“开始”菜单中搜索“ 开发人员命令提示符”
Navigate to extracted folder in Developer Command Prompt: 导航到“开发人员命令提示符”中的提取文件夹:
cd "C:\\Users\\HP\\Downloads\\temp\\jpeg-9c"
cd“ C:\\ Users \\ HP \\ Downloads \\ temp \\ jpeg-9c”
Use right click to paste. 使用右键单击粘贴。
Run this command: 运行以下命令:
NMAKE /f makefile.vs setup-v15
NMAKE / f makefile.vs setup-v15
jpeg.sln
(which was now created). jpeg.sln
(现已创建)。 If you want to build your project for 64-bit platform: Right click on solution, select Configuration Manager and add new platform x64 如果要为64位平台构建项目:右键单击解决方案,选择Configuration Manager并添加新平台x64
Build solution 建立解决方案
Now let's move to your project with CImg. 现在,让我们使用CImg移至您的项目。 Create a folder jpeglib in your project (it's IncludedLibraries\\jpeglib for me).
在您的项目中创建一个文件夹jpeglib(对我来说是IncludedLibraries \\ jpeglib)。 And add it to
Project Properties -> C/C++ -> Additional Include Directories
and to Project Properties -> Linker -> Additional Library Directories
并将其添加到
Project Properties -> C/C++ -> Additional Include Directories
并添加到Project Properties -> Linker -> Additional Library Directories
Add jpeg.lib
to Project Properties -> Linker -> Input -> Additional Dependencies
将
jpeg.lib
添加到Project Properties -> Linker -> Input -> Additional Dependencies
Copy x64/Release/jpeg.lib
to this folder. 将
x64/Release/jpeg.lib
复制到此文件夹。
Search for all header files (` .h ) in jpeg project and copy them to this folder. 在jpeg项目中搜索所有头文件(` .h ),然后将它们复制到此文件夹。
#define cimg_use_jpeg
before #include "CImg.h"
in your project #include "CImg.h"
之前添加#define cimg_use_jpeg
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.