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

Author Topic: How to convert my PixelArray ?  (Read 1238 times)

0 Members and 1 Guest are viewing this topic.

Mog

  • Newbie
  • *
  • Posts: 2
    • View Profile
How to convert my PixelArray ?
« on: October 03, 2014, 06:40:26 pm »
Hello everyone, I've a project where I have to compress an image. Everything is ok except the displaying of the image (after decompression).
To have my pixelArray, I made:
struct Pixel
{
        uint8_t B, G, R;
};

Pixel** pixelArray = new Pixel*[bmpHeader.ImageWidth];
        for (int i = 0; i < bmpHeader.ImageWidth; ++i)
                pixelArray[i] = new Pixel[bmpHeader.ImageHeight];
 
Do not care about what is bmpHeader, that just give the size of the image.

Now I would like to display it with SFML, I think texture.update() would work fine but I don't know how to convert Pixel** to sf::Uint8*, and are we oblige to use rbga ? (because I don't use transparency)

Can you please tell me how  sf::Uint8* works ? Or even better, can you guide me to convert Pixel** to  sf::Uint8* ?

Thanks a lot !

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: How to convert my PixelArray ?
« Reply #1 on: October 03, 2014, 07:10:00 pm »
Just thinking aloud here. Could you do something like:
sf::Image image(bmpHeader.ImageWidth, bmpHeader.ImageHeight);
and then use setPixel() to modify each pixel based on pixel.
Maybe something like:
image.setPixel(sf::Color(pixel.r, pixel.g, pixel.b));
or equivalent, modifying to accommodate pointer usage.

Then, to create a texture, it's just:
sf::Texture texture;
if (!image.loadFromImage(image))
    return errorCode;

The reason I suggest this method is because your pixels are stored in a different format (type Pixel) to how sfml seems to require (type sf::Color) and would need conversion.

Can you please tell me how  sf::Uint8* works ?
sf::Uint8 is an 8-bit unsigned integer (actually it's an unsigned char - a byte: 0-255)

EDIT: Corrected setPixel's link.
« Last Edit: October 03, 2014, 10:08:12 pm by Hapax »
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Mog

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: How to convert my PixelArray ?
« Reply #2 on: October 03, 2014, 07:46:33 pm »
Awesome, thanks a lot !  ;D

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: How to convert my PixelArray ?
« Reply #3 on: October 03, 2014, 10:08:40 pm »
You're welcome. Glad I could help  :)
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*