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

unable to rotate image? #12

Closed
teras opened this issue Apr 5, 2020 · 8 comments
Closed

unable to rotate image? #12

teras opened this issue Apr 5, 2020 · 8 comments

Comments

@teras
Copy link

teras commented Apr 5, 2020

Hello!

I am trying to do a test rotation on an image.
I am using this code

var test = newImage(50,50,4)
discard test.rotate(2)

but it fails with the following error message:

/Users/teras/Works/Development/Nim/emailform/emailform.nim(7) emailform
/Users/teras/.nimble/pkgs/flippy-0.4.1/flippy.nim(684) rotate
/Users/teras/.nimble/pkgs/flippy-0.4.1/flippy.nim(578) getSubImage
/Users/teras/.nimble/pkgs/flippy-0.4.1/flippy.nim(159) getRgba
/Users/teras/.choosenim/toolchains/nim-1.0.6/lib/system/assertions.nim(27) failedAssertImpl
/Users/teras/.choosenim/toolchains/nim-1.0.6/lib/system/assertions.nim(20) raiseAssert
/Users/teras/.choosenim/toolchains/nim-1.0.6/lib/system/fatal.nim(39) sysFatal
Error: unhandled exception: /Users/teras/.nimble/pkgs/flippy-0.4.1/flippy.nim(159, 10) `y >= 0 and y < image.height`  [AssertionError]

Am I using it wrong?

@teras
Copy link
Author

teras commented Apr 5, 2020

I added before line 684 in flippy.nim the following code to debug it:

  echo $result & " -- " , 1+int((result.width - newWidth)/2) , " " , 1+int((result.height - newHeight)/2) , " " , newWidth , "/" , newHeight

I found that the problem is indeed with out of bounds numbers and the result is
<Image 50x51:4> -- 1 1 51/51
different values, I even have negative numbers, like for

var image = newImage(500,50,4)
echo image.rotate(20)

I get negative results
<Image 547x223:4> -- 166 -130 217/486

Should this happen?

@treeform
Copy link
Owner

There seems to be a bug with the rotate method. @benjif could you take a look?

@treeform
Copy link
Owner

Oh I have figured it out. It was trimming the image incorrectly. I created a better trim function and will now use that.

0c1a9f9

Also added a test case for your error.

Thank you for reporting the bug!

@teras
Copy link
Author

teras commented Apr 10, 2020

Please excuse my comment.
I think the trim function is wrong.
It is good as a generic method, but not in this case.
What if an image is mostly "transparent", but only the center is painted?
Then rotation will minimize the image instead, not just rotate it.

IMHO the four edges of the rotated box should be calculated and crop the image accordingly.
Do you think we should discuss it more?

@treeform
Copy link
Owner

treeform commented Apr 10, 2020 via email

@teras
Copy link
Author

teras commented Apr 10, 2020

Maybe take into account something like this
https://gamedev.stackexchange.com/questions/86755/how-to-calculate-corner-positions-marks-of-a-rotated-tilted-rectangle

Find the location of the four points

  • (x,y)
  • (x+width, y)
  • (x+width,y+height)
  • (x,y+height)

and keep the min/max X and min/max Y as bounding box of the clipped area

@treeform
Copy link
Owner

Yes you are right. I will take a look.

@treeform treeform reopened this Apr 13, 2020
@treeform
Copy link
Owner

I thing it was fixed by @guzba #16

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