Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / Swing

Java 48 Bit TIFF Image Processing

0.00/5 (No votes)
10 Sep 2012CPOL2 min read 26.9K   1.1K  
48 Bit Image Processing using JAI(Java Advanced Imaging Library)

Introduction

This article explains in detail about how 48 bit TIFF image can be processed using Java, processing in the sense like modifying each pixels RGB component in a 48 bit image and saving the resulting image.

Background  

TIFF Image can be processed easily in Java using the JAI (Java Advanced Imaging API). There are lot of examples around how can a 24 bit image (each component in RGB is of 8 bit) can be processed. But there is no examples or articles about how we can process 48 bit image (each component in RGB is of 16 bit) can be processed.  

Usage 

TIFF Images produces large images and used where loss of quality is not acceptable like scientific image processing, medical imaging, astronomical images, etc. By using the provided code it will be easier to manipulate high quality TIFF images.

Using the code   

This code takes several 48 bit 1 Page TIFF Image ,extracts the Red, Green, and Blue component from each pixel of all images. Then it computes the average for each component, recreates the pixel and produces new image.

Sample image and output below,

(+)/2=

All the images is of exact same size and looks similar and has some variance in the pixel colors.

Reading the TIFF image as a Raster

The below snippet reads the TIFF image as raster which in turn will be used to read the pixels from it.

Java
File file = new File(fileName.get(i));
 
SeekableStream s = new FileSeekableStream(file);
TIFFDecodeParam param = null;

/*
 * ImageDecoder is from com.sun.media.jai.codec.ImageDecoder package
 */
ImageDecoder dec = ImageCodec.createImageDecoder("tiff", s, param);
raster[i] = dec.decodeAsRaster();

Create a Writable Raster from the source image

The below code creates a Writable Raster using one of the source image attributes.

Java
if (wRaster == null) { // Create a writable raster and get the color
    // model.We'll use this to write the image

    cm = image.getColorModel();
    wRaster = image.getData().createCompatibleWritableRaster();
}

Getting the Pixel and Computing the average and Update the Writable Raster

The below snipped gets each pixels RGB component,computes the average and then updates the writable raster with the calculated average.

Java
int w = raster[0].getWidth(), h = raster[0].getHeight();
int averagePixel[][][] = new int[w][h][3];
for (int i = 0; i < totalFiles; i++) {
    for (int width = 0; width < w; width++) {
        for (int height = 0; height < h; height++) {
            int[] pixelA = null;

            pixelA = raster[i].getPixel(width, height, pixelA);

            averagePixel[width][height][0] += pixelA[0];
            averagePixel[width][height][1] += pixelA[1];
            averagePixel[width][height][2] += pixelA[2];

            if (i == totalFiles - 1) // update the raster while
                         // processing last file
            {
                averagePixel[width][height][0] /= totalFiles;
                averagePixel[width][height][1] /= totalFiles;
                averagePixel[width][height][2] /= totalFiles;
                wRaster.setPixel(width, height,
                        averagePixel[width][height]);
            }

        }
    }
}

Create the Resulting TIFF Image  

Below code saves the Raster to the TIFF image.

Java
File file = new File(directoryName + "\\Output_"
                + System.currentTimeMillis() + ".tiff");
FileOutputStream fileoutput = new FileOutputStream(file);
TIFFEncodeParam encParam = null;
ImageEncoder enc = ImageCodec.createImageEncoder("tiff", fileoutput,
    encParam);

enc.encode(wRaster, cm);
fileoutput.close();

Points of Interest  

This is just an example how 48 bit image can be accessed, processed, and re-created. With this as starter step, we can do advance processing of the image like:

  • Finding the variance
  • Working with alpha components
  • Processing different channels/bands of the image
  • Processing other size images

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)