Skip to main content

J2KImageReadParam: setDestination and setDestinationType conflict?

2 replies [Last post]
tomgroup
Offline
Joined: 2010-09-05
Points: 0

Hi,

I am trying to decode a jpeg 2000 image. I use ImageIO with the jai-imageio plugins to support this and other formats.

Because all my images are the same size and I have to read a lot of images and process them in turn I thought it would be wise to use the "setDestination" method of the J2KImageReadParam class. I might avoid creating many instances of the BufferedImage class and therefore have more control over the memory usage in my application.

Just to show you how I proceeded, here is a code snippet:

--- Begin snippet ---

int width = 400;
int height = 400;
String filename = "data/block.jp2"; // <-- jpeg 2000 file I am trying to read

BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

J2KImageReadParam p = new J2KImageReadParam();
p.setDestination(img);
p.setDestinationOffset(new Point(0,0));

ImageReader reader = ImageIO.getImageReadersByFormatName("jpeg2000").next(); // plugins load correctly, no problems here
reader.setInput(ImageIO.createImageInputStream(new FileInputStream(filename)));

BufferedImage img2 = reader.read(0, p);

--- End of snippet ---

Unfortunately it appears that img2 is not equal to img and thus a new BufferedImage was allocated. I want to avoid this. I thought I had to call "setDestination" on the J2KImageReadParam object, right?

I browsed the source code for the jai-imageio-core project and think I might have found the cause of this problem. I used a debugging tool to step through the involved code. Consider the java package com.sun.media.imageioimpl.plugins.jpeg2000. At some point the J2KImageReader creates some J2KImageReadParamJava object. Now if you open the source of J2KImageReadParamJava you can see the following code (I have added some annotations)

--- Begin snippet ---

public J2KImageReadParamJava(ImageReadParam param) {
super();

// Generic settings.
if(param.hasController()) {
setController(param.getController());
}
setSourceRegion(param.getSourceRegion());
setSourceBands(param.getSourceBands());
setDestinationBands(param.getDestinationBands());
setDestination(param.getDestination()); // <-- This is fine, just what we would expect

setDestinationOffset(param.getDestinationOffset());
setSourceSubsampling(param.getSourceXSubsampling(),
param.getSourceYSubsampling(),
param.getSubsamplingXOffset(),
param.getSubsamplingYOffset());
setDestinationType(param.getDestinationType()); <--- This is BAD! This will just set the destination to null (check the code of ImageReadParam, and you will see :).

--- End snippet ---

If I understand correctly, it appears that the call to setDestinationType will just cancel any settings made to the "destination" member of the read parameters. Is this intentional and I have I missed the correct strategy to set the destination? Or have I encountered some bug in the J2KImageReadParamJava class (maybe reported before)?

Thank you for your help :)

Regards,
Tom

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
tomgroup
Offline
Joined: 2010-09-05
Points: 0

[b](Apparently some part of my first post was cut off at the end, here is the sequel)[/b]

ImageReader reader = ImageIO.getImageReadersByFormatName("jpeg2000").next(); // plugins load correctly, no problems here
reader.setInput(ImageIO.createImageInputStream(new FileInputStream(filename)));

BufferedImage img2 = reader.read(0, p);

[b]--- End of snippet ---[/b]

Unfortunately it appears that img2 is not equal to img and thus a new BufferedImage was allocated. I want to avoid this. I thought I had to call "setDestination" on the J2KImageReadParam object, right?

I browsed the source code for the jai-imageio-core project and think I might have found the cause of this problem. I used a debugging tool to step through the involved code. Consider the java package com.sun.media.imageioimpl.plugins.jpeg2000. At some point the J2KImageReader creates some J2KImageReadParamJava object. Now if you open the source of J2KImageReadParamJava you can see the following code (I have added some annotations)

[b]--- Begin snippet ---[/b]

public J2KImageReadParamJava(ImageReadParam param) {
super();

// Generic settings.
if(param.hasController()) {
setController(param.getController());
}
setSourceRegion(param.getSourceRegion());
setSourceBands(param.getSourceBands());
setDestinationBands(param.getDestinationBands());
setDestination(param.getDestination()); [b]// <-- This is fine, just what we would expect[/b]

setDestinationOffset(param.getDestinationOffset());
setSourceSubsampling(param.getSourceXSubsampling(),
param.getSourceYSubsampling(),
param.getSubsamplingXOffset(),
param.getSubsamplingYOffset());
setDestinationType(param.getDestinationType()); <--- This is BAD! This will just set the destination to null (check the code of ImageReadParam, and you will see :).
[b]--- End snippet ---[/b]

If I understand correctly, it appears that the call to setDestinationType will just cancel any settings made to the "destination" member of the read parameters. Is this intentional and I have I missed the correct strategy to set the destination? Or have I encountered some bug in the J2KImageReadParamJava class (maybe reported before)?

Thank you for your help :)

Regards,
Tom

tomgroup
Offline
Joined: 2010-09-05
Points: 0

[b](third part)[/b]

setDestination(param.getDestination()); // <-- This is fine, just what we would expect

setDestinationOffset(param.getDestinationOffset());
setSourceSubsampling(param.getSourceXSubsampling(),
param.getSourceYSubsampling(),
param.getSubsamplingXOffset(),
param.getSubsamplingYOffset());
setDestinationType(param.getDestinationType()); <--- This is BAD! This will just set the destination to null (check the code of ImageReadParam, and you will see :).

--- End snippet ---

If I understand correctly, it appears that the call to setDestinationType will just cancel any settings made to the "destination" member of the read parameters. Is this intentional and I have I missed the correct strategy to set the destination? Or have I encountered some bug in the J2KImageReadParamJava class (maybe reported before)?

Thank you for your help :)

Regards,
Tom