0 Replies - 337 Views - Last Post: 22 April 2011 - 03:22 PM Rate Topic: -----

#1 tubbzor  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 29-November 10

2D arrays in PGM file modification

Posted 22 April 2011 - 03:22 PM

I'm working on a program which does various things to a .pgm file. I have the invert, extreme, and horizontalFlip methods working perfectly, my trouble is the grow, shrink, and verticalFlip methods. For the grow and shrink, my code resizes the window to the right size, but I cannot fill the temporary array correctly to get it to work, and for the verticalFlip, I cannot get my for loops to work correctly like they do in the horizontalFlip..any help/hints in the right direction would be apprectiated, thanks!
import java.io.*;
import java.util.Scanner;

public class Pic
{
    // Once we've read an image, it implicitly has this maximum value:
    static final int MAXVAL = 255;

    // This 2-d array contains the pixel values.
    private int[][] pixels = null;

    // Constructor for the class Pic
    public Pic(String path) throws Exception {
        pixels = readPGM(path);
    }

    // Read the picture from the given path.
    //
    // An ASCII PGM file looks like this:
    // P2
    // width height
    // maxval
    // one number per pixel

    private int[][] readPGM(String path) throws Exception {
	Scanner in = new Scanner(new File(path));
	String magic = in.next();
	if (!magic.equals("P2")) {
	    System.err.println("ERROR: "+path+" is not an ASCII PGM file");
	    System.exit(1);
	}
	int width = in.nextInt();
	int height = in.nextInt();
	int fileMaxval = in.nextInt();
	int[][] pic = new int[height][width];

	// Read the pixels
	for (int y=0; y<height; y++)
	    for (int x=0; x<width; x++)
	        pic[y][x] = in.nextInt();

	in.close();

	// We want our values to be in the range 0-MAXVAL.  If the file data
	// wasn't in that range, scale it up to be in that range.

	if (fileMaxval != MAXVAL)
	    for (int j=0; j<height; j++)
		for (int i=0; i<width; i++)
		    pic[j][i] = (pic[j][i] * MAXVAL)/fileMaxval;

	return pic;
    }


    // Flip the image horizontally.
    // If it's a person looking left, they'll end up looking right.

	public void horizontalFlip(){
		//CHANGE THIS
		int x;
		int y;
		for(int r = 0; r < pixels.length; r++)
		{
			for(int c = 0; c < pixels[r].length/2; c++)
			{
				x = pixels[r].length-1 - c;
				y = pixels[r][c];
				pixels[r][c] = pixels[r][x];
				pixels[r][x] = y;
			}
		}
	}


    // Flip the image vertically.
    // A picture of a person will end up standing on their head.

    public void verticalFlip() {
	// CHANGE THIS
		int x;
		int y;
		for(int c = 0; c < pixels[0].length/2; c++)
		{
			for(int r = 0; r < pixels.length; r++)
			{
				x = pixels[c].length - 1 - r;
				y = pixels[r][c];
				pixels[r][c] = pixels[r][x];
				pixels[x][c] = y;
			}
		}
		
		
    }

    // Switch to extreme pixels.
    // Each pixel becomes 0 or MAXVAL, whichever is closer.

    public void extreme() {
	// CHANGE THIS
	for(int r = 0; r < pixels.length; r++)
	{
		for(int c = 0; c < pixels[0].length; c++)
		{
			if((MAXVAL - pixels[r][c]) > pixels[r][c])
				pixels[r][c] = MAXVAL;
			else
				pixels[r][c] = 0;
		}
	}
    }

    // Invert the image.
    // That is, if a pixel has the value 0, replace it with MAXVAL.
    // If a pixel has the value 1, replace it with MAXVAL-1.
    // If a pixel has the value 42, replace it with MAXVAL-42, etc.

    public void invert() {
	// CHANGE THIS
	for(int r = 0; r < pixels.length; r++)
	{
		for(int c = 0; c < pixels[0].length; c++)
		{
			if(pixels[r][c] == 0)
				pixels[r][c] = 0;
			else
				pixels[r][c] = MAXVAL - pixels[r][c];
		}
	}
    }


    // Shrink the image.
    // Use every other pixel--the ones from the even-numbered rows & columns.

    public void shrink() {
	// CHANGE THIS
	int x = 0;
	int y = 0;
	int[][] array = new int[pixels.length/2][pixels[0].length/2];
	for(int r = 0; r < pixels.length; r++)
	{
		for(int c = 0; c < pixels[0].length; c++)
		{
			
		}
	}
	pixels = array;
    }


    // Grow the image.
    // Double each pixel in each direction.

    public void grow() {
	// CHANGE THIS
	int x = 0;
	int y = 0;
	int[][] array = new int[pixels.length*2][pixels[0].length*2];
	for(int r = 0; r < pixels.length; r+=2)
	{
		for(int c = 0; c < pixels[0].length; c+=2)
		{
			array[r][c] = pixels[r][c];
			array[r][c+1] = pixels[r][c];
			array[r+1][c] = pixels[r][c];
			array[r+1][c+1] = pixels[r][c];			
		}
		
	}
	pixels = array;
    }



    // Write the picture to the given path.

    public void writePGM(String path) throws Exception {
	int height = pixels.length;
	int width = pixels[0].length;
	PrintStream out = new PrintStream(new FileOutputStream(path));
	out.println("P2");
	out.println(width+" "+height);
	out.println(MAXVAL);			// maximum pixel value
	for (int y=0; y<height; y++)
	    for (int x=0; x<width; x++)
		out.println(pixels[y][x]);	// Yeah, one pixel per line.  So?
	out.close();
    }

    public static void usage() {
	System.err.println("usage: java Pic <infile> <outfile> (h|v|e|i|s|g) ...");
	System.exit(1);
    }

    public static void main(String[] args) throws Exception {
	if (args.length < 2)
	    usage();

        Scanner keyboard = new Scanner(System.in);

        Pic picture = new Pic(args[0]);

	for (int i=2; i<args.length; i++) {
	    String op = args[i];
	    if (op.equals("h"))
		picture.horizontalFlip();
	    else if (op.equals("v"))
		picture.verticalFlip();
	    else if (op.equals("e"))
		picture.extreme();
	    else if (op.equals("i"))
		picture.invert();
	    else if (op.equals("s"))
		picture.shrink();
	    else if (op.equals("g"))
		picture.grow();
	    else
	    	System.err.println("Invalid operation \""+op+"\"");
	}

	picture.writePGM(args[1]);
    }
}


This post has been edited by tubbzor: 22 April 2011 - 03:24 PM


Is This A Good Question/Topic? 0
  • +

Page 1 of 1