import java.awt.*;
import java.awt.image.*;
import java.io.*;
import java.util.*;
import javax.imageio.*;


public class Transformations {
    private int[][] myPixels;

    /**
     * Constructor takes in the file that contains the image
     * and converts it to a BufferedImage. This image is stored
     * in an instance variable in the form of an integer array.
     *
     *         DO NOT MODIFY THIS METHOD
     * 
     * @param file, file that contains the image
     */
    public Transformations(File file) {
        BufferedImage img;
        try {
            img = ImageIO.read(file);
        } catch (Exception e) {
            System.out.println("Incorrect File ");
            return;
        }
        if (img.getType() == BufferedImage.TYPE_INT_RGB) {
            myPixels = imageToPixels(img);
        } else {
            BufferedImage tmpImage = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB);
            tmpImage.createGraphics().drawImage(img, 0, 0, null);
            myPixels = imageToPixels(tmpImage);
        }        
    }
    /**
     * Converts a two-dimensional array of image data to a BufferedImage.
     *
     *         DO NOT MODIFY THIS METHOD
     *
     * @param pixels    the source two-dimensional array of image data
     *
     * @return  a BufferedImage representing the source image.
     *
     * @throws  IllegalArgumentException if the source image is
     *          ill-defined.
     */

    public static BufferedImage pixelsToImage(int[][] pixels) throws IllegalArgumentException {

        if (pixels == null) {
            throw new IllegalArgumentException();
        }
        
        int width = pixels[0].length;
        int height = pixels.length;
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        for (int row = 0; row < height; row++) {
            image.setRGB(0, row, width, 1, pixels[row], 0, width);
        }
        return image;
    }
    
    /**
     * Converts a BufferedImage to a two-dimensional array of image data.
     *
     *         DO NOT MODIFY THIS METHOD
     *
     * @param image the source BufferedImage
     *
     * @return  a two-dimensional array of image data representing the
     *          source image
     *
     * @throws  IllegalArgumentException if the source image is
     *          ill-defined.
     */
    public static int[][] imageToPixels(BufferedImage image) throws IllegalArgumentException {
        if (image == null) {
            throw new IllegalArgumentException();
        }
        
        int width = image.getWidth();
        int height = image.getHeight();
        int[][] pixels = new int[height][width];
        for (int row = 0; row < height; row++) {
            image.getRGB(0, row, width, 1, pixels[row], 0, width);
        }
        return pixels;
    }
        
    /**
     * Method to return the original image
     *
     *         DO NOT MODIFY THIS METHOD
     * 
     * @return image, the original image
     */
    public int[][] getPixels() {
        return myPixels;
    }
    
    /**
     * Create a new image that warps 
     * 
     */
    public int[][] playWarp(int pieces) {
     
        int height = myPixels.length;   // Since we are going to use these a bunch
        int width = myPixels[0].length; // put them in local variables
        
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
            }
        }
         return null;
    }
    /**
     * Create a new image that is bigger than the old one
     * assumes only positive growth factors
     * 
     */
    public int[][] grow(int widthFactor, int heightFactor) {
     
        int height = myPixels.length;   // Since we are going to use these a bunch
        int width = myPixels[0].length; // put them in local variables
        
        int[][] big = new int[height*heightFactor][width*widthFactor];
        
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                int repeat = myPixels[i][j];
                for (int k = 0; k < heightFactor; ++k) {
                    for (int l = 0; l < widthFactor; ++l) {
                        int hopI = i * heightFactor;
                        int hopJ = j * widthFactor;
                        big[hopI + k][hopJ + l] = repeat;
                    }                  
                }
            }
        }
         return big;
    }
    /**
     * Create a new image that is smaller
     * 
     */
    public int[][] shrink(int widthFactor, int heightFactor) {
     
        int height = myPixels.length;   // Since we are going to use these a bunch
        int width = myPixels[0].length; // put them in local variables
        
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {

            }
        }
         return null;
    }
    /**
     * Create a new image that flips the old one
     * 
     */
    public int[][] flip() {
 
        int height = myPixels.length;   // Since we are going to use these a bunch
        int width = myPixels[0].length; // put them in local variables
        
        int[][] flipper = new int[height][width];
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                flipper[i][j] = myPixels[height - i - 1][j];
            }
            
        }
        return flipper;
    }
 
    /**
     * Make all the pixels in the image red
     * 
     */
    
    public void setRed() {
        // The color class is in java.awt.*
        // Color constructor:   Color(int r, int g, int b)
        // Each of these integers are between 0 and 255

        // int red = myColor.getRed();
        // int green = myColor.getGreen();
        // int blue = myColor.getBlue();
        // int RGBvalue = myColor.getRGB();

        // some colors: Color.pink, Color.white, Color.magenta, Color.green
        // red ... or Color myColor = new Color(255, 0, 0);
        Color myColor = Color.red;
        int red = myColor.getRGB();
        int r;
        int b;
        int g;
        System.out.println(myColor.getRed() + " " + myColor.getGreen() + " " 
              + myColor.getBlue() + "r g b" );
        
        for (int i = 0; i < myPixels.length; ++i) {
            for (int j = 0; j < myPixels[i].length; ++j) {
                myPixels[i][j] = red;
            }   
        }
    }

    public static void main(String[] args) {
        Transformations tr = new Transformations(new File("rabbit.jpg"));
        DrawingBox box = new DrawingBox("Rabbit");
        
        // display the original image
        BufferedImage originalImage = pixelsToImage(tr.getPixels());
        box.drawImage(originalImage, 10, 10);
        
//        int[][] flipped = tr.flip();
//        box.drawImage(pixelsToImage(flipped), 10, 150);
          int[][] tPixels = tr.grow(3, 1);
          box.drawImage(pixelsToImage(tPixels), 10, 220);
        
       
    }
}
