5 Replies - 15444 Views - Last Post: 24 April 2011 - 12:50 AM Rate Topic: -----

#1 ecekid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 23-April 11

Using the CImg library with Visual C++ 2010 Express Edition

Posted 23 April 2011 - 12:01 PM

Hey everybody!

Am new here, an new to C++ programming as well, so please bear with me :)

I am doing an image processing project using Visual C++ 2010 Express, and am not really used to this format since I usually work with MATLAB.

I was advised to use the CImg library since it is supposed to be quite good with handling images, however in spite of working for days trying to get it to work with Visual C++ 2010,I haven't even been able to read the image into the program..

So this is a really beginner-level question - how can I use Visual C++ 2010 with CImg to read an image into the C++ code?

I'm running Windows 7..

Any and all help will be appreciated greatly in this regard!

Thank you so much for your time..

(The error I get when I run the code using the standard settings is:

1>c:\users\admin\documents\visual studio 2010\projects\cimgtry\cimgtry\cimg.h(2765): error C2664: 'CreateWindowExW' : cannot convert parameter 2 from 'const char [10]' to 'LPCWSTR'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
)

Is This A Good Question/Topic? 0
  • +

Replies To: Using the CImg library with Visual C++ 2010 Express Edition

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: Using the CImg library with Visual C++ 2010 Express Edition

Posted 23 April 2011 - 01:17 PM

In the C++ FAQs topic pinned at the top of the forum is a subcategory "Common Windows/Visual Studio Problems". Look there for the answer.
Was This Post Helpful? 1
  • +
  • -

#3 ecekid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 23-April 11

Re: Using the CImg library with Visual C++ 2010 Express Edition

Posted 23 April 2011 - 07:52 PM

Thank you for the reply! :)

I changed the character set to multi-byte and then I received this error when I tried to build it:

1>c:\users\admin\documents\visual studio 2010\projects\cimgtry\cimgtry\cimgtry.cpp(9): error C2664: 'cimg_library::CImg<T> &cimg_library::CImg<T>::draw_text(const char *const ,const int,const int,const T *const ,const T *const ,const float)' : cannot convert parameter 1 from 'int' to 'const char *const '
1> with
1> [
1> T=unsigned char
1> ]
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast

My code is:

#include "StdAfx.h"
#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;
}



I don't understand how to correct it.. can anybody help please?
Was This Post Helpful? 0
  • +
  • -

#4 ecekid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 23-April 11

Re: Using the CImg library with Visual C++ 2010 Express Edition

Posted 23 April 2011 - 08:21 PM

Also, what all do i need to include in my stdafx.h and stdafx.cpp files for my code to work?

In my main code I have included these libraries :
<stdafx> <cmath> <stdio> <string> and <CImg>.

(This is for another code I'm working on)
Thank you!
:)

This post has been edited by ecekid: 23 April 2011 - 08:22 PM

Was This Post Helpful? 0
  • +
  • -

#5 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2030
  • View blog
  • Posts: 5,430
  • Joined: 27-December 05

Re: Using the CImg library with Visual C++ 2010 Express Edition

Posted 24 April 2011 - 12:00 AM

You can make your life simpler by setting this up as a generic console application, not a win32 app. Then you can omit the stdafx stuff.

When you start the new project wizard, click Win32 Console Application, enter a project name, click OK, then on the next screen click "Application Settings", then "Console application" and "Empty project".

That gives you a completely empty project, so click on Project/Add New Item/C++ File, enter a filename for your main file, and that will open an empty .cpp file.

You'll also have to copy CImg.h into your project root directory (the same folder that your .cpp file is in).

For CImg your .cpp file will need (at least):
#include "CImg.h"
// and any other  headers your program needs

using namespace cimg_library;

int main () {
 // and write your program




You can use this to test it:
#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;
  }

Of course, for that you need a copy of the image file "lena.jpg" in the project root directory too.

Your hello world code should work too -- without the StdAfx.h.

In VS2010 you probably don't have to add any special linker settings -- I think the gdi32 library is linked in by default. In CodeBlocks you would have to add gdi32 to the "Link libraries" list under Project build options/Linker settings.

This post has been edited by r.stiltskin: 24 April 2011 - 12:06 AM

Was This Post Helpful? 2
  • +
  • -

#6 ecekid  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 23-April 11

Re: Using the CImg library with Visual C++ 2010 Express Edition

Posted 24 April 2011 - 12:50 AM

Thank you so much, really appreciate it!

It's much better without the stdafx confusion, and the Cimg library finally worked :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1