2

I'm trying to make CImg work with Visual Sudio 2017 and jpeg-9b but for some reason it doesn't.

Code:

#define cimg_use_jpeg
#include "../CImg/CImg.h"
using namespace cimg_library;

#pragma comment(lib, "libjpeg.a")

CImg<unsigned char> image("../images/img.jpg"), visu(500, 400, 1, 3, 0);

The first step is the build of the library, where I followed a description how to use the MinGW compiler. Even if the build succeeds, the generated library libjpeg.a is not linkable in the Visual Studio solution. Error Message:

State Error LNK2019 unresolved external symbol __imp___iob referenced in function _output_message

Another generated library libjpeg.dll.a throws an error when loading a .jpg file CImg Exception (picture)

After a couple of rebuilds the issue still exists. I also tried to use the VC compiler, but I cannot find the needed win32.mak file which is required when calling the build comand nmake -f makefile.vc setup-v10. Unofficial downloads of this file don't work properly on my pc. The generated solution cannot be opened in Visual Studio.

Is there maybe some workaround - if there is not a solution?

valiano
  • 16,433
  • 7
  • 64
  • 79
Klyse
  • 59
  • 2
  • 11

1 Answers1

5

You have several options if you want to read/write JPEG images with CImg:

Option 1

You can either install libjpeg which I have already covered in some detail here. If you follow this option, you must use:

#define cimg_use_jpeg

and CImg will compile and link against libjpeg and read and write JPEG images. You will need to have compiled and installed libjpeg to use this option.

Option 2

You can install ImageMagick and CImg can delegate reading and writing to it. If you choose this route, be sure to tick the box entitled ”Install legacy commands” or similar, during installation of ImageMagick. This option gives you the benefit of being able to read and write about 200 other image formats in addition to JPEG. It is also very useful for generating test images and trying out algorithms without needing to code any C++. The main downside is that it is quite a large distribution.

If you want to use this option, you must not #define cimg_use_jpeg nor #define cimg_use_magick. CImg will look for the convert.exe or magick.exe of ImageMagick at runtime and ImageMagick will not be known to, or involved with your Visual Studio project. You can call:

imagemagick_path()

to tell CImg at runtime where you have installed ImageMagick's convert.exe or magick.exe. If you use this option, you should use the generic save() method when saving images, rather than save_jpeg() or save_png().

This is, by far, the easiest option.

Option 3

If you want to use this option, you must do:

#define cimg_use_magick

and then CImg will compile and link with Magick++ - the C++ interface to ImageMagick. This will require you to have installed the source code for Magick++ and is probably the hardest option to achieve.


Depending on your goals, you could conceivably install the much simpler, lighter weight, NetPBM tools and convert your images to PBM/PGM/PPM format since CImg can natively read and write those without any external libraries at all.

You could, conceivably, use C++'s system() to do this dynamically when loading images.

Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • I tried to follow option 1 however, i could not find the win32.mak file, so this didn't work. [link](https://gist.github.com/ynkdir/688e62f419e5374347bf) I found a workaround by downloading this file, which generated a .sln file, but it was corrupt and Visual Studio 2017 couldn't open it. Using `ImageMagick-7.0.7-11-Q16-x64-dll.exe` the compiler throws the following error: `Error C2039 'PaletteMatteType': is not a member of 'Magick'` and so on – Klyse Nov 19 '17 at 12:08
  • The `win32.mak` is in the Windows SDK which you should have got if you selected all the things I marked in red on my other post. – Mark Setchell Nov 19 '17 at 12:13
  • Why is the compiler even looking at ImageMagick? When you install it, it is already compiled and built. You are doing something fundamentally wrong if you are compiling ImageMagick - it is not necessary. – Mark Setchell Nov 19 '17 at 12:14
  • Actually no, I'm just defining `#define cimg_use_magick` but the compiler doesn't recognize a lot of the elements. I also tried different versions of ImageMagick. Installing all packages to Visual Studio, as described in your other post, doesn't add win32.mak to my files. Do you know any other workaround? – Klyse Nov 19 '17 at 13:26
  • I normally use macOS and Linux, so I am not the best for Windows. However, if you look in `CImg.h` around line 59,725 you will see that `CImg` is just trying to find the path to `C:\ImageMagick-??-\convert.exe` so I always believed that the compiler doesn't need to even see ImageMagick, but that the program finds ImageMagick's `convert.exe` at runtime and uses that... There is also `imagemagick_path()` function to tell CImg where ImageMagick is installed. – Mark Setchell Nov 19 '17 at 13:30
  • I don't think you should do `#define cimg_use_magick` actually. I think that causes CImg to try and compile in Magick++. I think you just omit it and it will look for the pre-built binaries at run-time. – Mark Setchell Nov 19 '17 at 13:41