March 29, 2008

Java math performance

Java 1.6 on an x86 (Intel Core 2 Duo in this case), how much faster are the 4 basic math operations +-*/ when it comes to integer vs floating point operations? We’ve all been taught that integer operations are vastly faster than floating point operations, but that was probably only true when you were taught that back in school.

Now a days 32-bit floating point operations are on par with their integer counter parts. Most modern processors can do most floating point operations in an single instruction, just like integers.

I wrote a micro benchmark doing the same operation 4,000,000 times on the same set of vectors and here are the results in milliseconds:

client	+	-	*	/	sqrt	sin	cos
integer	16	16	15	26
long	30	30	30	120
float	15	15	16	30
double	29	26	27	52	242	4205	4205
fixed	13	12	40	191	427	60	59

Fixed is fixed-point library I am testing. The fixed point sin/cos functions are so fast, since they are pre-calculated tables at start-up. On the other hand, Java’s sin/cos functions are abysmal. Java has strict requirements on sin/cos functions and this issue has been brought up as bug 5005861. This is probably one of the last major Java performance issue. If you need a fast sin, then approximate it.

The table above shows there is no performance difference between integers/floats and longs/doubles, except that double’s divide is faster than long’s!

The above was running without the -server flag. Below we have the flag turned on.

server	+	-	*	/	sqrt	sin	cos
integer	21	115	14	21
long	24	25	25	106
float	12	12	12	29
double	24	24	25	54	87	3317	3260
fixed	14	13	32	189	314	57	64

With the server flag turned on, there is some performance issues at the start of the benchmark, which erroneously shows integer addition and subtracting being worst than with the server flag turned off. I believe this is just a loading issue, and in the real world you’ll see performance at the same level as multiplication. One very interesting data point is Math.sqrt is much faster with the server flag on. This benchmark shows an improvement of almost 3x!

I would say it is probably safe for everybody to use doubles for math intensive applications. I not saying just use doubles everywhere, floats and doubles have their pitfalls as well, but that is for another article (the one sentence summary is “Time and money are valuable, so don’t use a float or double to represent it!”) Performance wise, don’t fret it, go double.

February 17, 2008

Innovative Feature: D300’s HDMI + Live View

I just noticed that you can use Live View via HDMI with the Nikon D300. That is just amazing. Imagine using a small 7″ LCD with HDMI (or DVI with converter) input on the D300. Sure you’ll need a small battery to power the LCD, but it is far lighter than tethered shooting with a laptop.

I hope Canon realizes how useful a HDMI port is and adds one to their future models. This is the first time I’ve ever been truly tempted to purchase a Nikon.

January 31, 2008

Future of Digital Photography: Megasample

With 8 megapixel SLRs today, we can already produce 300 DPI prints on 8×12″. The vast majority of the population would not need a print any larger. As the megapixel race continues, we will reach a point where the print size at 300 DPI will satisfy practically everybody. Personally, I would like 300 DPI prints on 12×18″, so once a 20 megapixel camera comes out, am I done? Nope. I will still chase for more megapixels.

Bayer sensors interpolate filters to produce a color image. It works out to be 1/3 rgb samples per pixel due to interpolation. This produces images that are not completely sharp when viewed at 100% crop. However, we can increase the number of rgb samples per pixel by downsize the image. By combining the color value of many pixels into one, we add all those rgb samples into one pixel. Therefore, given a 32 megapixel image, I would downsize the image to 20 megapixel image, since it would still give me 300 DPI on 12×18″. But by going from 32 to 20, we have gone from 1/3 rgb samples per pixel to over 1/2 rgb samples per pixel. This improves color accuracy and image quality.

At the rate todays technology, the future will soon hold a million rgb samples per pixel, or a megasample. Then marketing will take us on a megasample war.

January 25, 2008

Getting the shot: Stitching

I was up on Mount Seymour hoping to get some nice snow photos, but the weather never cleared up as promised. As I was about to give up, I noticed a bright spot between the trees. Although the sky over my head was gray, westward the sun was finally showing its face.

It lit up a mountain close by nicely, but I couldn’t get the shot.

Trees were in my way, and there was no way to either get higher or in front of the trees. So I turned to leave thinking what a waste of a day. As I walked down the mountain, I kept my eye on the mountain and noticed I can clearly see the either mountain as I was walking. I thought to myself, “If only my camera can capture what I just saw, I’d have a great shot.” Then I realized, it can.

