Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [Solved]Image.loadFromFile() also fails in SFML 2.0  (Read 2970 times)

0 Members and 2 Guests are viewing this topic.

Luapina

  • Newbie
  • *
  • Posts: 7
    • View Profile
[Solved]Image.loadFromFile() also fails in SFML 2.0
« on: May 26, 2013, 05:21:05 am »
Dear SFML-Community,

i started a topic ealier, where i searched for help with a strange problem. The "Solution" of my last topic was, that i updated to sfml 2.0.

So here's my problem now in sfml 2.0:

I have a class called "Position" with an sfml-Image as member. Declared as following:
class Position
{
    private:
        sf::Image CaptchaImage;
[...]
}

The constructor of the Class:
Position::Position(const char* Filename)
{
    CaptchaImage.create(IMAGE_WIDTH, IMAGE_HEIGHT, sf::Color(0, 0, 0));    //Create an empty Image
    if(!CaptchaImage.loadFromFile(Filename));    //Load the file into our Image
    {
        std::cout << "I couldnt read the image from " << Filename << std::endl;
        exit(12);
    }
}
 

So in my main.cpp I create an Instance of class Position:
Position Position("cb.bmp");

And guess what? I get the same output as in sfml 1.6:
"I couldnt read the image from cb.bmp".
This is the output I do, NOT the output that the loadFromFile() does.
So loadFromFile() fails loading the image and is not giving any Reason.

The Image IS compatible with SFML because if I do the following in the main.cpp:
sf::Image image;
if(!image.loadFromFile("cb.bmp"))
    std::cout << "Failed";
else
    std::cout << "Success";
 

It works fine!! Is this just me or is this kind of strange? I can post more code if needed.
Many thanks in advance,
Luapina


Edit:

Even IN THE SAME project but in the main.cpp the following code works:
sf::Image image;
if(!image.loadFromFile("cb.bmp"))
    std::cout << "Failed";
else
    std::cout << "Success";
 
« Last Edit: May 26, 2013, 07:38:50 pm by Luapina »

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
Re: Image.loadFromFile() also fails in SFML 2.0
« Reply #1 on: May 26, 2013, 07:36:54 am »
It's nice to know what output you print, but you HAVE to tell us what output SFML itself gives, or we CAN'T help you.

ALSO, please post a COMPLETE and MINIMAL code that demonstrates your problem so we can see if it's a problem with your code or with SFML itself.
I use the latest build of SFML2

Luapina

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Image.loadFromFile() also fails in SFML 2.0
« Reply #2 on: May 26, 2013, 04:30:36 pm »
Maybe its a problem because of my bad english but:
The biggest part of my Problem is, that sfml does NOT give any output.
I call the loadFromFile function:
    if(!CaptchaImage.loadFromFile(Filename));    //Load the file into our Image
    {
        std::cout << "I couldnt read the image from " << Filename << std::endl;
        exit(12);
    }

And it only says "I couldnt read the image from cb.bmp". That is my output and the function itself doesnt give any output. This is what makes it so hard for me to solve this Problem.

My three files:

main.cpp
#include "Word_Position.hpp"

Position Position("cb.bmp"); //Calling the constructor here

int main()
{
    Position.SetWordRect();

    std::cout << "Left: " << Position.tX
                << "\nTop: " << Position.tY
                << "\nWidth: " << Position.tWidth
                << "\nHeight: " << Position.tHeight
                << std::endl;
}

 

Word_Position.cpp:


/*
        Word_Position.cpp

        Paul Brandt
        24.05.2013

    This code preforms a simple Pixel-by-Pixel check, to get the position and size of the word in the captcha.
*/


#include "Word_Position.hpp"



bool Position::PixelIsNonWhite(int pX, int pY)  //Preform a simple test if one single pixel is set to a non-white color
{
        if(CaptchaImage.getPixel(pX, pY) != sf::Color(255, 255, 255))   //Check if the Pixel has an other color than white
                return true;    //Pixel is not white (HIT)
        else
                return false;   //Pixel is white (NO HIT)
}

