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

Author Topic: [solved]Byte Manipulation for Image Processing  (Read 4198 times)

0 Members and 1 Guest are viewing this topic.

Ashenwraith

  • Sr. Member
  • ****
  • Posts: 270
    • View Profile
[solved]Byte Manipulation for Image Processing
« on: April 16, 2010, 06:10:52 pm »
Hi, my tool is working right now, but it's way too slow and I'm using GetPixel,SetPixel, to basically reconstruct and process images.

What I think I need to do is:

--get a dump of the bytes (GetPixelsPtr() ?)

--sort through uints by 4 for rgba pixels

--do my comparison/maths on range of bytes and edit/replace

My question is what is the best way to do this?

GetPixelsPtr returns a pointer so I can't just use sizeof() to sort?

Is the size stored in
  • ?


My memory is kind of screwed up because I'm going off when I did something like this manually a long time ago. Does using GetPixelsPtr and just inputting width and height handle everything?

Is it fast working directly with pixel data for large images?

I was thinking of converting to an indexed form based off a palette for the the loop-through operations and then converting back to pixels.

//////////////////////////////////////////////////////////////////////

Part 2

////////////////////////////////////////////////////////////////////

Okay so let's see if I can recap some to be more clear.

GetPixel == slow

GetPixelsPtr() to grab entire image and set a new instance to be operated on?

OR

GetPixelsPtr() many times as replacement to GetPixel?


See what my problem is?

gsaurus

  • Sr. Member
  • ****
  • Posts: 262
    • View Profile
    • Evolution Engine
[solved]Byte Manipulation for Image Processing
« Reply #1 on: April 16, 2010, 08:12:10 pm »
Yes, getPixel/setPixel are quite slow if you intend to do image processing.

You should use getPixelsPtr and operate over it as you do with getPixel/setPixel, but with the necessary transformations on the coordinates. You know the pixelsPtr size from the image itself. pixelsPtr size is img.GetWidth()*img.GetHeight()*4.

to access a specific value of a pixel:
Code: [Select]
pixelsPtr[ ( x + y*img.GetWidth() )*4 + offset ]
where offset is 0, 1, 2 or 3 to obtain red, green, blue or alpha respectively.
Pluma - Plug-in Management Framework

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
[solved]Byte Manipulation for Image Processing
« Reply #2 on: April 17, 2010, 10:14:28 am »
GetPixelsPtr() gives you a read-only access to the pixels, thus you'll need to copy them in your own array before modifying them. This may be expensive if you do it several times, and you'd better use your own array directly, only using LoadFromPixels to update the image when necessary.

You should also tell us what kind of processing you do.
Laurent Gomila - SFML developer

gsaurus

  • Sr. Member
  • ****
  • Posts: 262
    • View Profile
    • Evolution Engine
[solved]Byte Manipulation for Image Processing
« Reply #3 on: April 17, 2010, 12:46:42 pm »
Quote from: "Laurent"
GetPixelsPtr() gives you a read-only access to the pixels (...) you'd better use your own array directly, only using LoadFromPixels to update the image when necessary.

wops  :oops: , I forgot that for a moment  :lol:
Pluma - Plug-in Management Framework

Ashenwraith

  • Sr. Member
  • ****
  • Posts: 270
    • View Profile
[solved]Byte Manipulation for Image Processing
« Reply #4 on: April 17, 2010, 01:34:52 pm »
I currently use the sf::Color and GetPixel/SetPixel to to compare/manipulate pixel image to save.

It's not a blendmode type op/bit shift operation/etc, but requires pixel to pixel comparison based on unique variables.

gsaurus

  • Sr. Member
  • ****
  • Posts: 262
    • View Profile
    • Evolution Engine
[solved]Byte Manipulation for Image Processing
« Reply #5 on: April 17, 2010, 03:59:29 pm »
If you don't need to modify pixels in general, use GetPixelsPtr() once and use that pointer to access the pixels and make the comparisons. If you need to modify a small amount of pixels you can still use SetPixel(), the pointer keeps up to date.

But if you constantly need to read/write pixels:
    If you have to do that processing just a few times, you can just copy the pointer, process it and use the LoadFromPixels to restore it back.
    If you need to do it recurrently you can use only your own pointers and not use sf::Image unless when you really need it (LoadFromFile and then copy the pointer; LoadFromPixels and SaveToFile; LoadFromPixels and use a sprite to display).
Pluma - Plug-in Management Framework