I just need to take pictures from different positions to see different parts of the mountain (ie. abusing perspective). Finally, stitch the images together. The most common use of stitching is to create panoramas, but this is a little different. I will be stitching images together to “virtually” remove the trees.

Here are the original images, as you can see each section of the mountain is guarded by trees.

From those images, I erased the trees.

Then into your favorite stitching program, and out pops this (after some cleaning up).

I’ve never seen this technique used before (not that I believe I am the first to use it either), but this shows how you must remember tools are versatile. Don’t limit your thinking that stitching is only for panoramas.

January 7, 2008

Broken by Design: USB Ball Mouse

I was in need of a mouse, so I pick a random one from IT. It was brand new and unopened from Dell. I didn’t have high expectations from the included mice, but boy was I in for a surprise.

It was a USB Ball Mouse. A what? I am sure most of you have replaced all your mice with optical ones, since ball mice suck. But it didn’t end here, when I tried to use the mouse, it complained I didn’t have the proper drivers. Drivers?? For a USB Mouse?? So they were cheap, therefore they used a ball mouse, but so cheap that they didn’t implement the HID interface for this mouse!

How many young computer users are going to plug in this “funny” mouse and wonder, “WTF is a driver?” Just plug in the mouse and it works…right?

If they were going to be this cheap, they might as well as not included it at all.

December 24, 2007

Next Generation Operating System: Part II Immutable

Operating systems should be immutable. No amount of installed programs or add-ons should change any of the operating system files. The only exception of course is when the operating system updates itself.

A quick look in my Vista install C:\Windows, I see files that don’t belong: KHALMNPR.Exe (Logitech), War3Unin.* (Warcraft 3 Uninstaller). Why does Windows let (or require) third-party programs add files to the Windows directory? It gets even worst in System32.

Linux is far better in this regard, but it is still possible to replace say ls with another version. I know you’ll need root access, and it is unlikely for this to occur, but I believe it should not be allowed.

The current state of affairs is to allow for customization, configuration, and extension. But these should not modify the operating system, it should be well defined on how to interact with the operating system.

Security and reliability require sacrifices. This proposed immutable system would reduce flexibility, but it would make things simpler and better defined.

December 9, 2007

Aliens, Aliens, Frickin’ Aliens

My favourite game genre is first person shooter. Nothing beats shooting baddies through the eyes of your avatar. There is one annoying trend in the fps worlds - aliens. So many great first person shooters just boil down to fighting aliens. I hate fighting aliens, I much prefer killing humanoids or androids. Ok, that didn’t come out right, but you get my point. The main reason why I hate aliens in fps is it blends a perfectly good fps with the horror genre. Look, if I wanted to play a horror game, I would go a buy one - but stop messing with my first person shooters!

There are some exceptions like the Half-Life and Doom series. In these games you expected aliens. But there are some offenders that have tricked me by first introducing humanoids, and then switching to aliens. Red Faction, System Shock, and Return to Castle Wolfenstein (here I lump the undead in with the aliens) are all games that I started playing and loved, right up until the aliens showed up.

I don’t understand why we don’t have more android in first person shooters. I would much rather unplug the enemy than step on them.

December 6, 2007

Next Generation Operating System: Part I Sandboxing

The current state of operating systems is very painful. They are fundamentally flawed in multiple ways. In this multi-part series I will talk about what I think will be the next generation operating system.

With all the noise about security, why hasn’t operating system level sandboxing been done? I mean, its been done, but never done properly.

An application running in a sandbox only has permission to whatever security policy it has been given. Thus, it is possible to run arbitrary code with confidence given the right security policy.

Arbitrary code?? Yes, arbitrary code. The sandbox I have in mind will allow me to download and run any virus, keylogger, or malware.

How can sandboxing achieve this? It’s all about the default security policy. A new program will be given the minimal amount of permission by default. The default set of permission would be:

  1. Run code
  2. Take keyboard/mouse input
  3. Manipulate user interface
  4. Save/read files

“Wait a second, if by default programs have access to keyboard input, can’t it be a keylogger?” Nope. This permission only allows keyboard input to the application window. It doesn’t give permission to get keyboard input when the application is not in focus. “Wait again, if the program can save/read files, can’t it destroy all my data and phish for personal information?” Nope. The saving and reading of files will use a standard file chooser. The program doesn’t have access to any files at all without the user pointing to it.

