Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PNMImage.flip() documentation doesn't match method, can't transpose with x/y mirror #1652

Closed
izzyboris opened this issue May 3, 2024 · 2 comments

Comments

@izzyboris
Copy link

Description

Documentation for PNMImage.flip implies that mirroring/transposing can be performed at the same time (with transpose after mirroring), and in fact all arguments are required at the same time:

(https://docs.panda3d.org/1.10/python/reference/panda3d.core.PNMImage#panda3d.core.PNMImage.flip)

However the actual implementation transposes if requested, then short-circuits mirroring. If mirroring in X and/or Y is required along with transposition, two calls to flip() are required.

Steps to Reproduce

PNMImage("somefile.png").flip(flip_x=True, flip_y=True, transpose=True) and note that only transposition is performed.

Environment

  • Operating system: Windows 11
  • System architecture: x64
  • Panda3D version: 1.10.14
  • Installation method: pip
  • Python version (if using Python): 3.11
  • Compiler (if using C++): N/A
@rdb
Copy link
Member

rdb commented May 14, 2024

Can't repro this using the following code:

from panda3d.core import *

a = PNMImage(2, 2)

a.set_xel_val(0, 0, 0)
a.set_xel_val(0, 1, 1)
a.set_xel_val(1, 0, 2)
a.set_xel_val(1, 1, 3)

print("Original:")
print(a.get_xel_val(0, 0), a.get_xel_val(0, 1), a.get_xel_val(1, 0), a.get_xel_val(1, 1))

a.flip(True, True, True)

print("Post transform:")
print(a.get_xel_val(0, 0), a.get_xel_val(0, 1), a.get_xel_val(1, 0), a.get_xel_val(1, 1))

It works as expected. Looking at the code I also see nothing wrong.

@izzyboris
Copy link
Author

You're right. The problem I'm actually running into is that the ordering of the operations seems to be inverted in my reference data (produced from the Tiled map editor). It appears that Tiled transposes from top-left to bottom-right (so [0, 1, 2, 3] turns into [0, 2, 1, 3]) whereas Panda3D transposes bottom-left to top-right ([0, 1, 2, 3] -> [3, 1, 2, 0]).

So the right approach in my case is to reverse the flip_x/flip_y when also transposing. So this seems to work:

                if flags.flipped_diagonally:
                    tmpimg.flip(flip_x=flags.flipped_vertically,
                                flip_y=flags.flipped_horizontally,
                                transpose=flags.flipped_diagonally)
                else:
                    tmpimg.flip(flip_x=flags.flipped_horizontally,
                                flip_y=flags.flipped_vertically,
                                transpose=flags.flipped_diagonally)

Otherwise a less efficient solution would be to perform a separate flip() and only transpose, followed by a flip for mirroring X/Y only.

That makes sense now. Thanks for your time.

@rdb rdb closed this as completed May 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants