Skip to main content

Single stripped multi-page tiff file

2 replies [Last post]
ssubra
Offline
Joined: 2008-05-28
Points: 0

Hi All,

I tried to create multi-page tiff file using jai and it worked fine.
But the output file is not single striped, even though i am specifying RowsPerStrip is equal to image length.
If anybody know the solution for this, please help me.

Thanks,
sassubra

Source Code:

import java.awt.image.RenderedImage;
import java.awt.image.renderable.ParameterBlock;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Vector;

import javax.media.jai.InterpolationNearest;
import javax.media.jai.JAI;
import javax.media.jai.PlanarImage;
import javax.media.jai.RenderedOp;

import com.sun.media.jai.codec.ImageCodec;
import com.sun.media.jai.codec.ImageEncoder;
import com.sun.media.jai.codec.TIFFEncodeParam;
import com.sun.media.jai.codec.TIFFField;

public class Multipagetiff {

public static PlanarImage readAsPlanarImage(String filename) {
RenderedOp op = JAI.create("fileload", filename);
ParameterBlock pb1 = new ParameterBlock();
pb1.addSource(op);
pb1.add(1700F / op.getWidth());
pb1.add(2200F / op.getHeight());
pb1.add(0.0F);
pb1.add(0.0F);
pb1.add(new InterpolationNearest());
return JAI.create("scale", pb1, null);
}

public static void saveAsMultipageTIFF(RenderedImage[] image, String filename)
throws java.io.IOException {
OutputStream out = new FileOutputStream(filename);
TIFFEncodeParam param = new TIFFEncodeParam();
param.setCompression(TIFFEncodeParam.COMPRESSION_GROUP4);
param.setLittleEndian(true);
param.setWriteTiled(false);

TIFFField[] fields = new TIFFField[7];
// PhotometricInterpretation
TIFFField fieldPhotoInter = new TIFFField(262, TIFFField.TIFF_SHORT, 1,
(Object) new char[] { 0 });
fields[0] = fieldPhotoInter;

// ResolutionUnit
TIFFField fieldResUnit = new TIFFField(296, TIFFField.TIFF_SHORT, 1,
(Object) new char[] { 2 });
fields[1] = fieldResUnit;

// XResolution
TIFFField fieldXRes = new TIFFField(282, TIFFField.TIFF_RATIONAL, 1,
(Object) new long[][] { { 200, 1 } });
fields[2] = fieldXRes;

// YResolution
TIFFField fieldYRes = new TIFFField(283, TIFFField.TIFF_RATIONAL, 1,
(Object) new long[][] { { 200, 1 } });
fields[3] = fieldYRes;

// BitsPerSample
TIFFField fieldBitSample = new TIFFField(258, TIFFField.TIFF_SHORT, 1,
(Object) new char[] { 1 });
fields[4] = fieldBitSample;

// FilOrder
TIFFField fieldFillOrder = new TIFFField(266, TIFFField.TIFF_SHORT, 1,
(Object) new char[] { 1 });
fields[5] = fieldFillOrder;

// RowsPerStrip - Single Stripped
TIFFField fieldRowsStrip = new TIFFField(278, TIFFField.TIFF_LONG, 1,
(Object) new long[] { 2200 });
fields[6] = fieldRowsStrip;

param.setExtraFields(fields);
ImageEncoder encoder = ImageCodec.createImageEncoder("TIFF", out, param);
Vector vector = new Vector();
for (int i = 1; i < image.length; i++) {
vector.add(image[i]);
}
param.setExtraImages(vector.iterator());
encoder.encode(image[0]);
out.close();
}

public static void createMultipageTiff(File[] files, String outFileName) {
RenderedImage image[] = new PlanarImage[files.length];
for (int i = 0; i < files.length; i++) {
image[i] = readAsPlanarImage(files[i].getAbsolutePath());
}
try {
saveAsMultipageTIFF(image, outFileName);
} catch (Exception e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
File dir = new File("sample");
if (dir.isDirectory()) {
File[] files = dir.listFiles();
long time1 = System.currentTimeMillis();
Multipagetiff.createMultipageTiff(files, "sample.tif");
long time2 = System.currentTimeMillis();
System.out.println("Time : " + String.valueOf(time2 - time1));
} else {
System.err.println("sample is not a directory");
}
}
}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
ssubra
Offline
Joined: 2008-05-28
Points: 0

Source code:
-------------------
import java.awt.image.RenderedImage;
import java.awt.image.renderable.ParameterBlock;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.Vector;

import javax.media.jai.InterpolationNearest;
import javax.media.jai.JAI;
import javax.media.jai.PlanarImage;
import javax.media.jai.RenderedOp;

import com.sun.media.jai.codec.ImageCodec;
import com.sun.media.jai.codec.ImageEncoder;
import com.sun.media.jai.codec.TIFFEncodeParam;
import com.sun.media.jai.codec.TIFFField;

public class Multipagetiff {

public static PlanarImage readAsPlanarImage(String filename) {
RenderedOp op = JAI.create("fileload", filename);
ParameterBlock pb1 = new ParameterBlock();
pb1.addSource(op);
pb1.add(1700F / op.getWidth());
pb1.add(2200F / op.getHeight());
pb1.add(0.0F);
pb1.add(0.0F);
pb1.add(new InterpolationNearest());
return JAI.create("scale", pb1, null);
}

public static void saveAsMultipageTIFF(RenderedImage[] image, String filename)
throws java.io.IOException {
OutputStream out = new FileOutputStream(filename);
TIFFEncodeParam param = new TIFFEncodeParam();
param.setCompression(TIFFEncodeParam.COMPRESSION_GROUP4);
param.setLittleEndian(true);
param.setWriteTiled(false);

TIFFField[] fields = new TIFFField[7];
// PhotometricInterpretation
TIFFField fieldPhotoInter = new TIFFField(262, TIFFField.TIFF_SHORT, 1,
(Object) new char[] { 0 });
fields[0] = fieldPhotoInter;

// ResolutionUnit
TIFFField fieldResUnit = new TIFFField(296, TIFFField.TIFF_SHORT, 1,
(Object) new char[] { 2 });
fields[1] = fieldResUnit;

// XResolution
TIFFField fieldXRes = new TIFFField(282, TIFFField.TIFF_RATIONAL, 1,
(Object) new long[][] { { 200, 1 } });
fields[2] = fieldXRes;

// YResolution
TIFFField fieldYRes = new TIFFField(283, TIFFField.TIFF_RATIONAL, 1,
(Object) new long[][] { { 200, 1 } });
fields[3] = fieldYRes;

// BitsPerSample
TIFFField fieldBitSample = new TIFFField(258, TIFFField.TIFF_SHORT, 1,
(Object) new char[] { 1 });
fields[4] = fieldBitSample;

// FilOrder
TIFFField fieldFillOrder = new TIFFField(266, TIFFField.TIFF_SHORT, 1,
(Object) new char[] { 1 });
fields[5] = fieldFillOrder;

// RowsPerStrip
TIFFField fieldRowsStrip = new TIFFField(278, TIFFField.TIFF_LONG, 1,
(Object) new long[] { 2200 });
fields[6] = fieldRowsStrip;

param.setExtraFields(fields);
ImageEncoder encoder = ImageCodec.createImageEncoder("TIFF", out, param);
Vector vector = new Vector();
for (int i = 1; i < image.length; i++) {
vector.add(image[i]);
}
param.setExtraImages(vector.iterator());
encoder.encode(image[0]);
out.close();
}

public static void createMultipageTiff(File[] files, String outFileName) {
RenderedImage image[] = new PlanarImage[files.length];
for (int i = 0; i < files.length; i++) {
image[i] = readAsPlanarImage(files[i].getAbsolutePath());
}
try {
saveAsMultipageTIFF(image, outFileName);
} catch (Exception e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
File dir = new File("sample");
if (dir.isDirectory()) {
File[] files = dir.listFiles();
long time1 = System.currentTimeMillis();
Multipagetiff.createMultipageTiff(files, "sample.tif");
long time2 = System.currentTimeMillis();
System.out.println("Time : " + String.valueOf(time2 - time1));
} else {
System.err.println("sample is not a directory");
}
}
}

bpb
Offline
Joined: 2004-06-23
Points: 0

Firstly, this is a JAI question (https://jai.dev.java.net) not one for JAI Image I/O Tools (https://jai-imageio.dev.java.net).

Secondly, I don't see where you've called this method:

http://download.java.net/media/jai/javadoc/1.1.3/jai-apidocs/com/sun/media/jai/codec/TIFFEncodeParam.html#setTileSize(int,%20int)

Brian

> Source code:
> -------------------
> import java.awt.image.RenderedImage;
> import java.awt.image.renderable.ParameterBlock;
> import java.io.File;
> import java.io.FileOutputStream;
> import java.io.OutputStream;
> import java.util.Vector;
>
> import javax.media.jai.InterpolationNearest;
> import javax.media.jai.JAI;
> import javax.media.jai.PlanarImage;
> import javax.media.jai.RenderedOp;
>
> import com.sun.media.jai.codec.ImageCodec;
> import com.sun.media.jai.codec.ImageEncoder;
> import com.sun.media.jai.codec.TIFFEncodeParam;
> import com.sun.media.jai.codec.TIFFField;
>
> public class Multipagetiff {
>
> public static PlanarImage readAsPlanarImage(String
> g filename) {
> RenderedOp op = JAI.create("fileload", filename);
> ParameterBlock pb1 = new ParameterBlock();
> pb1.addSource(op);
> pb1.add(1700F / op.getWidth());
> pb1.add(2200F / op.getHeight());
> pb1.add(0.0F);
> pb1.add(0.0F);
> pb1.add(new InterpolationNearest());
> return JAI.create("scale", pb1, null);
> }
>
> public static void
> d saveAsMultipageTIFF(RenderedImage[] image, String
> filename)
> throws java.io.IOException {
> OutputStream out = new FileOutputStream(filename);
> TIFFEncodeParam param = new TIFFEncodeParam();
> param.setCompression(TIFFEncodeParam.COMPRESSION_GRO
> UP4);
> param.setLittleEndian(true);
> param.setWriteTiled(false);
>
>
> TIFFField[] fields = new TIFFField[7];
> // PhotometricInterpretation
> TIFFField fieldPhotoInter = new TIFFField(262,
> 2, TIFFField.TIFF_SHORT, 1,
> (Object) new char[] { 0 });
> fields[0] = fieldPhotoInter;
>
> // ResolutionUnit
> TIFFField fieldResUnit = new TIFFField(296,
> 6, TIFFField.TIFF_SHORT, 1,
> (Object) new char[] { 2 });
> fields[1] = fieldResUnit;
>
> // XResolution
> TIFFField fieldXRes = new TIFFField(282,
> 2, TIFFField.TIFF_RATIONAL, 1,
> (Object) new long[][] { { 200, 1 } });
> fields[2] = fieldXRes;
>
> // YResolution
> TIFFField fieldYRes = new TIFFField(283,
> 3, TIFFField.TIFF_RATIONAL, 1,
> (Object) new long[][] { { 200, 1 } });
> fields[3] = fieldYRes;
>
> // BitsPerSample
> TIFFField fieldBitSample = new TIFFField(258,
> 8, TIFFField.TIFF_SHORT, 1,
> (Object) new char[] { 1 });
> fields[4] = fieldBitSample;
>
> // FilOrder
> TIFFField fieldFillOrder = new TIFFField(266,
> 6, TIFFField.TIFF_SHORT, 1,
> (Object) new char[] { 1 });
> fields[5] = fieldFillOrder;
>
> // RowsPerStrip
> TIFFField fieldRowsStrip = new TIFFField(278,
> 8, TIFFField.TIFF_LONG, 1,
> (Object) new long[] { 2200 });
> fields[6] = fieldRowsStrip;
>
> param.setExtraFields(fields);
> ImageEncoder encoder =
> = ImageCodec.createImageEncoder("TIFF", out, param);
> Vector vector = new Vector();
> for (int i = 1; i < image.length; i++) {
> vector.add(image[i]);
> }
> param.setExtraImages(vector.iterator());
> encoder.encode(image[0]);
> out.close();
> }
>
> public static void createMultipageTiff(File[] files,
> , String outFileName) {
> RenderedImage image[] = new
> ew PlanarImage[files.length];
> for (int i = 0; i < files.length; i++) {
> image[i] =
> ] = readAsPlanarImage(files[i].getAbsolutePath());
> }
> try {
> saveAsMultipageTIFF(image, outFileName);
> } catch (Exception e) {
> e.printStackTrace();
> }
> }
>
> public static void main(String[] args) {
> File dir = new File("sample");
> if (dir.isDirectory()) {
> File[] files = dir.listFiles();
> long time1 = System.currentTimeMillis();
> Multipagetiff.createMultipageTiff(files,
> es, "sample.tif");
> long time2 = System.currentTimeMillis();
> System.out.println("Time : " +
> " + String.valueOf(time2 - time1));
> } else {
> System.err.println("sample is not a directory");
> }
> }
> }