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

Author Topic: Image rotation  (Read 11081 times)

0 Members and 1 Guest are viewing this topic.

nihohit

  • Newbie
  • *
  • Posts: 37
    • View Profile
Image rotation
« on: April 10, 2012, 11:10:52 am »
as described here, I think that there should be at least the three common rotations available to Image, to enable complex Images. Don't know if this is relevant, but I know that in System.Drawing Image has these options.

BTW, I'm a bit question-heavy at the moment, but thanks for the awesome help and quick replies. SFML really eased my learning to code games, and the help here is really great.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Image rotation
« Reply #1 on: April 10, 2012, 11:28:49 am »
This is something that can be considered for future minor releases of SFML 2.
Laurent Gomila - SFML developer

JayArby

  • Jr. Member
  • **
  • Posts: 68
    • View Profile
Re: Image rotation
« Reply #2 on: April 10, 2012, 05:42:31 pm »
Couldn't you rotate a sprite and render it to an image?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Image rotation
« Reply #3 on: April 10, 2012, 07:23:06 pm »
Couldn't you rotate a sprite and render it to an image?
This would be overkill, and too complicated for users.
Basically, since rotating sprites is free, there's almost no need to pre-rotate images. But there are use cases where it is necessary, for example when you compose more complex images.
Laurent Gomila - SFML developer

RokB

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Image rotation
« Reply #4 on: August 06, 2015, 09:10:57 am »
Would this still be considered? This would indeed be very useful when creating complex images.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Image rotation
« Reply #5 on: August 06, 2015, 09:26:02 am »
When rotation is provided, vertical/horizontal mirroring should be provided, too.

But is it something that needs to be done within SFML? When the implementation copies the entire image, the user could as well do that himself... Such operations would mainly be interesting when they transform indices rather than actually rewriting the image.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

RokB

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Image rotation
« Reply #6 on: August 06, 2015, 09:59:54 am »
I believe vertical/horizontal mirroring is already provided, which is why I thought it was odd that rotation isn't.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Image rotation
« Reply #7 on: August 06, 2015, 12:31:39 pm »
Rotation is a more complicated than flipping. Even 90° is still a little more complicated than flipping.

That said, since flipping is already included, you'd think that both 90° rotations would be too. (180° could be included too since it would be more efficient than having to do both flips)

I'm not sure if the original suggestion was for arbitrary angles though but I think that's far too high for SFML.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Image rotation
« Reply #8 on: August 06, 2015, 12:58:36 pm »
As mentioned, an interesting optimization are lazy indices. Whether such an optimization makes sense depends on the use cases, e.g. how often people use getPixel() and setPixel() to access single pixels.

Depending on the transform, an internal proxy transforms the index pair before actual array access. For example:
  • rotate by 90°: (x, y) -> (-y, x)
  • flip horizontally: (x, y) -> (width - x - 1, y)
They can of course be chained. When a function needs direct access to the underlying array, the latter is rewritten.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Image rotation
« Reply #9 on: August 06, 2015, 01:10:48 pm »
So, the pixel co-ordinate would be transformed internally when getting and setting the pixel?
That sounds like a good idea, as long as the direct pixel access is not covered and removed.
One scenario to consider is if that image is not square and it's rotated 90°:
  • which colour is returned from the getter when outside of the actual image?
  • which pixel should be set if the rotated co-ordinate is outside of the actual image?

Also, do we assume that the image is rotated and then 'moved' to fit into the top-left co-ordinate (0, 0) after rotation?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11035
    • View Profile
    • development blog
    • Email
Re: Image rotation
« Reply #10 on: August 06, 2015, 01:34:18 pm »
As mentioned, an interesting optimization are lazy indices.
Do you mean optimization or did you just use that word?
Because lazy indices are not optimized at all, due to high cache misses. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Image rotation
« Reply #11 on: August 06, 2015, 02:06:35 pm »
which colour is returned from the getter when outside of the actual image?
Access outside bounds will be undefined behavior,  like before.

Also, do we assume that the image is rotated and then 'moved' to fit into the top-left co-ordinate (0, 0) after rotation?
If the image rotates, its dimensions will change, there's no need to fit/align/move something.

Do you mean optimization or did you just use that word?
The optimization would obviously be the copies we can avoid as long as we don't need them. Imagine that a user wants to rotate by 90° and then flip horizontally. Without lazy evaluation, there are two copies required, with lazy evaluation at most one -- possibly none.

Because lazy indices are not optimized at all, due to high cache misses.
Good point. However I'm not sure if setPixel/getPixel is mainly used in iteration; it's a random access interface. And even now, you can easily have bad cache access patterns, because as a user you don't know whether you should iterate row-major or column-major.

If iteration proves to be an often-needed feature, it would still be possible to provide a dedicated iterator interface, which would walk efficiently in memory. But don't forget that it's always possible to "evaluate" the image and rewrite it in memory, so that the index transform becomes again the identity.

Maybe we should ask ourselves, how sf::Image is usually used... I think it's:
1. as a container to convert between different image classes
2. to perform "global" pixel operations, like color-mask, pre-alpha multiplying, flip, rotate
3. to set single pixels, e.g. for UI or tileset composition
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

RokB

  • Newbie
  • *
  • Posts: 8
    • View Profile
Re: Image rotation
« Reply #12 on: August 06, 2015, 02:13:59 pm »
There wouldn't have to be copies of the image when rotating. The image can be rotated by rearranging the pixels.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Image rotation
« Reply #13 on: August 06, 2015, 02:17:42 pm »
If the image rotates, its dimensions will change, there's no need to fit/align/move something.
Ah, I see. I was thinking that the image would be the same size and the pixels would be rotated inside them, rather than (apparently) rotating the image object itself.
However, the dimensions themselves wouldn't change as the image isn't actually being rotated. The retrieved image size would just be the size transformed by the rotation.

RokB, re-arranging the pixels is computationally expensive. Transforming the getPixel() and setPixel() co-ordinates would be comparatively much faster. One problem with that approach is when you create a texture from that image. It looks like it would have to do the pixel re-arranging before/during that conversion.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11035
    • View Profile
    • development blog
    • Email
Re: Image rotation
« Reply #14 on: August 06, 2015, 02:19:27 pm »
Maybe we should ask ourselves, how sf::Image is usually used... I think it's:
1. as a container to convert between different image classes
2. to perform "global" pixel operations, like color-mask, pre-alpha multiplying, flip, rotate
3. to set single pixels, e.g. for UI or tileset composition
Yeah, as with most feature requests, I'm missing a few use case scenarios.

And I'm fully satisfied with:
But there are use cases where it is necessary, for example when you compose more complex images.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

 

anything