/* 回転 ImageFilter Ver 0.20 */ /* */ /* */ /* by Atsushi 98/5/21 */ package net.antun.lib.awt.image; import java.awt.Dimension; import java.awt.Rectangle; import java.awt.image.ColorModel; import java.awt.image.ImageFilter; import java.util.Hashtable; public class RotateImageFilter extends SinglepassImageFilter { /* --- Field --- */ private int width,height; private double angle; private int ox,oy; private double sin,cos; // 高速化用 /* --- Constructor --- */ public RotateImageFilter(double angle) { this.angle=angle; sin=Math.sin(angle); cos=Math.cos(angle); } /* --- フィルタリング --- */ public void setDimensions(int width,int height) { double minx,miny,maxx,maxy; double xx,yy; super.setDimensions(width,height); minx=Double.POSITIVE_INFINITY; miny=Double.POSITIVE_INFINITY; maxx=Double.NEGATIVE_INFINITY; maxy=Double.NEGATIVE_INFINITY; for (int y=0;y<=1;y++) { for (int x=0;x<=1;x++) { xx=cos*(x*width )+sin*(y*height); yy=cos*(y*height)-sin*(x*width ); minx=Math.min(minx,xx); miny=Math.min(miny,yy); maxx=Math.max(maxx,xx); maxy=Math.max(maxy,yy); } } ox=-(int)(Math.floor(minx)); oy=-(int)(Math.floor(miny)); this.width =(int)(Math.ceil(maxx))+ox+1; this.height=(int)(Math.ceil(maxy))+oy+1; consumer.setDimensions(this.width,this.height); } protected void filterImage() { int p[],xx,yy; double x1,y1,x2,y2,dx,dy; p=new int[width]; for (int y=0;y<=height-1;y++) { x1=cos*(0-ox)-sin*(y-oy); y1=cos*(y-oy)+sin*(0-ox); x2=cos*(width-ox)-sin*(y -oy); y2=cos*(y -oy)+sin*(width-ox); dx=(x2-x1)/width; dy=(y2-y1)/width; for (int x=0;x<=width-1;x++) { xx=(int)(Math.round(x1)); yy=(int)(Math.round(y1)); if (0<=xx && xx<=originalSize.width -1 && 0<=yy && yy<=originalSize.height-1) { p[x]=original[xx+yy*originalSize.width]; } else { p[x]=0; } x1+=dx; y1+=dy; } consumer.setPixels(0,y,width,1,defaultRGB,p,0,width); } } public void setHints(int hint) { consumer.setHints(TOPDOWNLEFTRIGHT | COMPLETESCANLINES | SINGLEPASS | (hint & SINGLEFRAME)); } /* --- 文字列化 --- */ public String toString() { return getClass().getName()+"[angle="+angle+"]"; } }