Friday, January 29, 2010

Image rotation in 8 lines using the Java Advanced Imaging API


Last time, I showed that you could rotate an image 180 degrees in what amounts to one line of JavaScript, basically just doing pixels.reverse( ). Rotating an image by an arbitrary amount (something other than 180 degrees) is almost as easy. It requires about 8 lines of code.

JavaScript for doing the rotation with the Java Advanced Imaging API is shown below. JAI makes short work of this and a ton of other graphics transformations. All you have to do is be sure the JAI JARs are in your classpath. Then you can set up a transformation by creating a ParameterBlock with appropriate parameters (in this case, the x- and y-coordinates of the rotation origin, the amount of rotation in radians, and optionally a rendering hint as to what kind of pixel interpolation you'd like; in this case, we don't specify a hint and thus accept the default of INTERP_NEAREST).

/*
* rotate.js
* Kas Thomas
* 29 January 2010
* Public domain.
*
* Requires Java Advanced Imaging library:
* http://java.sun.com/products/java-media/jai/current.html
*
* Run this file using ImageMunger:
* http://asserttrue.blogspot.com/2010/01/simple-java-class-for-running-scripts.html
*/


pb = new Packages.java.awt.image.renderable.ParameterBlock( );
pb.addSource( Image );

pb.add( new java.lang.Float(0) ); // x-origin
pb.add( new java.lang.Float(0) ); // y-origin
pb.add( new java.lang.Float( Math.PI/8) ); // rotation amount

renderedOp = Packages.javax.media.jai.JAI.create( "rotate", pb );
image = renderedOp.getRendering().getAsBufferedImage();

Panel.setImage( image );

Note that JAI expects parameters of type float, which is not what JavaScript provides by default. By default, numbers in JavaScript are doubles. So you have to explicitly create java.lang.Floats as shown.

As in previous posts, I'm using my little ImageMunger app to run the script.

Also, as with previous scripts, performance is quite good (through no fault of my own): rotation occurs at a rate of about 500 pixels per millisecond on a Dell Inspiron laptop with 2.2 GHz Intel Duo processor running (gack!) Windows Vista. Which ain't bad at all. I'll take 500-pixels-per-millisec throughput any day, on any OS.

