Progressive JPEG
Algorithm 11.9 Encoding Refining
AC Scans
Procedure RefineBand (COEFFICIENTS [0..63], START, END) Begirt
For II = START To END Begin
VALUE = COEFFICIENTS [II] / (1 LeftShift SUCCESSIVEAPPROXIMATION) If VALUE <> 0 Then
Begin
If VALUE < 0 Then VALUE = - VALUE
OutputLiteralBits (1, VALUE And 1) End
End End
Procedure PrintEOBRun Begin
If EOBRUN = 0 Then Return
BITCOUNT = CountBits (EOBRUN RightShift 1) HuffmanEncodeAC (BITCOUT LeftShift 4) OutputLiteralBits (BITCOUNT, EOBRUN) For II = 1 To EOBRUN DO
Begin
RefineBand (DATAUNITS [RUNSTARTDATAUNIT + II - 1], RUNSTARTCOEFFICIENT, SSE)
RUNSTARTCOEFFICIENT = SSS End
EOBRUN = 0 End
Procedure EncodeACRefine (COEFFICIENTS [0..63]) Begin
ZERORUN = 0
For II = SSS To SSE DO Begin
VALUE = COEFFICIENTS [II] / (1 LeftShift SUCCESSIVEAPPROXIMATION) If VALUE = 0 Then
Begin
If ZERORUN = 0 Then ZEROSTART = II ZERORUN = ZERORUN + 1 End
Else If Value = 1 Or Value = -1 Then Begin
PrintEOBRun
Data Unit Encoding 167
Algorithm 11.9 Continued
(continued
Progressive JPEG While ZERORUN >= 16 Do
Begin
ZEROLIMIT = ZER0START ZEROCOUNT = 0
While ZEROCOUNT < 16 Do Begin
OLDVALUE = COEFFICIENTS [ZEROLIMIT]
/ (1 LeftShift SUCCESSIVEAPPROXIMATION) If OLDVALUE = 0 Then
ZEROCOUNT = ZEROCOUNT + 1 ZEROLIMIT = ZEROLIMIT + 1 End
HuffmanEncodeAC (F016)
RefineBand (COEFFICIENTS, ZEROSTART, ZEROLIMIT - 1) ZEROSTART = ZEROLIMIT
ZERORUN = ZERORUN - 16 End
If VALUE > 0 Then Begin
BITCOUNT = CountBits (VALUE)
HuffmanEncodeAC ((ZERORUN LEFTSHIFT 4) Or BITCOUNT) OutputLiteralBits (BITCOUNT, VALUE)
End Else
Begin
BITCOUNT = CountBits (-VALUE)
HuffmanEncodeAC ((ZERORUN LEFTSHIFT 4) Or BITCOUNT) OutputLiteralBits (BITCOUNT, VALUE Xor FFFFFFFF16) End
RefineBand (COEFFICIENTS, ZEROSTART, II - 1) ZERORUN = 0
End End
If ZERORUN <> 0 Then Begin
Conclusion 169
Conclusion
This chapter on progressive mode brings to a close our coverage of JPEG. In this and preceding chapters, we have discussed all of the JPEG features that are in common use. The material presented covers all of the JPEG modes that you are likely to encounter in JPEG files on the Internet.
The source code for this chapter is not a complete example, but rather the additional code required to expand the sample applications from Chapters 8 and 9 to include progressive encoding and decoding.
Chapter 12 GIF
This chapter describes the CompuServe GIF format and the LZW compression method used to compress image data in this format. Until recently, CompuServe GIF (Graphics Interchange Format) was the most widely used format for image storage.
In 1987 CompuServe published the first GIF specification called GIF87a.
This specification was freely distributed, and the format was adopted by practi-cally every image processing application. CompuServe later released an enhanced, upwardly compatible version of the standard known as GIF89a.
However, most GIF images use only the features in GIF87a.
The main features of GIF are:
• Up to 256 colors using 1 to 8 bits per pixel
• Multiple images per file
Because of its better compression and greater color depth, JPEG has gen-erally replaced GIF for photographic images. GIF continues to be used for other applications, but legal entanglements have certainly condemned it to obsolescence.
171
172 GIF
Byte Ordering
The GIF format stores multi-byte integers with the least significant byte first (lit-tle-endian). Bit strings are read from the least significant bit to the most signifi-cant bit. In bit strings that cross byte boundaries, the bits in the second byte are more significant than the bits in the first byte.
File Structure
Figure 12.1 GIF File Structure
A GIF file consists of a fixed area at the start of the file, followed by a variable number of blocks and ending with an image trailer. In the GIF87a format the variable area consists solely of image definitions. In the GIF89a format these can be either images or extension blocks. The general format of a GIF file is shown in Figure 12.1.
GIF Header
The GIF header is required and must occur at the very start of the file. The header allows an application to identify the format as GIF and determine the version.
Table 12.1 shows the structure of the GIF header. It is still common for applica-tions that do not use the features added in GIF89a to create GIF87a headers in order to insure the image is compatible with older decoders.
Logical Screen Descriptor
The global screen description defines the logical screen area in which the indi-vidual images in the GIF file are displayed. It specifies the dimensions of the area as well as the background color (selected from the global color table). The indi-vidual image descriptors specify where the image is to be placed within the log-ical screen. The screen descriptor structure is 7 bytes and is shown in Table 12.2.
Figure 12.2 illustrates the relationship between the logical screen and the individual images in a GIF file.
Global Color Table
The individual images within the file can either use the global color table or define a color table of their own. Having the images in a file share the global
Table 12.1 GIF Header Structure
Field Name Signature Version
Size 3 bytes 3 bytes
Description
Must be the ASCII string GIF.
Must be the ASCII string 87a or 89b.
File Structure 173
Figure 12.2 Logical Screen/Image Relationship
Images
Logical Screen Field Name
Logical Screen Width Logical Screen Height Bit Fields
Global Color Table Size Color Table Sort Flag Bits Per Pixel
Global Color Table Flag Background Color
Pixel Aspect Ratio
Size 2 bytes 2 bytes 1 byte Bits 0-2 Bit 3 Bits 4-6 Bit 7 1 byte 1 byte
Description
2(N+1) gives the number of entries in the global color table.
Set when the colors in the global color table are sorted in order of importance.
Bits per pixel minus 1.
Set when there is a global color table.
Index into the global color table.
If this value is nonzero, the pixel width and height are not equal. (N+15)/64 is the pixel width divided by the pixel height.
Table 12.2 Logical Screen Descriptor Format
174 GIF
color table reduces the file size and makes it easier for systems that can display a limited number of colors. In addition, the global color table specifies the back-ground color for the logical screen.
If the global color table bit is set in the screen descriptor, the global color table immediately follows the screen descriptor. The global color table is an array of structures whose format is shown in Table 12.3. The number of entries in the array is determined from the Global Color Table Size field in the screen descriptor.
Block Types
After the global color table, the variable part of the GIF file begins. The file con-tains a sequence of blocks that are identified by a 1-byte code at the start of the block. Table 12.4 lists the block types and the associated block code.
Terminator
The terminator block marks the end of the GIF file. This block is 1 byte long and consists solely of the block code.
Image Block
An image block defines an image within the GIF file. The image starts with an image header structure that defines its size and placement. The format of this structure is given in Table 12.5. If the Local Color Table flag in the image header is set, the image uses a local color table rather than the global color table.
Just like the global color table, the local color table is an array of color entries (Table 12.3). The number of elements in the array is determined from the Local Color Table Size field in the header.
Figure 12.3 shows the general layout of an image definition within a GIF file.
File Structure 175
Figure 12.4 Extension Block Format
The colortable (or the image header if there is no color table) is followed by one byte that contains the initial code size used in the compressed data. The value in this field is usually 8.
Data Blocks
The code size byte is immediately followed by an uninterrupted series of data blocks that contain the compressed image data. A data block consists of a 1-byte count field, followed by 1 to 255 data bytes.
The chain of data blocks for an image is always terminated by a data block with zero data bytes—in other words, a single zero-valued byte. We will postpone the discussion of the compressed block format until later in the chapter.
Extension Blocks
Extension blocks were added to GIF in the GIF89a specification. The layout of an extension block is shown in Figure 12.4. The first byte in the extension block contains the code 2116. This is followed by a second byte that contains a code that specifies the extension type. The extension type codes are listed in Table 12.6.
The format of the extension header is specific to the extension type. The first byte in the header gives the size of the header not including the size byte. The header is followed by a list of data blocks. Each block consists of a count byte followed by 1 to 255 data bytes. The list of data blocks is terminated by a zero byte (data block with zero data bytes).
The structure of the extension blocks is such that an application can skip over them without having to understand the structure of each individual extension type. Algorithm 12.1 illustrates how to skip over an extension.
Table 12.5
Local Color Table Size Reserved
Sort Flag Interlace Flag
Local Color Table Flag
Size
Left offset of the image within the logical screen.
Top offset of the image within the logical screen.
(N+1)2 is the number of entries in the local color table.
Unused.
If set, the colors in the local color table are sorted in order of importance.
If set, the image data is interlaced.
If set the image uses a local color table.
176 GIF
DATA = ReadByte () // Extension Type DATA = ReadByte () // Count
The plain text extension block is used to draw a grid of fixed-spaced text on the logical screen. This extension consists of a header followed by a series of data blocks containing the text to draw. The data blocks following the header contain the text to display. The format of the plain text extension header is shown in Table 12.7.
Graphic Control Extension
The graphic control extension affects how the next image in the GIF file is to be drawn. Graphic control extensions are commonly used to specify how the
Table 12.7
Text position in the logical screen Text position in the logical screen Size of the text block in pixels Size of the text block in pixels Width in pixels of each character Height in pixels of each character Index into the global color table for the text color
Index into the global color table for the background color