Graphics Programming with Perl Graphics Programming with Perl MARTIEN VERBRUGGEN MANNING Greenwich (74° w. long.) For online information and ordering of this and other Manning books, go to www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact: Special Sales Department Manning Publications Co. 209 Bruce Park Avenue Fax: (203) 661-9018 Greenwich, CT 06830 email: orders@manning.com ©2002 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Manning Publications Co. Copyeditor: Sharon Mullins 209 Bruce Park Avenue Typesetter: Syd Brown Greenwich, CT 06830 Cover designer: Leslie Haimes ISBN 1-930110-02-2 Printed in the United States of America 12345678910–VHG–0605040302 To Margaret, because she thought that “Advanced RenderMan” should be the name of a Super Hero To Maxine, for being excited about appearing in this book vii contents preface xiii About this book xiii Who should read this book? xiv Coding practices xv Source code downloads xvi author online xvii acknowledgments xix about the cover illustration xxi Part 1 Foundations 1 1 Overview of graphics 3 1.1 Perl and graphics 4 1.2 The bits and pieces in graphics programming 5 1.3 Color spaces and palettes 7 RGB 7 ✦ CMY and CMYK 8 ✦ HSV and HLS 9 YUV, YIQ and YCbCr 12 ✦ Grayscale 12 Color distance 13 ✦ Reducing the number of colors in an image 13 1.4 Summary 14 2 Overview of graphics file formats 15 2.1 Some graphics formats 16 GIF 17 ✦ JPEG, JFIF 18 ✦ PNG 18 MNG 19 ✦ SVG 19 ✦ TIFF 19 2.2 Finding the size and type of an image 20 Image::Size 20 ✦ Image::Magick 21 Do it yourself 22 ✦ More on file size and information 24 2.3 Conversion of graphics formats 24 2.4 Summary 26 viii CONTENTS 3 Overview of tools and modules 27 3.1 CPAN 28 3.2 The tools 29 The Chart::* modules 29 ✦ Gnuplot 29 GD 29 ✦ GD::Graph, GIFgraph and Chart::PNGgraph 30 GD::Text 30 ✦ The Gimp 30 ✦ Image::Magick 30 Image::Size 31 ✦ Inline 31 ✦ OpenGL 32 PGPLOT 32 ✦ RenderMan 33 ✦ Term::Gnuplot 33 3.3 A note on module versions 34 3.4 Summary 34 Part 2 Creating graphics 35 4 Drawing 37 4.1 Drawing with GD 39 An example drawing 40 ✦ Filling objects 42 Drawing text with GD 43 ✦ GD’s built-in fonts 44 TrueType fonts and GD 45 4.2 Drawing with Image::Magick 46 An example drawing 46 ✦ Anti-alias and fuzz 47 Drawing by combining images 48 ✦ Drawing with paths 50 Drawing text with Image::Magick 51 4.3 Combining GD and Image::Magick 53 4.4 Drawing with Term::Gnuplot 55 4.5 PostScript and SVG 59 4.6 Summary 59 5 Creating charts 60 5.1 GD::Graph and friends 62 Preparing your data sets for GD::Graph 63 Controlling the look of your chart 65 GD::Graph and fonts 65 ✦ Saving your chart to a file 66 Bar and area charts 68 ✦ Lines, Points and LinesPoints charts 70 ✦ Mixed charts 72 5.2 The Chart distribution 74 Pareto charts 75 5.3 PGPLOT 76 PGPLOT devices 76 ✦ Example: A simple X-Y plot 77 Example: A contour plot 81 ✦ Example: Plotting a galaxy 84 5.4 Interfacing with gnuplot 85 5.5 Summary 89 CONTENTS ix 6 Graphics and the Web 90 6.1 The Common Gateway Interface 91 HTTP and CGI 91 6.2 Suitable image formats 92 Web safe color palettes 92 6.3 CGI and dynamically generated graphics 94 6.4 Forms and encapsulated graphics 96 6.5 Image collections and thumbnailing 100 Thumbnails with Image::Magick 101 Thumbnails with GD 102 ✦ Contact sheets with Image::Magick’s ✦ visual directory 103 Contact sheets with Image::Magick::Montage 104 An example application: A web photo album 105 Designing the data 107 6.6 Summary 121 7 Animations for the Web 122 7.1 Animation file formats 123 GIF 123 ✦ MNG 124 Macromedia Flash 124 ✦ SVG 125 7.2 GIF animations 125 7.3 Perl and animations 126 7.4 Repeatedly applying a filter 127 Example: making objects appear 127 Example: Zooming in on an object 129 7.5 Animated text 131 7.6 Animated charts 132 7.7 Animations with the Gimp 134 7.8 Summary 137 8 Resizing and combining images 138 8.1 Scaling and cropping an image 139 Image::Magick geometry specification 139 Cropping an image 140 8.2 Combining Images 140 Combining GD images 140 Combining Image::Magick images 142 Adding a watermark to an image 145 8.3 Summary 151 x CONTENTS 9 Three-dimensional graphics 153 9.1 OpenGL 154 OpenGL library functions in Perl 155 Capturing your OpenGL output 157 Example: a planetary system 160 OpenGL summary 164 9.2 RenderMan 164 How to use the module and the BMRT 165 The RenderMan language binding for Perl 166 Example: A rotating cube of cubes 167 9.3 Summary 172 Part 3 Special topics 173 10 Writing your own graphics modules 175 10.1 Interface design 176 Coordinate transformation 180 Choosing your drawing primitives 181 Implementing the interface 182 10.2 An OO implementation of a clock 186 10.3 Summary 188 11 Controlling text placement 190 11.1 Determining text size 191 Text sizes in GD 191 Text sizes in Image::Magick 192 11.2 Aligning text in graphics 194 Aligning text with GD 194 Aligning text with Image::Magick 196 11.3 Wrapping text 197 The GD::Text modules 207 11.4 Summary 209 12 Manipulating pixels and transparency 210 12.1 GD and pixels 211 Example: rotating RGB values 211 Removing duplicate color palette entries 212 12.2 Image::Magick and pixels 213 Rotating RGB values 214 . Graphics Programming with Perl Graphics Programming with Perl MARTIEN VERBRUGGEN MANNING Greenwich (74° w. long.) For online information and ordering of this and other Manning books,. 295 xiii preface Welcome to Graphics Programming with Perl. When Manning Publications contacted me to assess my interest in writing a book about graphics programming for the Perl language, I was a. graphics tasks in Perl. So I accepted the challenge and started working, with the goal of providing a book that would sum up the ways to work with graphics from within Perl, and to offer suf- ficient