Creating a Raw Image in iText

This article is based on iText in Action, Second Edition, published on October, 2010. It is being reproduced here by permission from Manning Publications. Manning publishes MEAP (Manning Early Access Program,) eBooks and pBooks. MEAPs are sold exclusively through Manning.com. All pBook purchases include free PDF, mobi and epub. When mobile formats become available all customers will be contacted and upgraded. Visit Manning.com for more information. [ Use promotional code ‘java40beat’ and get 40% discount on eBooks and pBooks]

Creating a Raw Image

Introduction

An image consists of a series of pixels. Each pixel has a color. The color value of the sequence of pixels can be stored in a byte array, and the byte array can be compressed, for instance, using zlib/flate compression. Figure 1 shows some images that were created byte per byte.

Figure 1 Images built using raw image data

Let’s have a look at the source code that was used to create these images.

We’re creating three images in listing 1. The first one has 256 × 1 pixels. The colorspace is DeviceGray (1 component), and we’re using 8 bits per component (#1). When we create the image data, we let the color value vary from 0 to 255. This produces the gradient from black to white in figure 1. (Note that we scaled the height of the image.)

For the second and third images, we use three components with 8 bits per component. This means that we’ll need 256 × 3 bytes to describe an image that consists of 256 pixels. We use the image data to create an image of 256 × 1 pixels (#2), and to create an image of 16 × 16 pixels. Note that this image uses the DeviceRGB color space. If you create an image with four components, you’re working in the DeviceCMYK color space. The getInstance() method used in listing 1 also accepts an extra parameter to define a transparency range.

What we’re doing manually in this example, is done automatically with GIF, PNG, BMP, and some TIFF images internally. These bytes are added to a zipped stream using the zlib/flate algorithm, except for some TIFF and PNG images that are CCITT-encoded.

Summary

In this article, we discussed creating an image manually or byte per byte. This operation is done automatically with GIF, PNG, BMP, and some TIFF images.

Comments

comments

Related posts:

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

About Us

Javabeat.net is a blog dedicated to Java/J2EE developers. This site focus on beginners and advanced developers. We constantly write fresh content on J2EE topics like Spring, Hibernate and wide variety of J2EE frameworks.