Archive for the ‘Hacking’ Category

Bazaar attraction

Tuesday, July 1st, 2008

My life was saved yesterday.

Less dramatically, a piece of software made my life better whereas, without it, life would have been worse.

Let’s give a bit of context first though: I’ve a very basic form of regression testing for Firtree (reflecting its somewhat volatile nature) which consists of a number of ‘example’ python scripts which exercise Firtree’s features. Each script also prints out the allocated object count at program exit which, if non-zero, indicates something has fecked up in my reference counting, either inside Firtree itself or in the Python bindings.

Usually, being a good little software engineer, after writing a feature, but before committing, I run these scripts confirming they still look right and this reference count is zero. Yesterday I went a bit mad and added a ton of features and an associated ton of commits. As is virtually guaranteed when you behave so recklessly, when I did run the scripts everything looked screwed up and memory leaked like a sieve. This prompted one of the worst things you could hear when, for example, you are sitting in the passenger seat of a moving car: a single, sharp, under the breath uttering of the word ’sh*t’.

Fortunately I had used Firtree as a test-bed for developing with Bazaar, a decision I am now very glad of. Suddenly I had the coding equivalent of a TARDIS and an infinite number of universes to play with. I branched off from the last ‘good’ version of Firtree and gently rolled forward in time until the naughty patch was revealed. Then, in true time-traveller style, I made a change in the past but then let the future merge in. Painless in the extreme with bazaar. Finally we reached the ‘alternate 1985′ where, far from being the apocalyptic vision I had created, all was well and no memory leaked (that I could detect).

I am far from groking Bazaar in any meaningful way (as anyone who cares to notice the fsck-ups in the Firtree tree will attest) but already I have experienced a complex branch and step-by-step merge which would have been incredibly painful with other tools.

Oh, by the way, feel free to s/Bazaar/$MY_VCS/. This is more a post of how rocking modern VCSs are, not how Bazaar is necessarily the best.

Update: An example of the process is available on my website. I’m not recommending this as the ‘Right Way’ but it worked for me.

Chroma key with Firtree

Sunday, June 29th, 2008

The best way to test your dog food is to eat it yourself and so this afternoon I set out to write a little PyGTK app that would use the Firtree Python bindings to allow you to pull an alpha matte via chroma key. Chroma key, for the less SFX minded amongst you, is the process whereby the green backgrounds so beloved by special effects people everywhere are ‘cut out’ and replaced by… well, whatever you want your actors to be standing in front of.

Pulling a matte, as such a process is known, is a little tricksy and requires not a little tweaking. I had in mind an app which would let you load a high-res photo, pan and zoom around it lightning fast and let you see the results of tweaking all the matting parameters in real-time. Just the sort of thing Firtree is supposed to be good at. An afternoon’s hacking later and we have this little gem:

It is a recording of the chroma key app that, as of 10 minutes ago, resides in Firtree’s examples directory. You load your image, select something that is background coloured and fiddle with some sliders. Firtree’s pixel pushing powers allow it to reflect the result in real-time. I love it when software starts to become useful and not just Another Framework TM.

This is actually the second little app I’ve written which has used Firtree. I find writing little apps like this very good for shaking out bugs and exposing use cases I hadn’t though of. The first ‘non-trivial’ Firtree app was one I wrote at work. The app displays streams from a pair of computer vision cameras and allows you to monitor the stream, record it and overlay detected features in real time. It is suprisingly useful to be able to get on-demand feedack as to whether your funcky computer vision algorithm is working :).

Update: Should anyone care, you can also browse the source to the app via launchpad.

Firtree redux

Wednesday, June 11th, 2008

This evening, firtree managed to render this:

blobby checkerboards!

Why is this impressive? Well, it means that lots of things are working and it is slowly reaching the point where it might actually be useful. Read on for an overview of the system.

(more…)

GLSL front end source code

Tuesday, June 10th, 2008

Recently, for a personal project of mine, I was trying to find the code 3Dlabs released for the front end to a GLSL compiler. This code is useful for a number of reasons; for me it was a nice starting point for a domain-specific, vector-oriented, C-like language.

In any case, it appears that the 3Dlabs developer website, where this code was hosted, is no more. Google searching is almost fruitless giving, as it does, a load of dead links. After a bit of a struggle I found a mirror of the September 2005 release and I’ve decided to mirror it on my site to make it a bit easier to download. The license is below. It’s a bit like a MIT or BSD license.

Copyright (C) 2002-2005  3Dlabs Inc. Ltd.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:

    Redistributions of source code must retain the above copyright
    notice, this list of conditions and the following disclaimer.

    Redistributions in binary form must reproduce the above
    copyright notice, this list of conditions and the following
    disclaimer in the documentation and/or other materials provided
    with the distribution.

    Neither the name of 3Dlabs Inc. Ltd. nor the names of its
    contributors may be used to endorse or promote products derived
    from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.

Reverting to a previous version of the Matrix

Wednesday, May 28th, 2008

Back in t’day, between using the original Netscape Messenger and GMail, I used two mail clients. They were pine and mutt. Pine was what I read my University mail in and mutt took care of my personal mail.

