4
#include <iostream>
#include <stdlib.h>
#include "CImg.h"

using namespace cimg_library;
using namespace std;

int main(){
CImg<unsigned char> image("lena.jpg"), visu(500,400,1,3,0);

const unsigned char red[] = { 255,0,0 }, green[] = { 0,255,0 }, blue[] = { 0,0,255 };



return 0;}

When i compile this code the error: CImg::load(): Failed to recognize format of the file "lena.jpg" shows up.Any suggestion?

I installed the imageMagick but the error still happens.

PSlayer
  • 61
  • 1
  • 2
  • 11

3 Answers3

7

To enable native JPG file support in CImg, put this before including CImg.h:

#define cimg_use_jpeg
#include "CImg.h"
....

and link your code with the libjpeg library. It works flawlessly for me. If you don't use this, CImg will try to do an external call to ImageMagick's convert tool to load the file, which is not the cleanest solution. Using libjpeg inside CImg is definitely better. That works the same for other image formats (tiff, png, ...).

bvalabas
  • 116
  • 1
  • How do i do a link the libjpeg on my project? i downloaded here: http://www.ijg.org/ But it is a collection of headers and codes and other files. – PSlayer Sep 04 '15 at 06:52
  • That depends on what compiler you use, on which system, etc.. "Linking a library to your C++ project" is probably out of scope here, but you'll easily find the answer somewhere (for instance here : http://www.linuxquestions.org/questions/programming-9/linking-libjpeg-461331/) – bvalabas Sep 04 '15 at 10:25
  • Huh. Well that explains why jpeg already works. But with regards to compiling, pointing to a site named "linuxquestions" seems... unhelpful. @PSlayer did you ever figure out how to link libjpeg visual studio's tools? – Michael Apr 06 '18 at 02:04
1

Have you tried any other image files other then "lena.jpg"? Is "lena.jpg" in the same directory than the current program? What compiler you using?

Does this example work (wouldn't really make sense if it did though)?

#include "CImg.h"
using namespace cimg_library;
int main() {
  CImg<unsigned char> image("lena.jpg"), visu(500,400,1,3,0);
  const unsigned char red[] = { 255,0,0 }, green[] = { 0,255,0 }, blue[] = { 0,0,255 };
  image.blur(2.5);
  CImgDisplay main_disp(image,"Click a point"), draw_disp(visu,"Intensity profile");
  while (!main_disp.is_closed() && !draw_disp.is_closed()) {
    main_disp.wait();
    if (main_disp.button() && main_disp.mouse_y()>=0) {
      const int y = main_disp.mouse_y();
      visu.fill(0).draw_graph(image.get_crop(0,y,0,0,image.width()-1,y,0,0),red,1,1,0,255,0);
      visu.draw_graph(image.get_crop(0,y,0,1,image.width()-1,y,0,1),green,1,1,0,255,0);
      visu.draw_graph(image.get_crop(0,y,0,2,image.width()-1,y,0,2),blue,1,1,0,255,0).display(draw_disp);
      }
    }
  return 0;
}

Source: http://cimg.eu/reference/group__cimg__tutorial.html

I noticed the documentation says it only supports jpg's if imageMagick is installed, perhaps you did something wrong there and it isn't properly installed?

EDIT:

Does this work?

#include "CImg.h"
using namespace cimg_library;
int main() {
  CImg<unsigned char> img(640,400,1,3);  // Define a 640x400 color image with 8 bits per color component.
  img.fill(0);                           // Set pixel values to 0 (color : black)
  unsigned char purple[] = { 255,0,255 };        // Define a purple color
  img.draw_text(100,100,"Hello World",purple); // Draw a purple "Hello world" at coordinates (100,100).
  img.display("My first CImg code");             // Display the image in a display window.
  return 0;
}
Futuza
  • 144
  • 8
  • I'm following this tutorial but it doesn't work.They say i need imageMagick installed but then what? I'm using windows! I don't know what i have to do to link this package to my project. – PSlayer Sep 04 '15 at 05:27
  • Maybe check that you did the stuff listed here: http://cimg.eu/reference/group__cimg__visual2005.html ? (It's for VS2005 so slightly outdated). – Futuza Sep 04 '15 at 05:47
-1

What worked for me was using the absolute path to the file, rather than just the file name:

Ex. Change this: CImg image("lena.jpg"), visu(500,400,1,3,0);

to this: CImg image("C:\Users\youruser\Desktop\lena.jpg"), visu(500,400,1,3,0);

Of course, this path would differ depending on where you have your file.

Leonardo Lopez
  • 1,113
  • 6
  • 20
  • 39