Book Image

D Cookbook

By : Adam Ruppe
Book Image

D Cookbook

By: Adam Ruppe

Overview of this book

Table of Contents (21 chapters)
D Cookbook
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Working with image files


Reading and writing image files is a way to present visualized results to the user and is also a starting point to write out images to a web browser or GUI window. Here, we'll use my color.d and png.d or bmp.d modules to load an image file, convert it to grayscale, and then write it out to a new file.

Getting ready

Download color.d and either png.d or bmp.d from my Github repository and put them in your project's directory. In the example, we'll use png.d. Neither module requires any additional D or C libraries.

How to do it…

Let's work with image files by executing the following steps:

  1. Import arsd.png.

  2. Load the file with readPng, passing it a filename. Be sure that the file is already in the PNG format.

  3. Use the getAsTrueColorImage function to convert the input from whatever PNG format it was saved as into an RGBA array.

  4. Manipulate the pixels. Here, we'll convert to greyscale with a simple average-of-components algorithm.

  5. Write the new image out to a file with writePng.

The...