JpegFile - free JPG (and BMP) reading and writing code.

For a complete image processing toolkit (with hundreds of image processing functions), see ImgSource.

Looking for a JPEG package that will allow you to do this :
    BYTE * RGBbuffer = ReadJPEGFile(filename,&width,&height);
and this:
    SaveJPG(RGBbuf, width, height...);
? ? ?
Don't want to pay 100's of dollars? 
Comfortable with C/C++ ? 
Look no further!

What Is it?

The class is called JpegFile. 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

  • Also included, is code to read :

  • 1, 4, 8 and 24 bit BMP files
And to 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.. It uses a small portion of what the IJG code is capable of doing.

IJG has released version 6b of their JPG read/write code. This class will work correctly with 6b. If you want to use 6b, you will need to make the same changes that I made for 6a.

This code was written with Visual C++ v5 and v6. We don't have code for Borland, VB or any other platform, sorry.

Color quantization code is based on Dennis Lee's DL1Quant.

Terms of Use

1: This code entirely free - no royalties, no time bombs, no annoyance screens. All we ask is that you mention me in your software, especially if it's commercial software - and, if possible, add a link to our site.

2: Because it is free, however, there are no guarantees - use at your own risk!

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 ?" The BMP support is not complete, it doesn't support RLE, or some of the more exotic bit depths.

4:Come to us first for help with this code.


We have packaged this code (along with PNG, TIFF, PhotoCD, Photoshop, BMP, PCX, TGA, WMF/EMF, i/o, hundreds of image processing routines, etc.) into a very powerful, but inexpensive, Win32 DLL and static library. If this interests you, click here.


This package is not the responsibility of the people who's code we have used here - the IJG people, for example. Please, come to us first with any problems you are having with this package.

Email questions to:

If you run into any problems with this software, please feel free to contact us. If it turns out that the problem is in our code, we will fix it. Please do not ask the IJG people, or any of the other people who's code we have included here for help unless we've first determined that the problem is not in our code (or your's).

If you feel the problem is with the IJG code that is included, we 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.

"boolean" Redefinition Problem / Struct Size Mismatch 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, "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.

General fix.

    1. In the Jpeglib2.lib files, change jmorecfg.h :

    from (line 228):
    #ifndef HAVE_BOOLEAN
    typedef int boolean;


    #ifndef HAVE_BOOLEAN
    typedef char boolean;

    2. Compile Jpeglib2.lib as usual - ignore the warnings...

    3. In your app, in jpegfile.cpp, add

    #define HAVE_BOOLEAN

    #include "jpeglib.h"

Thanks to Bryce Burrows, Brian Krisler and Alexandre Maltais for taking the time to point out the problem.

VC 6 Problems

Microsoft has changed a lot of stuff behind the scenes. To fix the typedef problems that you will find if you try to compile this using VC6, you need to make the following change in jmorecfg.h :

change this :
   #ifndef XMD_H                   /* X11/xmd.h correctly defines INT32 */
   typedef long INT32;
to this :
   #ifndef XMD_H                   /* X11/xmd.h correctly defines INT32 */
   #include <basetsd.h>
   //typedef long INT32;

The Package

MFCAPP This is a small MFC C++ sample application using a class called JpegFile. JpegFile is a wrapper around the JpegLib 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.

The application provides the following functions in C++ : 

  • Read a BMP (1, 4, 8 or 24-bit) or JPG file, display the result.
  • Save a BMP (1, 4, 8 or 24 bit) or JPG (RGB or grayscale) file.
  • Examine the dimensions of a JPG 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 JpegLib.Lib.
  • Using Tools/Options/Directories/Include Files, add a path entry to the location of the JpegLib 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.
JPEGLIB This is the VC++ 6.0 project of the IJG source. In this example, it is a subproject of the MfcApp project. The dependecies and paths are set in MfcApp so that this library will be compiled when needed.

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. 

We have changed only two lines of the original source. We 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. 


Smaller Animals Software make no guarantees about this code's suitablilty for use. It is supported as much as we can afford to support something we give away for free. If you'd like to use a package with full JPG support and excellent support, check out our ImgSource package.

Since the IJG source is 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 here. You are strongly encouraged to look in the original IJG source for any questions you might have about using the JpegLib library. There is excellent documentation included in the IJG package, which will probably answer all of your JPG questions.

Get It!


Bookmark this page. It is the official JpegFile Documentation!

Copyright © 2015, Smaller Animals Software, Inc.

End Of The Line

Smaller Animals Software has been in business for 23 years. And it's been fun for every one of them. But, we've reached a point where the cost of keeping the virtual doors open far exceeds what sales bring in. And so, with much regret, we will be shutting down Smaller Animals Software. The proposed final date of operation is June 30, 2016. After that date, sales and support of all Smaller Animals Software products will cease.

ImgSource users are encouraged to purchase a source subscription, to allow them to diagnose and correct any issues they may encounter in the future.

We realize this is inconvenient for our customers. And we wish there was a way we could continue to develop and support the products we've been working on for all these years. But, there doesn't seem to be a way to do that.

We would like to thank all of our customers and especially those who have stuck with us over the years and have contributed greatly with their feedback and suggestions. It seems safe to say that the majority of the features in our products came directly from user suggestions. We wouldn't have done it without you.

Smaller Animals News


ThumbNailer 10