lundi 29 juin 2015

SFML leaves part of texture blank, if I create it from PixelPtr


I have a code, which converts plain BGR data to sf::Texture. "ifs" is opened ifstream to file which contains byte triplets of BGR colors (header of source file is omitted). And Width & Height 100% valid. In my example image is 800x600.

struct h3pcx_color_bgr { uint8_t b, uint8_t g, uint8_t r };

sf::Uint8* pixels = new sf::Uint8[width * height * 4];
h3pcx_color_bgr* fileData = new h3pcx_color_bgr[width*height]; 
ifs.read((char*)fileData, width * height * sizeof(h3pcx_color_bgr));

for (uint32_t i = 0; i < width*height; ++i) {
    pixels[i * 4] = fileData[i].r;
    pixels[i * 4 + 1] = fileData[i].g;
    pixels[i * 4 + 2] = fileData[i].b;
    pixels[i * 4 + 3] = 255;
}

This code works good, problem comes after. Once I draw my texture:

m_tex.update(pixels); //sf::Texture
m_sprite.setTexture(m_tex); //sf::Sprite
m_window->draw(m_sprite); // m_window is sf::RenderWindow

I have this annoying grey line in image below: enter image description here What I did:

  • Verified, that pixels contains valid value

Code snippet below (700 * 595 is inside "grey area") shows, that both pixels and fileData contains valid data (not grey color, which is appears just uninitialized memory).

auto f = fileData[700 * 595]; // 32, 31, 38
auto r = pixels[700 * 595 * 4]; // 38
auto g = pixels[700 * 595 * 4 + 1]; // 31
auto b = pixels[700 * 595 * 4 + 2]; // 32

"Grey" color is 204, 204, 204.

  • Tried to use sf::Image

If we do something like this:

img.create(width, height, pixels); // img is sf::Image
img.setPixel(700, 595, sf::Color::Blue);

And then convert it to sf::Texture, and draw. Result will be same image with grey area, but pixel 700, 585 will be blue!

If I get color value from "grey area":

auto clr = img.getPixel(700,600); //sf::Color(204,204,204)

So, it looks like, there are some hard-limit(???) on quantity of pixels (but I doubt it, since I've looked on actual SFML code, and did not found anything suspicious) or my stupid mistake. I would be very grateful, if someone can point out - why this grey line appears.


Aucun commentaire:

Enregistrer un commentaire