Leave That Thing Alone Blog

Opening/Viewing multipage TIFFs with ColdFusion 8

Here's a quick example of how to open and/or view a multipage TIFF with ColdFusion 8. This example requires no external Java libraries.

The first step is to open the multi-page tiff and using the JAI ImageCodec we're able to determine the number of pages in that tif and create an array of the pages.


<cfscript>
    //set TIFF file name

    tifFileName = expandpath('samples\patent.tif');
    //create FileSeekableStream

    ss = createObject("Java","com.sun.media.jai.codec.FileSeekableStream").init(tifFileName);
    //create JAI ImageDecoder

    decoder = createObject("Java","com.sun.media.jai.codec.ImageCodec").createImageDecoder("tiff", ss, JavaCast("null",""));
    //create array

    tifPageArray = arrayNew(1);
    //loop through pages of tiff add to array

    for (i=0;i
<decoder.getNumPages();i++) {
        //add each to array (com.sun.media.jai.codecimpl.TIFFImage)

        tifPageArray[i+1] = decoder.decodeAsRenderedImage(i);
    }
</cfscript>

At this point we have array of tiff images (of type com.sun.media.jai.codecimpl.TIFFImage). Based on your needs your code will probably need to change after this. To be able to easliy 'use' these TIFF pages in ColdFusion we need to convert them to the cfimage format. One of the great features about the ColdFusion 'imageNew' tag is that it accepts BufferedImage as an input

So to move from TIFF to cfimage we do this:
TIFF -> PlanarImage -> BufferedImage -> cfimage

Here's some code to loop though all the pages and write a JPG for each page in the mulitpage tiff:


<cfscript>
    //loop through all tiff pages

    for (i=0;i
<decoder.getNumPages();i++) {
        //create PlanarImage

        pImg = createObject("Java","javax.media.jai.PlanarImage").wrapRenderedImage(tifPageArray[i+1]);
        //get buffered image

        bufferedImage = pImg.getAsBufferedImage();
        //use CF image tage to create CF image from bufferedImage

        myImage = imageNew(bufferedImage);
        //write image

        imageWrite(myImage,"page#i+1#.jpg");
    }
</cfscript>


You may want to alter the code above as writing TIFF pages to JPGs might not be what you need. After 'imageNew(bufferedImage)' we have a cfimage so you are able to do any ColdFusion 8 image function on the TIFF page.

Update:
Multi-Page Tiff support has now been added to ImageUtils

Related Blog Entries

Comments

Adrian J. Moreno's Gravatar Excellent post! By chance is there a way to convert the TIFF images to PDF? I've done that with GhostScript using a simple call to the command line. I wonder if this could be done by going from cfimage to cfdocument or cfpdf.
# Posted By Adrian J. Moreno | 4/7/08 8:06 AM
Seth's Gravatar @Adrian

Here's a way to create a PDF once you've got the array of TIFF pages. This is surely not the best approach as a large number of pages may cause memory issues:

<cfdocument format="PDF">
   <cfloop from="1" to="#decoder.getNumPages()#" index="i">
      <cfscript>
         //create PlanarImage
         pImg = createObject("Java","javax.media.jai.PlanarImage").wrapRenderedImage(tifPageArray[i]);
         //get buffered image
         bufferedImage = pImg.getAsBufferedImage();
         //use CF image tage to create CF image from bufferedImage
         myImage = imageNew(bufferedImage);
      </cfscript>
      <cfdocumentsection>
         <cfimage action="writetobrowser" source="#myImage#" />
      </cfdocumentsection>
   </cfloop>
</cfdocument>
# Posted By Seth | 4/7/08 9:42 AM
Patrick's Gravatar Hi Seth,
nice example! There is an updated Version of imageUtils.cfc available on RIAForge.org, which also supports multipage tiffs ;)
# Posted By Patrick | 4/16/08 5:02 AM
Seth's Gravatar @Patrick

Yeah i see that just got added to imageUtils.cfc, it has some nice features. More complete than this example which is great.
# Posted By Seth | 4/16/08 6:55 AM
Gary Funk's Gravatar I've looked at the code and it seems that I can follow it pretty well. I have three features that I want to add. The first is to TiffSplit(). I want to take a 500+ multi-page tiff and split it into 100 plag files.

Then I want a TiffJoin() where I can have several single page tiffs and put them all into one file.

Finally, a PDFToTiff() where a multi-page PDF file can be put into a multi-page tiff.

Items one and twop seem like they might be pretty simple. On item three, I have no idea how simple or not this might be able to do.

Can you give an idea on the best way to code item one and two?
# Posted By Gary Funk | 6/2/08 1:47 PM
charlie chisholm's Gravatar I am trying to use the example given on this page, but am working with the handicap of not having a clue what I am doing. I managed to download jai_codec-1.1.3-alpha.jar
onto our coldfusion server in c:\program files\java\jre6\bin folder and extracted it using jar.exe. Seemed to be ok, when I tryed to run your code, I got
"Error Occurred While Processing Request
Object Instantiation Exception.
An exception occurred when instantiating a Java object. The class must not be an interface or an abstract class. Error: '' which I suspect indicates the class is not being found.
Haven't a clue what I was suppose to do that I didn't. Any insight would be appriciated. Thanks ether way.
# Posted By charlie chisholm | 6/13/11 4:43 PM