简体   繁体   中英

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 . This happens for both jpg and png files.

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?

If you want to open JPEG images, you need to install libjpeg and compile and link against it.

If you want to open PNG images, you need to install libpng and libz (for the compression) and compile and link against them.

At the moment, you should be able to use NetPBM format images - ie PBM, PGM and 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.

Step 1

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"

在此处输入图片说明

Step 2

Download libjpeg from the IJG website . I took jpegsr9b.zip as it is the latest.

在此处输入图片说明

Step 3

Unzip the file and store it in a directory called libjpeg in your HOME directory.

Step 4

Go to Start>All Programs>Microsoft Visual Studio 2017>Visual Studio Tools > Developer Command Prompt for VS2017

Navigate to the directory you just unzipped. That will be something like this:

cd libjpeg
cd jpeg-9b

Step 5

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

Step 6

Now run nmake to get your SLN - some weird Microsoft "solution" file. The command is:

nmake -f makefile.vc setup-v10

And you should get a file called jpeg.sln - hurray!

Step 7

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 .

在此处输入图片说明

Step 8

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;
}

Step 9

Download CImg.h from Github , and save it in a directory called CImg in your HOME directory.

Step 10

Now tell Visual Studio where the include files (for CImg and libjpeg ) are and where the library files (for libjpeg ) are:

在此处输入图片说明

Step 11

Now tell Visual Studio 2017 that you want to link with libjpeg :

在此处输入图片说明

Step 12

Now you can compile, link and run your CImg program and load JPEG files and display them on the screen!

在此处输入图片说明


If you are using cmake on Linux / macOS , this answer shows you the way.


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

If you want CImg to work with jpeg or png images, you need to supply appropriate library.

Here's a tutorial how to install libjpeg to Visual Studio project. It's basically a 2019 update to Mark Setchell's answer.

How to install libjpeg to your project

  1. Download jpegsr9c.zip (or newer) zip with source code 下载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"

Use right click to paste.

更改目录

  1. Run this command:

    NMAKE /f makefile.vs setup-v15

运行nmake

  1. Open jpeg.sln (which was now created).

新文件

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

  2. Build solution

建立解决方案

建立成功

  1. Now let's move to your project with CImg. Create a folder jpeglib in your project (it's IncludedLibraries\\jpeglib for me). And add it to Project Properties -> C/C++ -> Additional Include Directories and to Project Properties -> Linker -> Additional Library Directories 其他包含目录 附加图书馆目录

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

其他依赖

  1. Copy x64/Release/jpeg.lib to this folder.

  2. Search for all header files (` .h ) in jpeg project and copy them to this folder.

全部文件

  1. Add #define cimg_use_jpeg before #include "CImg.h" in your project

定义cimg_use_jpeg

  1. All done! CImg is now able to read and save jpeg files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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