import java.awt.*;
import java.awt.image.*;

/**
 *  The MyRaster class extends the Raster class to provide the 
 *  added functionality of pixBlts (with alpha blending or without)
 *  and modified the alpha value of non-transparent pixel in the
 *  raster.
 */
 
class MyRaster extends Raster {

    /**
     *  This constructor which takes no arguments
     *  allows for future extension.
     */
    public MyRaster() {
        super();
    }

    /**
     *  This constructor creates an uninitialized
     *  MyRaster Object of a given size (w x h).
     */
    public MyRaster(int w, int h) {
        super(w,h);
    }

    /**
     *  This constructor creates an MyRaster initialized
     *  with the contents of an image.
     */
    public MyRaster(Image img) {
        super(img);
    }

    ////////////////////////// Methods //////////////////////////


    /**
     *  This copies the pixels from the raster passed into the function into 
     *  this raster.  xs, ys specify the sources starting coordinates, and w, h
     *  specify the width and height to copy.  xd, yd specify the starting 
     *  coordinates to copy into in the destination raster.
     *
     *  This function doesn't do alpha blending.
     */
         
    public void pixBlt(MyRaster raster, int xs, int ys, int w, int h, int xd, int yd) {
        for (int j = 0; j < h; j++)  {
            for (int i = 0; i < w; i++) {
                setPixel(raster.getPixel(xs+i, ys+j), xd+i, yd+j);
            }
        }
    }
  
    /**
     *  This copies the pixels from the raster passed into the function into 
     *  this raster.  xs, ys specify the sources starting coordinates, and w, h
     *  specify the width and height to copy.  xd, yd specify the starting 
     *  coordinates to copy into in the destination raster.
     *
     *  This function does do alpha blending.
     */
    
    public void pixBltAlpha(MyRaster raster, int xs, int ys, int w, int h, int xd, int yd) {
        int pixNew, pixExisting;
        int re, ge, be, an, rn, gn, bn;
        
        for (int j = 0; j < h; j++)  {
            for (int i = 0; i < w; i++) {
                pixExisting = getPixel(xd+i, yd+j);
                pixNew = raster.getPixel(xs+i, ys+j);

                re = (pixExisting >> 16) & 0xFF;
                ge = (pixExisting >> 8) & 0xFF;
                be = (pixExisting >> 0) & 0xFF;
                
                an = (pixNew >> 24) & 0xFF;
                rn = (pixNew >> 16) & 0xFF;
                gn = (pixNew >> 8) & 0xFF;
                bn = (pixNew >> 0) & 0xFF;
                
                rn = rn * an / 256 + re * (256 - an) / 256;
                gn = gn * an / 256 + ge * (256 - an) / 256;
                bn = bn * an / 256 + be * (256 - an) / 256;

                pixNew = (255 << 24) | (rn << 16) | (gn << 8) | (bn);
                setPixel(pixNew, xd+i, yd+j);
            }
        }
    }

  
    /**
     *  This function sets the alpha value for non-transparent pixels to the new value.
     */
  
    public void setAlpha(byte alpha) {
        int pix;
        int a, r, g, b, anew;
        
        for (int j = 0; j < height; j++)  {
            for (int i = 0; i < width; i++) {
                pix = getPixel(i, j);
                a = (pix >> 24) & 0xFF;
                r = (pix >> 16) & 0xFF;
                g = (pix >> 8) & 0xFF;
                b = (pix >> 0) & 0xFF;
                
                anew = (a != 0) ? alpha : 0;
                
                pix = ((anew & 0xFF) << 24) | ((r & 0xFF) << 16) | ((g & 0xFF) << 8) | ((b & 0xFF) << 0);
                setPixel(pix, i, j);
                
            }
        }
    }

}