Sunday, January 9, 2022

The mathematical art of "image rotation"

In the jargon of image manipulation and broadly, computer graphics, rotation is a very essential operation in our day-to-day lives. One requires basic knowledge of trigonometry and Cartesian coordinate system to grasp the concept of rotation mathematically.

One must also know programming in order to accomplish the task. A person needs little formal premier knowledge of computer science to write such code. The reference program given is written in Java.

Derivation

A digital image rectangular is rectangular in shape. Let w be its width and h its height.


Let O be a point within the rectangle (most likely in center).
Let OR be a ray parallel to the width of the rectangle.
Let P be a point within the rectangle existing at a clockwise angle ϕ from OR.

Further rotating it α angle apart clockwise we arrive at a point Q which expressing in form of Cartesian coordinates we find it to be,

(OR, RQ) = (x', y')
                 = (OQ × cos(ϕ+α), OQ × sin(ϕ+α))
                 = (OQ × (cos(ϕ)cos(α) - sin(ϕ)sin(α)), OQ × (cos(ϕ)sin(α) + sin(ϕ)cos(α)))
                 = (OP × cos(ϕ)cos(α) - OP × sin(ϕ)sin(α), OP × cos(ϕ)sin(α) + OP × sin(ϕ)cos(α))
                 = (OS × cos(α) - SP × sin(α), OS × sin(α) + SP × cos(α))
                 = (cos(α) - y sin(α), x sin(α) + y cos(α))

Angle addition provided a way to not requiring to determine ϕ saving us from complicated trigonometric functions (eg. arctan, atan2). This becomes a linear transformation which can be written in matrix form but it's omitted here.

Application

For example we take this image.


In theory, it should work like a charm but it always produces poor quality results in reality. Suppose we rotate the previously mentioned image with this method by 60 degrees then it looks really awful.


As x' and y' aren't integers sometimes thus truncating their decimal part would overlap with other pixels, leaving patterned gaps in the output. Though aesthetic, but are really annoying, unwanted and of course impractical.

So instead of finding for a coordinate what its rotated coordinate would be, we find for a rotated coordinate what its original coordinate was such that it does nearest-neighbor sampling while fetching the pixel which fills the void pixels. Simply the process has to be reversed.

(x, y) = (OQ × cos(ϕ-α), OQ × sin(ϕ-α)) 
          = (cos(α) + y sin(α), y cos(α) - x sin(α))

Here (x', y') is the rotated point and (x, y) the original one. We iterate over all points (pixels) of the output Cartesian plane assigning coordinate for each to (x', y') and find the corresponding coordinate in the input Cartesian plane and quantize it because fractional coordinates aren't possible.

One might clip the coordinates to width and height of the bounding rectangle (image), in order to avoid out-of-bounds access or they may extend the output image considering the farthest radial distance from the origin (mostly in center) to include parts that go outside the picture.

Doing it the reverse way yields us,

Perfect! Isn't it? It is really crispy too. If we've used a sampler using bilinear, bicubic or Lanczos interpolation it would have appeared smooth but blurry too and would complicate the process.

I hope you learnt something from this and be able to use it in your future works. This might also come around helpful for me in the future '-'

Thursday, January 6, 2022

The "holy" Archmen

An ancient tale

A Gentoopur native intruded the territory of Archgarh in search for her lost lover who was held hostage by the Archmen after an argument in which he won.

Gentoopur and Archgarh are two opposing kingdoms, none can tolerate the other. If two native of those kingdoms clash at some place, one insults the other like there's no tomorrow.

