The Warp function is a mapping from a destination space to source space. It is a backward mapping as opposed to the forward mapping of the Affine operator. I don't know whether it suits your needs. Anyways, here is a small method I created to apply the Warp operator:
public static RenderedOp createWarpImage(
RenderedImage img,
Warp warp){
ParameterBlock pb = new ParameterBlock();
pb.addSource(img);
pb.add(warp);
pb.add(Interpolation.getInstance(Interpolation.INTERP_NEAREST));
return JAI.create("warp", pb);
}
Modify it and use it in your code.
Here are the steps to transform an image:
1. Create the PerspectiveTransform object.
PerspectiveTransform pt = new PerspectiveTransform(....);
Call the appropriate methods that are needed to achieve the desired transformation.
2. Create a WarpPerspective object using the PerspectiveTransform object created in (1).
Warp warpPerspective = new WarpPerspective(pt);
3. With the original image and the warpPerspective object call the method I created above.
RenderedOp op = createWarpImage(origImage, warpPerspective);
origImage need to be a rendered image. A BufferedImage object can be a rendered image because the BufferedImage class implements the RenderedImage interface.
4. Extract the RenderedImage object from the RenderedOp object.
RenderedImage renderedImage = op.createInstance();
renderedImage is what you need.
5. To display this image, use the drawRenderedImage() method of the Graphics2D class in your paint() or paintComponent() method.
I haven't tested myself the perspective transformation using JAI, so I don't know what problems you may encounter. Please look at all the relevant Java docs before you proceed. The Warp operator is described in the javax.media.jai.operator.WarpDescriptor class.
The AffineTransform argument is for geometrically manipulating the resulting Warp image. For example, this image may be too big to fit the viewport, in which case you would scale the resulting image.
If you want to display the image as is, you can pass the identity affine transfrom, i.e.,
drawRenderedImage(resultingImage, new AffineTransform());
Note that it is preferable to use drawRenderedImage() rather than drawRenderableImage(). Renderable images are a different ball game alltogether.
Larry,
I have tried the code and I have Run-time error in line 2:
1) RenderedOp op = createWarpImage((RenderedImage)srcImage, wp);
2) RenderedImage dstImage = op.createInstance();
where srcImage is of type Image.
Is it the error from the type conversion of srcImage from Image to RenderedImage?
Thank you.
srcImage cannot be of type Image. As I mentioned in my previous post, it can be a BufferedImage object, which is of RenderedImage type.
A simple way (there are other ways) to convert an Image object to a BufferedImage object is to draw the Image object on the graphics context obtained from a BufferedImage object.
Here is an example:
BufferedImage bi = new BufferedImage(w,h, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = bi.createGraphics();
g2d.drawImage(bi, 0,0,w,h,null);
w and h are the width and height of the image respectively.
Call the createWarpImage() method with bi.
Your other option is to load images using the JAI codec. If you use the FileLoad (or any other ) operator to load an image, you get the image encapsulated in a RenderedOp object. From this object, extract PlanarImage (which is a RenderedImage type) and pass it to createWarpImage().
If you need more examples, you can download the source code from the book I wrote, Building Imaging Applications with Java Technology. Here is the URL:
http://cseng.aw.com/book/0,3828,0201700743,00.html
The warp example is in chapter 12. This example doesn't use PerspectiveTransform, though.
larryhr (Laurence H. rodriges) is very wright: His book 'Building Imaging Applications with Java Technology" is quite remarquable.
I bought it 1 month ago and I felt like falling on THE book that suited me perfectly ... and that I wish I had found years ago...
There are a lot of explanations, very nicely made programs around each topic. It did cost quite a lot (70 euros for a normal french man is a very expensive book) but it is really worth it.
If you are working on software that manipulates images, you got to get it.
Thierry, Paris, France