Perl saved my vacation!

Posted by Tom Moertel Sat, 25 Apr 2009 14:28:00 GMT

My wife and I are on vacation. We spent yesterday at Longwood Gardens in eastern Pennsylvania. It’s beautiful: words don’t do it justice – you really do need to see the photos. That’s why I took about 500 photos yesterday. At least, that’s what I thought until I got back to our bed & breakfast and tried to download the photos from my camera to my laptop.

Crap! About a quarter of the photos were missing! I wasn’t sure what had happened, but I suspect that my budget 16-GB SD card had started throwing bad blocks. There go my priceless vacation memories, right down the technological toilet.

But maybe those photos weren’t irretrievably flushed. Maybe the data behind most of them was still on the SD card, if only I could get at it. Hey, I’ve got Perl on my laptop: I can get at that data.

Here’s how it went down.

First, I scanned the raw blocks of the suspected-faulty SD card, looking for the markers that indicate the start of a JPEG file. My laptop runs Linux, so it was easy to access the blocks. I just read the device file corresponding to the SD card’s filesystem. I used a Perl script to walk through the file in block-sized steps, hunting JPEG headers. Here’s the meat of the script:

while (my $bytes_read = sysread($fh, $buffer, $READ_BLOCKS*$BLOCK_SIZE)) {
    for (my $offset = 0; $offset < $bytes_read; $offset += $BLOCK_SIZE) {
        my $tag = substr($buffer, $offset + 6, 4);
        if (grep $tag eq $_, qw(JFIF Exif)) {
            print $pos + $offset/$BLOCK_SIZE, "\n";  # emit "interesting" block
        }
    }
    $pos += $bytes_read/$BLOCK_SIZE;
}

I handled the second half of the rescue mission with another Perl script. This script grabbed the data starting at each interesting block, as determined by the first script, and tried to decode the data losslessly as a JPEG file, writing the result to a new file on my laptop’s hard drive. Here are the tasty bits of that script:

$SIG{PIPE} = 'IGNORE';  # pipe will break on damaged images

while (my $target = <>) {

    # seek forward until we hit the desired block
    while ($pos != $target) {
        my $diff = $target - $pos;
        $diff = $MAX_SEEK_BLOCKS if $diff > $MAX_SEEK_BLOCKS;
        sysseek($fh, $diff * $BLOCK_SIZE, 1);
        $pos += $diff;
    }

    # read the data starting at that block, attempting to decode as JPEG
    if (my $bytes_read = sysread($fh, $buffer, $BLOCK_SIZE*$DATA_READ_SIZE)) {
        my $outfile = sprintf("%s/%010d.jpg", $outdir, $target);
        open(my $pipe, "|jpegtran -copy all -outfile $outfile")
            or die "can't open pipe: $!";
        print $pipe $buffer;
        close($pipe);
        $pos += $DATA_READ_SIZE;
    }
}

With these two scripts, I was able to retrieve almost all of the lost photos. That’s 120 more priceless memories that I can post to Flickr to annoy my friends. Yay, Perl!

(BTW, if you want to see some of the rescued photos, see my Longwood Gardens, Spring 2009 photo set on Flickr.)

Posted in
Tags , ,
14 comments
no trackbacks
Reddit Delicious

My photo of British soldier lichen is now published!

Posted by Tom Moertel Tue, 14 Aug 2007 05:05:00 GMT

I wrote in an earlier post that one of my photos of British soldier lichen was going to be published in an upcoming issue of a popular Pennsylvania magazine. Now that the issue is out, I can reveal that the periodical is none other than Milford Magazine. My photo appears in the August 2007 issue, on top of the second page of the lichen feature, “The Secret Lives of Lichen” (PDF).

Do check it out. This may be your only opportunity to read an article that celebrates lichen in interview form.

I kid you not.

Posted in
Tags , ,
no comments
no trackbacks
Reddit Delicious

Hailstorm!

Posted by Tom Moertel Sun, 15 Jul 2007 04:40:00 GMT

Yesterday, in the middle of a beautiful, sunny afternoon, a storm front came out of nowhere and cut across southern Pittsburgh. I was in my backyard at the time, and I could tell by the sudden icy wind that something unusual was happening. A few moments later I heard a sharp thwack! as something struck my deck and bounced into the yard – a nickel-sized hailstone. Then, another one. Thwack!

Hailstorm!

Then the hail fell like rain – thwack! thwack! thwack! – faster and faster, until the air was filled with icy missiles, some bigger than quarters, streaking to the earth around me. As the deluge intensified, I was deafened by the sound of a million berserk carpenters hammering away at my home, my deck, and my garden.

In a few short minutes it was over. The ground was littered with ice, leaves, and small branches. My home’s window screens had holes punched through them. My garden was torn to shreds.

If you haven’t experienced the combined effects of hailstones and high winds, count yourself lucky.

Photos

If you wonder what the storm and its aftermath looked like, I put a set of hailstorm photos on flickr. My neighbor and fellow programmer Casey West had his new iPhone handy and snapped some great photos, too.

I also captured the storm on my front-yard webcam, which looked into the oncoming hail. For reference, here is what the webcam saw just before the storm:

Front-yard webcam 2007-07-13 15:34:21

If you look in the upper-right corner of that photo, you can see the first few hailstones streaking into the frame.

Then, the storm hit in force:

Front-yard webcam 2007-07-13 15:37:26
Front-yard webcam 2007-07-13 15:37:47
Front-yard webcam 2007-07-13 15:37:51
Front-yard webcam 2007-07-13 15:37:57

The wind was so strong that it drove the hail horizontally at times.

When the storm ended, just 3 and a half minutes later, the ground was littered with ice. In the photo below, that’s not snow in the driveway:

Front-yard webcam 2007-07-13 15:38:08

In the garden, it was easier to see just how much ice the storm had dropped upon us:

Hailstones litter the garden

The garden, itself, was shredded:

Hosta shredded by hail

Amazing.

Posted in
Tags , , , , ,
2 comments
no trackbacks
Reddit Delicious

I'm going to be a published photographer!

Posted by Tom Moertel Thu, 31 May 2007 04:01:00 GMT

Earlier today I received an email from the editor of a Pennsylvania-based magazine. (I won’t mention the name of the magazine in case what I’m about to write next amounts to a spoiler.) He asked if I would allow the magazine to publish one of my photographs of British soldier lichen in an upcoming issue.

Of course, I said yes. (I’m always looking for ways to spread the word about British soldier lichen.)

My fee? I asked for a free issue of the magazine when it goes to press. They said it will be in my mailbox.

Cool.

Posted in
Tags , , ,
no comments
no trackbacks
Reddit Delicious