Upon entering she saw, Archmen chattering with themselves. When she proceed further, some folks looked at her in a questioning sight. One ugly-clumsy beast looking at her in vulgar sight, comes forward filthily and asks, "O churi, tumi kida re? Age kohno dehinai" (Ay girl, who are you? I've never seen you before).

For her it was as if a thunder stoke her. She somehow controlled herself and with a trembling voice she replied, "Gen-t-t-oo-pp-uu-r th-th-kke a-a--asccc-hi" knowing her eternal fate. She put her head down in fear.

Men there had their sight fastened at her, they looked like incarnate demons with lust swelling down their horrific body. The questioner replies, "Oh tahli ei byapar?" (Oh, so that's why) bursting into a unearthly poignant laugh that'd be only possible by Baphomet.

That spine-chilling laugh alone snatched her pranshokti. Human-like demons around him insult with obscene words of an unknown language. If someone were to stand there would tear his eardrums apart.

For a moment everything paused. It was the silence before the storm. They all, jumped on her and gang-raped her. They were raping her as if they had accumulated their sexual desires for centuries and letting it out all at once. Semen flowed on the ground as if it were a river and she was dipping herself in it. 

Sometime later, she got consciousness back and discovered herself in an abandoned forest. She was traumatized, she even forgot to weep for it, but none except the tress were there to witness this. She almost lost hope in life, but a voice from inside told her "tumi ei choyti sontaner jononi, tomake bachtei hobe!" (You're the mother of these six children, you must live). This voice moved her.

She made a shelter for herself out of wood and she was pregnant at that time. Nine months had passed, she gave birth to six children, 3 girls and 3 boys. One day finding food in the forest she noticed a skyscraper, she instantly deduced it to be the Fortress of Gentoopur.

She took her six children and went there. Everyone looked at her in questioning sight and it were her known people but they avoided her, she felt herself collapsing into darkness for world becoming insanely cruel against her for a inhumane crime she didn't instigate but was the victim of.

When she reached home, her parents saw her and they felt a warmth of happiness but as soon as they saw the children along her they were stunned. They questioned her, "tor chele meye hoyeche bolish ni toh" (Oh, you never told you had kids). She burst into tears and told them what happened to her. In the face of the world, she was the criminal but her parents were the only one to fathom the truth and the injustice done to her.

Everything was fine for a period, but unfortunately one day the king was reported of this and asking the rajpurohit he replied "Shankarviryajata santana na-accept()ami." (We are not accepting mixed-semen derived offspring), so the fate of her was to be thrown away the kingdom. Her family resisted but they were told to be executed if they dishonor the order.

In pain, frustration and given up the hopes on life she wrote "kill(-9)" mantra on a mantraputa prastarkhand ("sanctified rock") with her HID (human identity, a unique integer for each human) and thrown it to the "syscall" lake blaming herself and her children for everyone's interruption.

The main point

Okay enough of that gory, erotic, ironic story parodying ancient India. I know I made the Archmen evil here but the point is —

Their reasons for using ArchLinux is the lamest of all. “But Apple seems never really create the soil for RICE.” — but then again, its you RICEers who desperately want their desktops to look that of OSX. “But to some extent, it is a failure” — and how is Ubuntu a failure? “Most of them live in Terminal or even access Linux remotely with SSH from either Windows or macOS — why is this stereotype of terminal usage? And no, developers do not SSH into a Linux machine.

“Arch Linux has the best home page (IMO)” — ever seen the Gentoo homepage? It's stylistically superior to it (IMO). Ironically, he shows the homepage of ArchLinux in a Mac or his "Mac like RICE." “Arch Linux chooses blue as major color, which is vivid and shows the sense of technology. I kept on copying the color scheme when I was building my own home page.” — LOL WTF. This is kind of person you'd expect to ejaculate as soon as a girl makes a ArchLinux logo-like position. Perhaps, this is his “waifu.”

“I know that the second point is the most important reason why it is popular in RICE culture.” — in fact, what happens in the "RICE" culture is the exact opposite. “And Pacman makes it even easier and powerful.” — Pacman is the most non-intuitive package manager I've ever seen. What's up with these "flags" you got? Why can't you be sane like dnf or apt and use normal sub-commands like "install", "update", "purge" instead of terse flags like "-S", "-Su", "-R"?

“It is somehow like Gentoo Linux but seems to be a lot easier, because the Gentoo packages and base system are built directly from source code but Arch tends to make user build packages quicker.” — is this guy seriously retarded or something? Since when Arch has been "building" packages?

The only thing great about Arch is its Wiki aka. documentation, much of it also applies to other distributions too. Apart from that Arch tends toward package coupling, that itself violates its minimalist philosophies. The guy praising Arch for the minimalist approach he mentioned are also available in other distributions (eg. Ubuntu MinimalCD, Debian (DE-less), Alpine, etc.).

ArchLinux is nothing special, it's just another hippie trend and a false sense of supremacy. Real (Linux) geeks don't care about the, distribution they're in. For example, mostly you'll find people on SO using Ubuntu or Debian, they're still much knowledged about computer science than these hippies. Take the genius, Terry Davis, he used Ubuntu and his work is beyond our appreciation.

The main point is — ArchLinux users prominently demean Gentoo users on the basis that they "waste" their time compiling for an insignificant benefit in performance. They're like the Muslims of computing in the sense that they always wander in groups and have strong unity and when they meet a person not using a distribution of their ancestry, they do their best to insult them in obscene ways, the above tale is an exaggeration of that.

Performance isn't the reason you compile your software, it's a bonus you get. The main benefit is feature customization. Gentoo equips you with a framework and community assistance to do just that painlessly and automatically.

USE flags make selecting and deselecting features of packages just toggling keywords off or on. That's why you need local compilation. Also, for large programs like LibreOffice, Chromium, Firefox, etc they provide you choice for binary packages in-case you want to save time.

With ArchLinux, you're kind of forced to take a configuration decided by the creators. But on Gentoo, you're your own god, you truly build it from scratch. Gentoo is for experienced Linux users, but experimentation earns you experience so have fun hacking around it. Arch is for intermediate Linux users.

At the end of the day, it essentially doesn't matter what distribution we use and what matters is that we're using GNU/Linux. GNU/Linux is unity in diversity. We are all brothers and sisters. Our unity, is our power.