package image;

import stdlib.Color;
import stdlib.Picture;

public class Image {

	/**
	 * Scales an image to w-by-h and displays both the original
	 * and the scaled version to the screen.
	 * @param filename
	 * @param w width
	 * @param h height
	 */
    public void scale(String filename, int w, int h) {
        Picture source = new Picture(filename);
        Picture target = new Picture(w, h);

        for (int tx = 0; tx < w; tx++) {
            for (int ty = 0; ty < h; ty++) {
                int sx = tx * source.width()  / w;
                int sy = ty * source.height() / h;
                Color color = source.get(sx, sy);
                target.set(tx, ty, color);
            }
        }

        source.show();
        target.show();
    }
    
    /**
     * Reads in an image from a file, converts it to grayscale, and
     * displays it on the screen.
     * @param filename
     */
    public void grayscale(String filename) {
        Picture pic = new Picture(filename);
        int width  = pic.width();
        int height = pic.height();

        // convert to grayscale
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                Color color = pic.get(x, y);
                Color gray = Luminance.toGray(color);
                pic.set(x, y, gray);
            }
        }
        pic.show();
    }

    public Color combine(Color c1, Color c2, double alpha) {
        int r = (int) (alpha * c1.getRed()   + (1 - alpha) * c2.getRed());
        int g = (int) (alpha * c1.getGreen() + (1 - alpha) * c2.getGreen());
        int b = (int) (alpha * c1.getBlue()  + (1 - alpha) * c2.getBlue());
        return new Color(r, g, b);
    }

    /**
     * Produce animated effect, fading from image1.jpg to image2.jpg,
     * using N-1 intermediate frames.
     * @param filename1 begin picture
     * @param filename2 end picture
     * @param n number of frames
     */
    public void fade(String filename1, String filename2, int n){
        Picture pic1 = new Picture(filename1);
        Picture pic2 = new Picture(filename2);
        fade(pic1, pic2, n);
    }

    /**
     * Produce animated effect, fading from image1.jpg to image2.jpg,
     * using N-1 intermediate frames.
     * @param pic1 begin picture
     * @param pic2 end picture
     * @param n number of frames
     */
    public void fade(Picture pic1, Picture pic2, int n){
        int width  = pic1.width();
        int height = pic1.height();
        Picture pic = new Picture(width, height);
        for (int k = 0; k <= n; k++) {
            double alpha = 1.0 * k / n;
            for (int i = 0; i < width; i++) {
                for (int j = 0; j < height; j++) {
                    Color c1 = pic1.get(i, j);
                    Color c2 = pic2.get(i, j);
                    pic.set(i, j, combine(c2, c1, alpha));
                }
            }
            pic.show();
        }
    }

    public static void main(String[] args) {
    	Image im = new Image();
//    	im.scale("resources/mandrill.jpg", 200, 300);
//    	im.grayscale("resources/mandrill.jpg");
    	im.fade("resources/darwin.jpg", "resources/mandrill.jpg", 200);
    }
}
