Chrisdl@pagesz.net 

JpegLib2 library and JpegFile class

new version 9/17/97

(+ BMP + GIF)

 
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
     GIF 87a and 89a files

It can also write

     1, 4, 8 and 24-bit BMP files
     GIF 87a 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 code that is used in Edward 
McCreary's DIBQUANT.DLL. I am including my version of his code compiled 
to a Win32 DLL which I have called RGBQUANT.DLL. Perhaps at some later 
time I will include the source for this as well. 

The use of RGBQUANT.DLL is not necessary when using the JPG routines; 
it is only used in the sample app as a way to save GIF and 8-bit BMP 
files.



                                      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 and GIF functions are in there only so that I won't 
have to answer the question "I got your JPG stuff, do you do BMP / 
GIF?"

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. 

If you run into any problems with this software, please feel free to 
contact me. If it turns out that the problem is in the 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 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 ftp.uu.net . 



  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 Win32. Most of the IJG source is included here - 
everything but the example programs. 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 great starting place 
for those who wish to figure out more of the IJG codebase. I use it for 
all of my JPEG needs. 

The application demonstrates the following functions in C++ :
	
Read a BMP (1, 4, 8 or 24-bit) or GIF or JPG file, display the result.
Save a BMP (1, 4, 8 or 24 bit) or GIF (256 color) or JPG (RGB or 
grayscale) file.
Examine the dimensions of a JPG or GIF file.

It should be very simple to compile and run this app, as well. If you 
want to use the JpegFile class in another app, you need to :

Add JpegFile.cpp to your project.
#include "JpegFile.h" 
Link with JPEGLIB2.LIB you built as descibed above.
Tell the linker using Build/Settings/Link/Input to ignore the LIBC 
and/or LIBCD files. There are some conflicts with this library. I've 
never been able to figure out what they are. But, I've never found 
ignoring these libraries to be a problem. If someone knows what's up 
here, tell me.

Using Tools/Options/Directories/Include Files, add a path entry to the 
location of the JPEGLIB2 source files. There are many .H files here 
that need to be included by way of JpegFile.cpp.

The BMP code is fairly simple to extract to different apps. I do it all 
the time. 

The GIF code is completely independent of the MFCAPP app. It should be 
no problem to use this code in other apps. It is C++ now, but is really 
C that was forced into C++ for the sake of this app - it should be no 
problem to turn it into plain Windows C. 

Likewise, the BMP code should be easy to move from this app to any 
other.

    RGBQUANT.DLL

This is a Win32 DLL that performs color quantization on 24-bit RGB 
buffers to form 8-bit colormapped buffers. I use it in the GIF and 8-
bit BMP sections only. It is not necessary that you use this DLL for 
the JPG code. 

This DLL is based on Edward McCreary's DIBQUANT.DLL. I modified his 
code to better fit what I am doing here. The source for this is not yet 
provided.

You must link MFCAPP with RGBQUANT.LIB. Specifiy this in Build | 
Settings | Link Input.

    DLGVC5

This is a small sample MSVC 5.0 dialog app. It shows how to fix the 
struct size mismatch (boolean size) problem. 





  Disclaimer Disclaimer Disclaimer Disclaimer Disclaimer 

I make no guarantees about this code's suitablilty for use. I find it 
very useful for my applications, but cannot anticipate all places this 
code could wind up. If you have problems with this software, you should 
ask me about it.If there is a problem with anything I wrote, I will 
certainly try to help solve it. If it is a problem with the IJG code, I 
will try my best.

Since I have left the IJG source essentially intact (but for two lines 
in jerror.c) you should be able to find all the answers you need in the 
full IJG distribution, which can be found at ftp.uu.net. You are 
strongly encouraged to look in the original IJG source for any 
questions you might have. There is excellent documentation included in 
the IJG package, which will probably answer all of your JPG questions. 


