GPU-accelerated Photo processing

Seems a bit passé right? Well apparently not. Apparently using the GPU to do all the heavy lifting isn’t something that the popular photography applications do. Lightroom and Photoshop have some GPU acceleration, but apparently Lightroom doesn’t really benefit unless you have a big display (I guess they do all their rendering on a smaller view of the picture)[1].

On Linux the use of the GPU in the most popular photo suites is spotty at best. RawTherapee doesn’t seem to do it at all, and Darktable’s is flaky. In fairness to Darktable, they do support OpenCL in some circumstances [2], but if it doesn’t work on the GPUs I have (2x Radeon R9 290x), which are now fairly old and have reasonably good driver support, you have to question whether it is worth it. In fact, even on my laptop, which has a Broadwell (ie 5th generation intel Core) series CPU in it, Darktable didn’t want to work with OpenCL.

What I’m wondering is, why OpenCL? OpenGL has a shader pipeline that is pretty much purpose built for processing 2D images, and for the slightly more complex tasks (like generating the histogram) it has had compute shaders since v4.3. Also, it has much better driver support than OpenCL, especially on linux, thanks to Valve’s work on SteamOS.

So, over the Christmas break I started working on an OpenGL-based, GPU-first photo editor (I’m calling it Monet). I didn’t get very far, and haven’t done much since, but I did just recently get the Demosaic operation to work correctly (Read more about that here [3], I based what I did on this article). So, I did a fairly non-scientific comparison of a very simple action: resizing the window, and monitored the CPU and memory usage while I was at it.

CPU/Memory comparison - Darktable vs Monet vs Idle
CPU/Memory comparison – Darktable vs Monet vs Idle

My method was simple: open both programs, and let them idle long enough that all CPUs and memory were mostly stable. Then resize each program’s window up and down by dragging the resize handle in and out so that the image goes from about 2cm wide to about 15cm wide and back, at a frequency of around 2Hz, for about 10 seconds. You can see from the image that Darktable had to work the CPU (in fact all of them) quite hard do do this. Monet is barely distinguishable from idle. There is a small increase in memory usage while resizing Darktable, and none for Monet. Note that in Darktable I turned off all the processing modules except Demosaic and White-balance, because that’s all Monet can do right now.

This has, at least, motivated me to push on with this project, because there are some real gains to be had.

Monet is Open Source, and available at github:


  1. Adobe Community Forums: “What graphics card for Lightroom in July 2017”.
  2. Darktable: “Darktable and OpenCL (updated)”.
  3. Max Smolens: “COMP 238: Advanced Image Generation: Assignment 4: Programmable Shading”.

Pro Tip: Batch-remove the audio from a set of video files

I recently wanted to remove the audio from a bunch of B-Roll I was giving to someone else, because lets face it, I’m always talking about something ultra-nerdy in the background.

In my case they were the MOV files straight out of the camera, and I wanted them put into a directory called `no-audio`. On linux or mac, you can do this:

for i in *.MOV; do ffmpeg -i "$i" -vcodec copy -an "no-audio/${i%.*}.MOV"; done

We make use of the ability to write small shell scripts on one line, as well as bash Parameter Expansion to allow us to form the new name out of the old name.

Thanks to the following Stack Overflow posts:

Guitar exercise generator

One of my hobbies is playing guitar, but unlike when I was a child learning the Cornet, I have never had lessons on guitar, I have learned what I know from books, and the internet, and just fiddling around. Most recently I’ve been putting a lot of effort into scales, but I’ve learned them as patterns on the fret-board instead of being able to play them off sheet music. I think both approaches are necessary (ie, knowing scales from memory, and being able to navigate them without thinking, and being able to read from sheet music). I have found that simply reading along with standard runs up and down scales that I already know doesn’t really help – I find the starting note and my muscle memory takes it from there. So, I’ve written a small program to help.

Continue reading “Guitar exercise generator”

Building Ardour on arch

UPDATE: It turns out if you want LV2 GUIs to show up you need to install the suil package, it is an optional dependency, but well worth it!

NB: I haven’t written this post with the intent of showing every-day users how to build Ardour on Arch, if you just want to use it, go make a donation or subscribe over at, and support the development of a great application.

I switched to Arch Linux recently, because I was having major issues with Ubuntu and Debian. Build Ardour on Ubuntu/Debian is really easy because of the apt-get build-dep command, which (if a package publishes the data) pulls down all the build-time dependencies of a package.

Arch doesn’t have that, but as I found out Arch also packages headers and static libs in with the normal packages (no separate -dev packages to install), so it somewhat evens out.

Step 1 – Dependencies

I used the package list from my post as the basis for this post because MSYS uses pacman, and has similar naming conventions for packages.

So, first step (assuming you’ve got a working Arch Install with Jack set up and working).

