CImg: Failed to recognize the jpg format

怎甘沉沦 提交于 2019-12-01 08:51:00

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, ...).

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!