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

Author Topic: [Solved] Reading pixel color issue  (Read 3237 times)

0 Members and 1 Guest are viewing this topic.

thatoneguy

  • Newbie
  • *
  • Posts: 25
    • View Profile
[Solved] Reading pixel color issue
« 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.
« Last Edit: June 01, 2013, 11:51:01 pm by thatoneguy »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Reading pixel color issue
« Reply #1 on: June 01, 2013, 12:56:08 pm »
Please show your code and upload the image.
Laurent Gomila - SFML developer

thatoneguy

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: Reading pixel color issue
« Reply #2 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);
        }
    }

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Reading pixel color issue
« Reply #3 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.
Laurent Gomila - SFML developer

thatoneguy

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: Reading pixel color issue
« Reply #4 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;
    }
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Reading pixel color issue
« Reply #5 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 ;)
Laurent Gomila - SFML developer

thatoneguy

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: Reading pixel color issue
« Reply #6 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";
    }
}

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Reading pixel color issue
« Reply #7 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.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

thatoneguy

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: Reading pixel color issue
« Reply #8 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.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Reading pixel color issue
« Reply #9 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";
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

thatoneguy

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: Reading pixel color issue
« Reply #10 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.

 

anything