-
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).
-
Please show your code and upload the image.
-
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)
-
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.
-
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;
}
}
-
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 ;)
-
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";
}
}
-
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.
-
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.
-
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";
-
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.