Skip to main content

TIFF ImageReader ImageWriter IllegalArgumentException: sourceRegion.x < 0!

2 replies [Last post]
nyholku
Offline
Joined: 2005-03-15

i
While working on my huge multires image building code I came created attached test code which probably exposes my ignorance. This should be straight forward, read in a tiff file, sub sample it, and write it out. But running the code produces IllegalArgumentException: sourceRegion.x < 0!

Also, if I comment out the sub sampling operation, then the code runs to completion but the result is bad, see:

input image
http://www.sparetimelabs.com/test.tif
output image
http://www.sparetimelabs.com/test-out.tif

If I further comment out the setTiling stuff then the code correctly reads/writes the image (but of course without the necessary sub sampling and tiling).

I guess I'm missing some parameters but after hours of googling the web and list archives I still do not have clue.

I'll include the complete stack trace and source below.

br Kusti

java.lang.IllegalArgumentException: sourceRegion.x < 0!
at javax.imageio.IIOParam.setSourceRegion(IIOParam.java:171)
at com.sun.media.imageioimpl.plugins.tiff.TIFFRenderedImage.read(TIFFRenderedImage.java:251)
at com.sun.media.imageioimpl.plugins.tiff.TIFFRenderedImage.getData(TIFFRenderedImage.java:243)
at javax.media.jai.RenderedImageAdapter.getData(RenderedImageAdapter.java:166)
at javax.media.jai.GeometricOpImage.computeTile(GeometricOpImage.java:676)
at com.sun.media.jai.util.SunTileScheduler.scheduleTile(SunTileScheduler.java:912)
at javax.media.jai.OpImage.getTile(OpImage.java:1139)
at javax.media.jai.PlanarImage.getData(PlanarImage.java:2098)
at javax.media.jai.RenderedOp.getData(RenderedOp.java:2287)
at com.sun.media.imageioimpl.plugins.tiff.TIFFImageWriter.writeTile(TIFFImageWriter.java:1754)
at com.sun.media.imageioimpl.plugins.tiff.TIFFImageWriter.write(TIFFImageWriter.java:2686)
at com.sun.media.imageioimpl.plugins.tiff.TIFFImageWriter.write(TIFFImageWriter.java:2417)
at renderer.SubSampleTest.main(SubSampleTest.java:56)

package renderer;

import java.io.*;
import javax.imageio.*;
import javax.imageio.stream.*;
import javax.media.jai.*;
import javax.imageio.metadata.*;

import com.sun.media.imageio.plugins.tiff.*;
import com.sun.media.jai.codec.*;
import java.util.*;
import java.awt.*;
import java.awt.image.*;
import java.awt.image.renderable.*;
import javax.imageio.metadata.*;

public class SubSampleTest {

public static void main(String args[]) {
try {
long t0 = System.nanoTime();

File sourceFile = new File("test.tif");
File destFile = new File("test-out.tif");

FileImageOutputStream outputStream = new FileImageOutputStream(destFile);
ImageWriter writer = ImageIO.getImageWritersByFormatName("tiff").next();

FileImageInputStream inputStream = new FileImageInputStream(sourceFile);
ImageReader reader = ImageIO.getImageReader(writer);
reader.setInput(inputStream);

IIOMetadata metadata = reader.getImageMetadata(0);

ImageReadParam readParam =reader.getDefaultReadParam();
RenderedImage inputImg=reader.readAsRenderedImage(0, readParam);

ParameterBlock pb = new ParameterBlock();
pb.addSource(inputImg);
pb.add(2).add(2);
pb.add(new float[] { 0.5F, 1.0F / 3.0F, 0.0F, -1.0F / 12.0F });
pb.add(Interpolation.getInstance(Interpolation.INTERP_BILINEAR));
inputImg = JAI.create("filteredsubsample", pb);

writer.setOutput(outputStream);

TIFFImageWriteParam writeParam = (TIFFImageWriteParam) writer.getDefaultWriteParam();
writeParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
writeParam.setCompressionType("PackBits");
writeParam.setTilingMode(ImageWriteParam.MODE_EXPLICIT);
writeParam.setTiling(128, 128, 0, 0);

IIOImage iioImage = new IIOImage(inputImg, null, metadata);
writer.write(null, iioImage, writeParam);

JAI.getDefaultInstance().getTileCache().flush();

writer.dispose();
outputStream.flush();
outputStream.close();

reader.dispose();
inputStream.flush();
inputStream.close();

System.out.println("sub image " + inputImg.getWidth() + "x" + inputImg.getHeight() + " time " + (System.nanoTime() - t0) / 1000000 / 1000.0 + " sec");

System.out.println("DONE!");
} catch (Throwable e) {
e.printStackTrace();
}
}
}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
bpb
Offline
Joined: 2004-06-23

Hi Kusti,

Sorry but which versions of things are you using? This code also fails for us but with a different message.

Brian