Given those permissions, it is impossible to author malicious programs, expect for showing an offensive image. So the worst you have to worry about it those “scary popup images”. Quite the difference from complete compromization of security.

But what programs can you run under those permissions? Well, to be honest, pretty much nothing. You could run Notepad, but that’s about it. To allow for the majority of non-Internet applications to run, we need a few more permissions, namely:

  1. Create threads
  2. Read files in local directory tree

Now we can create powerful applications like Photoshop and Microsoft Office. If given permission to access the Internet (I mean selectively to applications - not adding it to the default permissions list), then we get the rest of the Internet applications like Firefox, World of Warcraft, etc. Given these minimal set of permissions, we can do the majority of what we use the computer for. And remember, all this is achieved without compromising security. The power of sandboxing.

More complex applications can be selectively given more permissions. The method in which extra permissions are given to an applications is a little tricky. The application will have to ask the operating system for permission, and in turn the operating system will ask the user. As much as I hate Vista’s User Access Control, that is exactly how you would give extra permissions. Of course it wouldn’t be broken like Vista’s. I imagine a standard dialog which lists the extra permissions the application is asking for. The user can then check off which permissions are allowed or denied. This dialog can even be integrated into the application. The API to this dialog simply doesn’t give access to check off permissions programmatically. Since there is no permission to move and control the mouse, the application can never give it self extra permissions.

This is so simple, so powerful, and yet there is no major operating system that does this. Well, there is a reason for that. Current operating systems cannot just turn on sandboxing, or else it would break all the applications in existence. Also, sandboxing needs to be done at the kernel level, it can’t just be patch on top of the operating system. It can be hard to imagine how sandboxing would work, since current operating systems allow you to do so much by default.

So where does this leave us? What we need is a new operating system from the ground up designed around sandboxing. This operating system will be immune from malicious programs. But of course it doesn’t stop the user from being stupid by allowing all permissions to a virus and running it.  However, this is far more difficult that running any old .exe.

November 25, 2007

The Importance of Post-Processing

The first step in learning is making a mistake. Fortunately in the digital world, mistakes are generally correctable. I want to show some mistakes I have made in the past so we can all learn from them. This mistake is particular is about lighting and post-processing direction. I will show how important post-processing is and how it can go wrong.

This crane was shot recently in a Bird Sanctuary nearby. At the time of the shot, I didn’t consider back lighting, hence the crane was under-exposed.

I liked the composition, so I tried to fix the exposure in Photoshop. To fix the under-exposed crane, I increased the Fill Light. Now that the crane is nicely exposed, I leveled it.

Original crane Filled Light Crane Leveled Fill Light Crane
Original Fill Light Leveled Fill Light

It looks way better now. But there is a problem. This image is boring. It is just a photo of a crane. There’s nothing special about it. This occurs at times during post-processing, you realize its all wrong.

That’s when you need to start all over again. I actually like how the crane is dark in the original image. So instead of brightening the crane, I darken it using Blacks. This turns the water a nice dark blue. A nice surprise. After leveling, this image now has a lot of kick to it. But there are still some issues. The water is littered with specks of garbage. This is where the Heal brush (or Clone) comes in handy.

Blacks Crane Leveled Blacks Crane Healed Leveled Blacks Crane
Blacks Leveled Blacks Healed Leveled Blacks

The image is much cleaner now. The water is silky smooth. There’s still one thing bothering me. The crane isn’t completely dark. There’s still a small edge to it. A quick session with a soft black brush fixes that.

Filled Healed Leveled Blacks Crane
Filled healed Leveled Blacks

This dark crane is a much better image than bright one. It goes to show the direction of post-processing affects the end result heavily. Its also interesting that removing the garbage from the from the water greatly improved the image. It makes one realize how misleading the final image is. There probably does not exist a body of water that clean with a crane standing in it. Post-processing is lying, but how so beautiful it is.

Looking at the original image, I would have never guessed it would turn out as it did. You only really need two things in an image: focus and composition. Everything else is more or less fixable with some post-processing.

November 18, 2007

Time to get the sphere rotating along a vector

Hello world!

I’ve been using this inter-web thing for a while now, and when I came across this post by Jeff Atwood, I just knew I had to create my own blog. Now you know who to blame.

I am a Software Engineer by day, Gamer at night, and Photographer all the other times in between.

Powered by WordPress