microsoft / DirectXTex

DirectXTex texture processing library
https://walbourn.github.io/directxtex/
MIT License
1.79k stars 438 forks source link

How to rotate an image by x degrees #390

Closed ElCapor closed 1 year ago

ElCapor commented 1 year ago

Hey there , i'm very thankful for the contributors and original writers of the DirectxTex library , it allowed me to work with images in an easier way. I've been tinkering around with it today , so i got loading & resizing working , and then i wanted to rotate my image by x degrees , but i didnt find any function for that in this library, the closest one being FlipRotate , but it can only handle 90 - 180 -270 degrees rotation , which is not what i need. I eventually found out the TransformImage function that uses a callback with which you can directly modify the pixels of the image , but i really have no ideas how i could rotate images by modifying their pixels. I was really surprised that there was no implementations for this. So in short , my question is the following : How would i implement a function that rotates an image by x degrees (where x is between 0 and 360, and the image being a DirectX::ScratchImage struct) ?

Thanks in advance to everyone who tries to help me with this issue.

walbourn commented 1 year ago

What you are trying to implement is quite complicated and beyond the scope of the Windows Imaging Component (WIC) or DirectXTex. The special cases of rotate 0, 90, 180, and 270 can be easily achieved with shuffling around the pixels, but to create an arbitrary rotation requires much more complicated pixel sampling and blending.

If you can do this on the GPU, then you may find it's a lot easier to just draw your image as a rotated sprite on a render target and then capture that to save to a file. DirectX Tool Kit might help there.

https://github.com/microsoft/DirectXTK/wiki/Sprites-and-textures https://github.com/microsoft/DirectXTK/wiki/ScreenGrab

Otherwise, if you have to implement it using only the CPU, then a classic on the topic would be Wolberg, "Digitial Image Warping" (IEEE Computer Society Press, 1990)