Browse code

Organized and documented

haywalk authored on 30/03/2021 12:30:21
Showing 2 changed files

1 1
Binary files a/PixelPoke.class and b/PixelPoke.class differ
... ...
@@ -1,14 +1,26 @@
1 1
 /*
2
- * PixelPoke by Hayden Walker, for the AP Computer Science Principles final project
3
- * 26 March 2021
2
+ * PixelPoke by Hayden Walker, 26-30 March 2021.
3
+ * Made for the AP Computer Science Principles final project.
4
+ * 
5
+ * This is a simple program that will create a negative of an image.
6
+ * Usage: java PixelPoke [inputfile] [outputfile]
7
+ * (Output file is optional; if none is provided, the input file will be written over.)
4 8
  */
5 9
 
10
+// Import the necessary libraries for reading and writing from/to image files
6 11
 import java.awt.image.*;
7 12
 import java.io.*;
8 13
 import javax.imageio.*;
9 14
 
10 15
 public class PixelPoke {
11 16
 	public static void main(String[] args) throws IOException {
17
+		/*
18
+		 * The main function will read an image from a file (provided by the user
19
+		 * via command-line arguments) , pass it into the function that returns its negative, 
20
+		 * and will write the return value of that function to an output file, or if none 
21
+		 * is specified, it will overwrite the input file.
22
+		 */
23
+		 
12 24
 		// Create variables to store names of input and output files
13 25
 		String inputFileName = "";
14 26
 		String outputFileName = "";
... ...
@@ -19,10 +31,11 @@ public class PixelPoke {
19 31
 			inputFileName = args[0];
20 32
 			outputFileName = args[1];
21 33
 		} else if(args.length == 1) {
22
-			// If user only specified input, overwrite it.
34
+			// If user only specified input, overwrite it
23 35
 			inputFileName = args[0];
24 36
 			outputFileName = inputFileName;
25 37
 		} else {
38
+			// If there are no/too many arguments, throw an error
26 39
 			inputError();
27 40
 		}
28 41
 		
... ...
@@ -30,16 +43,29 @@ public class PixelPoke {
30 43
 		File inputFile = new File(inputFileName);
31 44
 		File outputFile = new File(outputFileName);
32 45
 		
33
-		// Create a new buffered image
46
+		// Create a new buffered image to store the input file
34 47
 		BufferedImage img = null;
35 48
 				
36
-		// Read the input file
37 49
 		try {
50
+			// Read the input file to the buffered image variable
38 51
 			img = ImageIO.read(inputFile);
39 52
 		} catch(IOException e) {
53
+			// If reading fails, throw an error
40 54
 			inputError();
41 55
 		}
42 56
 		
57
+		// Make a negative of the image
58
+		BufferedImage imgOut = negative(img);
59
+		
60
+		// Write the negative to the output file
61
+		ImageIO.write(imgOut, "jpg", outputFile);
62
+	}
63
+	
64
+	static BufferedImage negative(BufferedImage img) {
65
+		/*
66
+		 * This function takes in a buffered image and returns the image's negative.
67
+		 */
68
+		
43 69
 		// Get the dimensions of the image
44 70
 		int width = img.getWidth();
45 71
 		int height = img.getHeight();
... ...
@@ -47,18 +73,21 @@ public class PixelPoke {
47 73
 		// Iterate through each row of the image
48 74
 		for(int row = 0; row < height; row++) {
49 75
 			// Iterate through each column
50
-			for(int column = 0; column < width; column++){
76
+			for(int column = 0; column < width; column++) {
51 77
 				// Invert each pixel by subtracting its current colour value from that of white
52 78
 				img.setRGB(column, row, 0xFFFFFF - img.getRGB(column, row));
53 79
 			}
54 80
 		}
55 81
 		
56
-		// Write the modified image to the output file
57
-		ImageIO.write(img, "jpg", outputFile);
82
+		// Return the modified image
83
+		return img;
58 84
 	}
59
-	
85
+			
60 86
 	static void inputError() {
61
-		// Handles any input errors
87
+		/*
88
+		 * This function responds to any IO errors
89
+		 */
90
+		 
62 91
 		System.out.println("Invalid arguments\nUsage: negative [inputfile.jpg] [outputfile.jpg]");
63 92
 		System.exit(1);
64 93
 	}