| BMPJPG | Lab Report |
Purpose
The 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.
Materials and Equipment
Software Requirements
Windows 95
Delphi 3 or 4 (to recompile)
BMPJPG.EXE
TULIP3.BMP (or other 24-bit color BMP file)Hardware Requirements
Best 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
JPG-to-BMP
Discussion
JPEG 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.
To convert from a TJPEGImage to a TBitmap:
| // See Borland's TI 4528D VAR Bitmap : TBitmap; FileName : STRING; JPEGImage : TJPEGImage; ... Filename := FilePath + SearchRec.Name; JPEGImage := TJPEGImage.Create; TRY JPEGImage.LoadFromFile(Filename); Bitmap := TBitmap.Create; TRY Bitmap.PixelFormat := pf24bit; Bitmap.Height := JPEGImage.Height; Bitmap.Width := JPEGImage.Width; // Convert JPG to BMP Bitmap.Canvas.Draw(0,0, JPEGImage); Bitmap.SaveToFile( ChangeFileExt(Filename, '.BMP') ); FINALLY Bitmap.Free END FINALLY JPEGImage.Free END; |
To convert from a TBitmap to a TJPEGImage:
| // See Borland's TI 4528D VAR Bitmap : TBitmap; BMPFileName : STRING; JPEGFilename: STRING; JPEGImage : TJPEGImage; Quality : INTEGER; ... Quality := 90; // or desired value ... BMPFilename := FilePath + SearchRec.Name; Bitmap := TBitmap.Create; TRY Bitmap.LoadFromFile(BMPFilename); JPEGImage := TJPEGImage.Create; TRY JPEGImage.CompressionQuality := Quality; // Convert BMP to JPG JPEGImage.Assign(Bitmap); // Strip off '.BMP' JPEGFilename := COPY(BMPFilename, 1, LENGTH(BMPFilename)-4); JPEGFilename := JPEGFilename + Format('%3.3d', [Quality]) + '.JPG'; JPEGImage.SaveToFile(JPEGFilename); FINALLY JPEGImage.Free END FINALLY Bitmap.Free END; |
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's UseNet 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 Others
Dave 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."
Conclusions
JPEG 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.
Future Study
Study how image quality degrades after successive
conversions, e.g., BMP-to-JPG-to-BMP-to-JPG ...
Keywords
TJPEGImage, BMP, JPG, JPEG, BMP-to-JPG, JPG-to-BMP, JPEG quality factor,
progressive encoding, progressive display, incremental display, compression factor,
command-line utility, image comparison
Download
Delphi 3/4 Source, Delphi 3 EXE (459 KB): BMPJPG.ZIP
24-bit color .BMP test image: Parrots.ZIP (200 KB)
Delphi 4 Conversion Notes
To avoid D4 compiler warnings the following changes were made for D3/D4 compatibility::
The EXEs are somewhat larger in Delphi 4 than Delphi 3 with both the visual compiler and the command-line compiler:
| Program | Compiler | D3 EXE Size [bytes] | D4 EXE Size [bytes] |
| BMPJPG | Visual | 389,120 | 480,256 |
| BmpToJpg | Command Line | 178,176 | 186,368 |
| JpgToBmp | Command Line | 178,176 | 186,368 |
See the Delphi Graphics File Formats & Conversions page (especially Section A) for links about using BMPs and JPGs.
Updated 14 Sep 2004
since 1 Nov 1998