Question
JAVA QUestion I have main and image class and extend class already all i need to do is to make task 1 & 2 &
JAVA QUestion
I have main and image class and extend class already
all i need to do is to make task 1 & 2 & 3 and i think i finished 1 & 2 can you check for me
and plz do the task 3 plz
Task 1 - Conversion a 24-bit Color to a 8-bit Gray-scale (10 pts)
Read the 24bit input PPM image. Convert 24-bit color pixels to gray-scale pixels. Use the following equation to compute the gray-scale value from R(Red), G(Green), and B(Blue) of each pixel.
Gray = round(0.299 * R + 0.587 * G + 0.114 * B)
After the computation, make sure that Gray is an integer ranging 0-255. Truncate if it is not in the range. Display the output and save it into a PPM file.
Task 2 - Conversion a 24-bit Color to a N-level (50 pts) Read the 24bit input PPM color image. Convert 24-bit color pixels to N-level pixels. For example, if N=2, it is to convert the input to a bi-level image (i.e. quantize the gray scale value into one of two values, 0 or 255). If N=4, it is to convert the input to quad-level images (i.e. quantize the gray scale value into one of four values, 0, 85, 170 or 255).
Steps:
a. Convert a 24-bit color image into a gray-scale image using the method in Task1.
b. Receive a value for N from the user. Assume that the user will enter 2 (1bit), 4 (2bits), 8 (3bits), or 16 (4 bits) for N.
c. Display and save two output images: o Output1: N-level image produced by the threshold method. For example, if N=2, use a threshold value (such as 128) to determine to quantize each gray scale value into 0 or 255. o Output2: N-level image produced by the error diffusion method.
3. Task 3 - Uniform Color Quantization (40 pts)
Read the 24bit color image. Quantize 24bit colors to 8bit colors using the Uniform Color Quantization method.
Step1: Generate the 8-bit color Look Up Table (LUT) by the Uniform Color Quantization and display the table to the console as shown below.
Step2: Convert each 24-bit pixel of the input image into an 8-bit index value. Save the 8-bit color index values into [InputFileName]-index.ppm file. Assign the same index values to R, G, and B.
Step3: Save and display the 8-bit indexed-color image. To do this, you have to retrieve (or get) RGB values from the lookup table (LUT) using the index value of each pixel. Do not overwrite the original input in order to save your output. Save your output into a new image (named [InputFileName]-QT8.ppm).
Do not create one mega-size main class.
Do not modify existing methods of Image class.
Feel free to extend Image class.
You might want to add instance variables and methods to given classes, create subclasses, or add new classes.
Test your program with all test data.
------------------------------------------------------------ THis IS IMAGE CLASS -----------------------------------------------
import java.io.*;
import java.util.*;
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import javax.imageio.stream.FileImageInputStream;
// A wrapper class of BufferedImage
// Provide a couple of utility functions such as reading from and writing to PPM file
public class Image
{
private BufferedImage img;
private String fileName; // Input file name
private int pixelDepth=3; // pixel depth in byte
public Image(int w, int h)
// create an empty image with w(idth) and h(eight)
{
fileName = "";
img = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
System.out.println("Created an empty image with size " + w + "x" + h);
}
public Image(String fn)
// Create an image and read the data from the file
{
fileName = fn;
readPPM(fileName);
System.out.println("Created an image from " + fileName+ " with size "+getW()+"x"+getH());
}
public int getW()
{
return img.getWidth();
}
public int getH()
{
return img.getHeight();
}
public int getSize()
// return the image size in byte
{
return getW()*getH()*pixelDepth;
}
public void setPixel(int x, int y, byte[] rgb)
// set byte rgb values at (x,y)
{
int pix = 0xff000000 | ((rgb[0] & 0xff)
img.setRGB(x,y,pix);
}
public void setPixel(int x, int y, int[] irgb)
// set int rgb values at (x,y)
{
byte[] rgb = new byte[3];
for(int i=0;i
rgb[i] = (byte) irgb[i];
setPixel(x,y,rgb);
}
public void getPixel(int x, int y, byte[] rgb)
// retreive rgb values at (x,y) and store in the byte array
{
int pix = img.getRGB(x,y);
rgb[2] = (byte) pix;
rgb[1] = (byte)(pix>>8);
rgb[0] = (byte)(pix>>16);
}
public void getPixel(int x, int y, int[] rgb)
// retreive rgb values at (x,y) and store in the int array
{
int pix = img.getRGB(x,y);
byte b = (byte) pix;
byte g = (byte)(pix>>8);
byte r = (byte)(pix>>16);
// converts singed byte value (~128-127) to unsigned byte value (0~255)
rgb[0]= (int) (0xFF & r);
rgb[1]= (int) (0xFF & g);
rgb[2]= (int) (0xFF & b);
}
public void displayPixelValue(int x, int y)
// Display rgb pixel in unsigned byte value (0~255)
{
int pix = img.getRGB(x,y);
byte b = (byte) pix;
byte g = (byte)(pix>>8);
byte r = (byte)(pix>>16);
System.out.println("RGB Pixel value at ("+x+","+y+"):"+(0xFF & r)+","+(0xFF & g)+","+(0xFF & b));
}
public void readPPM(String fileName)
// read a data from a PPM file
{
File fIn = null;
FileImageInputStream fis = null;
try{
fIn = new File(fileName);
fis = new FileImageInputStream(fIn);
System.out.println("Reading "+fileName+"...");
// read Identifier
if(!fis.readLine().equals("P6"))
{
System.err.println("This is NOT P6 PPM. Wrong Format.");
System.exit(0);
}
// read Comment line
String commentString = fis.readLine();
// read width & height
String[] WidthHeight = fis.readLine().split(" ");
int width = Integer.parseInt(WidthHeight[0]);
int height = Integer.parseInt(WidthHeight[1]);
// read maximum value
int maxVal = Integer.parseInt(fis.readLine());
if(maxVal != 255)
{
System.err.println("Max val is not 255");
System.exit(0);
}
// read binary data byte by byte and save it into BufferedImage object
int x,y;
byte[] rgb = new byte[3];
img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for(y=0;y { for(x=0;x { rgb[0] = fis.readByte(); rgb[1] = fis.readByte(); rgb[2] = fis.readByte(); setPixel(x, y, rgb); } } fis.close(); System.out.println("Read "+fileName+" Successfully."); } // try catch(Exception e) { System.err.println(e.getMessage()); } } public void write2PPM(String fileName) // wrrite the image data in img to a PPM file { FileOutputStream fos = null; PrintWriter dos = null; try{ fos = new FileOutputStream(fileName); dos = new PrintWriter(fos); System.out.println("Writing the Image buffer into "+fileName+"..."); // write header dos.print("P6"+" "); dos.print("#CS451"+" "); dos.print(getW() + " "+ getH() +" "); dos.print(255+" "); dos.flush(); // write data int x, y; byte[] rgb = new byte[3]; for(y=0;y { for(x=0;x { getPixel(x, y, rgb); fos.write(rgb[0]); fos.write(rgb[1]); fos.write(rgb[2]); } fos.flush(); } dos.close(); fos.close(); System.out.println("Wrote into "+fileName+" Successfully."); } // try catch(Exception e) { System.err.println(e.getMessage()); } } public void display() // display the image on the screen { // Use a label to display the image //String title = "Image Name - " + fileName; String title = fileName; JFrame frame = new JFrame(title); JLabel label = new JLabel(new ImageIcon(img)); frame.add(label, BorderLayout.CENTER); frame.pack(); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); } } // Image class ------------------------------------------------------------------ end of image class ----------------------------------------------- ----------------------------------------------------------------- Main Class ---------------------------------------------------- import java.util.Scanner; public class MainApp { public static void main(String[] args) { // if there is no commandline argument, exit the program if (args.length != 1) { usage(); System.exit(1); } getMenu(); Image image1 = new Image(args[0]); } public static void getMenu() { Scanner input = new Scanner(System.in); System.out.println("Main Menu----------------------------------------------"); System.out.println("1) Conversion to Gray-scale Image (24bits->8bits)"); System.out.println("2) Conversion to N-level Image"); System.out .println("3) Conversion to 8bit Indexed Color Image using Uniform Color Quantization (24bits->8bits)"); System.out.println("4) Conversion to 8bit Indexed Color Image using [your selected method] (24bits->8bits)"); System.out.println("5) Quit"); boolean quit = false; int option; do { System.out.print("Please choose an option: "); option = input.nextInt(); switch (option) { case 1: convertToGrayScale(); break; case 2: convertToNthLevel(); break; case 3: convertUCQ(); break; case 4: convertOwnMethod(); break; case 5: quit = true; break; default: System.out.println("Invalid choice."); } } while (!quit); System.out.println("--Good Bye--"); } public static void usage() { System.out.println(" Usage: java CS4551_Main [input_ppm_file] "); } } -------------------------------------------- end of main class --------------------------------------------- -----------------------------------extend class ( plz do the all the change here only ) check task 1 & 2 works and make task 3 ----------------------- import java.awt.BorderLayout; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.Scanner; import javax.imageio.ImageIO; import javax.swing.ImageIcon; import javax.swing.JFrame; import javax.swing.JLabel; public class ImageQuantization extends Image { private BufferedImage img; private String fn; private int pixelDepth = 3; public ImageQuantization(String fn) { super(fn); readPPM(fn); System.out.println("Created an image from " + fn + " with size " + getW() + "x" + getH()); } public void convertToGrayScale(String fn) { BufferedImage imgCopy = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_RGB); imgCopy = img; try { File file = new File(fn); imgCopy = ImageIO.read(file); for (int y = 0; y for (int x = 0; x int pixel = imgCopy.getRGB(x, y); int red = ((pixel >> 16) & 0xff); int green = ((pixel >> 8) & 0xff); int blue = (pixel & 0xff); int gray = (int) Math.round(0.299 * red + 0.587 * green + 0.114 * blue); pixel = (gray img.setRGB(x, y, pixel); } } File ouptut = new File("grayscale.jpg"); ImageIO.write(img, "jpg", ouptut); } catch (IOException error) { System.out.println(error); } } public void convertToNthLevel(String fn) { Scanner scan = new Scanner(System.in); BufferedImage imgCopy = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_RGB); imgCopy = img; BufferedImage finalThresholdImage = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_RGB); int red = 0; int green = 0; int blue = 0; System.out.print("Enter the N value: "); int n = scan.nextInt(); try { File file = new File(fn); imgCopy = ImageIO.read(file); for (int y = 0; y for (int x = 0; x int pixel = imgCopy.getRGB(x, y); red = ((pixel >> 16) & 0xff); green = ((pixel >> 8) & 0xff); blue = (pixel & 0xff); int gray = (int) Math.round(0.299 * red + 0.587 * green + 0.114 * blue); pixel = (gray img.setRGB(x, y, pixel); if ((red + green + green) / 3 finalThresholdImage.setRGB(x, y, red File ouptut = new File("new.jpg"); ImageIO.write(img, "jpg", ouptut); } else { finalThresholdImage.setRGB(x, y, 255 File ouptut = new File("new.jpg"); ImageIO.write(img, "jpg", ouptut); } } } } catch (IOException error) { System.out.println(error); } } public void convertUCQ(String fn) { BufferedImage imgCopy = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_RGB); imgCopy = img; try { File file = new File(fn); imgCopy = ImageIO.read(file); } catch (IOException error) { System.out.println(error); } } public void display() // display the image on the screen { // Use a label to display the image // String title = "Image Name - " + fileName; String title = fn; JFrame frame = new JFrame(title); JLabel label = new JLabel(new ImageIcon(img)); frame.add(label, BorderLayout.CENTER); frame.pack(); frame.setVisible(true); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); } public void getRedComp(String fileName) { BufferedImage imgCopy = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_RGB); imgCopy = img; try { File file = new File(fileName); imgCopy = ImageIO.read(file); } catch (IOException error) { System.out.println(error); } for (int y = 0; y for (int x = 0; x int pixel = imgCopy.getRGB(x, y); int red = ((pixel >> 16) & 0xff); pixel = 0xff000000 | ((red) imgCopy.setRGB(x, y, pixel); } } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started