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

I am trying to set this Java program up correctly. It is supposed to read a PPM

ID: 3803831 • Letter: I

Question

I am trying to set this Java program up correctly. It is supposed to read a PPM image file and then alter the image in someway. I've written the negateRed method to change red color values to their negative value. I have set up the array buffer to read in one row of image at a time according to assignment specifications. However, when I run the program to negate the red colors, it is only iterating through the first row of the image. How can I change this so that I iterate through values of the entire file?

import java.util.Scanner;
import java.io.File;
import java.io.PrintWriter;

public class PPM Image {
public static void main ( String args[] ) {
try {
System.out.print ( " PPM Image Processor " );
Scanner keyboard = new Scanner ( System.in );
System.out.print ( " Enter input filename: " );
String inFile;
inFile = keyboard.next();
File file = new File( inFile );
Scanner input = new Scanner ( file );

String first = input.next();
int columns = input.nextInt();
int row = input.nextInt();
int max = input.nextInt();
int x = ( columns * 3 );
int y = 0;
int buffer[] = new int [x];

Scanner keyboardTwo = new Scanner ( System.in );
String outFile;
System.out.print ( "Enter output filename: " );
outFile = keyboardTwo.next();

PrintWriter writer = new PrintWriter ( outFile );
writer.println ( first );
writer.print ( columns );
writer.printf ( " %d", row );
writer.printf ( " %d ", max );
  
while ( y < x ) {
int v = input.nextInt();
buffer[y] = v;
y++;
}

negateRed ( buffer, x );

for ( int i = 0; i < buffer.length; i++ ) {
writer.println ( buffer[i] );
}
writer.close();
}
  

catch ( Exception ex ) {
System.out.println ( "An error has occurred!" );
ex.printStackTrace();
}
}
  
/**
* negateRed
*
* This method negates the red value in the image. All P3-format
* PPM images have an image depth of 255, which are used in
* the method's calculations.
*
* @param buffer an integer array containing the image buffer
* @param x an integer w/ the number of columns in the image
* @return NONE
*/
public static void negateRed ( int [] buffer, int x ) {
  
for ( int i = 0; i < x; i++ ) {
if ( i % 3 == 0 )
buffer[i] = 255 - buffer[i];
}
}
}

Explanation / Answer

import java.util.Scanner;
import java.io.File;
import java.io.PrintWriter;

public class PPM Image {
public static void main ( String args[] ) {
try {
System.out.print ( " PPM Image Processor " );
Scanner keyboard = new Scanner ( System.in );
System.out.print ( " Enter input filename: " );
String inFile;
inFile = keyboard.next();
File file = new File( inFile );
Scanner input = new Scanner ( file );

String first = input.next();
int columns = input.nextInt();
int row = input.nextInt();
int max = input.nextInt();
int x = ( columns * 3 );
int y = 0;
int buffer[] = new int [x];

Scanner keyboardTwo = new Scanner ( System.in );
String outFile;
System.out.print ( "Enter output filename: " );
outFile = keyboardTwo.next();

PrintWriter writer = new PrintWriter ( outFile );
writer.println ( first );
writer.print ( columns );
writer.printf ( " %d", row );
writer.printf ( " %d ", max );
  
while ( y < x ) {
int v = input.nextInt();
buffer[y] = v;
y++;
}

negateRed ( buffer, x );

for ( int i = 0; i < buffer.length; i++ ) {
writer.println ( buffer[i] );
}
writer.close();
}
  

catch ( Exception ex ) {
System.out.println ( "An error has occurred!" );
ex.printStackTrace();
}
}
  
/**
* negateRed
*
* This method negates the red value in the image. All P3-format
* PPM images have an image depth of 255, which are used in
* the method's calculations.
*
* @param buffer an integer array containing the image buffer
* @param x an integer w/ the number of columns in the image
* @return NONE
*/
public static void negateRed ( int [] buffer, int x ) {
  
for ( int i = 0; i < x; i++ ) {
if ( i % 3 == 0 )
buffer[i] = 255 - buffer[i];
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote