Skip to main content

Trouble reading j2k image

1 reply [Last post]
mcolak72
Offline
Joined: 2006-10-19

For my project and also for learning I am trying to read j2k images. I can open and read j2k images. But I have some problem while getting a tile at highest resolution level. (when resolution level increases it begins spending much more time for getting a tile) It spends a lot of time while getting requested tile. My code is listed below. My test.jp2 file (55 MB) is lossy compressed and not tiled. Please help where is the problem. Thanks.

package JAI;

import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.image.*;
import java.awt.image.renderable.ParameterBlock;
import java.io.*;
import java.util.HashMap;

import javax.imageio.stream.FileImageInputStream;
import javax.media.jai.*;
import javax.swing.JComponent;
import javax.swing.JFrame;

import com.sun.media.imageio.plugins.jpeg2000.J2KImageReadParam;
import com.sun.media.imageioimpl.plugins.jpeg2000.J2KImageReaderCodecLib;
import com.sun.media.imageioimpl.plugins.jpeg2000.J2KImageReaderCodecLibSpi;

public class JAITest2 {
File inImg = new File("e:/data/test.jp2");
int tileWidth = 512;
int tileHeight = 512;
int maxLevel = 7;
int level = 7;

public JAITest2() {
FileImageInputStream stream = null;
ParameterBlockJAI pbj = null;
RenderedOp img = null;
ImageLayout layout = null;

try {
stream = new FileImageInputStream(inImg);

J2KImageReaderCodecLib reader0 = new J2KImageReaderCodecLib(new J2KImageReaderCodecLibSpi());

J2KImageReadParam J2KReadParam = new J2KImageReadParam();
J2KReadParam.setResolution(level);
J2KReadParam.setDecodingRate(Double.MAX_VALUE);

pbj = new ParameterBlockJAI("ImageRead");

reader0.setInput(stream);
layout = getLayout(new Rectangle(0, 0, reader0.getWidth(0),
reader0.getHeight(0)));

pbj.setParameter("Input", stream);
pbj.setParameter("ReadParam", J2KReadParam);
pbj.setParameter("Reader", reader0);
img = JAI.create("ImageRead", pbj,
layout);

} catch (FileNotFoundException e) {
e.printStackTrace(); ;
} catch (IOException e) {
e.printStackTrace();
System.exit(0);
}

RenderedOp imgOp = reformatImage(img,
new Dimension(tileWidth, tileHeight));

PlanarImage displayImage = imgOp.createInstance();

ImagePanel ip = new ImagePanel(displayImage);
JFrame window = new JFrame("JAI Sample Program");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.add(ip);
ip.repaint();
window.pack();
window.show();
}

public static void main(String[] args) {
JAITest2 test = new JAITest2();
}

private ImageLayout getLayout(Rectangle initialSize) {
if (level == maxLevel || level == -1) {
return new ImageLayout(initialSize.x, initialSize.y,
initialSize.width, initialSize.height);
}
int divisor = 1 << (maxLevel - level);
int x1 = (initialSize.x + divisor - 1) / divisor;
int y1 = (initialSize.y + divisor - 1) / divisor;
int x2 = (initialSize.x + initialSize.width + divisor - 1) / divisor;
int y2 = (initialSize.y + initialSize.height + divisor - 1) / divisor;
ImageLayout layout = new ImageLayout(x1, y1, x2 - x1, y2 - y1);
layout.setTileGridXOffset(0);
layout.setTileGridYOffset(0);
layout.setTileHeight(tileHeight);
layout.setTileWidth(tileWidth);
return layout;
}

public static RenderedOp reformatImage(PlanarImage img, Dimension tileDim) {
int tileWidth = tileDim.width;
int tileHeight = tileDim.height;
ImageLayout tileLayout = new ImageLayout(img);
tileLayout.setTileWidth(tileWidth);
tileLayout.setTileHeight(tileHeight);

HashMap map = new HashMap();
map.put(JAI.KEY_IMAGE_LAYOUT, tileLayout);
map.put(JAI.KEY_INTERPOLATION,
Interpolation.getInstance(Interpolation.INTERP_NEAREST));
RenderingHints tileHints = new RenderingHints(map);
ParameterBlock pb = new ParameterBlock();
pb.addSource(img);
return JAI.create("format", pb, tileHints);
}

protected class ImagePanel extends JComponent {
protected PlanarImage image;
protected int width, height;

public ImagePanel(PlanarImage img) {
image = img;
width = img.getWidth();
height = img.getHeight();
System.out.println("width:" + image.getWidth() + " height:" +
image.getHeight());
}

public void paintComponent(Graphics gc) {
Graphics2D g = (Graphics2D) gc;
for (int i = 0; i < 2; i++) {
Raster tile = image.getTile(0,0);
DataBuffer dataBuffer = tile.getDataBuffer();
WritableRaster wr = tile.createWritableRaster(image.
getSampleModel(),
dataBuffer, new Point(0, 0));
BufferedImage bi = new BufferedImage(image.getColorModel(), wr, false, null);
int xInTile = image.tileXToX(i);
int yInTile = image.tileYToY(0);
AffineTransform tx = AffineTransform.getTranslateInstance(
xInTile, yInTile);
g.drawRenderedImage(bi, tx);
}
}
}
}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
robert engels

If the source image is not tiled, it will be slow getting a tile...
(unless it is an uncompressed format).

On Feb 4, 2008, at 4:48 PM, jai-imageio@javadesktop.org wrote:

> For my project and also for learning I am trying to read j2k
> images. I can open and read j2k images. But I have some problem
> while getting a tile at highest resolution level. (when resolution
> level increases it begins spending much more time for getting a
> tile) It spends a lot of time while getting requested tile. My code
> is listed below. My test.jp2 file (55 MB) is lossy compressed and
> not tiled. Please help where is the problem. Thanks.
>
>
> package JAI;
>
> import java.awt.*;
> import java.awt.geom.AffineTransform;
> import java.awt.image.*;
> import java.awt.image.renderable.ParameterBlock;
> import java.io.*;
> import java.util.HashMap;
>
> import javax.imageio.stream.FileImageInputStream;
> import javax.media.jai.*;
> import javax.swing.JComponent;
> import javax.swing.JFrame;
>
> import com.sun.media.imageio.plugins.jpeg2000.J2KImageReadParam;
> import
> com.sun.media.imageioimpl.plugins.jpeg2000.J2KImageReaderCodecLib;
> import
> com.sun.media.imageioimpl.plugins.jpeg2000.J2KImageReaderCodecLibSpi;
>
> public class JAITest2 {
> File inImg = new File("e:/data/test.jp2");
> int tileWidth = 512;
> int tileHeight = 512;
> int maxLevel = 7;
> int level = 7;
>
> public JAITest2() {
> FileImageInputStream stream = null;
> ParameterBlockJAI pbj = null;
> RenderedOp img = null;
> ImageLayout layout = null;
>
>
> try {
> stream = new FileImageInputStream(inImg);
>
> J2KImageReaderCodecLib reader0 = new
> J2KImageReaderCodecLib(new J2KImageReaderCodecLibSpi());
>
> J2KImageReadParam J2KReadParam = new J2KImageReadParam();
> J2KReadParam.setResolution(level);
> J2KReadParam.setDecodingRate(Double.MAX_VALUE);
>
> pbj = new ParameterBlockJAI("ImageRead");
>
> reader0.setInput(stream);
> layout = getLayout(new Rectangle(0, 0, reader0.getWidth
> (0),
> reader0.getHeight(0)));
>
> pbj.setParameter("Input", stream);
> pbj.setParameter("ReadParam", J2KReadParam);
> pbj.setParameter("Reader", reader0);
> img = JAI.create("ImageRead", pbj,
> layout);
>
> } catch (FileNotFoundException e) {
> e.printStackTrace(); ;
> } catch (IOException e) {
> e.printStackTrace();
> System.exit(0);
> }
>
> RenderedOp imgOp = reformatImage(img,
> new Dimension(tileWidth,
> tileHeight));
>
> PlanarImage displayImage = imgOp.createInstance();
>
> ImagePanel ip = new ImagePanel(displayImage);
> JFrame window = new JFrame("JAI Sample Program");
> window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
> window.add(ip);
> ip.repaint();
> window.pack();
> window.show();
> }
>
> public static void main(String[] args) {
> JAITest2 test = new JAITest2();
> }
>
> private ImageLayout getLayout(Rectangle initialSize) {
> if (level == maxLevel || level == -1) {
> return new ImageLayout(initialSize.x, initialSize.y,
> initialSize.width,
> initialSize.height);
> }
> int divisor = 1 << (maxLevel - level);
> int x1 = (initialSize.x + divisor - 1) / divisor;
> int y1 = (initialSize.y + divisor - 1) / divisor;
> int x2 = (initialSize.x + initialSize.width + divisor -
> 1) / divisor;
> int y2 = (initialSize.y + initialSize.height + divisor -
> 1) / divisor;
> ImageLayout layout = new ImageLayout(x1, y1, x2 - x1, y2 -
> y1);
> layout.setTileGridXOffset(0);
> layout.setTileGridYOffset(0);
> layout.setTileHeight(tileHeight);
> layout.setTileWidth(tileWidth);
> return layout;
> }
>
> public static RenderedOp reformatImage(PlanarImage img,
> Dimension tileDim) {
> int tileWidth = tileDim.width;
> int tileHeight = tileDim.height;
> ImageLayout tileLayout = new ImageLayout(img);
> tileLayout.setTileWidth(tileWidth);
> tileLayout.setTileHeight(tileHeight);
>
> HashMap map = new HashMap();
> map.put(JAI.KEY_IMAGE_LAYOUT, tileLayout);
> map.put(JAI.KEY_INTERPOLATION,
> Interpolation.getInstance
> (Interpolation.INTERP_NEAREST));
> RenderingHints tileHints = new RenderingHints(map);
> ParameterBlock pb = new ParameterBlock();
> pb.addSource(img);
> return JAI.create("format", pb, tileHints);
> }
>
> protected class ImagePanel extends JComponent {
> protected PlanarImage image;
> protected int width, height;
>
> public ImagePanel(PlanarImage img) {
> image = img;
> width = img.getWidth();
> height = img.getHeight();
> System.out.println("width:" + image.getWidth() + "
> height:" +
> image.getHeight());
> }
>
> public void paintComponent(Graphics gc) {
> Graphics2D g = (Graphics2D) gc;
> for (int i = 0; i < 2; i++) {
> Raster tile = image.getTile(0,0);
> DataBuffer dataBuffer = tile.getDataBuffer();
> WritableRaster wr = tile.createWritableRaster(image.
> getSampleModel(),
> dataBuffer, new Point(0, 0));
> BufferedImage bi = new BufferedImage
> (image.getColorModel(), wr, false, null);
> int xInTile = image.tileXToX(i);
> int yInTile = image.tileYToY(0);
> AffineTransform tx =
> AffineTransform.getTranslateInstance(
> xInTile, yInTile);
> g.drawRenderedImage(bi, tx);
> }
> }
> }
> }
> [Message sent by forum member 'mcolak72' (mcolak72)]
>
> http://forums.java.net/jive/thread.jspa?messageID=257334
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
> For additional commands, e-mail: interest-help@jai-
> imageio.dev.java.net
>

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
For additional commands, e-mail: interest-help@jai-imageio.dev.java.net