Advertisement
Guest User

Untitled

a guest
Jun 19th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.76 KB | None | 0 0
  1. class TransformPGM {
  2.  
  3. /*
  4. * Main method: opens files, prompts the user for choice of transformation, and calls
  5. * other methods to do the rest of the work.
  6. */
  7. public static void main(String unused[]) {
  8. Scanner inputFile; // object for reading from the input file
  9. PrintWriter outputFile; // object for reading from the output file
  10. Scanner keyboard = new Scanner(System.in); // object for reading from the keyboard
  11.  
  12. // Get input file name and open a Scanner
  13. System.out.print("input file name: ");
  14. String inName = keyboard.nextLine();
  15. inputFile = FileOpen.openScanner(inName);
  16.  
  17. // read the "magic number"
  18. String shouldBeP2 = inputFile.next();
  19. // No checking required, so just assume shouldBeP2 is in fact "P2"
  20.  
  21. // read the width, height and max. pixel value
  22. int width = inputFile.nextInt();
  23. int height = inputFile.nextInt();
  24. int maxValue = inputFile.nextInt();
  25.  
  26. // read the pixels into an array
  27. int pixels[][] = new int[height][width];
  28. System.out.println("reading input file...");
  29. readPixels(inputFile, pixels);
  30. inputFile.close();
  31.  
  32. // Ask user which transformation to do and call the appropriate method to do it.
  33. System.out.println("Possible transformations are:");
  34. System.out.println(" 1. horizontal flip (mirror image)");
  35. System.out.println(" 2. vertical flip (upside down)");
  36. System.out.println(" 3. rotate clockwise");
  37. System.out.println(" 4. rotate counter-clockwise");
  38. System.out.print("Enter your choice: ");
  39. int transformNumber = keyboard.nextInt();
  40. keyboard.nextLine(); // discard rest of line
  41.  
  42. // Get output file name and open a Scanner
  43. System.out.print("output file name: ");
  44. String outName = keyboard.nextLine();
  45. outputFile = FileOpen.openWriter(outName);
  46.  
  47. // echo the magic number to the output file (doesn't depend on which transformation)
  48. outputFile.println("P2");
  49.  
  50. // Each of the four methods called below performs a transformation while writing to the output file.
  51. if (transformNumber == 1) {
  52. horizFlip(pixels, outputFile, maxValue);
  53. }
  54. else if (transformNumber == 2) {
  55. vertFlip(pixels, outputFile, maxValue);
  56. }
  57. else if (transformNumber == 3) {
  58. rotateClockwise(pixels, outputFile, maxValue);
  59. }
  60. else { // the transformNumber must be 4
  61. rotateCC(pixels, outputFile, maxValue);
  62. } // end if
  63.  
  64. outputFile.close();
  65. System.out.println("Done!");
  66. } // end main
  67.  
  68.  
  69. /*
  70. * This method reads pixel values (integers) from an input file into a 2-D array. Assumes
  71. * the file contains exactly enough integers to fill the array.
  72. */
  73. public static void readPixels(Scanner inputFile, int pixels[][]) {
  74. for (int row = 0; row < pixels.length; row++) {
  75. for (int col = 0; col < pixels[0].length; col++) {
  76. pixels[row][col] = inputFile.nextInt();
  77. } // end for
  78. } // end for
  79. } // end readPixels
  80.  
  81.  
  82. /*
  83. * This function writes an array of pixels to an output file, changing their order
  84. * so as to flip the image horizontally (a mirror image).
  85. * To avoid possible problems with long lines in the output file, I'm putting
  86. * each pixel on its own line.
  87. * The function writes width, height, max value and pixels, but assumes the
  88. * " magic number" has already been written.
  89. *
  90. * Parameters:
  91. * - a 2D array of pixels describing the image
  92. * - the output file
  93. * - the maximum pixel value
  94. */
  95. public static void horizFlip(int pixels[][], PrintWriter outputFile, int maxValue) {
  96. // Write width, height and maximum pixel value to the output file.
  97. // The width and height are the same as the width and height of the array.
  98. outputFile.println(pixels[0].length + " " + pixels.length + " " + maxValue);
  99.  
  100. // Write out the pixels, reversing the order of the pixels in each column.
  101. System.out.println("writing output file...");
  102. for (int row = 0; row < pixels.length; row++) {
  103. for (int col = pixels[0].length-1; col >= 0; col--) {
  104. outputFile.println(pixels[row][col] + " ");
  105. } // end for
  106. } // end for
  107. } // end horizFlip
  108.  
  109. /*
  110. * This function writes an array of pixels to an output file, changing their order
  111. * so as to flip the image vertically (turning it upside down).
  112. * To avoid possible problems with long lines in the output file, I'm putting
  113. * each pixel on its own line.
  114. * The function writes width, height, max value and pixels, but assumes the
  115. * " magic number" has already been written.
  116. *
  117. * Parameters:
  118. * - a 2D array of pixels describing the image
  119. * - the output file
  120. * - the maximum pixel value
  121. */
  122. public static void vertFlip(int pixels[][], PrintWriter outputFile, int maxValue) {
  123. // Write width, height and maximum pixel value to the output file.
  124. // The width and height are the same as the width and height of the array.
  125. outputFile.println(pixels[0].length + " " + pixels.length + " " + maxValue);
  126.  
  127. // Write out the pixels, reversing the order of the rows
  128. System.out.println("writing output file...");
  129. for (int row = pixels.length-1; row >= 0; row--) {
  130. for (int col = 0; col < pixels[0].length; col++) {
  131. outputFile.println(pixels[row][col] + " ");
  132. } // end for
  133. } // end for
  134. } // end vertFlip
  135.  
  136. /*
  137. * This function writes an array of pixels to an output file, changing their order
  138. * so as to rotate the image 90 degrees clockwise.
  139. * To avoid possible problems with long lines in the output file, I'm putting
  140. * each pixel on its own line.
  141. * The function writes width, height, max value and pixels, but assumes the
  142. * " magic number" has already been written.
  143. *
  144. * Parameters:
  145. * - a 2D array of pixels describing the image
  146. * - the output file
  147. * - the maximum pixel value
  148. */
  149. public static void rotateClockwise(int pixels[][], PrintWriter outputFile, int maxValue) {
  150. // Write width, height and maximum pixel value to the output file.
  151. // The width and height are reversed from width and height of the array.
  152. int newWidth = pixels.length;
  153. int newHeight = pixels[0].length;;
  154. outputFile.println(newWidth + " " + newHeight + " " + maxValue);
  155.  
  156. // Write out the pixels in rotated order:
  157. // row N of output file = column N of input file, in reverse order
  158. System.out.println("writing output file...");
  159. for (int row = 0; row < newHeight; row++) {
  160. for (int col = 0; col < newWidth; col++) {
  161. outputFile.println(pixels[newWidth - col - 1][row]);
  162. } // end for
  163. } // end for
  164. } // end rotateClockwise
  165.  
  166. /*
  167. * This function writes an array of pixels to an output file, changing their order
  168. * so as to rotate the image 90 degrees counter-clockwise.
  169. * To avoid possible problems with long lines in the output file, I'm putting
  170. * each pixel on its own line.
  171. * The function writes width, height, max value and pixels, but assumes the
  172. * " magic number" has already been written.
  173. *
  174. * Parameters:
  175. * - a 2D array of pixels describing the image
  176. * - the output file
  177. * - the maximum pixel value
  178. */
  179. public static void rotateCC(int pixels[][], PrintWriter outputFile, int maxValue) {
  180. // Write width, height and maximum pixel value to the output file.
  181. // The width and height are reversed from width and height of the array.
  182. int newWidth = pixels.length;
  183. int newHeight = pixels[0].length;;
  184. outputFile.println(newWidth + " " + newHeight + " " + maxValue);
  185.  
  186. // Write out the pixels in rotated order:
  187. // First row of output = last column of input, reversed
  188. // Second row of output = next-to-last column of input, reversed
  189. // and so on
  190. System.out.println("writing output file...");
  191. for (int row = 0; row < newHeight; row++) {
  192. for (int col = 0; col < newWidth; col++) {
  193. outputFile.println(pixels[col][newHeight - row - 1]);
  194. } // end for
  195. } // end for
  196. } // end rotateCC
  197.  
  198. } // end class TransformPGM
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement