/******************************************************************************
This processor is used to shrink the original image.

only pick up the pixels from the candidates.  NO INTERFERENCE is done.

The default set is 1/2 of the original one.

When the original size can't be divided by the scale without rest, the 
remainning will be discarded.  Say 484*366, if is scaled by 10, will be 48*36
******************************************************************************/

package yi.iu.processor;

import java.awt.*;
import yi.iu.processor.*;
import yi.iu.processor.ImageProcessor;


public class Resizer extends ImageProcessor
{
    int scale=2;
    
    public Resizer() {}
    public Resizer(int s) {scale=s;}
    
    public int[] getProcessedPseudoColor(int source[], Dimension d)
    {
        return getProcessed(source, d);
    }

    public int[] getProcessed(int source[], Dimension d)
    {
        int w=d.width, h=d.height;
        int nw=w/scale,nh=(int)h/scale;
        int[] rst= new int[nw*nh];
        for (int i=0;i<nh;i++) {
            for (int j=0;j<nw;j++) {
                int r=0,g=0,b=0;
                for (int m=0;m<scale;m++) {
                    for (int n=0;n<scale;n++) {
                        int c=source[(i*scale+m)*w+j*scale+n];
                        r+=c>>16&0xff;
                        g+=c>>8&0xff;
                        b+=c&0xff;
                    }
                }
                r/=(scale*scale);
                g/=(scale*scale);
                b/=(scale*scale);
                rst[i*nw+j]=0xff<<24|r<<16|g<<8|b;
            }
        }
        d.width= nw; d.height=nh;
        return rst;
    }

}