> sudo pacman -S glib2 gobject-introspection c-ares \
libidn rtmpdump gnutls libutil-linux gtk-doc \
docbook-xsl intltool libjpeg-turbo jbigkit \
pkg-config ladspa icu boost curl fftw libusb \
libxml2 libogg flac libvorbis libsndfile \
libsamplerate soundtouch pcre cppunit taglib \
gnome-doc-utils gnome-common atk libpng harfbuzz \
cairo fontconfig freetype2 pixman pango jasper \
libtiff gdk-pixbuf2 shared-mime-info gtk2 \
libsigc++ cairomm atkmm pangomm gtkmm liblo \
serd sord sratom lilv aubio portaudio \
jack2 libltc rubberband soundtouch liblrdf cppunit suil

Step 2 – Get the code

Assuming you have already changed to the directory where you want to clone ardour

> git clone git://

Alternatively you could go to their github mirror and fork that, and then clone that to your machine. If you want to submit changes doing them via github PRs is by far the easiest way.

Step 3 – Build

Next change into the ardour directory that was cloned

> cd ardour

My arch system had Python 3 setup as python and the version of waf that Ardour uses doesn’t seem to like python 3, so I had to run it with python2:

> python2 waf configure
> python2 waf

If you are missing any depenencies then you should find out during the waf configure step.

Step 4 – Run

To run the version you just built

> cd gtk2_ardour
> ./ardev

Waf also lets you do install/uninstall/clean etc.


Update on my C++ development environment

A while back I posted this post:

At the time I was using GCC as my compiler, so I was using:
linter-gcc and autocomplete-clang. I found the GCC linter a bit flakey, and to be honest, GCC’s error messages are just the worst. So, I decided to move to Clang as my compiler and have switched to linter-clang. One of the nice things here is I now only need the .clang_complete file to supply options to Clang, which makes life a little simpler. The whole thing seems a great deal more reliable now. I also found out that the directives in the .clang_complete file need to be on separate lines.

