-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPPMImage.java
executable file
·191 lines (178 loc) · 5.71 KB
/
PPMImage.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.Scanner;
/**
* Develop a fully functional class (named PPMImage) that implements the PPM
* color image file format from the NetPBM image suite (netpbm.sourceforge.net).
*
*
* @author Tianchang Yang
* @version 06 April 2016
*/
public class PPMImage {
// Instance variables:
private int[][][] myPixels; // the dimensions are: rows, cols, rgb
private int numRows;
private int numCols;
/**
* PPMImage constructor - This constructor will create a new instance of
* PPMImage based on the single argument corresponding to a filename.
*
* @param filename
*
*/
public PPMImage(String filename) throws Exception {
readPPMImage(filename);
}
/**
* PPMImage constructor - This constructor will create a new instance of the
* PPMImage class based on the contents of a three-dimensional array of
* integers.
*
* @param pixelsIn
*/
public PPMImage(int[][][] pixelsIn) {
numRows = pixelsIn.length;
numCols = pixelsIn[0].length;
myPixels = new int[numRows][numCols][3];
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numCols; j++) {
for (int rgb = 0; rgb < 3; rgb++) {
myPixels[i][j][rgb] = pixelsIn[i][j][rgb];
}
}
}
}
/**
* PPMImage constructor - This constructor will create a new instance of the
* PPMImage class based on an existing PPMImage instance.
*
* @param imageIn
*/
public PPMImage(PPMImage imageIn) {
numRows = imageIn.myPixels.length;
numCols = imageIn.myPixels[0].length;
myPixels = new int[numRows][numCols][3];
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numCols; j++) {
for (int rgb = 0; rgb < 3; rgb++) {
myPixels[i][j][rgb] = imageIn.myPixels[i][j][rgb];
}
}
}
}
/**
* getNumRows - return the number of rows
*
* @return numRows
*/
public int getNumRows() {
return numRows;
}
/**
* getNumCols - return the number of columns
*
* @return numCols
*/
public int getNumCols() {
return numCols;
}
/**
* getPixels - return the array indicating pixels
*
* @return pixels
*/
public int[][][] getPixels() {
int[][][] pixels;
pixels = new int[numRows][numCols][3];
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numCols; j++) {
for (int rgb = 0; rgb < 3; rgb++) {
pixels[i][j][rgb] = myPixels[i][j][rgb];
}
}
}
return pixels;
}
/**
* setPixels - description here
*
* @param pixelsIn
*/
public void setPixels(int[][][] pixelsIn) {
numRows = pixelsIn.length;
numCols = pixelsIn[0].length;
myPixels = new int[numRows][numCols][3];
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < numCols; j++) {
for (int rgb = 0; rgb < 3; rgb++) {
myPixels[i][j][rgb] = pixelsIn[i][j][rgb];
}
}
}
}
/**
* readPPMImage - read from a PPM Image file
*
* This method should read a file (in PPM format) and store the contents
* into the instance variables.
*
* The method should both CONSTRUCT and POPULATE all instance variables from
* the PPM data stored in the file.
*
* Recall that the PPM image format is: P3 # comment width(cols)
* height(rows) maxColorLevel(probably 255) pixel0R pixel0G pixel0B pixel1R
* pixel1G pixel1B pixel2R pixel2G pixel2B pixel3R ...
*
* @param filename
*/
private void readPPMImage(String filename) throws Exception {
FileReader inFR = new FileReader(filename);
Scanner fin = new Scanner(inFR);
String magicNum = fin.nextLine();
String comment = fin.nextLine();
numCols = fin.nextInt();
numRows = fin.nextInt();
int maxColorLevel = fin.nextInt();
myPixels = new int [numRows][numCols][3];
for (int row = 0; row < numRows; row++) {
for (int col = 0; col < numCols; col++) {
for (int rgb = 0; rgb < 3; rgb++) {
myPixels [row][col][rgb] = fin.nextInt();
}
}
}
fin.close();
}
/**
* writePPMImage - write a PPM Image file
*
* This method should write to a file, named in the String parameter, the
* contents of the myPixels instance variable.
*
* The contents should be written according to the PPM image format. The
* pixel values should be written three per line, separated by spaces: P3 #
* comment with YOUR NAME in it width(cols) height(rows)
* maxColorLevel(probably 255) pixel0R pixel0G pixel0B pixel1R pixel1G
* pixel1B pixel2R pixel2G pixel2B pixel3R ...
*
* @param filename
*/
public void writePPMImage(String filename) throws Exception {
FileWriter outFW = new FileWriter(filename);
BufferedWriter fout = new BufferedWriter(outFW);
fout.write("P3\n");
fout.write("# created by Tianchang Yang \n");
fout.write(numCols + " " + numRows + "\n");
fout.write("255\n");
for (int row = 0; row < numRows; row++) {
for (int col = 0; col < numCols; col++) {
for (int rgb = 0; rgb < 3; rgb++) {
fout.write ( myPixels[row][col][rgb] + "\n");
}
}
}
fout.close();
}
}