PurposeThe purpose of this program, BMPJPG.EXE, is to
demonstrate how to convert a BMP file to a JPG, or a JPG file to a BMP. Nearly all of the
JPEG compression options can be controlled via the user interface, including image quality
and progressive encoding and display. The purpose of programs, BMPtoJPG.EXE and JPGtoBMP.EXE,
is to provide a way to perform batch conversion of BMP or JPG files to the other format.

A second purpose of this program is to evaluate how "JPEG Quality" affects file
size and image fidelity.

Hardware RequirementsBest when run on 800 x 600 pixel (or higher) monitor with high color or
true color.
Colors may not display correctly in 256-color mode.

Procedure (BMPJPG
program)

BMP-to-JPG

Double click on the BMPJPG.EXE icon to start the program.

Select the Browse button on the left (BMP) side of the screen.

Select TULIP3.BMP (or Parrots.BMP) from the list and select the Open
button. Select the Print BMP button if desired.

Check the two Stretch Checkboxes so the full image will display.

Using the Quality SpinBox, select a fairly low quality, e.g., 10.

Select the arrow speedbutton (points to the right) to convert the BMP to a JPG. Note the
JPG filename is the same as the BMP, but with a different file extension and the quality
factor as part of the filename. Select the Print JPG button if desired.

Select the Save JPG button to save the file TULIP3010.JPG. Select OK on
the dialog.

JPG-to-BMP

Select the Browse button on the right (JPG) side of the screen.

Select TULIP3010.JPG from the list and select the Open button.

Select the arrow speedbutton (points to the left) to convert the JPG to a BMP. Note the
JPG filename is the same as the BMP, but with a different file extension and the quality
factor as part of the filename. Select Print BMP button if desired.

DiscussionJPEG support may not automatically be installed with Delphi
3. Consult "Installing JPEG Components that Ship with Delphi 3 (Borland's
TI 4531D) for
additional information. JPEG support should be correctly and automatically installed
in Delphi 4 and 5.

The JPEG "Quality" factor directly affects the
size of a JPEG file, as well as the image quality itself. (Note: There is not a single
definition of JPEG "Quality." For example, PaintShop Pro Quality = 100 - Quality
shown here.)

A JPEG image is "lossy" even with 100% quality.
This means that even with 100% JPEG Quality, some of the information in an image is lost
when stored in JPEG format.

A BMP image of two parrots (see below) contains 294,966
bytes. With 100% JPEG Quality, the JPG version of this image contains 75,437 bytes -- a
significant reduction in the size of the file. But by about 80% JPEG Quality, the file
sizes drops to about 16,000 bytes. A lower JPEG Quality yields a smaller file size, as
shown in the following graph.

But how "good" are the images with lower JPEG Quality? Here are
three visual examples:

JPEG "Quality" Factor

1

52

100

From a DOS command-line window, run the Experiment.BAT
file, "Experiment filename", to repeat this experiment with any other 24-bit BMP
file. Three output files are created: JPEGList.DAT contains a list of the files
and their sizes. Statistics.DAT contains a list of relative and absolute error
differences between the original image and the various lossy images. Data are also
summarized by R,G,B color plane. Correlation.DAT computes a correlation
coefficient of each lossy image against the original image. At the end of the experiment
run the Cleanup.BAT to erase unneeded files.

The following graph summarizes the correlation of the JPEG
images with the original image by color plane:

The JPEG compression algorithm tries to exploit limitations
of our own eyes. Since the eye is less sensitive to blue, and most sensitive to green, the
JPEG compression algorithm maintains better fidelity with the green component of an image
and less with the blue component of an image.

Based on the above two graphs, for some images such as the
Parrots, a JPEG Quality Factor of roughly 20-80 gives good reduction in files size without
affecting the image fidelity significantly.

NOTE: The correlation facgtor would be 1.00
if an all-white image were compared with an all-black image. The
correlation factor measures how well one image "predicts" the other
image, and does not measure "error" directly.

The palettes are usually OK in 256-color mode after loading
a JPG image. This is rarely the case after loading a 24-bit color BMP image.

JPG image compression is very effective for "real
world" images, as opposed to line drawings or text, where it is not so good. Note the
graphs on this web page are stored in GIF format instead of JPG format.

efg'sUseNet Post
and Bob Villiers' Summary
Response about JPEGs and PixelFormat (Postscript: The code in this Lab
Report needs to be reviewed and updated. The user interface is misleading about how
8-bit/24-bit PixelFormat is used.) Bob Villiers verified that ".. after some
experiment, PixelFormat has no influence on the jpg colour depth, but GrayScale := True
produces an 8 bit B/W image."

Command-Line Programs. Enter BMPtoJPG
or JPGtoBMP in a DOS window without parameters for the syntax of these commands
-- or just look at the source code or the VaryQuality.BAT that is used in the Experiment.BAT
file.

The command-line program BitmapCompare can be used
to compare two images. Look in the CompareAll.Bat called by Experiment.BAT
for more information -- or just look at the source code.

Comments from OthersDave Martindale (23 Oct 1998):
"You need to specify which JPEG compressor you were using. A quality factor of 80 to
the IJG JPEG code isn't the same as a quality of 8 tothe Photoshop JPEG code, for example.
And there are other JPEG implementations out there besides these two."

Response: The Delphi 3/4 JPEG support is based on the public
release of the Independent JPEG Group's (IJG's) free JPEG software. The
"official" archive site for the IJG software is ftp.uu.net
in /graphics/jpeg

Tom Lane, IJG (23 Oct 1998):
"Not only that, but Adobe has changed their JPEG compressor significantly over time.
The IJG code has changed also. (And if I ever get some time to work on it, the next IJG
release will have a *substantially* new quality scale.)"

"Earl's conclusion is not quite content-free as given, but it's
pretty close. You cannot meaningfully describe the behavior of a JPEG compressor by a
single number. Most JPEG implementations do provide a "quality" setting as a
convenient user interface to the underlying complexity... but there's no standard about
what the quality scales really mean. So writing a quality number without mentioning which
release of which compressor it applies to is not very helpful."

Flavius Silva (23 Oct 1998):
"The problem with this is that a 'Quality' factor has nothing to do with JPEG, but
rather JPEG implementations. Quality factor is used to select quantization tables. The
larger the quality factor the (generally) smaller the values in the quantization tables.
However, different implementations can use different quantization tables, even when using
a scale of 1-100."

"There is also sampling frequency to be considered when compressing
color images. You can get better compression by increasing the sampling frequency for the
Y component."

ConclusionsJPEG image compression is very effective way of compressing many images. A
Quality Factor (IJG Version 6) of 75 or 80 is good for nearly all images. Sometimes, a
Quality Factor of 10 is even good enough for some images. Progressive Encoding is slower
but provides even more compression.