Jpeg (.jpg) File Format - Simple Encode
and Decoder
by bkenwright@xbdev.net
Using the basic understanding of the jpg format, you can decode and encode
jpg's to and from other formats, or to its raw rgb (red,green,blue) pixel
formats.....so I did a couple of programs which does just that, that you can
plug into your other various demos. LoadJpg.h/.cpp and SaveJpg.h/.cpp
which both contain the whole source code for encoding a jpg and decoding one.
The source can either take a .jpg file, and convert it to a *.bmp and save it
out. Or you can pass in the raw bytes from memory and that will return the
rgb pixel values in an array and the width and height of the image. The
codes not optimised and only works with the simple jpg format, non of this
progress stuff etc.....but it does its job in most cases.
The save source is independent of the load jpg code, and basically takes a
*.bmp and creates *.jpg and saves it out :) Sweet and simple...
The source code was compiled with visual studio, and both load and save the
smiley face which I use as my test example...codes still got a few quirks in,
but its pretty straight forwards :) Of course the code still needs more
work and I'm sure I'll be doing lots of tweeks and improvements to it over time,
to make it easier to read and less bug free.....but its like that with all
code....gets better the more you work on it :)
Download Source Code (80kb) |
|
Jpg file format is great for compressing real
images, you can go from a few hundred kilobytes to a few tens of bytes...its
that good. Code is compiled with visual studio, as is
self contained as possibly, only using the standard librarys to read the
file from the disk, using fopen/fread etc...the rest is all in the .cpp/.h The format isn't the easiest to get along with, and theres
dozens of flavours, but once you get how the basic version works the rest
sort of fall into place.
|
I'm sure you'll be using the demo code for various mini projects, great for
loading/create jpgs, and with a bit more work you could create loaders for tgs,
gifs and you'd have a whole library of image importers/decoders :)
|