Also, at some point the atom-build plugin changed so that I need to put fully-qualified paths into all my build commands, there is a way to put variables in there, so here’s what I have now:

    "cmd": "{PROJECT_PATH}/waf",
    "name": "Build",
    "sh": "true",
    "cwd": "{PROJECT_PATH}",
    "targets": {
        "Clean" : {
            "cmd": "{PROJECT_PATH}/waf clean",
            "sh": "true"

I’ve dispensed with the atom-debugger plugin, because it wasn’t mature enough, plus I feel hardcore using a command-line debugger!

A couple of other plugins I use are:

  • atom-snippets to have a preamble template when I create new files
  • open-terminal-here to make it quick to open a terminal window to do things like interacting with git, and debugging.

Finally, I found a neat theme which is inspired by Google’s Material Design design-language, you need two packages for it:

  • atom-material-ui
  • atom-material-syntax


So, this brings the package list to:

  • atom-snippets
  • autocomplete-clang
  • autocomplete-plus
  • build
  • linter
  • linter-clang
  • open-terminal-here
  • switch-header-source
  • atom-material-ui
  • atom-material-syntax

Exercise Update

At the start of the year I set myself a goal around health and fitness:

  • Run a total of 1200km this year
  • Get my resting heart rate below 55 and keep it there
  • Get my weight below 80kg and keep it there

I started the year off with gusto, I was ahead of my weekly distance requirement, my heart-rate was dropping, and I was beginning to see some weight-loss. Then my knees started hurting.

It turns out I had missed some important muscles while stretching. Since then I’ve had to take a lot of time off running, and sometimes off all exercise. I’m now way behind my distance schedule, my heart rate has gone back up, as has my weight. I’m not giving up, but I’ve certainly had to stop and take a new approach. The lesson? Start easy, build up, you’ll meet your goals eventually. Don’t focus too much on pushing hard when you start off, get a base strength and fitness first.

Here’s a chart of my running to date:



Using Let’s Encrypt to Generate an SSL Certificate for Azure, on Mac

Today I needed to set up an SSL certificate for our company website. Being frugal, and interested, I decided to try Let’s Encrypt (a new organisation which aims to make SSL free, so that nobody has any excuses).

First step, get the client by cloning the git repository:

git clone

Now, before you start: if you’re on a mac, you should make sure you have homebrew installed and working. Once that is done, install python through it (because the homebrew version comes with pip the python package manager):

brew install python

Then when that is installed, install virtualenv:

pip install virtualenv

Right, that’s all the dependencies for the letsencrypt client. Let’s move on to prepping your azure website. I assume that you have a running website under a custom domain already. The internet has plenty of documentation on how to get there. Let’s Encrypt uses a protocol called ACME to verify that you own the domain that you are trying to get a certificate for. It does this by asking you to serve a particular piece of content, unique to your request for a certificate. Getting Azure to do this is actually fairly simple, if you have the right documentation collated together, so here it is:

  1. Through your FTP access to your website create a directory called well-known as a sibling of the wwwroot directory, and within this new directory, create a directory called acme-challenge
  2. In the Application settings blade of your website, in the new Azure portal, right down the bottom of the blade, add an entry under Virtual applications and directories with the following values
    • Virtual Directory: /.well-known
    • Physical path rel: site\well-known
    • Leave the ‘application’ box unticked
  3. Next create a web.config file with the following contents:
    <?xml version="1.0"?>
                    <mimeMap fileExtension="." mimeType="text/plain" />
                    <mimeMap fileExtension=".txt" mimeType="text/plain" />
  4. Place the web config file into both the well-known, and acme-challenge directories.
  5. Create a .txt file with something in it, and place it in the acme-challenge directory, then try to access it. If you get the content of your text file showing up in your browser, you’re good.

Ok, now you’re good to go, so change to the directory where you cloned the letsencrypt repository

cd letsencrypt

The command is quite simple:

./letsencrypt-auto certonly -a manual --rsa-key-size 4096 -d --debug

If you’re using a Mac and you leave off the –debug flag, you’ll just get told to put it on. At this point you’ll go through a few screens with some prompts etc, and then you’ll get to this:

Make sure your web server displays the following content at before continuing:


If you don't have HTTP server configured, you can run the following
command on the target server (as root):

mkdir -p /tmp/letsencrypt/public_html/.well-known/acme-challenge

cd /tmp/letsencrypt/public_html

printf "%s" KkRlmp5Y7Vtitd37tyZ3M7rtwrZ7b9KyoFf4-UdGMBI.0XPKTuHHwF3vk2VV7czrfw72_Lnq8oEZPzXVPbkbk_E > .well-known/acme-challenge/KkRlmp5Y7Vtitd37tyZ3M7rtwrZ7b9KyoFf4-UdGMBI

# run only once per server:

$(command -v python2 || command -v python2.7 || command -v python2.6) -c \
"import BaseHTTPServer, SimpleHTTPServer; \
s = BaseHTTPServer.HTTPServer(('', 80), SimpleHTTPServer.SimpleHTTPRequestHandler); \

Press ENTER to continue

I wouldn’t worry about the lower bit, telling you how to run a very simple python server, but the upper half is quite important, particularly this bit:

printf "%s" KkRlmp5Y7Vtitd37tyZ3M7rtwrZ7b9KyoFf4-UdGMBI.0XPKTuHHwF3vk2VV7czrfw72_Lnq8oEZPzXVPbkbk_E > .well-known/acme-challenge/KkRlmp5Y7Vtitd37tyZ3M7rtwrZ7b9KyoFf4-UdGMBI

If you try to copy-paste the content into a file, you’ll probably get a newline in the file which will cause your attempt to fail. I didn’t read instructions properly the first three times, and I failed each attempt, until I looked more closely and realised they were telling me how to make the file I wanted in the first place!

Once you have a file generated as above, copy it into the acme-challenge directory you created via ftp. Now double check that it downloads correctly, but visiting the url they give you. If you get what they’re telling you they expect to see, then you’re good to go: press ENTER.

If you’re successful, you’ll get something like this:

    - Congratulations! Your certificate and chain have been saved
      at /etc/letsencrypt/live/ Your cert will
      expire on 2016-04-17. To obtain a new version of the certificate in
      the future, simply run Let's Encrypt again.
    - If you like Let's Encrypt, please consider supporting our work by:
        Donating to ISRG / Let's Encrypt:
        Donating to EFF:          

Now, that’s a really sucky place for them to put the certificates. I went in through finder and gave myself permissions to those directories. While you’re doing the ‘live’ directory, also do the ‘archive’ directory, as that is where the files really live – they’re just symlinked into live. I copied the archive folder elsewhere to do the next step, but you could do it in-place as well.

Ok, great so now you have a bunch of PEM files, but azure wants a PFX. That’s ok though because we can manage that with openssl. You might need to install this via homebrew, but try it first:

openssl pkcs12 -export -out /path/to/output/file.pfx -inkey /path/to/archive/privkey1.pem -in /path/to/archive/fullchain1.pem

Put in a password, as this file has a private key in it, so you don’t really want it lying around unprotected. I’ve subsequently deleted the extra copies I made, and I’ve removed my permissions from those directories as well. That way someone has to get into my computer, and log in as root in order to even see the files.

Ok, nearly there. Now you can upload the certificate to Azure, and once it is uploaded you can set up a binding to it. There are already plenty of docs out there about those two steps.




Don’t forget the cross-fade

A quick post about a little lesson I learned tonight whilst editing some audio: don’t forget the cross-fade.

I recorded some synth drum tracks to audio (so that I wouldn’t have to open up the drum machine again), and then trimmed the tracks down so that they ended tidily at the end of the bar: BAD! When I later when to loop those tracks I realised that I hadn’t left room for the quick fade from one clip to another that you generally need when putting clips right next to eachother. What I needed to do was leave a tiny bit extra on the end so that the clips could overlap without messing up the timing.

Luckily my trimming wasn’t destructive, so I could extend the clips back out.

Here’s a screenshot of what I mean.

Ardour Clip XFade

More valgrind goodies

Today I encountered the following error out of valgrind:

Syscall param recvmsg(msg.msg_iov[0]) points to unaddressable byte(s)

I’ve had some variant of this a couple of times now. The first time it was a pseudo-false-positive: in resorting to culling code until the error went away I found it was replaced my a memory leak. After fixing the leak and putting all the culled code back, the board was green.

Today I had it in another situation. Take the following classes:

class A {
     A(shared_ptr<B> b) : b(b) {}
class B {
     virtual void foo() {}
class C : public B {
     virtual void foo() {}

The substantial change I made was to introduce an extra parameter into the signature of B::foo:

class B {
     virtual void foo(int arg) {}

But I forgot to update it in C. The error was supposedly happening when A was constructed, which made little sens because there were no changes to A that would affect the layout of an A instance. So, I tried progressively reverting changes until the function signature was the only change left, and then it dawned on me that the error was in a unit test so it might be something with the mock (in this case C). I updated the signatures in the mock, and the error went away. At this point I delve into hearsay: I can only conclude that the uninitialized bytes were to do with the v-table, and because I had a weird situation with effectively an overload on a subclass that didn’t exist on the superclass the v-table must have been a bit odd-looking to valgrind.

Anyhow, the point of this post is not to get into the nitty gritty, because I just don’t have that level of compiler-foo, rather to note that this particular valgrind error is often quite cryptic, and that one of the possible causes is subclasses not properly extending their parents.

The value of valgrind

This weekend I got quite a lot done on liboca, mainly working on the network stack, and the basic implementation of OCP.1, which is the TCP/IP-based protocol for OCA (There is room in the OCA standard for other transports). Anyhow, at one point, my unit tests broke in a suite of tests that tested code that should not have been affected by the code I was working on. In the end the fact that I had set up Valgrind, ended up showing me how to solve my problem, so I learned that hard way that it is an essential tool to run along with your standard test suite.

What is Valgrind?

"Valgrind is an instrumentation framework 
for building dynamic analysis tools. There 
are Valgrind tools that can automatically 
detect many memory management and threading 
bugs, and profile your programs in detail. You 
can also use Valgrind to build new tools. " 

In its simplest form, it takes your code, instruments it and runs it on a processor emulator, and tells you if you’ve leaked memory, accessed memory you shouldn’t have, and a whole bunch of other stuff – I encourage you to check out the website for more detail.

So what happened?

So we already got to the fact that I had a theoretically non-impacted test stubbornly crashing with:

Segmentation fault (Core Dumped)

Frustratingly, I couldn’t reproduce the crash in a debugger, which left me thinking it was a race condition for a while, but all the tricks I tried to hunt down the race didn’t give me any inclination as to what was going on. Also, the core was not being dumped, by default Ubuntu doesn’t do that. You need to enable core dumps with:

$ ulimit -c unlimited

Anyhow, I then debugged the core dump:

$ gdb build/tests/all core

Finding that the Seg-fault was inside Google Test! Googling for this issue showed no known issues around this part of the code, so I was a little stumped.

So what next?

Looking further up the stack trace showed that it happened inside _malloc_consolidate which previous investigations have lead me to attribute to memory corruption, most often heap trashing. Anyhow, I have a bunch of verification scripts set up to automatically run the unit tests, static analysis and dynamic analysis (valgrind!), and was indeed running them. Lo and behold, valgrind (memcheck) was turning up 47 issues, all of which were relating to a few lines of code (that I had been working on) where I was deleting an object which was still needed by an asynchronous process. I fixed the errors rather easily, and hey presto! No more Segmentation Fault.

You see the problem was not even the unit test in question, but about 10 tests earlier, the heap was getting trashed, which was causing the test runner to seg-fault later on.

So what did I learn?

In the past I’ve had policies on repositories that no code makes it in without a successful build and test-run. Usually I compile with -Werror (or similar) so that my code never has any warnings (because I have to fix them, since they’re treated as errors). This time I added cppcheck and valgrind because I figured they would be useful. This experience taught me that if I ever encounter weird issues while I’m working, I should run the full verification suite because I will probably find something related to the weird issues that way.