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 installlibjpeg
and compile and link against it.If you want to open
PNG
images, you need to installlibpng
andlibz
(for the compression) and compile and link against them.At the moment, you should be able to use NetPBM format images - i.e. 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 tookjpegsr9b.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:
Step 5
Now you are going to need to find a file called
win32.mak
. I found mine inC:\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:Step 6
Now run
nmake
to get your SLN - some weird Microsoft "solution" file. The command is: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 aRelease
directory in yourlibjpeg
directory and inside theRelease
directory you will findjpeg.lib
. You have now installedlibjpeg
.Step 8
Close that project, and start a new C++ command-line project and write your CImg-based program. I wrote the simplest ever:
Step 9
Download
CImg.h
from Github, and save it in a directory calledCImg
in your HOME directory.Step 10
Now tell Visual Studio where the include files (for
CImg
andlibjpeg
) are and where the library files (forlibjpeg
) 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: