Graphics Programming with Perl
Total Page:16
File Type:pdf, Size:1020Kb
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: [email protected] ©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 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 vii 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 viii CONTENTS 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 CONTENTS ix 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 x CONTENTS 12.3 Convolution 215 Convolution with Image::Magick 217 Using Image::Magick's Convolve() method 224 Convolution with PDL 226 12.4 Alpha channels and transparency 229 Transparency and the GD module 229 Transparency and Image::Magick 230 How to view partially transparent images 233 12.5 Fast manipulation of image pixels 233 Using Inline::C 234 12.6 Summary 238 Part 4 Appendices 239 A Image::Magick introduction & reference 241 B Color space conversion algorithms 284 C Module code 288 references 293 index 295 CONTENTS xi 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 bit skeptical at first. I didn’t think there would be enough coherent material to write such a book, and I wasn’t entirely certain there would be much room for one. However, after doing some research I noticed that, in fact, there was a gap in the published material. The only book I could find that combined graphics programming and Perl was too general, and was largely a collection of recipes on how to use tools, many of which had no relation to Perl [1]. There were not enough references that described and explained how to achieve concrete 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 background information to improve the understanding of graphics program- ming and graphics in general. This work seeks to provide a programmer with enough fundamental knowledge and pointers to achieve the most common graphics program- ming tasks. I hope those goals have been realized. ABOUT THIS BOOK The objective of the book is to show how graphical programming projects may be accomplished. When commencing work on a program, you have probably formulated an idea of your goal, but you might not necessarily know how to reach it. In most cases all that is needed is a pointer to the right modules, tools, commands or techniques. This book aims at providing this sort of reference for graphics manipulation using the Perl pro- gramming language. The content includes a wide range of topics related to manipulation and produc- tion of graphics with Perl, including the creation, editing and combining of images, the development of libraries, interfaces and modules for graphics creation, massaging of text for graphics, the building of charts, as well as working with single pixels in images. This book is not meant as a reference work for the modules available for xiii graphics programming.1 For that you are encouraged to read the documentation that comes with the modules, which in most cases is satisfactory. This book is also not intended to be an exhaustive introduction to graphics in general, or a detailed expla- nation of graphics algorithms, although where appropriate, some graphics theory and algorithm description will be touched on. The reader is expected to have a basic knowledge of Perl. The programs and code fragments will be explained in terms of what they do and how they operate, and only occasionally in terms of what the Perl statements mean.