ITEC2120:Spring2012:Section09

From GGCWiki
Jump to: navigation, search

Contents

Course Resources

Game Development

XNA and Connect Resources

Reading, Writing and Editing PPM Images with Java

Walkthrough from class 08-Mar-2012

Screencast Rich Media

Screencast Vodcast (mp4)

SimplePPMCreator.java

import java.io.*;

public class SimplePPMCreator {
  
  public static void main(String[] args) throws IOException { 
    
    PrintWriter outputFile = new PrintWriter("simple.ppm");
    
    outputFile.println("P3");
    outputFile.println("# CREATOR: GIMP PNM Filter Version 1.1");
    outputFile.println("2 2");
    outputFile.println("255");
    for (int i=0; i<4; i++) {
      outputFile.println("0");
      outputFile.println("0");
      outputFile.println("255");
    }
    outputFile.close();
    
  }  
}

SimplePPMReader.java

import java.io.*;
import java.util.Scanner;

public class SimplePPMReader {
  
  
  public static void main(String[] args) throws IOException { 
    
    // is in the same directory as my java and class files
    File file = new File("simple.ppm");
    Scanner inputFile = new Scanner(file);
    
    // read the header info
    String magic = inputFile.nextLine(); 
    System.out.println("magic=" + magic);
    String comment = inputFile.nextLine(); // we'll assume only one comment, in this place only
    System.out.println("comment=" + comment);
    int width = inputFile.nextInt();
    int height = inputFile.nextInt();
    inputFile.nextLine(); // read past end of line
    System.out.println("width=" + width + ", height=" + height);
    String maxCols = inputFile.nextLine();
    System.out.println("maxCols=" + maxCols);
    
    // read the raster of height row
    for (int i=0; i<height; i++) { // process each row
      for (int j=0; j<width; j++) {
       int red = inputFile.nextInt();
       inputFile.nextLine(); // read past end of line
       int green = inputFile.nextInt();
       inputFile.nextLine(); // read past end of line
       int blue =inputFile.nextInt();
       inputFile.nextLine(); // read past end of line
       System.out.println("red=" + red + ", green=" + green + ", blue=" + blue);
      }
    }
    
    // cleanup and exit
    inputFile.close();
    
  }
  
}

ProcessPPM.java

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

public class ProcessPPM {
  
  public static void main(String[] args) throws IOException { 
    
    Scanner keyboard = new Scanner(System.in);
    
    // get input file (assume it's in the same directory as this class)
    System.out.print("Enter the PPM input file (with the ppm extension):");
    String input = keyboard.nextLine();
    
    System.out.print("Enter the name of the PPM output file");
    String output = keyboard.nextLine();
    
    // open the file we'll be reading from
    File file = new File(input);
    Scanner inputfile = new Scanner(file);
    
    // open the file we'll be writing to, allow overwrites!
    PrintWriter outputfile = new PrintWriter(output);
    
    //Read the first Line of the input file
    String magic =  inputfile.nextLine(); // this is the 'magic number, we'll ignore it
    
    // output the magic number without any further processing
    outputfile.println(magic);
    
    //Read the # comment line
    String comment = inputfile.nextLine();
    
    //output the comment line
    outputfile.println(comment);
    
    //Get the width
    int width = inputfile.nextInt();
    int height = inputfile.nextInt();
    inputfile.nextLine(); // consume the carriage return
    
    //output the width and height
    outputfile.println(width + " " + height);
    
    //Get the max color value
    int maxCol = inputfile.nextInt();
    inputfile.nextLine(); // consume the carriage return
    
    // echo maxcol to outputfile 
    outputfile.println(maxCol);
    
    for (int i=0; i<height; i++) {
      for (int j=0; j<width; j++) {
        int red = inputfile.nextInt();
        inputfile.nextLine(); 
        int green = inputfile.nextInt();
        inputfile.nextLine(); 
        int blue = inputfile.nextInt();
        inputfile.nextLine(); 
        // System.out.println("RGB=" + red + "," + green + "," + blue);
        int grayScale = (int) (0.30 * red + 0.59 * green + 0.11 * blue);
        //for (int k=0; k< 3; k++) outputfile.println(grayScale);
        outputfile.println(grayScale);
        outputfile.println("0");
        outputfile.println(grayScale);
      }
    }
    System.out.println("produced: " + output);
    inputfile.close();
    outputfile.close();
  }
}

3x3.ppm

P3
# CREATOR: GIMP PNM Filter Version 1.1
3 3
255
255
0
0
0
255
0
0
0
255
255
255
255
128
128
128
0
0
0
0
0
255
0
255
0
255
0
0

Other

JPG image of bird

PPM Test Files

File:ITEC2120-Spring2012-Section09-yellow-rectangle120x120.ppm.gz

File:ITEC2120-Spring2012-Section09-whitehouse.ppm.gz

File:ITEC2120-Spring2012-Section09-wh1.ppm.gz

File:ITEC2120-Spring2012-Section09-my-grad200x200.ppm.gz

File:ITEC2120-Spring2012-Section09-grizz.ppm.gz

File:ITEC2120-Spring2012-Section09-grizz-bw.ppm.gz

Personal tools