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

Author Topic: Help with getpixelptr()  (Read 2951 times)

0 Members and 1 Guest are viewing this topic.

Ajsgolf27

  • Newbie
  • *
  • Posts: 9
    • View Profile
Help with getpixelptr()
« on: June 10, 2014, 07:20:43 am »
Hi I am having a hard time setting up my pixel pointer to an array where I can modify pixels and eventually use the create function to display back to the window.

I understand the first part in the documentation where I wrote this:

const Uint8* pixels = myImage.getPixelsPtr();

I also understand that it's going to return the values in groups of 4.
So how would I turn this into an array I can modify? Any help is greatly appreciated  thanks in advance.

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Help with getpixelptr()
« Reply #1 on: June 10, 2014, 07:50:38 am »
As the documentation says, getPixelsPtr returns a read-only (ie, const) pointer.  If you really want to break the API and modify the data directly through that pointer, you can use a const_cast to make the pointer non-const, but as soon as you do this SFML is no longer responsible for whatever might happen.

You should probably use the getPixel and setPixel methods instead.

Ajsgolf27

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Help with getpixelptr()
« Reply #2 on: June 10, 2014, 07:56:23 am »
I've tried that and my image that I'm working with is 600 x 600 pixels it took 35 min to run the code unless my computer is just too slow

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Help with getpixelptr()
« Reply #3 on: June 10, 2014, 08:09:08 am »
Using a raw array probably won't make that any faster.  This is C++, so the getPixel/setPixel methods probably get optimized to exactly the same code as raw array reads/writes.

When you need to process images quickly, generally you should get the GPU to help you.  I have no idea what you're trying to do with those pixels, but drawing an image to a render texture with the right blend mode or perhaps a shader is typically a lot faster than modifying each pixel one at a time on the CPU.

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Help with getpixelptr()
« Reply #4 on: June 10, 2014, 08:31:26 am »
No matter how you do it there is no way that reading a 600x600 image takes 35 minutes. Even on a slow machine. You must be doing something wrong.
Could you post a complete and minimal example of your code?

Also, to answer your question about how to obtain a copy you can modify. A simple solution would be to just copy the pixel values into a std::vector.

Ajsgolf27

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Help with getpixelptr()
« Reply #5 on: June 10, 2014, 02:05:05 pm »
Exactly I am so glad you agree this is driving me crazy! I am probably missing something small, i am new to sfml.
Image myImage;
if (!myImage.loadFromFile("cone_nebula/cone_nebula_001.png")){
                return -1;
        }
              Texture texture;
              Sprite sprite;
              texture.loadFromImage(myImage);

              vector <int> pixelValues;

        x = myImage.getSize().x;
        y = myImage.getSize().y;

        cout << x << "," << y << endl;
       
        for (int i = 0; i < x; i++){
               
                for (int j = 0; j < y; j++){

                        const Color color = myImage.getPixel(i, j);
                        pixelValues.push_back((int)color.r);
                        cout << (int)color.r << endl;
                        cout << (int)color.b << endl;
                        cout << (int)color.g << endl;

                        cout <<"these are red pixels in array " << pixelValues[z++]  << "   " << z << endl;
                       
                }
               
        }
 
« Last Edit: June 10, 2014, 02:08:54 pm by Ajsgolf27 »

Ajsgolf27

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Help with getpixelptr()
« Reply #6 on: June 10, 2014, 04:07:41 pm »
I figured it out! If I take out all the cout's it's takes seconds it was a stupid mistake.