• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

Creating a Raw Image in iText

April 23, 2011 //  by Krishna Srinivasan//  Leave a Comment

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 ]

also read:

  • Java Tutorials
  • Java EE Tutorials
  • Design Patterns Tutorials
  • Java File IO Tutorials

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.

Listing 1 RawImage.java

[code lang=”java”](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);
document.add(img1);
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);
document.add(img2);
Image img3 = Image.getInstance(16, 16, 3, 8, cgradient); 3
img3.scaleAbsolute(64, 64);
document.add(img3);

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[/code]

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.

Category: JavaTag: iText, PDF

About Krishna Srinivasan

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.

Previous Post: « Dealing with Directories and Directory Trees in Java 7.0
Next Post: JavaScript Communication between HTML and PDF in iText »

Reader Interactions

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

np.zeros

A Complete Guide To NumPy Functions in Python For Beginners

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact