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.

Tuesday, October 12, 2021

Some notes on using libswresample

FFmpeg, one of the finest open source software which is a swiss-army knife of multimedia, being used by virtually every media transcoder or player in the world has no guides on how to use the API and provides out of date examples.

Out of the entire framework, libswresample would be the most miserable thing to work with. Its documentation is not clear about how resampling should be done and due to outdated examples none can figure out the elegant way out.

Ironically, the elegant way without swr_delay() calculation is in its fork, Libav. Combining that with the modern libswresample AVFrame API, I was able to get it working.

Despite all this libswresample provides FFmpeg framework interoperability and sample-format conversion or channel re-matrixing or up/downmixing, which most other libraries don't all at once.

Initialization

Initialization is done with swr_alloc_set_opts() or the AVOptions API. It's not that you can't change the resampler parameters once you've configured it, you can with the AVOptions API.

There's a large list of granule options you can pass, if you're a DSP nerd and performance freak and you want to configure the shit out of it do it through the AVOptions API and if you are insane you can explore the Internal API.
int ret;
SwrContext *swr = swr_alloc_set_opts(NULL,  // we're allocating a new context
                      AV_CH_LAYOUT_STEREO,  // out_ch_layout
                      AV_SAMPLE_FMT_S16,    // out_sample_fmt
                      44100,                // out_sample_rate
                      AV_CH_LAYOUT_MONO,    // in_ch_layout
                      AV_SAMPLE_FMT_S16P,   // in_sample_fmt
                      48000,                // in_sample_rate
                      0,                    // [ignore these
                      NULL);                //  we dont need]

// assume returning of -1 as error
if(swr != NULL)
  return -1;

ret = swr_init(swr);

if(ret != 0)
  return -1;

AVFrame* out = av_frame_alloc();

out->format = AV_SAMPLE_FMT_S16;
out->channel_layout = AV_CH_LAYOUT_STEREO;
out->sample_rate = 44100;
out->nb_samples = 666;

Conversion

Assuming you already get AVFrames from your decoder or anywhere. You pass it to the resampler and it will put the resampled data in a FIFO buffer, you can retrieve this at any time. Usually, you will keep feeding and taking data out, else the FIFO buffer will consume your precious memory.

It's a good idea to buffer it, but you can also retrieve it all at once as shown here.

You need to keep calling this piece of code over and over, put in a loop obviously.
// if you input supply ends stop feeding it data.
{
  ret = swr_convert_frame(swr, NULL, in);
  
  // if in a rare circumstance the audio config changes, reconfigure then convert.
  // you should ignore this if you can assure that your input never changes.
  if(ret == AVERROR_INPUT_CHANGED) {
    av_opt_set_channel_layout(swr, "icl", in->channel_layout, 0);
    av_opt_set_sample_fmt(swr, "isf", in->format, 0);
    av_opt_set_int(swr, "isr", in>sample_rate, 0);
	
    ret |= swr_convert_frame(swr, NULL, in);
  }

  if(ret != 0)
      return -1;
}

// keep taking out data, until it exhausts.
ret = swr_convert_frame(swr, out, NULL);
if(ret != 0)
  reutrn -1;

Packup

After all your work is done do swr_free(&swr) and av_frame_free(&out) to ensure no memory leaks.

It may sound all simple and straightforward to you but believe me, to reach this conclusion, it took me many hours of browsing examples, testing and debugging. Shameful, that the guys never bothered to document their so widely used library cum program.

I hope now you don't have to face the pain like I did '='

Saturday, September 18, 2021

Proof that JS is a right wing agenda

In a JavaScript interpreter the expression 1/x when x is 0 is Infinity. At a first glance, it may seem an innocent bad design choice of ECMA (as has been through the entire lifetime of it) but little people do realize the subliminal political preference underlaid, unless they're Byomkesh Bakshi-grade detective mathematicians that seek truth (like me).

So in today's article we prove that how this "simple" and apparently "bad design" is a pushing a political agenda. I advice the leftists who've been using it; to just simply switch to Python if they care about their ideals and not their web-developer certificate.

Why does it?

I assume you all to have preliminary knowledge of limits, though don't worry its not complicated at all.

We all are aware that right limit of 1/x as x tends to 0 is Infinity; but if we do it from the left it goes to -Infinity.

What did I just say? If we keep reaching towards zero in the X-axis from the positive side (right side in the graph) then 1/x will keep reaching Infinity, but if we do the same but from the negative side (left side in the graph) then 1/x will keep reaching -Infinity.

Now JS doesn't throw an exception ZeroDivisionError like Python or SIGFPE or querying with fetestexcept like C/C++ (another great™ decision of ECMA). Perhaps to make it a bit natural to mathematicians; it evaluates to Infinity.

The "proof"

Recall what I said about limits of 1/x . It has no two-sided limits but two one-sided limits. JS developers chose the right one, but why right one? Obviously because they're right-wing.

So next time if you come around a weird choice of a programming language, don't take it lightly. It always has a subliminal agenda underlaid which deceive the eyes of a computer enthusiast. Politics is everywhere my brother '-' you ain't safe.

Tuesday, August 17, 2021

Things wrong with OpenSSH

What is SSH?

I don't need to introduce it. Let's be honest, every "modern day" UNIX user knows about SSH. It was basically a security extension to RSH by the OpenBSD people. A user only ever uses SSH to spawn a shell remotely and administrate that particular system.

Though, as we know, every project only ever becomes unnecessarily complex and complicated as time passes; it's like a exponential function of time.

What is wrong with it?

  1. People have hard time setting it up.
  2. It violates the UNIX philosophy.

Why does it violate the UNIX philosophy? Because it does:

  • X11 forwarding (why not consider Wayland?)
  • Port forwarding
  • Tunneling
  • SFTP
  • SSHFS
all of which should be out of its scope.

X11 forwarding can be done without SSH by just setting the DISPLAY variable. This security feature should be in X.org (yes, it's the only implementation of X11) and not in SSH.

Port forwarding (NAT) is done with special kernel level subsystems like netfilter and iptables (Linux) or eBPF or PF in Linux and OpenBSD. It is a low-level I/O job dealt with accounting for flexibility and latency, thus kernel-to-user and user-to-kernel operations induce more CPU and memory usage whereas the kernel would just move the pages around.

Tunneling, solved problem. OpenVPN, IPSec, IKEv2 exist and specialized for the purpose.

SFTP, again; a solved problem. It is solved by FTPS, which uses SSL ensuring backwards compatibility with existing FTP infrastructure.

SSHFS. SSH only ever solves solved problems. NFSv4 mandates security, SAMBA has too, other sophisticated solutions might exist too.

But why are you writing this?

To dump what I've collected and compiled in my mind, I don't really care if anyone reads this or not. Also, there are absolutely no articles criticizing it so someone had it do it.

I'm not a UNIX purist but it's to make you purists (yes, you) aware of this.

What should I use now?

Talking about alternatives, I didn't see any except RSH. If you know put that in the comments.

Other resources on this

  1. http://harmful.cat-v.org/software/ssh