简体   繁体   English

无法使用CImg加载任何图像

[英]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_pngcimg_use_jpeg ,但是随后出现编译错误,告诉我需要png.njpeglib.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图像,则需要安装libpnglibz (用于压缩)并针对它们进行编译和链接。

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包含文件(对于CImglibjpeg )在哪里,以及库文件(对于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安装到您的项目

  1. Download jpegsr9c.zip (or newer) zip with source code 下载带有源代码的jpegsr9c.zip (或更高版本)zip 下载jpeg源代码

  2. Extract the archive to a folder. 将存档解压缩到文件夹。

  3. Copy the address of the folder extracted from archive 复制从存档中提取的文件夹的地址 存档中的文件夹

  4. Search for Developer Command Prompt in Start menu 在“开始”菜单中搜索“ 开发人员命令提示符” 开始菜单搜索

  5. 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. 使用右键单击粘贴。

更改目录

  1. Run this command: 运行以下命令:

    NMAKE /f makefile.vs setup-v15 NMAKE / f makefile.vs setup-v15

运行nmake

  1. Open jpeg.sln (which was now created). 打开jpeg.sln (现已创建)。

新文件

  1. In Solution Explorer, right click on the project. 在解决方案资源管理器中,右键单击该项目。 Select Retarget projects. 选择重新定位项目。 Confirm prompt. 确认提示。

重新定位项目

  1. 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 选择配置管理器 新平台 x64平台

  2. Build solution 建立解决方案

建立解决方案

建立成功

  1. 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 其他包含目录 附加图书馆目录

  2. Add jpeg.lib to Project Properties -> Linker -> Input -> Additional Dependencies jpeg.lib添加到Project Properties -> Linker -> Input -> Additional Dependencies

其他依赖

  1. Copy x64/Release/jpeg.lib to this folder. x64/Release/jpeg.lib复制到此文件夹。

  2. Search for all header files (` .h ) in jpeg project and copy them to this folder. 在jpeg项目中搜索所有头文件(` .h ),然后将它们复制到此文件夹。

全部文件

  1. Add #define cimg_use_jpeg before #include "CImg.h" in your project 在项目中的#include "CImg.h"之前添加#define cimg_use_jpeg

定义cimg_use_jpeg

  1. All done! 全部做完! CImg is now able to read and save jpeg files. CImg现在可以读取和保存jpeg文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM