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 All pBook purchases include free PDF, mobi and epub. When mobile formats become available all customers will be contacted and upgraded. Visit for more information. [ Use promotional code ‘java40beat’ and get 40% discount on eBooks and pBooks ]

also read:

Creating a Raw Image


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.

Listing 1

(int i = 0; i < 256; i++) A
gradient[i] = (byte) i; A
Image img1 = Image.getInstance(256, 1, 1, 8, gradient); 1
img1.scaleAbsolute(256, 50);
byte cgradient[] = new byte[256 * 3]; B
for (int i = 0; i < 256; i++) {
cgradient[i * 3] = (byte) (255 - i); C
cgradient[i * 3 + 1] = (byte) (255 - i); D
cgradient[i * 3 + 2] = (byte) i; E
Image img2 = Image.getInstance(256, 1, 3, 8, cgradient); 2
img2.scaleAbsolute(256, 50);
Image img3 = Image.getInstance(16, 16, 3, 8, cgradient); 3
img3.scaleAbsolute(64, 64);

A Create image data
1 Create a DeviceGray/8 bpc image
B Create image data
C Red
D Green
E Blue
2 Create an RGB/8bpc image
3 Create an RGB/8bpc image

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.


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.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>