Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class TransformPGM {
- /*
- * Main method: opens files, prompts the user for choice of transformation, and calls
- * other methods to do the rest of the work.
- */
- public static void main(String unused[]) {
- Scanner inputFile; // object for reading from the input file
- PrintWriter outputFile; // object for reading from the output file
- Scanner keyboard = new Scanner(System.in); // object for reading from the keyboard
- // Get input file name and open a Scanner
- System.out.print("input file name: ");
- String inName = keyboard.nextLine();
- inputFile = FileOpen.openScanner(inName);
- // read the "magic number"
- String shouldBeP2 = inputFile.next();
- // No checking required, so just assume shouldBeP2 is in fact "P2"
- // read the width, height and max. pixel value
- int width = inputFile.nextInt();
- int height = inputFile.nextInt();
- int maxValue = inputFile.nextInt();
- // read the pixels into an array
- int pixels[][] = new int[height][width];
- System.out.println("reading input file...");
- readPixels(inputFile, pixels);
- inputFile.close();
- // Ask user which transformation to do and call the appropriate method to do it.
- System.out.println("Possible transformations are:");
- System.out.println(" 1. horizontal flip (mirror image)");
- System.out.println(" 2. vertical flip (upside down)");
- System.out.println(" 3. rotate clockwise");
- System.out.println(" 4. rotate counter-clockwise");
- System.out.print("Enter your choice: ");
- int transformNumber = keyboard.nextInt();
- keyboard.nextLine(); // discard rest of line
- // Get output file name and open a Scanner
- System.out.print("output file name: ");
- String outName = keyboard.nextLine();
- outputFile = FileOpen.openWriter(outName);
- // echo the magic number to the output file (doesn't depend on which transformation)
- outputFile.println("P2");
- // Each of the four methods called below performs a transformation while writing to the output file.
- if (transformNumber == 1) {
- horizFlip(pixels, outputFile, maxValue);
- }
- else if (transformNumber == 2) {
- vertFlip(pixels, outputFile, maxValue);
- }
- else if (transformNumber == 3) {
- rotateClockwise(pixels, outputFile, maxValue);
- }
- else { // the transformNumber must be 4
- rotateCC(pixels, outputFile, maxValue);
- } // end if
- outputFile.close();
- System.out.println("Done!");
- } // end main
- /*
- * This method reads pixel values (integers) from an input file into a 2-D array. Assumes
- * the file contains exactly enough integers to fill the array.
- */
- public static void readPixels(Scanner inputFile, int pixels[][]) {
- for (int row = 0; row < pixels.length; row++) {
- for (int col = 0; col < pixels[0].length; col++) {
- pixels[row][col] = inputFile.nextInt();
- } // end for
- } // end for
- } // end readPixels
- /*
- * This function writes an array of pixels to an output file, changing their order
- * so as to flip the image horizontally (a mirror image).
- * To avoid possible problems with long lines in the output file, I'm putting
- * each pixel on its own line.
- * The function writes width, height, max value and pixels, but assumes the
- * " magic number" has already been written.
- *
- * Parameters:
- * - a 2D array of pixels describing the image
- * - the output file
- * - the maximum pixel value
- */
- public static void horizFlip(int pixels[][], PrintWriter outputFile, int maxValue) {
- // Write width, height and maximum pixel value to the output file.
- // The width and height are the same as the width and height of the array.
- outputFile.println(pixels[0].length + " " + pixels.length + " " + maxValue);
- // Write out the pixels, reversing the order of the pixels in each column.
- System.out.println("writing output file...");
- for (int row = 0; row < pixels.length; row++) {
- for (int col = pixels[0].length-1; col >= 0; col--) {
- outputFile.println(pixels[row][col] + " ");
- } // end for
- } // end for
- } // end horizFlip
- /*
- * This function writes an array of pixels to an output file, changing their order
- * so as to flip the image vertically (turning it upside down).
- * To avoid possible problems with long lines in the output file, I'm putting
- * each pixel on its own line.
- * The function writes width, height, max value and pixels, but assumes the
- * " magic number" has already been written.
- *
- * Parameters:
- * - a 2D array of pixels describing the image
- * - the output file
- * - the maximum pixel value
- */
- public static void vertFlip(int pixels[][], PrintWriter outputFile, int maxValue) {
- // Write width, height and maximum pixel value to the output file.
- // The width and height are the same as the width and height of the array.
- outputFile.println(pixels[0].length + " " + pixels.length + " " + maxValue);
- // Write out the pixels, reversing the order of the rows
- System.out.println("writing output file...");
- for (int row = pixels.length-1; row >= 0; row--) {
- for (int col = 0; col < pixels[0].length; col++) {
- outputFile.println(pixels[row][col] + " ");
- } // end for
- } // end for
- } // end vertFlip
- /*
- * This function writes an array of pixels to an output file, changing their order
- * so as to rotate the image 90 degrees clockwise.
- * To avoid possible problems with long lines in the output file, I'm putting
- * each pixel on its own line.
- * The function writes width, height, max value and pixels, but assumes the
- * " magic number" has already been written.
- *
- * Parameters:
- * - a 2D array of pixels describing the image
- * - the output file
- * - the maximum pixel value
- */
- public static void rotateClockwise(int pixels[][], PrintWriter outputFile, int maxValue) {
- // Write width, height and maximum pixel value to the output file.
- // The width and height are reversed from width and height of the array.
- int newWidth = pixels.length;
- int newHeight = pixels[0].length;;
- outputFile.println(newWidth + " " + newHeight + " " + maxValue);
- // Write out the pixels in rotated order:
- // row N of output file = column N of input file, in reverse order
- System.out.println("writing output file...");
- for (int row = 0; row < newHeight; row++) {
- for (int col = 0; col < newWidth; col++) {
- outputFile.println(pixels[newWidth - col - 1][row]);
- } // end for
- } // end for
- } // end rotateClockwise
- /*
- * This function writes an array of pixels to an output file, changing their order
- * so as to rotate the image 90 degrees counter-clockwise.
- * To avoid possible problems with long lines in the output file, I'm putting
- * each pixel on its own line.
- * The function writes width, height, max value and pixels, but assumes the
- * " magic number" has already been written.
- *
- * Parameters:
- * - a 2D array of pixels describing the image
- * - the output file
- * - the maximum pixel value
- */
- public static void rotateCC(int pixels[][], PrintWriter outputFile, int maxValue) {
- // Write width, height and maximum pixel value to the output file.
- // The width and height are reversed from width and height of the array.
- int newWidth = pixels.length;
- int newHeight = pixels[0].length;;
- outputFile.println(newWidth + " " + newHeight + " " + maxValue);
- // Write out the pixels in rotated order:
- // First row of output = last column of input, reversed
- // Second row of output = next-to-last column of input, reversed
- // and so on
- System.out.println("writing output file...");
- for (int row = 0; row < newHeight; row++) {
- for (int col = 0; col < newWidth; col++) {
- outputFile.println(pixels[col][newHeight - row - 1]);
- } // end for
- } // end for
- } // end rotateCC
- } // end class TransformPGM
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement