P
Patrick Boulay
Hi,
I'm trying to open a big tiff, 3400x4680, 16 million colors. I do this
operation and I get out of memory after 5 min.
- Open the file
- Resize the file to 1700x2340
- Converting to gray scale
- Converting to a bilevel format
- Encode this file to a tif, 200dpi, to an ouput stream
Look my code:
*********************************************
// Loading the file
PlanarImage img = JAI.create("fileload", f.getPath());
....
//Resizing the file
ParameterBlock parameterBlock = new ParameterBlock();
parameterBlock.addSource(img);
parameterBlock.add(scale);
parameterBlock.add(scale);
parameterBlock.add(0.0f);
parameterBlock.add(0.0f);
parameterBlock.add(Interpolation.getInstance(Interpolation.INTERP_BILINEAR));
Planar newImg = JAI.create("scale", parameterBlock);
// Get the gray image
PlanarImage imgGray = colorToGray(newImg);
// Convert to a bilevel image
PlanarImage newImg = toBilevel(imgGray);
// Set parameter for the new tif file
TIFFEncodeParam params = new TIFFEncodeParam();
params.setCompression(TIFFEncodeParam.COMPRESSION_GROUP3_2D);
TIFFField[] extras = new TIFFField[3];
extras[0] = new TIFFField(282, TIFFField.TIFF_RATIONAL, 1, (Object)new
long[][] {{(long)200, (long)1},{(long)0 ,(long)0}});
extras[1] = new TIFFField(283, TIFFField.TIFF_RATIONAL, 1, (Object)new
long[][] {{(long)200, (long)1},{(long)0 ,(long)0}});
extras[2] = new TIFFField(296, TIFFField.TIFF_SHORT, 1, (Object) new
char[] {2});
params.setExtraFields(extras);
//Set the encoder to my outputstream (DataOutputStream)
ImageEncoder encoder = ImageCodec.createImageEncoder("TIFF", out,
params);
encoder.encode(newImg);
public PlanarImage colorToGray(PlanarImage i)
{
double[][] matrix = { {0.114D, 0.587D, 0.299D, 0.0D} };
if (i.getSampleModel().getNumBands() == 3)
{
ParameterBlock pb = new ParameterBlock();
pb.addSource(i);
pb.add(matrix);
return ( (PlanarImage) JAI.create("bandcombine", pb, null));
}
return i;
}
public PlanarImage toBilevel(PlanarImage src) {
PlanarImage dst =
JAI.create("binarize", src, new Double(200));
return dst;
}
**********************************************
Everything work fine with smaller file... but with a big big file with
16million of colors, I got a out of memory in that operation
"encoder.encode(newImg);"
Anyone can help me?
Patrick
I'm trying to open a big tiff, 3400x4680, 16 million colors. I do this
operation and I get out of memory after 5 min.
- Open the file
- Resize the file to 1700x2340
- Converting to gray scale
- Converting to a bilevel format
- Encode this file to a tif, 200dpi, to an ouput stream
Look my code:
*********************************************
// Loading the file
PlanarImage img = JAI.create("fileload", f.getPath());
....
//Resizing the file
ParameterBlock parameterBlock = new ParameterBlock();
parameterBlock.addSource(img);
parameterBlock.add(scale);
parameterBlock.add(scale);
parameterBlock.add(0.0f);
parameterBlock.add(0.0f);
parameterBlock.add(Interpolation.getInstance(Interpolation.INTERP_BILINEAR));
Planar newImg = JAI.create("scale", parameterBlock);
// Get the gray image
PlanarImage imgGray = colorToGray(newImg);
// Convert to a bilevel image
PlanarImage newImg = toBilevel(imgGray);
// Set parameter for the new tif file
TIFFEncodeParam params = new TIFFEncodeParam();
params.setCompression(TIFFEncodeParam.COMPRESSION_GROUP3_2D);
TIFFField[] extras = new TIFFField[3];
extras[0] = new TIFFField(282, TIFFField.TIFF_RATIONAL, 1, (Object)new
long[][] {{(long)200, (long)1},{(long)0 ,(long)0}});
extras[1] = new TIFFField(283, TIFFField.TIFF_RATIONAL, 1, (Object)new
long[][] {{(long)200, (long)1},{(long)0 ,(long)0}});
extras[2] = new TIFFField(296, TIFFField.TIFF_SHORT, 1, (Object) new
char[] {2});
params.setExtraFields(extras);
//Set the encoder to my outputstream (DataOutputStream)
ImageEncoder encoder = ImageCodec.createImageEncoder("TIFF", out,
params);
encoder.encode(newImg);
public PlanarImage colorToGray(PlanarImage i)
{
double[][] matrix = { {0.114D, 0.587D, 0.299D, 0.0D} };
if (i.getSampleModel().getNumBands() == 3)
{
ParameterBlock pb = new ParameterBlock();
pb.addSource(i);
pb.add(matrix);
return ( (PlanarImage) JAI.create("bandcombine", pb, null));
}
return i;
}
public PlanarImage toBilevel(PlanarImage src) {
PlanarImage dst =
JAI.create("binarize", src, new Double(200));
return dst;
}
**********************************************
Everything work fine with smaller file... but with a big big file with
16million of colors, I got a out of memory in that operation
"encoder.encode(newImg);"
Anyone can help me?
Patrick