ImageGear for C and C++ on Linux v18.10 - Updated
JPEG
User Guide > File Formats and Compressions > File Formats > File Formats Reference > JPEG

Full Name JPEG File Interchange Format
Format ID IG_FORMAT_JPG = 21
File Extension(s) *.ipg, *jpeg
Data Type Raster Image
Data Encoding Binary
Color Profile Support Read, Write
Multi-Page Support No
Alpha Channel Support Read/write
ImageGear Platforms Support WIN32, WIN64, Unix, Unix64, .NET, .NET64, MAC, Java

ImageGear Supported Versions:

ImageGear Supported Features:

ImageGear Read Support:

ImageGear Write Support:

  1. RGBA saving is disabled by default. Set SAVE_ALLOW_RGBA control parameter to TRUE to enable it.
  2. CMYK saving is enabled by default. You can disable it by setting SAVE_ALLOW_CMYK control parameter to FALSE.

ImageGear Filter Control Parameters:

Filter Control Parameter Type Default Value Available Values Description
CALLER_ID Long 21 Internal parameter. It will be removed from public API in a future release.
DECIMATION_TYPE WORD IG_JPG_DCM_2x2_1x1_1x1 See the "JPEG Decimation" section below. Get/Set decimation value. For all available values see the "JPEG Decimation" section below.
ENTROPY_OPTIMIZE AT_BOOL FALSE TRUE, FALSE Get/Set entropy optimization flag. See the "JPEG Entropy Optimization" section below.
KEEP_ALPHA AT_BOOL FALSE TRUE, FALSE This parameter gets/sets alpha channel flag. If TRUE then alpha channel will be saved with original image (if it is present).
LOAD_SAVE_DCT AT_BOOL FALSE TRUE, FALSE Internal parameter. It will be removed from public API in a future release.
LOAD_SCALE_DENOM UINT 1 1, 2, 4, 8 If this parameter is set to any other than default value, ImageGear loads reduced version of the image, width and height of which are scaled by 1/load_scale_denom. This mode can be used for image preview, especially for those images that do not have embedded thumbnails, or where embedded thumbnails are smaller than desired. This mode allows you to make loading process 2-4 times faster.*
LOAD_SCANS UINT 0 Gets/Sets number of scans to load from Progressive JPEG image. See the "Progressive JPEG Scans" section below  for details.
OLD_LOSSLESS_READ AT_BOOL FALSE TRUE, FALSE
PREDICTOR AT_MODE 1 Gets/Sets predictor for lossless JPEG. See the "Lossless JPEG Predictor Settings" section below for details.
QUALITY INT 70 Possible value in range [1,100] Gets/Sets quality value for Lossy and Progressive JPEG compression. See the "JPEG Quality" section below for details.
SAVE_ALLOW_CMYK AT_BOOL TRUE TRUE, FALSE Set to TRUE (default) to allow saving JPEG CMYK images. Set to FALSE to save CMYK images to JPEG as RGB.
SAVE_ALLOW_LOSSY12BPCSAVING AT_BOOL TRUE TRUE, FALSE Controls the saving to Lossy JPEG format. If TRUE, saving of 12 bits per channel Lossy JPEG is allowed. This is the maximum channel depth supported by Lossy JPEG format. If source image's depth is more than 8 bits per channel, it will be saved to a 12 bpc JPEG. For example, 48-bit RGB image will be saved to 36-bit JPEG. Note that many viewers do not support 12 bpc JPEG. If FALSE, all images are saved to 8 bpc JPEG, regardless of their channel depth.
SAVE_ALLOW_RGBA AT_BOOL FALSE TRUE, FALSE Set this parameter to TRUE to enable RGBA JPEG saving. Note, that there is no official standard for RGBA JPEG. This format is recognized as CMYK rather than RGBA by most third-party software.
SAVE_JFIF_IN_EXIF AT_BOOL FALSE TRUE, FALSE Internal parameter. It will be removed from public API in a future release.
SAVE_THUMBNAIL AT_BOOL FALSE FALSE, TRUE Gets/Sets thumbnail flag. If TRUE then thumbnail will be added to image. See the "JPEG Decimation" section below for details.
SAVE_TYPE AT_MODE IG_JPG_LOSSY IG_JPG_LOSSY, IG_JPG_LOSSLESS, IG_JPG_PROGRESSIVE Get/Set type for output JPEG format. See the "Loading and Saving JPEG-Compressed Images" section below for details.
SCAN_INFO VOID|LP { { 0, 0, 7, 1, 0 }, { 1, 5, 7, 1, 1 }, { 1, 5, 7, 1, 2 }, { 1, 5, 7, 1, 3 }, { 0, 0, 0, 0, 0 }, { 6, 63,7, 1, 1 }, { 6, 63,7, 1, 2 }, { 6, 63,7, 1, 3 }, { 1, 63,0, 0, 1 }, { 1, 63,0, 0, 2 }, { 1, 63,0, 0, 3 },}; Gets/Sets scan configuration for Progressive JPEG format. See the "Progressive JPEG Scans" section below for details.
SCAN_INFO_COUNT UINT sizeof(scan_info)/sizeof(scan_info[0]); Gets/Sets length of array for previous parameter. See the "Progressive JPEG Scans" section below for details.
THUMBNAIL_COMPRESSED AT_BOOL FALSE FALSE, TRUE If TRUE then thumbnail will be compressed.
THUMBNAIL_HEIGHT UINT 16 Gets/Sets thumbnail height.
THUMBNAIL_WIDTH UINT 16 Gets/Sets thumbnail width.

Comments:

JPEG is normally associated with the JPEG compression scheme, but it is also implemented into the JFIF file format. This format was developed to store JPEG-encoded data, and to exchange it between applications or operating systems that are normally incompatible.

The JPEG compression scheme was developed by the Joint Photographic Experts Group (created by the joining of a subgroup of the International Standards Organizations, called PEG (Photographic Experts Group) and a subgroup of the CCITT). Their common goal was to produce a standard for the transmission of graphics image data over networks and through color facsimile systems.

The header of the JFIF contains the version number, the image dots per inch (DPI), or dots per centimeter, and an optional thumbnail (miniature) RGB representation of the main image. Version 1.02 handles thumbnails differently by storing them separately, rather than in the identification marker of the header.

The raw JPEG data is surrounded by two markers, an "SOI" (start of image) marker, and an "EOI" (end of image) marker. See the section entitled Compression Schemes for more about JPEG compression.

JFIF is considered a non-proprietary file format. Many proprietary file formats contain JPEG data, incorporating their own application-specific data structures. Other non-proprietary formats that use JPEG-encoded data: TIFF file format, version 6.0.

Loading and Saving JPEG-Compressed Images

ImageGear supports the reading and writing of three types of JPEG compression: baseline JPEG (Lossy), Progressive JPEG, and Lossless JPEG. When you load a JPEG-compressed file, ImageGear detects the type of JPEG compression and decompress the image automatically. But if you want to save an image with a JPEG compression scheme other than baseline JPEG, you must use the "SAVE_TYPE" parameter to specify the type of JPEG compression.

Use these constants for "SAVE_TYPE" control parameter:

JPEG Decimation

This table lists all possible decimation values:

IG_JPG_DCM_1x1_1x1_1x1 IG_JPG_DCM_2x1_1x1_1x1
IG_JPG_DCM_1x2_1x1_1x1 IG_JPG_DCM_2x2_1x1_1x1
IG_JPG_DCM_2x2_2x1_2x1 IG_JPG_DCM_4x2_1x1_1x1
IG_JPG_DCM_2x4_1x1_1x1 IG_JPG_DCM_4x1_1x1_1x1
IG_JPG_DCM_1x4_1x1_1x1 IG_JPG_DCM_4x1_2x1_2x1
IG_JPG_DCM_1x4_1x2_1x2 IG_JPG_DCM_4x4_2x2_2x2

The format of these ImageGear decimation constants is:

 
Copy Code
IG_JPG_DCM_<H1>x<V1>_<H2>x<V2>_<H3>x<V3>,

where Hi, Vi = horizontal and vertical decimation values for the i-channel.

The following is a simple example of decimation. For a more detailed definition, please see the JPEG Specification.

A decimation setting of IG_JPG_DCM_4x2_1x1_1x1 would yield the following results:

As shown, 8 Y components in the source image have yielded one Cb and one Cr component. In general, this setting will reduce the quality of the compression, unless the image has many continuous tone areas.

JPEG Thumbnails

The JPEG format can store thumbnails, which are small representations of the original image. These images are stored in uncompressed form and can significantly decrease your overall compression ratio. (Uncompressed thumbnails sometimes occupy more space than the original JPEG image when compressed). Use this option carefully.

The JPEG format does not allow the storage of "large" thumbnails. This is due to the marker segment length, which cannot be greater than 65,536 bytes. The maximum size of a color thumbnail is about 100x200 or 200x100 pixels, and the maximum size of a grayscale thumbnail is about 300x200 or 200x300 pixels.

Lossless JPEG Predictor Settings

The Lossless JPEG scheme is "predictive" in nature-it uses the values of surrounding pixels in addition to the value of the original pixel to calculate a predictor value, which it then subtracts from the value of the original pixel. The resulting pixel value will be reduced such that it can be compressed more than the original value. The higher the number of neighboring pixels used, the higher the compression will be.

Regardless of the predictor value setting, the quality of the image will remain the same. The difference is that if you choose to optimize for space by setting a high predictor value, you will have to give up some speed, as the decompression will take longer to perform.

ImageGear lets you set the predictor value using "PREDICTOR" control parameter. The ImageGear default for this setting is 1. The allowed range is 1-7. Values outside of the allowed range of 1-7 will be mapped to a value of 5. The graphic below shows a predictor (x) and three reconstructed samples (a,b,c) immediately to the left, immediately above, and diagonally to the left of the current sample

Lossless JPEG does not apply DCT for an image as per the Lossy JPEG compression. Instead, it uses a DPCM difference coding, which can be carried out with any one of seven different prediction modes. Correspondingly, the IG_CONTROL_JPG_PREDICTOR control parameter can be set to a value between 1 and 7. In Table 4, you will see what algorithm your setting of 1-7 will use, where Px is the predictor, and Ra, Rb, and Rc are the reconstructed samples:

Value Lossless JPEG Prediction
1 Px = Ra
2 Px = Rb
3 Px = Rc
4 Px = Ra + Rb - Rc
5 Px = Ra + ((Rb-Rc)/2)
6 Px = Rb + ((Ra-Rc)/2)
7 Px = (Ra + Rb)/2

JPEG Quality

The baseline JPEG specification calls for a quality setting. The lower the setting, the greater the number of original pixels lost, and therefore the smaller the resulting compressed file will be. ImageGear lets you set the quality of compression with values of type INT between 1-100, where 100 provides the highest retention of original pixel values. A setting of 100 does not mean that the image includes 100% of all original pixel values. With Lossy JPEG, there is no such thing as "no loss". Control parameter for setting Lossy JPEG quality is "QUALITY".

JPEG Entropy Optimization

Entropy optimization is relevant only to standard Lossy JPEG compression. If you set the parameter to TRUE, the default Huffman tables are not used. Instead, optimal Huffman tables are created for each component. This can bring a higher compression ratio but it takes more time for the compression.

Progressive JPEG Scans

A Progressive JPEG file stores more than just a copy of an image but rather several scans, each of which progressively adds a higher level of quality. Each scan contains a portion of the original image data. The purpose of this is to allow a very quick display of an image, beginning with a low-quality rendering and then increasing in quality as the remaining scans are added to it.

A Progressive JPEG image is stored as sequence of Huffman compressed blocks or "scans". Each scan contains the sequence of DCT coefficients in the given range. However, the coefficients are not complete. Only some of their bits will be stored in each scan.

ImageGear defines the following structure for holding the necessary configuration to write a JPEG image:

 
Copy Code
typedef struct tag AT_PJPEG_SCANINFO{
                LONG            Ss;
                LONG            Se;
                LONG            HBit;
                LONG            LBit;
                LONG            ChannelID;
}               AT_PJPEG_SCANINFO;
typedef AT_PJPEG_SCANINFO FAR* LPAT_PJPEG_SCANINFO;

The Ss and Se members of the AT_PJPEG_SCANINFO structure are used for spectral selection control coefficients:

 
Copy Code
LONG                 Ss; 
LONG                    Se; - after applying DCT of 8x8 pixels we get 64 

In Progressive coding, these coefficients are separated into different scans. Values Ss and Se specify the first and last number of the DCT coefficients that must be included in a given scan. The possible values are Ss = Se = 0 or 1<= Ss<=Se<=63. Please note following restrictions:

The HBit and LBit members of the AT_PJPEG_SCANINFO structure are used for successive approximation control:

 
Copy Code
        LONG HBit;
        LONG LBit;

