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

Author Topic: Best way to work with Pixel Pointers/Arrays?  (Read 2414 times)

0 Members and 1 Guest are viewing this topic.

Ashenwraith

  • Sr. Member
  • ****
  • Posts: 270
    • View Profile
Best way to work with Pixel Pointers/Arrays?
« on: April 23, 2010, 01:32:19 pm »
This is kind of important because a lot of my code is based on this.

Is there a better way than what I'm doing:

Code: [Select]
sf::Uint8* Get_Pixel(sf::Uint8* img_ptr,int w,int h,int x,int y)
{
    int index=(((y*h)-y)+(x))*4;
    sf::Uint8 *ptr=new sf::Uint8[3];

    ptr[0]=img_ptr[index];
    ptr[1]=img_ptr[index+1];
    ptr[2]=img_ptr[index+2];
    ptr[3]=img_ptr[index+3];

    return ptr;
}

sf::Uint8 *img_ptr=const_cast<sf::Uint8*>(img.GetPixelsPtr());

sf::Uint8 *ptr3=Get_Pixel(img_ptr,w,h,80,0);

Mindiell

  • Hero Member
  • *****
  • Posts: 1261
    • ICQ Messenger - 41484135
    • View Profile
Best way to work with Pixel Pointers/Arrays?
« Reply #1 on: April 23, 2010, 02:54:22 pm »
Why are you putting this question and code in another post ?
You asked it once in this post, no ?
Mindiell
----

Ashenwraith

  • Sr. Member
  • ****
  • Posts: 270
    • View Profile
Best way to work with Pixel Pointers/Arrays?
« Reply #2 on: April 23, 2010, 02:57:23 pm »
Quote from: "Mindiell"
Why are you putting this question and code in another post ?
You asked it once in this post, no ?


Because it's obviously a different thread topic.

Mindiell

  • Hero Member
  • *****
  • Posts: 1261
    • ICQ Messenger - 41484135
    • View Profile
Best way to work with Pixel Pointers/Arrays?
« Reply #3 on: April 23, 2010, 03:02:31 pm »
Ok,

1- I think there is an error in our calcul (y instead of w)

2- Why don't you simply use GetPixel ?
Mindiell
----

Ashenwraith

  • Sr. Member
  • ****
  • Posts: 270
    • View Profile
Best way to work with Pixel Pointers/Arrays?
« Reply #4 on: April 23, 2010, 03:05:37 pm »
1-I think you are confused of what an index of a pixel array is.

2-I originally did use GetPixel (and SetPixel and Image.Copy), but they are slow and not recommended for serious operations.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Best way to work with Pixel Pointers/Arrays?
« Reply #5 on: April 23, 2010, 04:33:54 pm »
Well, if you rewrite a GetPixel function if probably won't be faster than sf::Image's one (at least in SFML 2).

I don't understand why your code is so much complicated, what about this?
Code: [Select]

sf::Uint8* Get_Pixel(sf::Uint8* img_ptr,int w,int h,int x,int y)
{
    return img_ptr + (x + y * w) * 4;
}

And by the way, if Image::GetPixelsPtr returns a const pointer, it's not to force you to use a const_cast. You really can't modify the returned pixels ;)
Laurent Gomila - SFML developer