Exercise 4

Sample code

public class MyImageIO {
    // Copy functions:
    //  readImageFromFile(String fileName, boolean gray ), readImageFromFile(String fileName )
    //  writeImageToFile(String fileName, int[][][] pixels), writeImageToFile(String fileName, int[][] pixels)


    //My example code
    public static int[][][] createTwoLines(){
        int[][][] image = new int[3][50][50];

        //Red line
        for (int i = 0; i < 25; i++)
            for (int j = 0; j < image[0][0].length; j++)
                image[0][i][j] = 255;

        //Blue line
        for (int i = 25; i < image[0].length; i++)
            for (int j = 0; j < image[0][0].length; j++)
                image[2][i][j] = 255;

        return image;
    }




    //My example code
    // ============= createMicrosoftLogoRGB =============
    public static int[][][] createMicrosoftLogoRGB(){
        int[][][] image = new int[3][50][50];

//        for (int[][] RGB: image)
//            for (int[] row: RGB)
//                Arrays.fill(row, 255);

        for (int i = 0; i < 22; i++) {
            for (int j = 0; j < image[0].length; j++) {
                if (j <= 22){//Red tile, 1st tile RGB (243, 83, 37)
                    image[0][i][j] = 243; // Red
                    image[1][i][j] = 83;  // Green
                    image[2][i][j] = 37;  // Blue
                }
                else if (j >= 28) { //Green, 2nd tile. RGB (129, 188, 6)
                    image[0][i][j] = 129; //Green
                    image[1][i][j] = 188; //Green
                    image[2][i][j] = 6; //Green
                } else { //white
                    for (int k = 0; k < 3; k++)
                        image[k][i][j] = 255;
                }

            }
        }

        for (int i = 22; i < 28; i++)
            for (int j = 0; j < image[0].length; j++)
                for (int k = 0; k < 3; k++)
                    image[k][i][j] = 255;


        for (int i = 28; i < image[0].length; i++) {
            for (int j = 0; j < image[0][0].length; j++) {
                if (j <= 22){ //Blue tile, 3rd tile. RGB (5, 166, 240)
                    image[0][i][j] = 5;
                    image[1][i][j] = 166;
                    image[2][i][j] = 240;
                } else if (j >= 28){ //Yellow tile, 4th tile. RGB (255, 186, 8)
                    image[0][i][j] = 255;
                    image[1][i][j] = 186;
                    image[2][i][j] = 8;
                }else { //white
                    for (int k = 0; k < 3; k++)
                        image[k][i][j] = 255;
                }
            }
        }
        return image;
    }




    // ============= main  =============
    public static void main(String[] args) throws IOException {
        //microsoft logo
        int[][][] image = createMicrosoftLogoRGB();
        RGBImage rgbImage1 = new RGBImage(image);
        MyImageIO.writeImageToFile(rgbImage1, "image_example1");

        //read the saved image and write again to another image
        Frame rgbImage2 = MyImageIO.readImageFromFile("image_example1.jpg", false);
        MyImageIO.writeImageToFile(rgbImage2, "image_example2");

        //create 2 lines
        int[][][] image3 = createTwoLines();
        RGBImage rgbImage3 = new RGBImage(image3);
        MyImageIO.writeImageToFile(rgbImage3, "two_lines");
    }
}

here we can see in our main that we can either:

  • write (save) a Frame using MyImageIO.writeImageToFile

  • read a .jpg image using MyImageIO.readImageFromFile

I have created 2 methods to create a RGBImage (RGB 50x50 pixels), feel free to use them yourselves

  • createMicrosoftLogoRGB()

  • createTwoLines()

Last updated