CHRISDL@PAGESZ.NET 9/18/97
Note : GIF support has been removed as of 9/24/97. I am
pursuing a license from Unisys to enable me to include GIF support
in this package. Until then, no GIF. Sorry. -cdl
My JpegLib2 library and JpegFile class
I've been looking for a long time for a JPEG package that will allow me
to do this :
BYTE * RGBbuffer = ReadJPEGFile(filename,&width,&height);
and this:
SaveJPG(RGBbuf, width, height...);
I never found one that didn't cost many hundreds of dollars, so I wrote
my own.
What is it?
My class is called JpegFile.cpp. It is based on a Win32 compilation of
the IJG V.6a JPEG source. It does two main things :
it reads a JPEG file into an RGB buffer,
it saves an RGB buffer as an RGB or grayscale JPEG file.
There are some utility routines in it to do things like
RGB -> BGR
vertical flip
DWORD-align
un DWORD-align
convert to grayscale
Color quantize a 24-bit image to an 8-bit image
It can also read
1, 4, 8 and 24 bit BMP files
It can also write
1, 4, 8 and 24-bit BMP files
There are no provisions for progressive reading, progress bars, partial
reads/saves, extended error handling, etc.. I use a very small portion
of what the IJG code is capable of doing. I tried to keep the IJG
source as intact as possible, to ease debugging - I assume the IJG code
works.perfectly and that anything going wrong is my fault. You should
assume this is true, also.
This code has worked in every application I've tried it in. This has
only run (to my knowledge) on Win 95, VC++ 4.x and VC++5.0 . I have
heard, however, that one person successfully modified it to work on a
Borland compiler to create a DLL for use in Visual BASIC - I don't have
his code.
Color quantization code is based on Dennis Lee's DL1Quant.
If you don't like MFC, or would prefer to have your JPG and BMP code all
wrapped up in a DLL, with dozens of handy image processing utilities (
resize, zoom, rotate, matrix, LUT, crop, draw, etc.) then check out my
ImgDLL package. http://www.pagesz.net/software/imgdll.htm
Non-MFC support
I do offer a Win32 DLL that does everything this package does, for
those of you who don't have access to VC++ or would rather not mess
with VC++ (all you Borland or Visual BASIC types, for instance). Check
out my ImgDLL page for more info.
Terms of use
1: I've decided to make this class, as well as my compilation of the
IJG library available to the public. This is entirely free - no
royalties, no time bombs, no annoyance screens. All I ask is that you
mention me in your software, especially if it's commercial software -
and, if possible, add a link to my page . I just want some
acknowledgment... :)
2: Because it is free, however, there are no guarantees - use at your
own risk! If you want software that comes with someone you can point
your finger at if it fails, spend the money on a commercial package
from a company with a legal department. I'm just sharing.
3: The main reason for this package is to provide simple C++ JPG
support. The BMP functions are in there only so that I won't
have to answer the question "I got your JPG stuff, do you do BMP ?"
4: Come to me first for help with this code.
Troubleshooting
This package is not the responsibility of the people who's code I have
used here - the IJG people, for example. Let me rephrase that. Come to
me first with any problems you are having with this package.
If you run into any problems with this software, please feel free to
contact me. If it turns out that the problem is in my code, I will fix
it. Please do not ask the IJG people, or any of the other people who's
code I have included here for help unless we've first determined that
the problem is not in my code.
If you feel the problem is with the IJG code that I have included, I
recommend you get a full copy of the official IJG source. The full IJG
source comes with excellent documentation and should be able to help
you. You can get the IJG code at the simtel archives.
MSVC 5.0 Dialog App Problem + BOOL redefinition problem
Problem : There is some serious type conflict associated with the IJG
code's use of the name "boolean" and MSVC 5.0's use of the name
"boolean". In MSVC 5.0, "boolean" is a different size than it is in the
IJG code (one is a char and one is an int). You typically run into this
if you start using OLE stuff, which Dialog Apps come with by default.
You will see lots of "struct mismatch" errors and your code won't run.
It doesn't seem to happen in MSVC 4.x dialog apps. This is new.
Fix method #1 - Specific to MSVC 5.0 Dialog Apps
1. Compile the Jpeglib2.lib as-is.
2. In your app, stdafx.h, remove the line:
#include <afxdisp.h> // MFC OLE automation classes
3. In your main app class InitInstance() remove the line :
AfxEnableControlContainer();
4. In jpegfile.cpp, be sure not to :
#define HAVE_BOOLEAN
This might break a whole bunch of OLE things. But, if you're not using
OLE, I think this is safe.
Fix method #2 - General fix.
1. In the Jpeglib2.lib files, change jmorecfg.h :
from (line 228):
#ifndef HAVE_BOOLEAN
typedef int boolean;
#endif
to:
#ifndef HAVE_BOOLEAN
typedef char boolean;
#endif
2. Compile Jpeglib2.lib as usual - ignore the warnings...
3. In your app, in jpegfile.cpp, add
#define HAVE_BOOLEAN
before
#include "jpeglib.h"
This method allows you to use OLE. But makes a change in the IJG code.
This is something I've tried to avoid doing. All of my tests have
worked, though I'm unsure of what effect this change could have.
I include a file dlgvc5.zip, which contains a tiny sample MSVC 5.0
dialog app example. It simply loads and displays JPEG files. I used the
steps outlined in method #1 above in creating it.
Thanks to Bryce Burrows, Brian Krisler and Alexandre Maltais for taking
the time to point out the problem to me.
The Package
JPEGLIB2
This is the VC++ 4.x project (JPEGLIB2.MDP) for my compilation of the
IJG source for Win95. Most of the IJG source is included here -
everything but the example programs and the documentation. This is what
does the actual work. It is absolutely necessary that this library is
compiled and linked to the application using the JpegFile class.
Fortunately, it should be a matter of unZipping the project into it own
directory and aiming VC++ 4.x at it.
It is possible to use this library (or the source for the library) on
its own, if you know the IJG codebase. I really don't. I know that it
works for my JpegFile class, though.
I have changed only two lines of the original source. I commented out
the exit(..) in jerror.c and replaced the fprintf(stderr,..) with a
MessageBox(...). That's right, it launches a message box on warnings.
It's certainly possible to remove this line if you absolutely can not
have message boxes popping up. It is also possible for you to write
your own error handler.
The biggest benefit of using my JPEG package is that I've managed to
get the IJG codebase into a VC++ 4.x (Win32) project. The other part of
my package is really a thin wrapper.
MFCAPP
This is a small MFC C++ sample application using a class called
JpegFile. JpegFile is a wrapper around the JPEGLIB2 library. If you
know what you're doing, you can certainly ignore my class. If you want
simple functionality for reading and writing JPEG files, JpegFile does
it very well. If nothing else, this class is a gr