Archive for October, 2008

GIMP: Removing Noise from Digital Photos

Wednesday, October 29th, 2008

I shot some low light photography at the youth group Halloween dance using a Casio Exilim point-n-shoot, and some of the photos had digital noise, or grain (as we called it in the days of film).

I searched the internet for some assistance in removing the digital noise using the GNU Image Manipulation Program (GIMP), and stumbled upon a well written post about how to remove digital noise.  The technique makes use of the GREYCstoration plugin for the GIMP.

I use the GIMP on Kubuntu Linux, and wondered if there was already a package created for GREYCstoration.

$ apt-cache search greyc
gimp-plugin-registry - A repository of optional extensions for The GIMP

I found it!  So I installed it:

$ sudo apt-get install gimp-plugin-registry

After restarting the GIMP, here are the results:

Low light photo (scaled)

Low light photo (scaled)

Low light photo (GREYCstoration, scaled)

Low light photo (GREYCstoration, scaled)

Interrupt Safe Ring Buffer Library

Saturday, October 25th, 2008

Christoper looks through a circular tube

I created a ring buffer library for embedded systems in C some years ago and have been using it when I need to add a simple queue to my software.  I knew it worked correctly since I created unit tests to validate the code.  However, I noticed that it wasn’t interrupt safe since one of the consumer variables is shared by the producer (the count).  This library requires disabling interrupts when consuming.  Not what I wanted.

There are a number of difficulties when designing ring buffers, mostly dealing with efficiencies of code or data, or the struggle between consumers and producers.  My generic ringbuf library had used a single fill count, but the count had to be updated by the interrupt service routine (producer) and by the routine that retrieved the data (consumer).  I tried to use read and write offset indices, but that required that I always keep an element open since the you can’t tell if the ring is empty or full when the head and tail are the same.

Absolute indices appeared to be interrupt safe, although they require that the buffer length be a power of two.  That seemed to be flexible enough for most embedded work.  I modified and tested my ring buffer library using that method, but my ringbuf, which allowed for a fixed length element,  seemed to be overkill for what I was using it for – gathering bytes from a serial interrupt service routine.  Its API also had a slight flaw such that if the consumer retrieved the data (via Pop rather than just viewing the first element), the producer was free to put new data into the buffer, overwriting the data just retrieved.

I had started my ring buffer library as a simple character (one byte) FIFO – first in, first out.  So, I revisited that code, and modified it to use absolute indices.  A simple byte ring buffer, first in, first out.  Now I had interrupt safe code that would work in my serial interrupt service routine.

Why go to the trouble to make a library module to handle a ring buffer when slapping the following quick and dirty ring buffer code (from Stefan) into the serial module would have worked as well?

   #define N 128
   volatile unsigned int head, tail;
   volatile char buffer[N];
   unsigned int inuse() { return head - tail; }
   void put(char c) { if (inuse() != N) { buffer[head++%N] = c; } }
   void get(char* c) { if (inuse() != 0) { *c = buffer[tail++%N]; } }

Because I needed to know that it worked correctly, and I like reusable library modules.  Especially high quality reusable modules.  Especially high quality reusable modules that include unit tests to validate functionality.  Unit tests that also serve as guides for implementers.

The new module files are ringbuf.c and ringbuf.h for the ring buffer with fixed sized elements, and fifo.c and fifo.h for the ring buffer with byte sized elements. All my versions of the ring buffer and the unit testing framework can be found in the ringbuf.zip file.