
package ru.sakva.bsh;

import java.awt.*;
import java.awt.image.*;


/** Rotate image on +90,-90 or 180 degrees */

 public class Rot90 extends ImageFilter
/* * */ {

 int width;
 int height;
 int[] p;
 ColorModel colorModel = ColorModel.getRGBdefault();
 public int angle=-90;
 public boolean asIs=true;
 public int back  = 0xff000000;
 public int front = 0xfffffff;

 public void setDimensions(int w,int h)
{
 if(angle==90 || angle == -90){ width=h; height=w; }
 else { width=w; height=h; }
 p=new int[width*height];
 consumer.setDimensions(width,height);
 }

 public void imageComplete(int status)
{
 if(status != IMAGEERROR && status != IMAGEABORTED)
   {
    if(p != null)
      {
       consumer.setPixels(0,0,width,height,colorModel,p,0,width);
       }
    }
 consumer.imageComplete(status);
 }

 public void setPixels
   (int x,int y,int w,int h,ColorModel model,byte[] pixels,int off,int ssz)
{
 int i,j,si,di,c;
 j=0; while(j < h)
   {
    si=off+ssz*j+x;
    i=0; while(i < w)
      {
       if(angle == -90)di=width*(height-x-i-1)+(y+j); else
       if(angle ==  90)di=width*(x+i)+(width-y-j-1);  else
       if(angle == 180)di=(width-x-i-1)+width*(height-y-j-1); else
       di=width*(y+j)+(x+i); 
       c=model.getRGB(pixels[si++]&0xff);
       if(asIs)p[di]=c;
       else { if(c==0xff000000)p[di]=back; else p[di]=front; }
       i++;
       }
    j++;
    }
 }

 public void setPixels
   (int x,int y,int w,int h,ColorModel model,int[] pixels,int off,int ssz)
{
 int i,j,si,di,c;
 j=0; while(j < h)
   {
    si=off+ssz*j+x;
    i=0; while(i < w)
      {
       if(angle == -90)di=width*(height-x-i-1)+(y+j); else
       if(angle ==  90)di=width*(x+i)+(width-y-j-1);  else
       if(angle == 180)di=(width-x-i-1)+width*(height-y-j-1); else
       di=width*(y+j)+(x+i); 
       c=model.getRGB(pixels[si++]);
       if(asIs)p[di]=c;
       else { if(c==0xff000000)p[di]=back; else p[di]=front; }
       i++;
       }
    j++;
    }
 }


 public void setColorModel(ColorModel cm)
{ consumer.setColorModel(colorModel); }

/*
 public ImageFilter getFilterInstance(ImageConsumer ic){ return this; }
 public void resendTopDownLeftRight(ImageProducer ip){ }
 public void setHints(int hints){ }
 public void setProperties(Hashtable props){ }
*/
 
/* * */  }

