-2

Class 1 import java.util.Scanner;

public class Image {

public static void main(String[] args) {
    String filename = "test.ppm";
    TestClass img = new TestClass(filename);

    StdDraw.setCanvasSize(img.getWidth(), img.getHeight());
    StdDraw.setXscale(0, img.getWidth());
    StdDraw.setYscale(0, img.getHeight());

    Scanner keyboard = new Scanner(System.in);

    img.display();

    System.out.println("Type anything then enter");
    keyboard.next();

    TestClass modified = new TestClass(filename);
    modified.removeReds();
    modified.display();
}
}

Class 2

import java.awt.Color;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class TestClass {
private int width;
private int height;
private Color[][] data;

public TestClass(String filename) {
    Scanner fileIn = null;
    try {
        fileIn = new Scanner(new FileInputStream(filename));
    } catch (FileNotFoundException e) {
        System.out.println("File not found");
        System.out.println("Looked in " +
                System.getProperty("user.dir"));
        System.exit(1);
    }

    // Header information
    String format = fileIn.nextLine();
    width = fileIn.nextInt();
    height = fileIn.nextInt();
    int colorDepth = fileIn.nextInt();

    System.out.println("File: " + filename + " is " + width
            + " x " + height + " pixels");

    // Verify file format
    if (!format.equals("P3") || colorDepth != 255) {
        System.out.println("Unknown format for file");
        System.exit(1);
    }

    // Read in data pixel by pixel
    data = new Color[height][width];
    for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
            int red = fileIn.nextInt();
            int green = fileIn.nextInt();
            int blue = fileIn.nextInt();

            data[i][j] = new Color(red, green, blue);
        }
    }
}

public void display() {
    for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
            StdDraw.setPenColor(data[i][j]);
            StdDraw.point(j, height - i - 1);
        }
    }
}

public int getHeight() {
    return height;
}
public int getWidth() {
    return width;
}

public void removeReds() {
    for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
            int red = data[i][j].getRed();
            int green = data[i][j].getGreen();
            int blue = data[i][j].getBlue();

            int sum = red+green+blue;
            Color newColor = new Color(sum / 3);



        }
    }
}
}

So i need to turnn this image greyscale. I know that you need to avg the red green and blue. I tried to but when every i run the program it just alters the colors making them a little darker

1 Answers1

0

As far as I can tell, there's nowhere in your code where you actually modify the color of the pixels to the greyscale value. You can do this with the line data[i][j] = newColor in the for-loop in the removeRed() method (which should be called the greyScale() method). As it stands, you just find the greyscale color, then throw it away, and you also might be calculating it wrong.

Lavaman65
  • 863
  • 1
  • 12
  • 22