kolrabi's blog about coding and stuff...


Forking forks

Posted: 2014-06-28 at 15:31

There I was, writing my own image viewer when suddenly there was a gif that would make it crash. So I investigated and it turned out there was an error in the image library I was using (the Developer's Image Library). So I went and tried to find an updated version, but the author just doesn't have the time. Fortunately someone forked it and created the Resilient Image Library. From the description:

ResIL aims to display even corrupt image files as best as possible, and without crashing or exceptions.

Sounds great, so I switched to that one. And sure enough it didn't crash but showed me a corrupted image. All was fine. Except... the image wasn't corrupt. It displayed fine in the web browser.

So I had a look at the code. And changed some things here and there and before I knew it, I had completely rewritten the entire gif support of the library. After that I tried to add thread safety, as my image viewer is multithreaded and being able to load or manipulate multiple images at the same time sounds nice.

Then I might have gotten carried away a little.

For various reasons (including the fact that there is no source code reposity for either DevIL or ResIL) I have now created yet another fork (and it has a git repository). It is called "kolrabi's another Image Library" (don't ask) and can be found here:

kaIL Website


Project Ideas

Posted: 2013-06-08 at 20:42

Here are some great project ideas I'd like to do:

  1. Write a list of all project ideas I have

Stones Released

Posted: 2013-06-08 at 20:42

Stones logo

I finally got a game released on the android market. It is available as a ad supported, free version as well as a paid version.

If you have any problems or suggestions, feel free to contact me at bjoern.paetzel (at) googlemail (dot) com


It's now available on SlideMe as well.


And on AndroidPIT:

AndroidPIT Android Market

One screenshot of Stones Another screenshot of Stones

Division by Multiplication

Posted: 2013-06-08 at 20:42

A post in the forum on got me thinking about how to efficiently calculate the minutes and seconds to use in a function that prints the time if you have a given number of seconds. Here is some code that does the conversion:

void convert(
  unsigned int timeInSeconds,       // the number of seconds given
  unsigned int *minutes,            // store minutes here
  unsigned int *seconds             // store seconds here
) {
  *minutes = timeInSeconds / 60;
  *seconds = timeInSeconds - *minutes * 60;

As you can see, it first converts the passed seconds to minutes and then subtracts the seconds for the full minutes from the passed seconds. This didn't seem to be optimal to me, so I came up with another version:

void convert(
  unsigned int timeInSeconds,       // the number of seconds given
  unsigned int *minutes,            // store minutes here
  unsigned int *seconds             // store seconds here
) {
  *minutes = timeInSeconds/60;
  *seconds = timeInSeconds%60;

That way, I figured, the compiler could make use of the IDIV instruction on X86 machines. IDIV divides an integer and yields the quotient and the remainder of the division. Even though division is relatively slow, it can't be slower than a division, a multiplication and a substraction as in the first example, right?

I looked at the assembly code that is generated using gcc -c -O2 and as it turns out both versions are identical. Another failed attempt to outsmart gcc's optimizer, fat chance. However, that is not what is so interesting about it. What is in my opinion remarkable is the total lack of any division instruction whatsoever. Something very weird is going on. Let's have a look at the relevant lines:

 *minutes = timeInSeconds/60;
   1: ba 89 88 88 88        mov    $0x88888889,%edx
   6: 89 e5                 mov    %esp,%ebp
   8: 8b 4d 08              mov    0x8(%ebp),%ecx
   b: 89 c8                 mov    %ecx,%eax
   d: f7 e2                 mul    %edx
   f: 8b 45 0c              mov    0xc(%ebp),%eax
  12: c1 ea 05              shr    $0x5,%edx
  15: 89 10                 mov    %edx,(%eax)

Here is the magic that is happening:

  1. timeInSeconds is multiplied by 0x88888889
  2. The result is shifted by 5 bits to the right and stored minutes.

And that's it. The result is equal to timeInSeconds/60. Which is then, by the way, multiplied by 60 and subtracted from timeInSeconds to get the seconds. Here are some links with information about how and why this division method works:

Amazing stuff... =)

Experimental linux code for the NVIDIA 3d stereo controller

Posted: 2013-06-08 at 20:42

It wasn't easy (took me almost the whole weekend), but I have finally been able to hack together some code that activates and utilizes the 3d shutter glasses by NVIDIA together with my 120Hz monitor (haven't tested it on other refresh rates).

Nothing really useful for now, but it shows it can be done. There are still some timing issues, like a delay between buffer swapping and switching the shutter state leading to flicker at the top of the screen. I am also not entirely sure of many of the values I send to the controller. If you have any idea as to what exactly they mean or have any suggestions on how to make it more reliable, let me know.

Be aware, that I cannot completely rule out that this will damage your hardware, software or even your health. Use at your own risk!

But if you are interested in the code, you can get it here. You will need the neccessary hardware (120Hz Monitor, Hardware accelerated OpenGL and the NVIDIA 3D Vision Kit) as well as the nvstusb.sys from the Windows driver for the firmware and libusb for USB access. You might need to adjust a few things in the code if it doesn't work right from the start. Also I recommend having a look at the README file. That's what it is for. ;)

Edit: I changed the interface a bit and the firmware extractor should now work with any version. Get the new version here.

Edit: There is by the way a sourceforge project for this, for a while now.

Versatel DNS What The Fnord

Posted: 2013-06-08 at 20:42

This morning I suddenly had problems connecting to webservers. My DSL connection was still up and running, though. So I tried to ping a known host ( It took a while to resolve the hostname, but then I got this:

PING ( 56(84) bytes of data.
64 bytes from ( icmp_req=1 ttl=58 time=13.4 ms
64 bytes from ( icmp_req=2 ttl=58 time=13.0 ms
64 bytes from ( icmp_req=3 ttl=58 time=13.2 ms
64 bytes from ( icmp_req=4 ttl=58 time=12.7 ms
64 bytes from ( icmp_req=5 ttl=58 time=13.3 ms

Very mysterious. Maybe this is related to the fact Versatel also resolves every nonexistent domain to their search web page.

You may mail me at: kolrabi [at] kolrabi [dot] de