- As with other projects that require
more abilities than those offered
by the standard C library,
my first approach to solve this problem
was to look for an EXIF C language library.
The metadata extractor available from GitHub
looked like the perfect solution.
Despite the fact that the link said
it was a C language library,
the library was in Java.
That happens often.
Next I tried looking into the libjpeg library,
which I used to solve problem two.
While libjpeg does read the JPEG file header,
it doesn't extract any EXIF data
beyond the basic image information,
bytes per pixel, width, and height.
Finally, I stumbled upon the libexif library at SourceForge.
It most definitely did the trick.
I ran a few tests to determine how well
it extracted EXIF data.
It worked beautifully.
The problem was that it worked beautifully
only with EXIF version 1.2.
For EXIF version 1.1 files, no data would be read.
This was frustrating because a majority
of the sample images are in EXIF 1.1 format.
At this point, I created file dumps of the images
to study the EXIF and JPEG header format
to see whether I could manually
pluck out the image description or caption text.
The good news is that the header information
in a JPEG file isn't compressed.
It can be read by using a utility like hexdump,
which is what I used.
The bad news is that the format
isn't the most obvious thing to discern.
Despite that, I wrote a utility called JSE
for JPEG string extractor.
The code is included in the sample files
if you want to take a peek.
This code scans the first thousand
or so bytes of a file
and returns any character strings
four bytes or longer.
It's basically a persnickety file dumper.
When run on a typical JPEG file,
you see output like this.
A lot of the strings are just random,
but a few of them are actual EXIF text,
such as line 36, which is the image's caption.
At the worst-case scenario,
I could prompt the user to pluck out
the proper caption string for a JPEG file.
But truly, that is the worst-case scenario,
and I knew I could do better.
Enter the EXIF tool.
While I knew about it, and it's a pretty nifty tool,
I was reluctant to have my code call another program
to complete its basic task.
Weighing the options between using ExifTool
and requiring the user to do the caption hunting work,
I chose the former.
When run at the command prompt,
the ExifTool cops up copious amounts
of EXIF image data.
I'll scroll back here, and you can see
that it's all plain text and very neatly formatted.
See the colon?
It consistently appears at column 33.
I'll use that tidbit later.
ExifTool can also extract specific metadata
by specifying a switch.
To get the caption, you specify caption abstract.
And if that comes up empty,
you can use the description switch.
In either case, the output is a single line
or nothing when the text isn't available.
My goal was to run ExifTool from within my code
and extract the caption or title text.
In the next movie,
I'll explain how I managed to pull off that feat.