If successive approximation is used, the DCT coefficients are reduced in precision by the point transform defined in the scan header. This is equivalent to taking some binary digits from each coefficient. HBit and LBit specify the high and low range of bits to take. For example, if HBit = 7 and LBit = 2, the scan will have the following original bits of the original DCT coefficient: 7,6,5,4,3,2.

The ChannelID member of the AT_PJPEG_SCANINFO structure is used to specify the number of components that will be encoded:

 
Copy Code
LONG ChannelID;

It can be set to one of the following values:

There are two control parameters that operate with the AT_PJPEG_SCANINFO structure:

SCAN_INFO Points to array of AT_PJPEG_SCANINFO elements.
SCAN_INFO_COUNT Specifies the number of elements in this array. The nth entry of the SCAN_INFO array defines the configuration for the nth scan of the Progressive image.

The LOAD_SCANS control parameter specifies how many scans should be loaded. For example, if it is set to 1, the JPEG filter will load only the first scan of the image.

The following text blocks represent the different AT_PJPEG_SCANINFO structures that would be generated when loading a JPEG file using the ImageGear default settings for Progressive scans:

scan_config[0].Ss = 0; scan_config[1].Ss = 1;
scan_config[0].Se = 0; scan_config[1].Se = 5;
scan_config[0].HBit = 7; scan_config[1].HBit = 7;
scan_config[0].LBit = 2; scan_config[1].LBit = 2;
scan_config[0].ChannelID = 0; scan_config[1].ChannelID = 1;
scan_config[2].Ss = 1; scan_config[3].Ss = 1;
scan_config[2].Se = 5; scan_config[3].Se = 5;
scan_config[2].HBit = 7; scan_config[3].HBit = 7;
scan_config[2].LBit = 2; scan_config[3].LBit = 2;
scan_config[2].ChannelID = 2; scan_config[3].ChannelID = 3;
scan_config[4].Ss = 0; scan_config[5].Ss = 1;
scan_config[4].Se = 0; scan_config[5].Se = 5;
scan_config[4].HBit = 1; scan_config[5].HBit = 1;
scan_config[4].LBit = 1; scan_config[5].LBit = 1;
scan_config[4].ChannelID = 0; scan_config[5].ChannelID = 1;
scan_config[6].Ss = 1; scan_config[7].Ss = 1;
scan_config[6].Se = 5; scan_config[7].Se = 5;
scan_config[6].HBit = 1; scan_config[7].HBit = 1;
scan_config[6].LBit = 1; scan_config[7].LBit = 1;
scan_config[6].ChannelID = 2; scan_config[7].ChannelID = 3;
scan_config[8].Ss = 6; scan_config[9].Ss = 6;
scan_config[8].Se = 63; scan_config[9].Se = 63;
scan_config[8].HBit = 7; scan_config[9].HBit = 7;
scan_config[8].LBit = 1; scan_config[9].LBit = 1;
scan_config[8].ChannelID = 1; scan_config[9].ChannelID = 2;
scan_config[10].Ss = 6; scan_config[11].Ss = 0;
scan_config[10].Se = 63; scan_config[11].Se = 0;
scan_config[10].HBit = 7; scan_config[11].HBit = 0;
scan_config[10].LBit = 1; scan_config[11].LBit = 0;
scan_config[10].ChannelID = 3; scan_config[11].ChannelID = 0;
scan_config[12].Ss = 1; scan_config[13].Ss = 1;
scan_config[12].Se = 63; scan_config[13].Se = 63;
scan_config[12].HBit = 0; scan_config[13].HBit = 0;
scan_config[12].LBit = 0; scan_config[13].LBit = 0;
scan_config[12].ChannelID = 1; scan_config[13].ChannelID = 2;
scan_config[14].Ss = 1;
scan_config[14].Se = 63;
scan_config[14].HBit = 0;
scan_config[14].LBit = 0;
scan_config[14].ChannelID = 3;

References Used:

Brown, C. Wayne, and Barry J. Shepherd. Graphics File Formats: Reference and Guide. Greenwich, CT.: Manning Publications, 1992.

Kay, David C. and John R. Levine. Graphics File Formats. Windcrest Books, 1992.

Murray, James D. and William vanRyper. Encyclopedia of Graphics File Formats. Sebastopol, CA: O'Reilly & Associates, Inc., 1994.