CSE-8A / PA6 / PA6.java
PA6.java
Raw
import image.Image;
import java.awt.Color;

public class PA6 {

    // DO NOT CHANGE
    public static Image dummy = new Image(new Color[][]{{Color.white}});

    // DO NOT CHANGE
    public static int luminance(Color c) {
        return (int)(0.2126 * c.getRed() + 0.7152 * c.getGreen() + 0.0722 * c.getBlue());
    }

    // ------------------------------------------------------------------------
    // Filters
    // ------------------------------------------------------------------------

    public static Image grayscale(Image img) {
        // TODO: Implement method, replace return value and delete this comment

        // int height = img.getHeight();
        // int width = img.getWidth();

        // Color [][] imgArray = new Color [height][width];

        Color [][] imgArray = img.getPixels2D();

//figure out how to grab the color within an image
//luminance takes colors
//getGreen, getRed, and getBlue keyword gives int value
//Create color object> can do getRed method for color object
//take color from each pixel of the array\
//imgArray [row][col] REPRESENTS COLOR

        for (int row = 0; row < img.getHeight(); ++row){
            for(int col = 0; col < img.getWidth(); ++col){
                Color obj = imgArray[row][col];
                int red = luminance(obj);
                int green = luminance(obj);
                int blue = luminance(obj);

                imgArray [row][col] = new Color(red,green,blue);
            }
            Image grayItAll = new Image(imgArray);
        }
            Image grayItAll = new Image(imgArray);
            return grayItAll;
    }   

    public static Image blend(Image img1, Image img2) {
        // TODO: Implement method, replace return value and delete this comment

        Color [][] img1Array = img1.getPixels2D();
        Color [][] img2Array = img2.getPixels2D();
        int height = img1.getHeight();
        int width = img1.getWidth();
        Color [][] newImageArray = img1.getPixels2D();

        for(int row = 0; row< height; ++row){
            for(int col=0; col < width; ++col){
                newImageArray [row][col] = new Color((img1Array[row][col].getRed()+img2Array[row][col].getRed())/2, (img1Array[row][col].getGreen()+img2Array[row][col].getGreen())/2 , (img1Array[row][col].getBlue()+img2Array[row][col].getBlue())/2);

            }
            Image blendResult = new Image(newImageArray);
    
        }
        Image blendResult = new Image(newImageArray);
        return blendResult;

    }


    public static Image crop(Image img, int width, int height) {
        // TODO: Implement method, replace return value and delete this comment
        //2D array keywords have diferent functions
        //You can set the color as well as the indexes of the color

            Color [][]imgArray = img.getPixels2D();
            Color [][] newArray = new Color[height][width];

            
        for(int row = 0; row <height; ++row){
            for(int col = 0; col < width; ++col){
                newArray[row][col]=imgArray[row][col];
            }
        }
            Image cropResult = new Image(newArray);

        return cropResult;
    }
    //the filter will grab an image and would simply switch the value of green and blue
    public static Image complement(Image img){
        Color [][] imgArray = img.getPixels2D();
        Color [][] newArray = new Color[img.getHeight()][img.getWidth()];

    //switching the color values of green and blue at specific pixels
        for (int row = 0; row < img.getHeight(); ++row){
            for (int col = 0; col < img.getWidth(); ++col) {
                newArray[row][col] = new Color(imgArray[row][col].getRed(), imgArray[row][col].getBlue(), imgArray[row][col].getGreen());
            }
            Image complementResult = new Image(newArray);
            
        }
        //returning the image when it is complemented
        Image complementResult = new Image(newArray);
        return complementResult;
        }


    // ------------------------------------------------------------------------
    // Tests
    // ------------------------------------------------------------------------

    public static void testGrayscale() {
        // Create a 2x3 array of gray Color objects
        Color[][] test1 = {
            {Color.gray, Color.gray, Color.gray},   // row 0
            {Color.gray, Color.gray, Color.gray},   // row 1
        };

        // Create an image from the 2D array and visualize it
        Image img1 = new Image(test1);
        img1.explore();

        // Call grayscale and visualize the resulting image
        Image result1 = grayscale(img1);
        result1.explore();

        // TODO: Add two more test cases
    
        //These are the 6 corresponding color objects
        Color a1 = new Color(255, 50, 150);
        Color a2 = new Color(180, 190, 255);
        Color a3 = new Color(123, 0, 167);
        Color a4 = new Color(0, 185, 45);
        Color a5 = new Color(135, 85, 0);
        Color a6 = new Color(190, 255, 210);

        Color[][] test2 = {    
            {a1},
        };
        //creates original image of the 2D aray of test2
        Image img2 = new Image(test2);
        img2.explore();
        //using the method of grayscale of the created image
        Image GrayResult2 = grayscale(img2);
        GrayResult2.explore();
        //This array is 4x4
        Color [][] test3 = {
            {a4, a5, a2, a3},
            {a2, a2, a1, a3},
            {a5, a3, a5, a1},
            {a6, a2, a3, a4},

        };
        //creates original image
        Image img3 = new Image(test3);
        img3.explore();
        //using method of grayscale
        Image GrayResult3 = grayscale(img3);
        GrayResult3.explore();
    }

