crop operation not working in JAI 1.1.3?

The "crop" operation seems to be broken in JAI 1.1.3 (at least for Windows):

When cropping an image, the values for x and y are ignored and the new image is always the part from the upper left corner of the original image.

In JAI 1.1.2 this worked correctly.

Code to reproduce the problem (call program with old and new filename as parameters):

The program should cut out an image with half the height and width from the middle of the original image.

public static void main(String[] args) {

// load image

ParameterBlock loadPB = new ParameterBlock();

loadPB.add(args[0]);

RenderedOp image = JAI.create("fileload", loadPB);

// crop image

int width = image.getWidth();

int height = image.getHeight();

int newWidth = width / 2;

int newHeight = height / 2;

ParameterBlock pb = new ParameterBlock();

pb.addSource(image);

pb.add((float)(width - newWidth)/2);

pb.add((float)(height - newHeight)/2);

pb.add((float)newWidth);

pb.add((float)newHeight);

RenderedOp cropped = JAI.create("crop", pb, null);

// save image

ParameterBlock savePB = new ParameterBlock();

savePB.addSource(cropped);

savePB.add(args[1]);

savePB.add("jpeg");

JAI.create("filestore", savePB);

}

[1332 byte] By [norps007a] at [2007-11-14]
# 1
I have the same problem (in both OS windows and linux)It seem the problem is not in the crop but in the jpeg saving.If you save it as PNG it work well.Rotem.
roterla at 2007-7-8 > top of java,Security,Cryptography...
# 2
yes, i think you are right.as a workaround, i now simply convert the image into a bufferedimage before storing as jpeg. maybe this is not optimal, but at least it seems to work.
norps007a at 2007-7-8 > top of java,Security,Cryptography...
# 3

I also did that. I checked out if any crop had been done, and if so, save as BufferImage.

public void save(String destinationFilename) {

if (workaroundJPEG) {

//Workaround for JAI JPEG saving bug of no tile support (crop problems)

final int QUALITY = 75;

BufferedImage bufferedImage = objImage.getRendering().getAsBufferedImage();

try {

FileOutputStream fos = new FileOutputStream(destinationFilename);

BufferedOutputStream bos = new BufferedOutputStream(fos);

JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(bos);

JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bufferedImage);

param.setQuality(QUALITY / 100.0f, false);

encoder.setJPEGEncodeParam(param);

encoder.encode(bufferedImage);

bos.close();

workaroundJPEG = false;

} catch (IOException e) {

// handle errors

}

} else {

JAI.create("filestore", objImage, destinationFilename, "JPEG", null);

}

}

Each time I do a crop operation I set the workaroundJPEG to true.

roterla at 2007-7-8 > top of java,Security,Cryptography...
# 4

For a more non-Sun specific way of doing this that is shorter replace the lines from the JPEGImageEncoder... line up to the bos.close(); line with:

ImageIO.write(bufferedImage, fileType, bos);

Where the fileType is "jpeg". Of course if you want to tweak the JPEG settings you'll need to do that, but at least you're not tied to classes that are Sun specific.

tom_jnsna at 2007-7-8 > top of java,Security,Cryptography...
# 5
Hi friend,i am doing same thing.If u have got solution for same cropping problem then please post sample code.Also i am searching to performed brightness,contrast operation using JAI.Can anybody have solution .Thanks in advance
AP_javaa at 2007-7-8 > top of java,Security,Cryptography...