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

Author Topic: GetPixel, any faster method of access?  (Read 2243 times)

0 Members and 1 Guest are viewing this topic.

sparkzi

  • Newbie
  • *
  • Posts: 9
    • View Profile
GetPixel, any faster method of access?
« on: August 04, 2013, 08:10:27 pm »
Hi,
I want to compare images using SFML I tried to do using shaders (http://en.sfml-dev.org/forums/index.php?topic=12239.0) and now I want to make non-shader version.
Currently i have something like this:
                // drawing some polygons on texture

                texture.display();
                sf::Image& generatedImage = texture.getTexture().copyToImage();

               
                long double diff = 0;
                int j = 0;
                for(int j = 0; j<300; j++)
                {
                        for(int i = 0; i<300; i++)
                        {
                                diff += std::abs(generatedImage.getPixel(j,i).r - originalImage.getPixel(j,i).r) +
                                std::abs(generatedImage.getPixel(j,i).g - originalImage.getPixel(j,i).g) +
                                std::abs(generatedImage.getPixel(j,i).b - originalImage.getPixel(j,i).b);
                        }
                }
 

as you can see I'm using
sf::Image& generatedImage = texture.getTexture().copyToImage();

and then when I want access pixel values:
generatedImage.getPixel(x,y).r

I have run profiler and it shows that program spend almost whole time in getPixel() insted of copyToImage(). I thpught that when i call .getTexture.copyToImage() the whole image will be copied from GPU to CPU and then accessing pixels will be very quick. Maybe I do something wrong? Can anyone help me?
« Last Edit: August 04, 2013, 08:13:23 pm by sparkzi »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: GetPixel, any faster method of access?
« Reply #1 on: August 04, 2013, 08:24:51 pm »
First thing to optimize: call getPixel twice instead of 6 times.

Second thing to possibly optimize: getPixel is not optimized, you may get better results by accessing pixels directly (image.getPixelsPtr() + offset).
Laurent Gomila - SFML developer

sparkzi

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: GetPixel, any faster method of access?
« Reply #2 on: August 04, 2013, 08:27:32 pm »
Thank you for quick answer.
How it works: data is transfered from CPU to GPU during
 texture.getTexture().copyToImage()
or when getPixel is called?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: GetPixel, any faster method of access?
« Reply #3 on: August 04, 2013, 08:32:41 pm »
Data is transfered to CPU in copyToImage(). getPixel does nothing more than accessing an element in an array, but it could be faster.
Laurent Gomila - SFML developer