Compressed Image
File Formats
JPEG, PNG, GIF, XBM, BMP
Your guide to graphics
files on the Web
John Miano
Compressed Image
File Formats
Many of the designations used by manufacturers and sellers to distinguish their products are
claimed as trademarks. Where those designations appear in this book, and Addison-Wesley
was aware of a trademark claim, the designations have been printed in initial capital letters
or all capital letters.
The author and publisher have taken care in the preparation of this book, but make no
expressed or implied warranty of any kind and assume no responsibility for errors or
omissions. No liability is assumed for incidental or consequential damages in connection
with or arising out of the use of the information or programs contained herein.
The publisher offers discounts on this book when ordered in quantity for special sales.
For more information, please contact:
Corporate, Government, and Special Sales Group
Addison Wesley Longman, Inc.
One Jacob Way
Reading, Massachusetts 01867
Library of Congress Cataloging-in-Publication Data
Miano, John, 1961-
Compressed image file formats : JPEG, PNG, GIF, XBM, BMP / by John Miano
p. cm.
Includes bibliographical references.
ISBN 0-201-60443-4. — ISBN 0-201-61657-2 (CD-ROM)
1. Image processing—Computer programs. 2. Data compression (Computer
science) 3. Computer programming. 4. File organization (Computer science)
I. Title
TA1637.M53 1999
005.74'6—dc21 99-15179
CIP
Copyright © 1999 by the ACM Press,
a division of the Association for Computing Machinery, Inc. (ACM).
All rights reserved. No part of this publication may be reproduced, stored in a retrieval
system, or transmitted, in any form, or by any means, electronic, mechanical, photocopying,
recording, or otherwise, without the prior consent of the publisher. Printed in the United
States of America. Published simultaneously in Canada.
ISBN 0-201-60443-4
Text printed on recycled and acid-free paper.
1 2 3 4 5 6 7 8 9—MA—04 03 02 01 00 99
First printing, July 1999
Contents
Chapter 1
Chapter 2
Chapter 3
Chapter 4
Preface ix
Acknowledgments xi
Introduction
The Representation of Images 1
Vector and Bitmap Graphics 3
Color Models 5
True Color versus Palette 9
Compression 10
Byte and Bit Ordering 13
Color Quantization 16
A Common Image Format 18
Conclusion 21
Windows BMP
Data Ordering 23
File Structure 24
Compression 28
Conclusion 29
XBM
File Format 31
Reading and Writing XBM Files 33
Conclusion 34
Introduction to JPEG
JPEG Compression Modes 36
What Part of JPEG Will Be Covered in This Book? 39
What are JPEG Files? 40
SPIFF File Format 40
Byte Ordering 41
1
23
31
35
V
Contents
VI
Chapter 5
Chapter 6
Chapter 7
Chapter 8
Chapter 9
Sampling Frequency 41
JPEG Operation 44
Interleaved and Noninterleaved Scans 45
Conclusion 46
JPEG File Format
Markers 47
Compressed Data 49
Marker Types 49
JFIF Format 55
Conclusion 57
JPEG Human Coding
Usage Frequencies 61
Huffman Coding Example 63
Huffman Coding Using Code Lengths 65
Huffman Coding in JPEG 71
Limiting Code Lengths 72
Decoding Huffman Codes 73
Conclusion 75
The Discrete Cosine Transform
DCT in One Dimension 78
DCT in Two Dimensions 84
Basic Matrix Operations 85
Using the 2-D Forward DCT 87
Quantization 88
Zigzag Ordering 89
Conclusion 90
Decoding Sequential-Mode JPEG Images
MCU Dimensions 91
Decoding Data Units 94
Decoding Example 97
Processing DCT Coefficients 98
Up-Sampling 99
Restart Marker Processing 99
Overview of JPEG Decoding 100
Conclusion 100
Creating Sequential JPEG Files
Compression Parameters 105
Output File Structure 111
Doing the Encoding 111
Down-Sampling 112
Interleaving 113
Data Unit Encoding 115
Huffman Table Generation 117
Conclusion 119
47
61
77
91
105