12 comments:

  1. Great and informative stuff. Keep bloggingn nad thanks.

    Regards,
    clipping path

    ReplyDelete
  2. pb = new Packages.java.awt.image.renderable.ParameterBlock().addSource(Image);
    pb.add(new Float(0));
    pb.add(new Float(0));
    pb.add(new Float(Math.PI/8));
    image = Packages.javax.media.jai.JAI.create("rotate",pb).getRendering().getAsBufferedImage();
    Panel.setImage(image);

    ReplyDelete
  3. Useful tips, thanks!

    ReplyDelete
  4. Anonymous5:41 AM

    Drawing trend lines is one of the few easy techniques that really WORK. Prices respect a trend line, or break through it resulting in a massive move. Drawing good trend lines is the MOST REWARDING skill.

    The problem is, as you may have already experienced, too many false breakouts. You see trend lines everywhere, however not all trend lines should be considered. You have to distinguish between STRONG and WEAK trend lines.

    One good guideline is that a strong trend line should have AT LEAST THREE touching points. Trend lines with more than four touching points are MONSTER trend lines and you should be always prepared for the massive breakout!

    This sophisticated software automatically draws only the strongest trend lines and recognizes the most reliable chart patterns formed by trend lines...

    ==> http://www.forextrendy.com?vgcvasdiugf9g87346

    Chart patterns such as "Triangles, Flags and Wedges" are price formations that will provide you with consistent profits.

    Before the age of computing power, the professionals used to analyze every single chart to search for chart patterns. This kind of analysis was very time consuming, but it was worth it. Now it's time to use powerful dedicated computers that will do the job for you:

    ==> http://www.forextrendy.com?vgcvasdiugf9g87346

    ReplyDelete
  5. Drawing trend lines is one of the few easy techniques that really WORK. Prices respect a trend line, or break through it resulting in a massive move. Drawing good trend lines is the MOST REWARDING skill.

    The problem is, as you may have already experienced, too many false breakouts. You see trend lines everywhere, however not all trend lines should be considered. You have to distinguish between STRONG and WEAK trend lines.

    One good guideline is that a strong trend line should have AT LEAST THREE touching points. Trend lines with more than four touching points are MONSTER trend lines and you should be always prepared for the massive breakout!

    This sophisticated software automatically draws only the strongest trend lines and recognizes the most reliable chart patterns formed by trend lines...

    ==> http://www.forextrendy.com?kdhfhs93874

    Chart patterns such as "Triangles, Flags and Wedges" are price formations that will provide you with consistent profits.

    Before the age of computing power, the professionals used to analyze every single chart to search for chart patterns. This kind of analysis was very time consuming, but it was worth it. Now it's time to use powerful dedicated computers that will do the job for you:

    ==> http://www.forextrendy.com?kdhfhs93874

    ReplyDelete
  6. Drawing trend lines is one of the few easy techniques that really WORK. Prices respect a trend line, or break through it resulting in a massive move. Drawing good trend lines is the MOST REWARDING skill.

    The problem is, as you may have already experienced, too many false breakouts. You see trend lines everywhere, however not all trend lines should be considered. You have to distinguish between STRONG and WEAK trend lines.

    One good guideline is that a strong trend line should have AT LEAST THREE touching points. Trend lines with more than four touching points are MONSTER trend lines and you should be always prepared for the massive breakout!

    This sophisticated software automatically draws only the strongest trend lines and recognizes the most reliable chart patterns formed by trend lines...

    ==> http://www.forextrendy.com?kdhfhs93874

    Chart patterns such as "Triangles, Flags and Wedges" are price formations that will provide you with consistent profits.

    Before the age of computing power, the professionals used to analyze every single chart to search for chart patterns. This kind of analysis was very time consuming, but it was worth it. Now it's time to use powerful dedicated computers that will do the job for you:

    ==> http://www.forextrendy.com?kdhfhs93874

    ReplyDelete
  7. There are many packers and movers in Hyderabad who are looking for ways to make easy money from innocent families and individuals like you who are planning to relocate.
    Movers and Packers in Kondapur
    Movers and Packers in Gachibowli
    Movers and Packers in Kukatpally
    Movers and Packers in Chanda Nagar
    Movers and Packers in Manikonda

    ReplyDelete
  8. Many packers and movers Hyderabad, Telangana may boast about offering a huge list of services with little to no skill or expertise to carry out those services as required.
    Packers and Movers Hyderabad
    Packers and Movers in Kondapur
    Packers and Movers in Gachibowli
    Packers and Movers in Kukatpally

    ReplyDelete
  9. The packer and movers in Hyderabad load and unload your household items using the necessary equipment and techniques depending on your specific requirements.
    Movers and Packers in Chanda Nagar
    Movers and Packers in Nallagandla
    Movers and Packers in Kukatpally
    Movers and Packers in Miyapur

    ReplyDelete
  10. Thanks for your stop at Indian Packers and Movers in Mumbai. We really know the intentions of the people who are moving their homes or offices in Mumbai. We are one of the best Packers And Movers in Mumbai that gives values to the suggestions and recommendations of the client. It is this heart reading and friendly packing and moving service that made us the most loved packers and movers in Mumbai to depend for professional local transport in Mumbai, courier services in Mumbai and relocation services Mumbai and more.
    Movers and Packers in Chembur
    Movers and Packers in Jogeshwari
    Movers and Packers in Kharghar
    Movers and Packers in Dombivli

    ReplyDelete
  11. We are known manufacturers of modern classic chair. The designs are so attractive that you will be confused among which one to buy. Our products are well appreciated by top interior designers, esteemed customers and architects. We not also supply our products to offices but also in many hospitals, schools, restaurants, auditorium and cafeterias. We also deal in the supply of lounge sofas, education benches and office tables.
    Visitor Chair Supplier in Mumbai
    Chair Dealers in Mumbai
    Top Chair Manufacturers in Mumbai
    Best Chair Manufacturers in Mumbai

    ReplyDelete

Add a comment. Registration required because trolls.