- I think the most intimidating thing
about this challenge, for me, was to read a JPEG file.
I know that JPEG's an image file format
and that it's data is compressed.
You just can't examine raw bytes in a JPEG file.
The file must be properly handled.
I was in need of a JPEG library
to augment the C language's abilities.
After exhaustively searching the internet
for nearly three full minutes,
I determined that the libjpeg library was what I needed.
You can obtain a copy of the library from sourceforge.net.
I also checked out the libjpeg turbo version,
which I probably didn't need, but I used anyway.
You can check it out at libjpegturbo.org.
If you're using cygwin on a PC,
then use the cyg package manager to install libjpeg.
I began this challenge not by writing the file code,
but just to ensure that I could successfully
read a JPEG file into memory.
Examples of such code are provided on the internet,
such as this one by Windower on GitHub.
My first effort was a program I called duplicate.
Its job was to read in a JPEG file
and write that file back out at the same color levels,
pixel depth, and size.
It took me awhile, but eventually
I figured out how it worked.
Documentation on reading JPEG files is abundant,
but info on writing them is scarce,
and only by writing the file could I assure myself
that the data was read properly.
I had the most difficult time with setting
the image's width and height.
Libjpeg uses a structure for holding image data
and several members deal with width and height.
Finding the proper ones took time.
The duplicate program reads in a specific file,
liberty.jpg, which is included in the exercise files.
Upon success, a second file is created,
liberty-duplicate.jpg.
Both files look the same, which is the success I was after.
Next, I modified the code to see
whether or not I could reproduce the image in grayscale.
That's because it would be faster
to compare image data with the color information removed.
I modified my duplicate code to create
a program called grayscale.
It took less time to work out the kinks
because I grew to understand the whole JPEG reading thing.
I also customized a lot of the code.
Upon success, running the grayscale program
takes the same file, liberty.jpg,
and produces a monochrome duplicate, libertygrayscale.jpg.
Examining both files shows similar images,
but with a grayscale version, occupying less storage space.
My final JPEG reading and writing experiment
was to reproduce a JPEG image, not only in grayscale,
but at a fraction of its original size.
I called this code shrink.
The shrink code reads in a JPEG file,
removes color information,
then replicates the image at 1/4 scale.
By reducing the image, I would be able to scan
and compare two image files much more rapidly
than working with a full size, full color image.
Of course, with a reduction comes data loss,
but I could deal with that in the code,
which I'll demonstrate in a later movie.
The shrink program works on the same liberty.jpg file.
The result is a new image file, libertyshrink.jpg.
Examining the file shows that it's smaller in size
than the original, both visually
as well as occupying less storage.
Upon successfully completing my JPEG
reading and writing tests, I was able to start
coding a program to compare image files.