I'm making a program in Ubuntu with wxwidgets, the goal is to recognize faces but for now I just want to load an image into a frame. I load images by clicking on a load menu item which is linked to this function.

void Face_Catcher_Frame::on_load(wxCommandEvent &event)
{
	wxFileDialog * openFileDialog = new wxFileDialog(this);
	if (openFileDialog->ShowModal() == wxID_OK)
	{
		wxString file_name = openFileDialog->GetPath();
		file_name = file_name.Mid(file_name.Find('/', true) + 1);

		ILboolean success;

		ilGenImages(1, &texid);
		ilBindImage(texid);

		printf("%ls\n", getWChars(string((const char*)file_name.mb_str(wxConvUTF8))).c_str());
		//success = ilLoadImage(getWChars(string((const char*)file_name.mb_str(wxConvUTF8))).c_str());
		success = ilLoadImage((const char*)file_name.mb_str(wxConvUTF8));
		if(success)
		{
			success = ilConvertImage(IL_RGB, IL_UNSIGNED_BYTE);
			if (!success)
    		{
      			printf("Could not convert image.\n");
      			exit(1);
    		}

			glClearColor(0.0, 0.0, 0.0, 0.0);
  			glClear(GL_COLOR_BUFFER_BIT);
   			glViewport(0, 0, ilGetInteger(IL_IMAGE_WIDTH), ilGetInteger(IL_IMAGE_HEIGHT));

			glGenTextures(1, &image);
			glBindTexture(GL_TEXTURE_2D, image);
			glTexImage2D(GL_TEXTURE_2D, 0, ilGetInteger(IL_IMAGE_BPP), ilGetInteger(IL_IMAGE_WIDTH), ilGetInteger(IL_IMAGE_HEIGHT), 0, ilGetInteger(IL_IMAGE_FORMAT), GL_UNSIGNED_BYTE, ilGetData());
		}
		else
		{
			printf("Could not load image.\n");
      		exit(1);
		}

		ilDeleteImages(1, &texid);
		SetClientSize(ilGetInteger(IL_IMAGE_WIDTH), ilGetInteger(IL_IMAGE_HEIGHT));
	}
}

Everytime I try to load an image ilLoadImage fails. I'm able to run an example with no trouble

http://gpwiki.org/index.php/DevIL:Tutorials:Basics

But here it doesn't work even tough the path seems to be correct, I even shortened the path to just the image's file name and that didn't work either.

But what I'm really curious about is the fact that ilLoadImage seems to require an argument of type wchar_t*. It just seems weird and especially inconvenient on linux. Although when I looked at the documentation it said the function takes a char array, maybe that's because I got devil out of the ubuntu repository after I failed to compile the source, so my version is a little old.

> printf("%ls\n", getWChars(string((const char*)file_name.mb_str(wxConvUTF8))).c_str());
> success = ilLoadImage((const char*)file_name.mb_str(wxConvUTF8));
Some suggestions:
Use a separate char array to store the final result of your conversions before calling iLoadImage.
You can actually see what the final result is in your debug.

It also makes sure that what you print in the debug is the same as what you use in the call.

Second, check the path from the dialog.
Is it ALWAYS an absolute path (beginning with /)?
If not, then is the path relative to the current working directory, or relative to some other place?

Oh, and calling exit() in the middle of a GUI program, for something as simple as getting a filename wrong - not good.

I looked at the string in gdb using this guy's script

http://nic-nac-project.de/~skypher/wchar.gdb

and I did it in two situations, I made the string a relative path(test.jpg) and an absolute path(/home/jarl/face_catcher/test.jpg), the paths seem right but it doesn't work.

I fixed the problem, I cast a const char* into const ILstring and it works.

void Face_Catcher_Frame::on_load(wxCommandEvent &event)
{
	wxFileDialog * openFileDialog = new wxFileDialog(this);
	if (openFileDialog->ShowModal() == wxID_OK)
	{
		wxString file_name = openFileDialog->GetPath();

		printf("%i, %i\n", ilGetInteger(IL_IMAGE_WIDTH), ilGetInteger(IL_IMAGE_HEIGHT));

		ILboolean success;

		ilGenImages(1, &texid);
		ilBindImage(texid);
		
		string test = string(file_name.mb_str());
		printf("%s\n", test.c_str());

		if(!ilLoadImage((const ILstring)test.c_str()))
		{
			printf("Could not load image.\n");
      		exit(1);
		}
		if (!ilConvertImage(IL_RGB, IL_UNSIGNED_BYTE))
    	{
      		printf("Could not convert image.\n");
      		exit(1);
    	}

		width = ilGetInteger(IL_IMAGE_WIDTH), height = ilGetInteger(IL_IMAGE_HEIGHT);
		SetClientSize(width, height);
		printf("%i, %i\n", width, height);

		glGenTextures(1, &image);
		glBindTexture(GL_TEXTURE_2D, image);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
		glTexImage2D(GL_TEXTURE_2D, 0, ilGetInteger(IL_IMAGE_BPP), ilGetInteger(IL_IMAGE_WIDTH), ilGetInteger(IL_IMAGE_HEIGHT), 0, ilGetInteger(IL_IMAGE_FORMAT), GL_UNSIGNED_BYTE, ilGetData());

		ilDeleteImages(1, &texid);
		

		

		loaded = true;
	}
}

> I cast a const char* into const ILstring and it works.
Yeah, today maybe.

Check whether there is also an API for manipulating ILstring's, including creating such things from other kinds of strings.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.