Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

DUE BY MIDNIGHT SATURDAY import util.Util; /** * * Friday 26-Jan-2018 * * This i

ID: 3879799 • Letter: D

Question

DUE BY MIDNIGHT SATURDAY import util.Util; /** * * Friday 26-Jan-2018 * * This is what we practiced in our third lab. * * * @author aahmadzadeh1 - https://grid.cs.gsu.edu/~aahmadzadeh1/ * */ /** * In this class, we try to work on several different methods that manipulate * the 2D matrices extracted from images. We use the class "Util", as a blackbox, * only to read/load an image and convert it to a 2D array of int, and to * write/save a 2D array of int as an image. *

* Purpose: More practice on methods, 2D arrays, and using other classes that * you do not necessarily need to know anything about their implementation. *

* Source: This code is available in my repository, * here * * */ public class Main { private static final int row = 0; private static final int col = 0; private static double scalefactor; private static int offset; public static void main(String[] args) { /** Example: How to read an image, manipulate and write **/ String dir = "image.jpg"; int[][] img = Util.readImageToMatrix(dir); for (int row = 0; row < img.length; row++) { for (int col = 0; col < img[0].length; col++) { img[row][col] = (img[row][col] - 2); img[row][col] = (img[row][col] > 255) ? 255 : img[row][col]; } } Util.writeMatrixAsImage(img, "image"); /** CHECKPOINT 1: getImageHeight() **/ // TODO: Test your method here ImageHeight(); /** CHECKPOINT 2: getImageWidth() **/ // TODO: Test your method here ImageWidth(); /** CHECKPOINT 3: brightenImage() **/ // TODO: Test your method here brightenImage(); /** CHECKPOINT 4: darkenImage() **/ // TODO: Test your method here darkenImage(); /** CHECKPOINT 5: changeContrast() **/ // TODO: Test your method here changeContrast(); /** CHECKPOINT 6: flipHorizontally() **/ // TODO: Test your method here flipHorizontally(); /** CHECKPOINT 7: flipVertically() **/ // TODO: Test your method here flipVertically(); /** CHECKPOINT 8: blurImage() **/ // TODO: Test your method here blurImage(); /** CHECKPOINT 9: changeContrastByPatch() **/ // TODO: Test your method here changeContrastByPatch(); } private static void darkenImage() { // TODO Auto-generated method stub } private static void brightenImage() { // TODO Auto-generated method stub } private static char[] ImageHeight() { // TODO Auto-generated method stub return null; } private static void changeContrastByPatch() { // TODO Auto-generated method stub } private static void blurImage() { // TODO Auto-generated method stub } private static void flipVertically() { // TODO Auto-generated method stub } private static void flipHorizontally() { // TODO Auto-generated method stub } private static void changeContrast() { // TODO Auto-generated method stub } private static void ImageWidth() { // TODO Auto-generated method stub } private static char[] getchangeContrastByPatch() { // TODO Auto-generated method stub return null; } private static char[] getblurImage() { // TODO Auto-generated method stub return null; } private static char[] getflipVertically() { // TODO Auto-generated method stub return null; } private static char[] getflipHorizontally() { // TODO Auto-generated method stub return null; } private static char[] getchangeContrast() { // TODO Auto-generated method stub return null; } private static char[] getdarkenImage() { // TODO Auto-generated method stub return null; } private static char[] getImageHeight() { // TODO Auto-generated method stub return null; } /** * This method gets the height of the image based on the given 2D array. * * @param img * the given image in the form of a 2D array * * @return the height of the given image * */ public static int getImageHeight(int[][] img) { // TODO: Complete this method int[][]img1 = new int [row][col]; System.out.println(img1.length); return 0; } /** * This method gets the width of the image based on the given 2D array. * * @return the width of the given image */ public static int getImageWidth() { // TODO: Complete this method int[][]img1 = new int [row][col]; System.out.println(img1[0].length); return 0; } /** * This method brightens the given image by * * @param img the given image in the form of a 2D array. * @param shift a value by which the value of each pixel will be * shifted. * @return the brighten image in the form of a 2D array. */ public static int[][] brightenImage(int[][] img, int shift) { // TODO: Complete this method return null; } /** * This method darkens the given image by * * @param img the given image in the form of a 2D array. * @param shift shift a value by which the value of each pixel will be * shifted. * @return the darkened image in the form of a 2D array. */ public static int[][] darkenImage(int[][] img, int shift) { // TODO: Complete this method return null; } /** * This method increases or decreases the contrast of the given image * by applying the following procedure: *
*

*

Calculate the factor: f = [259 * (scale +_255)]/[255 * (259 - scale)] *

For each pixel: *

*

newValue = (f * (oldValue - 128)) + 128 *

Take care of the values outside the interval [0,255] *

*

*
http://www.dfstudios.co.uk/articles/programming/image-programming-algorithms/image-processing-algorithms-part-5-contrast-adjustment/ *
* @param img the given image in the form of a 2D array. * @param scale the contrast scale. This is a number between -255 to +255. * @return a 2D image whose contrast is adjusted. */ public static int[][] changeContrast(int[][] img, double scale){ // TODO: Complete this method return null; } /** * This method flips the image horizontally. That is, while it reads * the image from the first column to the last, it writes it to another * array starting from the last column to the first. * @param img * @return a 2D array representing a horizontally flipped version * of the given image. */ public static int[][] flipHorizontally(int[][] img) { // TODO: Complete this method return null; } /** * This method flips the image vertically. That is, while it reads * the image from the first row to the last, it writes it to another * array starting from the last row to the first. * * @param img the given image the form of a 2D array. * @return a 2D array representing a vertically flipped version * of the given image. */ public static int[][] flipVertically(int[][] img) { // TODO: Complete this method return null; } /** * This method takes an image (2D array), applies the blur filter * to it and returns results in the form of a 2D array. The filter * procedure is as follows: *
* For each pixel: *

*

Get the average of the 8 neighboring pixels. *

Replace the value of this pixel with the average. *

* * @param img the source image to which the filter will be applied. * @return a 2D array representing the filtered image. */ public static int[][] blurImage(int[][] img) { // TODO: Complete this method return null; } /** * This method applies the function 'changeContrast' that you already * implemented, on patches of the image. The difference is that as it * iterates over the patches, the contrast scale will increase. *
* Procedure:
*

*

For each patch: *

*

call the function 'changeContrast' *

copy the result to the corresponding patch of the output matrix. *

increment the contrast scale. *

*

* @param img * @return */ public static int[][] changeContrastByPatch(int[][] img, int patchSize){ // TODO: Complete this method return null; } }

Explanation / Answer

import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.io.IOException;
import javax.imageio.ImageIO;

public class PerformanceTest {

public static void main(String[] args) throws IOException {

BufferedImage hugeImage = ImageIO.read(PerformanceTest.class.getResource("12000X12000.jpg"));

System.out.println("Testing convertTo2DUsingGetRGB:");
for (int i = 0; i < 10; i++) {
long startTime = System.nanoTime();
int[][] result = convertTo2DUsingGetRGB(hugeImage);
long endTime = System.nanoTime();
System.out.println(String.format("%-2d: %s", (i + 1), toString(endTime - startTime)));
}

System.out.println("");

System.out.println("Testing convertTo2DWithoutUsingGetRGB:");
for (int i = 0; i < 10; i++) {
long startTime = System.nanoTime();
int[][] result = convertTo2DWithoutUsingGetRGB(hugeImage);
long endTime = System.nanoTime();
System.out.println(String.format("%-2d: %s", (i + 1), toString(endTime - startTime)));
}
}

private static int[][] convertTo2DUsingGetRGB(BufferedImage image) {
int width = image.getWidth();
int height = image.getHeight();
int[][] result = new int[height][width];

for (int row = 0; row < height; row++) {
for (int col = 0; col < width; col++) {
result[row][col] = image.getRGB(col, row);
}
}

return result;
}

private static int[][] convertTo2DWithoutUsingGetRGB(BufferedImage image) {

final byte[] pixels = ((DataBufferByte) image.getRaster().getDataBuffer()).getData();
final int width = image.getWidth();
final int height = image.getHeight();
final boolean hasAlphaChannel = image.getAlphaRaster() != null;

int[][] result = new int[height][width];
if (hasAlphaChannel) {
final int pixelLength = 4;
for (int pixel = 0, row = 0, col = 0; pixel < pixels.length; pixel += pixelLength) {
int argb = 0;
argb += (((int) pixels[pixel] & 0xff) << 24); // alpha
argb += ((int) pixels[pixel + 1] & 0xff); // blue
argb += (((int) pixels[pixel + 2] & 0xff) << 8); // green
argb += (((int) pixels[pixel + 3] & 0xff) << 16); // red
result[row][col] = argb;
col++;
if (col == width) {
col = 0;
row++;
}
}
} else {
final int pixelLength = 3;
for (int pixel = 0, row = 0, col = 0; pixel < pixels.length; pixel += pixelLength) {
int argb = 0;
argb += -16777216; // 255 alpha
argb += ((int) pixels[pixel] & 0xff); // blue
argb += (((int) pixels[pixel + 1] & 0xff) << 8); // green
argb += (((int) pixels[pixel + 2] & 0xff) << 16); // red
result[row][col] = argb;
col++;
if (col == width) {
col = 0;
row++;
}
}
}

return result;
}

private static String toString(long nanoSecs) {
int minutes = (int) (nanoSecs / 60000000000.0);
int seconds = (int) (nanoSecs / 1000000000.0) - (minutes * 60);
int millisecs = (int) ( ((nanoSecs / 1000000000.0) - (seconds + minutes * 60)) * 1000);


if (minutes == 0 && seconds == 0)
return millisecs + "ms";
else if (minutes == 0 && millisecs == 0)
return seconds + "s";
else if (seconds == 0 && millisecs == 0)
return minutes + "min";
else if (minutes == 0)
return seconds + "s " + millisecs + "ms";
else if (seconds == 0)
return minutes + "min " + millisecs + "ms";
else if (millisecs == 0)
return minutes + "min " + seconds + "s";

return minutes + "min " + seconds + "s " + millisecs + "ms";
}
}