void Position::SetWordRect() //Store the Position, Size(Pixel) of a word we found. Also provides some usefull memberfunctions.
{

        //Our first step is to find out the X-Coordinate
        for(int x = 0; x<IMAGE_WIDTH; x++)      //Use the X-Coordinate as Outer-LOOP
        {
                for(int y = 0; y<IMAGE_HEIGHT; y++)     //Use the Y-Coordinate as Inner-LOOP
                {
                        if(PixelIsNonWhite(x, y)) tX = x;
                }
        }

        //Next step is to find out the Y-Coordinate
        for(int y = 0; y<IMAGE_HEIGHT; y++)     //Use the Y-Coordinate as Outer-LOOP
        {
                for(int x = 0; x<IMAGE_WIDTH; x++)      //Use the Y-Coordinate as Inner-LOOP
        {
                        if(PixelIsNonWhite(x, y)) tY = y;
                }
        }

        // We did some great work on finding the upper left corner
        // Now we will find the downer right corner.
        // Same procedure just the other was round:

        //Our first step is to find out the X-Coordinate
        for(int x = IMAGE_WIDTH; x>0; x--)      //Use the X-Coordinate as Outer-LOOP
        {
                for(int y = IMAGE_HEIGHT; y>0; y--)     //Use the Y-Coordinate as Inner-LOOP
                {
                        if(PixelIsNonWhite(x, y)) tWidth = x - tX;
                }
        }

        //Next step is to find out the Y-Coordinate
        for(int y = IMAGE_HEIGHT; y>0; y--)     //Use the Y-Coordinate as Outer-LOOP
        {
                for(int x = IMAGE_WIDTH; x>0; x--)      //Use the Y-Coordinate as Inner-LOOP
                {
                        if(PixelIsNonWhite(x, y)) tHeight = y - tY;
                }
        }
}

Position::Position(const char* Filename)
{
    CaptchaImage.create(IMAGE_WIDTH, IMAGE_HEIGHT, sf::Color(0, 0, 0)); //Create an empty Image
    if(!CaptchaImage.loadFromFile(Filename));   //Load the file into our Image
    {
        std::cout << "I couldnt read the image from " << Filename << std::endl;
        exit(12);
    }
}

 


Word_Position.hpp:
#include <SFML/Graphics.hpp>    //We use the SFML library to preform our image-checks
#include <iostream>


#define IMAGE_WIDTH     300             //300 by 57 is the Defaut Image Size in ReCaptcha.
#define IMAGE_HEIGHT    57              //Change it with you needs.

#define POS_ENABLED             true    //Define if we want to scan for pixel with tolerance in Position - Doesnt make that much sense without
#define ROT_ENABLED             false   //Define if we want to scan for pixel with tolerance in Rotation - Mostly we dont need this
#define POS_TOLERANCE   0.05f
#define ROT_TOLERANCE   0.10f
#define COLOR_TOLERANCE 0.30f


class Position
{
    private:
        sf::Image CaptchaImage;
        bool PixelIsNonWhite(int pX, int pY);

    public:
        void SetWordRect();
        Position(const char* Filename);

        int tX; //We store the Coordinates public since this is the fastest way to access them.
        int tY;

        int tWidth; //Same here
        int tHeight;



};


 


EDIT:
Made a video which descibes the problem better than I do:
« Last Edit: May 26, 2013, 06:49:15 pm by Luapina »

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
Re: Image.loadFromFile() also fails in SFML 2.0
« Reply #3 on: May 26, 2013, 07:12:02 pm »
Found your problem.

if(!CaptchaImage.loadFromFile(Filename));    //Load the file into our Image
{
    std::cout << "I couldnt read the image from " << Filename << std::endl;
    exit(12);
}
 
Look VERY carefully at these lines. Notice anything? You have an extra semicolon in there. Try removing it.
I use the latest build of SFML2

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Image.loadFromFile() also fails in SFML 2.0
« Reply #4 on: May 26, 2013, 07:17:05 pm »
And you should never do non-trivial stuff outside the main function. Calling complex constructors/destructors at global scope is dangerous; maybe it works this time, but it's going to get you into troubles someday.

And why do you create an empty image before loading it?
Laurent Gomila - SFML developer

Luapina

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Image.loadFromFile() also fails in SFML 2.0
« Reply #5 on: May 26, 2013, 07:38:13 pm »
What a fail.  :-[

@Laurent all this was done when I was trying to find my fail after the error appeared.

Thanks for your help. I feel a bit ashamed now :/

Luapina