SFML community forums

Help => Graphics => Topic started by: thatoneguy on June 01, 2013, 08:58:35 am

Title: [Solved] Reading pixel color issue
Post by: thatoneguy on June 01, 2013, 08:58:35 am
I'm using SFML 2.0 and the getPixel() fuction of the Image class. The function returns 2,0,1 for what I drew to be 0, 0, 0 in Photoshop. http://stackoverflow.com/questions/16601839/reading-wrong-pixel-color (http://stackoverflow.com/questions/16601839/reading-wrong-pixel-color).
Title: Re: Reading pixel color issue
Post by: Laurent on June 01, 2013, 12:56:08 pm
Please show your code and upload the image.
Title: Re: Reading pixel color issue
Post by: thatoneguy on June 01, 2013, 08:57:08 pm
    sf::Image map;
    ImageHandler::SetImage(map, "/Data/Maps/001.png");
    for (uchar x = 0; x < map_width; ++x) {
        for (uchar y = 0; y < map_height; ++y) {
            tiles[x][y].index = ColorToIndex(map.getPixel(x, y));
            SetTile(x, y, tiles[x][y].index);
        }
    }
(http://i557.photobucket.com/albums/ss13/KookehMonsters/Public/001_zps761196e4.png)
Title: Re: Reading pixel color issue
Post by: Laurent on June 01, 2013, 08:58:24 pm
Your image link is broken. And how are we supposed to know what your functions do? Please write a complete and minimal example that reproduces the problem.
Title: Re: Reading pixel color issue
Post by: thatoneguy on June 01, 2013, 09:02:17 pm
Fixed now. I went through debugging and the value of the pixel at 0, 0 returns 2, 0, 1 for getPixel(). Code below was used to test it while debugging.
uchar TileGrid::ColorToIndex(sf::Color &color) {
    return 1;
    if (color == sf::Color(2, 0, 1)) {
        return 2;
    } else if (color == sf::Color(0, 255, 0)) {
        return 1;
    } else {
        Root::SetStatus(EXIT_FAILURE);
        return 255;
    }
}
Title: Re: Reading pixel color issue
Post by: Laurent on June 01, 2013, 09:18:15 pm
Can you please show a minimal example? We don't care about what's around it, about your whole project. Just write a main() which loads an image and calls getPixel on it. That will be enough (and needed!) to solve your problem ;)
Title: Re: Reading pixel color issue
Post by: thatoneguy on June 01, 2013, 09:49:35 pm
Sorry I didn't know what you meant by a minimal example  ;D
I still don't understand what's going on.
#include <SFML/Graphics.hpp>
#include <iostream>

int main(int argc, char **argv) {
    sf::Image img;
    img.loadFromFile("D:/Maps/001.png");
    sf::Color tmp(img.getPixel(0, 0));
    if (tmp == sf::Color (0, 0, 0)) {
        std::cout << "Black";
    } else {
        std::cout << "Other";
    }
}
Title: Re: Reading pixel color issue
Post by: Nexus on June 01, 2013, 10:21:24 pm
What is the actual pixel value in the image, and what do you get from SFML? Don't just compare with black, output the RGBA components.

Also, verify that loading suceeds.
Title: Re: Reading pixel color issue
Post by: thatoneguy on June 01, 2013, 10:53:03 pm
The image loads fine.
Through debugging SFML gives me the value 2, 0, 1 while in Photoshop it gives me 0, 0, 0
I tried printing it out, and I'm not getting the same results. I'll look more into it.
Title: Re: Reading pixel color issue
Post by: Nexus on June 01, 2013, 11:23:11 pm
If your debugger is not buggy itself, the results must be the same when you use std::cout. Just keep in mind you have to cast the sf::Color members to int, because they are interpreted as characters by the C++ streams.
std::cout << static_cast<int>(color.r) << ", " << ... << "\n";
Title: Re: Reading pixel color issue
Post by: thatoneguy on June 01, 2013, 11:50:46 pm
I re-saved the image again and it seems to be working fine. Was not an issue with SFML :) I guess it had to do with the way I saved it.