    public static void testBlend() {
        // Create two 1x1 2D arrays
        Color[][] test1_1 = {{new Color(0, 0, 0)}};
        Color[][] test1_2 = {{new Color(100, 100, 100)}};

        // Create the test images and call blend
        Image result1 = blend(new Image(test1_1), new Image(test1_2));

        // Explore the image and ensure that the pixel color is (50, 50, 50)
        result1.explore();

        // TODO: Add two more test cases

        //creating 2 different arrays with different color
        //3x1 arrays
        Color [][] test2_1 = {
            {new Color (120,255,255)},
            {new Color (65, 0, 10)},
            {new Color (65, 255, 0)}
        };
        Color [][] test2_2 = {
            {new Color (255, 0, 135)},
            {new Color (150, 200, 120)},
            {new Color (0, 85, 255)},
        };
        // using the blend method with 2 images
        Image blendResult2 = blend(new Image(test2_1), new Image(test2_2)); 
        blendResult2.explore();

        //Arrays 2x2
        Color [][] test3_1 ={
            {new Color (65, 80, 255), new Color(160, 245, 50)},
            {new Color (180,255,195), new Color(130, 50, 70)},
        };

        Color [][] test3_2 = {
            {new Color (255, 0, 140), new Color(70, 90, 255)},
            {new Color (140, 210, 70), new Color(40, 0, 80)},
        };
        //using the blend method with 2 images
        Image resultBlend3 = blend(new Image(test3_2), new Image(test3_1));
        resultBlend3.explore();

    }

    public static void testCrop() {
        // Create a 4x4 2D array
        Color[][] test1 = {
            {Color.black, Color.black, Color.red, Color.red},   // row0
            {Color.black, Color.black, Color.red, Color.red},   // row1
            {Color.black, Color.black, Color.red, Color.red},   // row2
            {Color.red, Color.red, Color.red, Color.red}        // row3
        };

        // Create the test image and call crop
        Image img1 = new Image(test1);
        Image result1 = crop(img1, 2, 3);

        // Visualize the result and make sure that it is 3x2 and that all colors
        // are black (0, 0, 0) (first three rows and two columns of test1 image)
        result1.explore();
        //creating a 2D array, 3x4
        Color [][]test2 = {
            {Color.blue, Color.blue, Color.cyan, Color.green},
            {Color.blue, Color.black, Color.magenta, Color.red},
            {Color.white, Color.pink, Color.gray, Color.yellow},
        
        };


        //creating an image from the 2D array and cropping it
        Image img2 = new Image(test2);
        Image result2 = crop(img2, 1, 1);
        result2.explore();

        //creating a 2D array, 5x3
        Color [][] test3 = {
            {Color.gray, Color.black, Color.pink},
            {Color.red, Color.yellow, Color.blue},
            {Color.magenta, Color.gray, Color.white},
            {Color.blue, Color.red, Color.white},
            {Color.cyan, Color.blue, Color.green},
        };
        //creating an image from the 2D array of test3 and cropping the image
        Image img3 = new Image(test3);
        Image result3 = crop(img3, 2, 4);
        result3.explore();

        }
    //Making a function that calls the complement of an image
    public static void testComplement() {
        Color [][] test1 = {
            {new Color(100, 120, 180), new Color(240, 0, 130), new Color(150, 85, 50)},
            {new Color(230, 130, 160), new Color(190, 50, 90), new Color(255, 170, 0)},
        };

        Image img1 = new Image(test1);
        Image resultComplement1 = complement(img1);
        resultComplement1.explore();


        }
        // TODO: Add two more test cases


    // ------------------------------------------------------------------------
    // Main Method
    // ------------------------------------------------------------------------

    public static void main(String[] args) {
        //Calling the image from the folder
        Image crane = new Image("images/crane.jpg");
        //Opening the original image
        crane.explore();
        //Using the complement method and creating a new image
        Image resultCrane = complement(crane);
        //calling the image of the complemented image
        resultCrane.explore();

        // You may want to uncomment one test at a time

        //  testGrayscale();
        //  testBlend();
        //  testCrop();

        // TODO: Add code for Part 2 here
    }
}