// purpose: provide basic photo manipulation import java.util.*; public class PhotoManip { // getWidth(): return the width of the pixel grid public static int getWidth( int[][] grid ) { return grid.length; } // getHeight(): return the height of the pixel grid public static int getHeight( int[][] grid ) { return grid[ 0 ].length; } // mirror(): returns the mirrored image of the pixel grid public static int[][] mirror( int[][] grid ) { // image dimensions int width = PhotoManip.getWidth( grid ); int height = PhotoManip.getHeight( grid ); // make the holder for the mirrored version int[][] reverse = new int[ width ][ height ]; for ( int x = 0; x < width; ++x ) { for ( int y = 0; y < height; ++y ) { // get the source for the pixel at (x, y) int pixel = grid[ width - x - 1 ][ y ]; // set pixel at (x, y) reverse[ x ][ y ] = pixel; } } // handback the mirror return reverse; } // flip(): returns the flipped image of the pixel grid public static int[][] flip( int[][] grid ) { // image dimensions int width = PhotoManip.getWidth( grid ); int height = PhotoManip.getHeight( grid ); // make the holder for the flipped version int[][] flip = new int[ width ][ height ]; for ( int x = 0; x < width; ++x ) { for ( int y = 0; y < height; ++y ) { // get the source for the pixel at (x, y) int sx = x; int sy = height - 1 - y; int pixel = grid[ sx ][ sy ]; // set pixel at (x, y) flip[ x ][ y ] = pixel; } } // handback the flip return flip; } // grayscale(): returns the NTSC conversion of the pixel grid public static int[][] grayscale( int[][] grid ) { // NTSC conversion constants final double RED_TO_GRAY = 0.33; // 299; final double GREEN_TO_GRAY = 0.50; // 587; final double BLUE_TO_GRAY = 0.17; // 114; // image dimensions int width = PhotoManip.getWidth( grid ); int height = PhotoManip.getHeight( grid ); // make the holder for the gray scale version int[][] gray = new int[ width ][ height ]; // process every pixel of the image for ( int x = 0; x < width; ++x ) { for ( int y = 0; y < height; ++y ) { // set the gray scale pixel for location (x, y) of the image // get the original int pixel = grid[ x ][ y ]; // deterine the RGB components of the pixel int r = RGB.getRed( pixel ); int g = RGB.getGreen( pixel ); int b = RGB.getBlue( pixel ); // convert RGB to gray scale using the NTSC standard int gr = (int) ( RED_TO_GRAY * r ); int gg = (int) ( GREEN_TO_GRAY * g ); int gb = (int) ( BLUE_TO_GRAY * b ); // make the gray scale version of the pixel int gpixel = RGB.toInt( gr, gg, gb ); // set the pixel at location (x, y) of the grey version gray[ x ][ y ] = gpixel; } } // handback the gray scale version return gray; } // combine(): produce a new image whose pixels are the average of the // corresponding pixels of grid1 and grid2 public static int[][] combine( int[][] grid1, int[][] grid2 ) { int[][] grid3 = Matrix.average( grid1, grid2 ); return grid3; } // rgbCombine(): produce a new image whose pixels are the average of the // RGB components of the corresponding pixels of grid1 and grid2 public static int[][] rgbCombine( int[][] grid1, int[][] grid2 ) { // image dimensions int width = PhotoManip.getWidth( grid1 ); int height = PhotoManip.getHeight( grid1 ); // make the holder for the the blend int[][] grid3 = new int[ width ][ height ]; // set every pixel of the blend for ( int x = 0; x < width; ++x ) { for ( int y = 0; y < height; ++y ) { // get the colors of the two corresponding pixels int pixel1 = grid1[ x ][ y ]; int pixel2 = grid2[ x ][ y ]; int r1 = RGB.getRed( pixel1 ); int g1 = RGB.getGreen( pixel1 ); int b1 = RGB.getBlue( pixel1 ); int r2 = RGB.getRed( pixel2 ); int g2 = RGB.getGreen( pixel2 ); int b2 = RGB.getBlue( pixel2 ); // compute the RGB averages int r3 = ( r1 + r2 ) / 2; int g3 = ( g1 + g2 ) / 2; int b3 = ( b1 + b2 ) / 2; // set the pixel using the averages int pixel3 = RGB.toInt( r3, g3, b3); grid3[ x ][ y ] = pixel3; } } // hand back the blend return grid3; } // blend(): produce a new image whose pixels are randomly selected from // the corresponding pixels of grid1 and grid2 public static int[][] blend( int[][] grid1, int[][] grid2 ) { // image dimensions int width = PhotoManip.getWidth( grid1 ); int height = PhotoManip.getHeight( grid1 ); // make the holder for the the blend int[][] grid3 = new int[ width ][ height ]; // set every pixel of the blend for ( int x = 0; x < width; ++x ) { for ( int y = 0; y < height; ++y ) { // randomly choose the image to supply the current pixel Random die = new Random(); int toss = die.nextInt(2) + 1; // use the toss to pick the image int pixel = 0; // replace with your code // use the selected pixel for the blend grid3[ x ][ y ] = pixel; } } // hand back the blend return grid3; } // scale(): produce a new image of requested size with grid as the basis public static int[][] scale( int[][] grid, double percentage ) { // dimensions of existing image int gwidth = PhotoManip.getWidth( grid ); int gheight = PhotoManip.getHeight( grid ); // dimensions of the scaled image int width = (int) ( gwidth * percentage ); int height = ( int) ( gheight * percentage ); // container for the scaled image int[][] sgrid = new int[ width ][ height ]; for ( int x = 0; x < width; ++x ) { for (int y = 0; y < height; ++y ) { // determine source pixel from original image; int ox = (int) ( x / percentage ); int oy = (int) ( y / percentage ); int pixel = grid[ ox ][ oy ]; // set the pixel in the scaled image sgrid[ x ][ y ] = pixel; } } return sgrid; } // soften(): produce a softening of the image public static int[][] soften( int[][] grid ) { // image dimensions int width = PhotoManip.getWidth( grid ); int height = PhotoManip.getHeight( grid ); // make the holder for the softened version int[][] sgrid = new int[ width ][ height ]; for ( int x = 0; x < width; ++x ) { for ( int y = 0; y < height; ++y ) { // compute the average of the source pixel's area // set pixel at (x, y) to the average } } // handback the mirror return sgrid; } // crop(): grabs the indicated portion of the image public static int[][] crop( int[][] grid, int ulx, int uly, int width, int height ) { // image dimensions of original int owidth = PhotoManip.getWidth( grid ); int oheight = PhotoManip.getHeight( grid ); // make the holder for the softened version int[][] cgrid = new int[ width ][ height ]; for ( int x = 0; x < width; ++x ) { for ( int y = 0; y < height; ++y ) { // get the pixel location from the original // use the location to get the pixel // set crop (x, y) to the pixel } } // handback the mirror return cgrid; } }