Spin forward a few years to the Great Unification of my email system which happened when I left University (for the first time). I moved everything over to a large, brittle, system consisting of equal parts exim magic, procmail sourcery and fetchmail pain. This towering edifice of hacks had, at its treacherous pinnacle, mutt and a huge custom .muttrc.

Then along came GMail and all was well. Well, nearly. GMail was missing some of the nice features that I had come to rely on. These included:

  • Mutt would let me edit mail in VIM. GMail wanted me to do serious editing within a TEXTAREA widget. After some pain, I managed to deal with writing slightly less pretty mails.
  • Mutt has a lovely ‘reply to list’ feature. This feature helps me deal with some of the vagarities of various mailing lists I’m on. In particular it allows one to choose to ‘reply to sender’, ‘reply to list’ or ‘reply to all’. GMail lacks this middle option.
  • Mutt has the concept of ‘hooks’. The simplest use of which was to use a different signature automatically if I was either replying from a particular address or the original email had come to me via a rôle list rather than a personal mail. GMail lets you set up multiple sending addresses but doesn’t let you specify rules for signatures or for choosing these.
  • Mutt is prettier. OK, this is entirely subjective but I find Mutt’s clean interface far preferable to web-based email. I’m a bit of a conservative and somehow get reassured by curses-based UIs.

On the other hand, GMail was run by people with email CLU and my email system was run by someone lacking a great deal of this CLU, i.e. me. In time I came to either work around these problems or find Firefox extensions/hacks which fixed some issues. Said workarounds were non-ideal, however, and the extensions would often stop working as GMail was upgraded.

Then GMail allowed IMAP access. Recently, at home, I was testing evolution, mainly to try out the Google Calandar integration. I set up IMAP access to GMail while I was there and suddenly realised the obvious: I could have the best of both worlds, using mutt to read/send/compose mail and using GMail’s far better maintained email servers.

So today I dusted off my old .muttrc, deleted the lines relating to my old setup, added some simple stuff to let mutt use GMail’s IMAP service and fired it up. I’m trialling mutt as my primary client for the next week and we’ll see if I regain my former email joy.

As a corollary to the above, email from me might be a bit… erratic… this week as I retune myself to the One True Way. :)

My Location

Thursday, November 29th, 2007

Google have launched a new feature called My Location for their mobile Google Maps application. It uses knowledge of which base stations your phone is near to calculate an approximate location. All this is irrelevant though. I’d suggest watching the video just to find out what American’s think the most famous English food is…

Visitor pattern

Wednesday, November 14th, 2007

Today I are be’n mostly reminding myself that I should brush up on my software engineering. Specifically I should invest some time actually reading most of the common design patterns. Due to a chance sentence in a paper I was reading, I discovered the visitor pattern which neatly solves a problem I had been having in some of my software.

I have a crazy dream!

Tuesday, October 9th, 2007

Dear crazy h4×0rs out there,

Do you think it is technically possible to port GDK to an AJAX/Canvas sick hybrid?

CZip

Monday, October 8th, 2007

Recently I wanted to compile a quick OpenGL-based utility and be able to ship it to different machines as a single binary. For those not in the know, shaders — the micro-programs which run on the GPU — are usually compiled at run-time for the specific GPU that is accelerating OpenGL. This means one has to ship, in some form, the source code. This isn’t a problem except that I wanted a) a stand-alone binary and b) not to have to remember to copy around any resource files.

Now, I could simply write my code as a big static string at the top of my C source. That is pretty ugly though. What would be nice would be to embed the file in the executable somehow so that it can be retrieved at run-time. Windows has a mechanism for this termed resources but an equivalent is not available to Unix people without an advanced degree of ELF guru-ness.

Not possessing such guru skills I looked for a simpler solution and, crawling like a dæmon from past, the XBM format reminded me of itself. XBM is a simple format for storing 2d bitmaps. What makes it interesting is that it is capable of being parsed by a C compiler at compile time into a ready packaged chuck of data inside your executable.

“Surely someone will have written an equivalent for general data,” I said to myself. Five minutes Googling failed to turn up the (almost certainly existing) prior implementation and so using the maxim, “If it takes longer to Google it than to write it, write it,” I did so.

The result is czip.c. It is a trivially simple utility that, using zlib, takes a file and outputs a C-compatible compressed representation for later reading. Looking at the result of making czip compress its own source you can see quite how similar to XBM it is :).

Why Gtk+ is teh suckz0r

Thursday, October 4th, 2007

Let’s consider for a moment the selection of cross-platform C/C++ UI toolkits one might use to develop a few test apps at work. One might consider looking at Gtk+, Qt, FLTK or wxWidgets. All of these have neat little widgets that provide you with an OpenGL context to render into so that you can display the results of your exciting graphics algorithms. All of these? It turns out the Gtk+ doesn’t. There are unmaintained solutions but nothing ‘official’.

Looking into it a bit more, one finds a bug which requests some OpenGL integration in Gtk+. A bug started just over four years ago. This is frankly shocking and so it appears I’ll be keeping with the f-ugly but perfectly suitable FLTK for the time being then :(.