Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

public class Image{ // These are the class attributes needed to complete this lab // image dimensions // MaxVal is used for reading in the

public class Image{ // These are the class attributes needed to complete this lab // image dimensions // MaxVal is used for reading in the ppm file. for the default constructor, you can init the value as 0

private int width; private int height; private int maxVal;

// image array and processed array definitions private int[] image_array;// array holding the pixel values of the read in image ([r,g,b,r,g,b,...etc]) private int[] processed_array;// array holding the pixel values of the grayscale image ([r,g,b,r,g,b,...etc])

// constructors public Image() { width = 5; height = 8; maxVal = 6;

}

// creates an image object by reading the input image public Image(String input_file) { try { Scanner sc = new Scanner(new File(input_file)); width = sc.nextInt(); height = sc.nextInt(); maxVal = sc.nextInt();

image_array = new int[height * width * 3]; int k = 0; for (int i = 0; i < height * width * 3; i++) { image_array[i] = sc.nextInt(); } } catch (FileNotFoundException ex) { System.out.println("Input image file mot found!" + ex); ex.printStackTrace();

}

}

// accessor public int getWidth() { return width; }

//TODO: set width mutator public void setWidth(int w) { width = w;

}

public int getHeight() { return height; }

public void setHeight(int h) { height = h;

}

// reads an image from the given input image file //This function is given to you as a help in understanding how the pixel data is read and initialized public void read(String file_name) { // open the file try { Scanner sc = new Scanner(new File(file_name));

// read the header (assumes PPM text images) String magic; magic = sc.next(); width = sc.nextInt(); height = sc.nextInt(); maxVal = sc.nextInt();

System.out.println("Header:" + magic + " " + width + " " + height + " " + maxVal);

//make sure the image arrays are null and then initialize them if (image_array == null) { image_array = new int[width * height * 3]; }

if (processed_array == null) { processed_array = new int[width * height * 3]; }

// read the pixels int k = 0; for (int i = 0; i < height * width * 3; i++) { image_array[i] = sc.nextInt(); } } catch (FileNotFoundException ex) { System.out.println("Input image file not found!" + ex); ex.printStackTrace(); } }

public void setColorGrid(ColorGrid cg, Boolean processed) {

if (processed) { for (int i = 0; i < height; i++) { for (int j = 0; j < width; i++) { int r = image_array[(i * width + j) * 3]; int g = image_array[(i * width + j) * 3 + 1]; int b = image_array[(i * width + j) * 3 + 2]; cg.set(i, j, new Color(r, g, b));

} } } else { for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { int value = image_array[i * width + j]; cg.set(i, j, new Color(value, value, value)); } } }

}

//Function to convert the pixel values from an image into grayscale. //TODO: Loop over the image array and convert the the pixels using this grayscale formula and store them //in the processed array //(r * 0.299 + g * 0.587+ b * 0.114) public void toGrayscale(){ }

};

I just need the code for the last TODO. Please answer correctly, please. Thanks.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Object Databases The Essentials

Authors: Mary E. S. Loomis

1st Edition

020156341X, 978-0201563412

More Books

Students also viewed these Databases questions