> i
> While working on my huge multires image building code
> I came created attached test code which probably
> exposes my ignorance. This should be straight
> forward, read in a tiff file, sub sample it, and
> write it out. But running the code produces
> IllegalArgumentException: sourceRegion.x < 0!
>
> Also, if I comment out the sub sampling operation,
> then the code runs to completion but the result is
> bad, see:
>
> input image
> http://www.sparetimelabs.com/test.tif
> output image
> http://www.sparetimelabs.com/test-out.tif
>
> If I further comment out the setTiling stuff then the
> code correctly reads/writes the image (but of course
> without the necessary sub sampling and tiling).
>
> I guess I'm missing some parameters but after hours
> of googling the web and list archives I still do not
> have clue.
>
>
> I'll include the complete stack trace and source
> below.
>
> br Kusti
>
>
> java.lang.IllegalArgumentException: sourceRegion.x
> < 0!
> at
> t
> javax.imageio.IIOParam.setSourceRegion(IIOParam.java:1
> 71)
> at
> t
> com.sun.media.imageioimpl.plugins.tiff.TIFFRenderedIma
> ge.read(TIFFRenderedImage.java:251)
> at
> t
> com.sun.media.imageioimpl.plugins.tiff.TIFFRenderedIma
> ge.getData(TIFFRenderedImage.java:243)
> at
> t
> javax.media.jai.RenderedImageAdapter.getData(RenderedI
> mageAdapter.java:166)
> at
> t
> javax.media.jai.GeometricOpImage.computeTile(Geometric
> OpImage.java:676)
> at
> t
> com.sun.media.jai.util.SunTileScheduler.scheduleTile(S
> unTileScheduler.java:912)
> at
> t javax.media.jai.OpImage.getTile(OpImage.java:1139)
> at
> t
> javax.media.jai.PlanarImage.getData(PlanarImage.java:2
> 098)
> at
> t
> javax.media.jai.RenderedOp.getData(RenderedOp.java:228
> 7)
> at
> t
> com.sun.media.imageioimpl.plugins.tiff.TIFFImageWriter
> .writeTile(TIFFImageWriter.java:1754)
> at
> t
> com.sun.media.imageioimpl.plugins.tiff.TIFFImageWriter
> .write(TIFFImageWriter.java:2686)
> at
> t
> com.sun.media.imageioimpl.plugins.tiff.TIFFImageWriter
> .write(TIFFImageWriter.java:2417)
> at
> t renderer.SubSampleTest.main(SubSampleTest.java:56)
>
>
> package renderer;
>
> import java.io.*;
> import javax.imageio.*;
> import javax.imageio.stream.*;
> import javax.media.jai.*;
> import javax.imageio.metadata.*;
>
> import com.sun.media.imageio.plugins.tiff.*;
> import com.sun.media.jai.codec.*;
> import java.util.*;
> import java.awt.*;
> import java.awt.image.*;
> import java.awt.image.renderable.*;
> import javax.imageio.metadata.*;
>
> public class SubSampleTest {
>
> public static void main(String args[]) {
> try {
> long t0 = System.nanoTime();
>
> File sourceFile = new File("test.tif");
> File destFile = new File("test-out.tif");
>
> FileImageOutputStream outputStream = new
> new FileImageOutputStream(destFile);
> ImageWriter writer =
> r =
> ImageIO.getImageWritersByFormatName("tiff").next();
>
> FileImageInputStream inputStream = new
> new FileImageInputStream(sourceFile);
> ImageReader reader =
> r = ImageIO.getImageReader(writer);
> reader.setInput(inputStream);
>
> IIOMetadata metadata =
> a = reader.getImageMetadata(0);
>
> ImageReadParam readParam
> ram =reader.getDefaultReadParam();
> RenderedImage
> age inputImg=reader.readAsRenderedImage(0,
> readParam);
>
> ParameterBlock pb = new ParameterBlock();
> pb.addSource(inputImg);
> pb.add(2).add(2);
> pb.add(new float[] { 0.5F, 1.0F / 3.0F, 0.0F,
> 0F, -1.0F / 12.0F });
> pb.add(Interpolation.getInstance(Interpolation.INTE
> RP_BILINEAR));
> inputImg = JAI.create("filteredsubsample", pb);
>
> writer.setOutput(outputStream);
>
> TIFFImageWriteParam writeParam =
> m = (TIFFImageWriteParam)
> writer.getDefaultWriteParam();
> writeParam.setCompressionMode(ImageWriteParam.MODE_
> EXPLICIT);
> writeParam.setCompressionType("PackBits");
> writeParam.setTilingMode(ImageWriteParam.MODE_EXPLI
> CIT);
> writeParam.setTiling(128, 128, 0, 0);
>
> IIOImage iioImage = new IIOImage(inputImg, null,
> ll, metadata);
> writer.write(null, iioImage, writeParam);
>
> JAI.getDefaultInstance().getTileCache().flush();
>
> writer.dispose();
> outputStream.flush();
> outputStream.close();
>
> reader.dispose();
> inputStream.flush();
> inputStream.close();
>
> System.out.println("sub image " +
> " + inputImg.getWidth() + "x" + inputImg.getHeight()
> + " time " + (System.nanoTime() - t0) / 1000000 /
> 1000.0 + " sec");
>
> System.out.println("DONE!");
> } catch (Throwable e) {
> e.printStackTrace();
> }
> }
> }

nyholku
Offline
Joined: 2005-03-15

Hi Brian,

for some reason my reply ended up in here:

http://forums.java.net/jive/thread.jspa?threadID=34043&tstart=0

br Kusti