-
Notifications
You must be signed in to change notification settings - Fork 0
/
RleProgram.java
296 lines (267 loc) · 12.3 KB
/
RleProgram.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
import java.io.ByteArrayOutputStream;
import java.util.Arrays;
import java.util.Scanner;
public class RleProgram {
public static String toHexString(byte[] data){
//Concatenates hexString based on value at each position in the array.
String hexString = "";
for (int i = 0; i < data.length; i++){
if (data[i] == 1){
hexString = hexString + "1";
} else if (data[i] == 2){
hexString = hexString + "2";
} else if (data[i] == 3){
hexString = hexString + "3";
} else if (data[i] == 4){
hexString = hexString + "4";
} else if (data[i] == 5){
hexString = hexString + "5";
} else if (data[i] == 6){
hexString = hexString + "6";
} else if (data[i] == 7){
hexString = hexString + "7";
} else if (data[i] == 8){
hexString = hexString + "8";
} else if (data[i] == 9){
hexString = hexString + "9";
} else if (data[i] == 10){
hexString = hexString + "a";
} else if (data[i] == 11){
hexString = hexString + "b";
} else if (data[i] == 12){
hexString = hexString + "c";
} else if (data[i] == 13){
hexString = hexString + "d";
} else if (data[i] == 14){
hexString = hexString + "e";
} else if (data[i] == 15){
hexString = hexString + "f";
} else if (data[i] == 0){
hexString = hexString + "0";
}
}
return hexString;
}
public static int countRuns(byte[] flatData){
//Runs through the entire array and checks every time consecutive numbers are not equal to each other and keeps a counter
int count = 1;
int rep = 1;
for (int i = 0; i < flatData.length; i++){
if (i + 1 < flatData.length && flatData[i] != flatData[i+1]){
count++;
} else {
rep++;
if (rep > 15){
count++;
rep = 1;
}
}
}
return count;
}
public static byte[] encodeRle(byte[] flatData) {
ByteArrayOutputStream encodedRle = new ByteArrayOutputStream();
// Runs through entire array and checks for consecutive numbers, once numbers are no longer consecutive, adds a counter to array and then adds the original consecutive number
for (int i = 0; i < flatData.length; i++) {
int count = 1;
while (i + 1 < flatData.length && flatData[i] == flatData[i + 1]) {
count++;
i++;
if (count >= 15){
break;
}
}
encodedRle.write((byte)count);
encodedRle.write(flatData[i]);
}
return encodedRle.toByteArray();
}
public static int getDecodedLength(byte[] rleData){
//Runs through the entire array and adds every number in the array skipping one in between each element in the array.
int length = 0;
for (int i = 0; i < rleData.length; i++){
if (i%2 == 0){
length = length + rleData[i];
}
}
return length;
}
public static byte[] decodeRle(byte[] rleData){
//Runs through entire array and checks the first element of the array and prints the second element of the array as many times as the value of the first element, repeated until array is over.
ByteArrayOutputStream decodedRle = new ByteArrayOutputStream();
for (int i = 1; i < rleData.length; i+=2){
for (int j = 0; j < rleData[i - 1]; j++) {
decodedRle.write(rleData[i]);
}
}
return decodedRle.toByteArray();
}
public static byte[] stringToData(String dataString){
// Runs through every character of dataString and assigns its corresponding value to a position in a new array.
byte[] rleArray = new byte[dataString.length()];
for (int i = 0; i < dataString.length(); i++){
if (dataString.charAt(i) == '0'){
rleArray[i] = 0;
} else if (dataString.charAt(i) == '1'){
rleArray[i] = 1;
} else if (dataString.charAt(i) == '2'){
rleArray[i] = 2;
} else if (dataString.charAt(i) == '3'){
rleArray[i] = 3;
} else if (dataString.charAt(i) == '4'){
rleArray[i] = 4;
} else if (dataString.charAt(i) == '5'){
rleArray[i] = 5;
} else if (dataString.charAt(i) == '6'){
rleArray[i] = 6;
} else if (dataString.charAt(i) == '7'){
rleArray[i] = 7;
} else if (dataString.charAt(i) == '8'){
rleArray[i] = 8;
} else if (dataString.charAt(i) == '9'){
rleArray[i] = 9;
} else if (dataString.charAt(i) == 'a'){
rleArray[i] = 10;
} else if (dataString.charAt(i) == 'b'){
rleArray[i] = 11;
} else if (dataString.charAt(i) == 'c'){
rleArray[i] = 12;
} else if (dataString.charAt(i) == 'd'){
rleArray[i] = 13;
} else if (dataString.charAt(i) == 'e'){
rleArray[i] = 14;
} else if (dataString.charAt(i) == 'f'){
rleArray[i] = 15;
}
}
return rleArray;
}
public static String toRleString(byte[] rleData){
//Converts the second element in rleData to RLE and adds a colon
String rleString = "";
for (int i = 0; i < rleData.length; i++){
if (rleData[i] == 1 && (i - 1)%2 == 0){
rleString = rleString + rleData[i - 1] + "1:";
} else if (rleData[i] == 2 && (i - 1)%2 == 0){
rleString = rleString + rleData[i - 1] + "2:";
} else if (rleData[i] == 3 && (i - 1)%2 == 0){
rleString = rleString + rleData[i - 1] + "3:";
} else if (rleData[i] == 4 && (i - 1)%2 == 0){
rleString = rleString + rleData[i - 1] + "4:";
} else if (rleData[i] == 5 && (i - 1)%2 == 0){
rleString = rleString + rleData[i - 1] + "5:";
} else if (rleData[i] == 6 && (i - 1)%2 == 0){
rleString = rleString + rleData[i - 1] + "6:";
} else if (rleData[i] == 7 && (i - 1)%2 == 0){
rleString = rleString + rleData[i - 1] + "7:";
} else if (rleData[i] == 8 && (i - 1)%2 == 0){
rleString = rleString + rleData[i - 1] + "8:";
} else if (rleData[i] == 9 && (i - 1)%2 == 0){
rleString = rleString + rleData[i - 1] + "9:";
} else if (rleData[i] == 10 && (i - 1)%2 == 0){
rleString = rleString + rleData[i - 1] + "a:";
} else if (rleData[i] == 11 && (i - 1)%2 == 0){
rleString = rleString + rleData[i - 1] + "b:";
} else if (rleData[i] == 12 && (i -1 )%2 == 0){
rleString = rleString + rleData[i - 1] + "c:";
} else if (rleData[i] == 13 && (i - 1)%2 == 0){
rleString = rleString + rleData[i - 1] + "d:";
} else if (rleData[i] == 14 && (i - 1)%2 == 0){
rleString = rleString + rleData[i - 1] + "e:";
} else if (rleData[i] == 15 && (i - 1)%2 == 0){
rleString = rleString + rleData[i - 1] + "f:";
} else if (rleData[i] == 0 && (i - 1)%2 == 0){
rleString = rleString + rleData[i - 1] + "0:";
}
}
//Removes last colon in rleString
rleString = rleString.substring(0, rleString.length() - 1);
return rleString;
}
public static byte[] stringToRle(String rleString) {
//Splits up rleString everywhere there is a ':' and stores them in an array
String[] splitStrings = rleString.split(":");
byte[] rleArray = new byte[2 * splitStrings.length];
char c;
for (int i = 0; i < splitStrings.length; i++) {
//Converts splitStrings string into byte
rleArray[2 * i] = Byte.parseByte(splitStrings[i].substring(0, splitStrings[i].length() - 1));
c = splitStrings[i].charAt(splitStrings[i].length() - 1);
if (c <= '9') {
rleArray[2 * i + 1] = (byte) (c - '0');
} else {
rleArray[2 * i + 1] = (byte) (10 + (c - 'a'));
}
}
return rleArray;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int option = 1;
String fileName, data = null;
String[] temp = null;
// 1. Display welcome message
System.out.println("Welcome to the RLE image encoder!");
// 2. Display color test with the message
System.out.println("Displaying Spectrum Image:");
ConsoleGfx.displayImage(ConsoleGfx.testRainbow);
byte[] imageData = null;
// 3. Display the menu - Part A: while loop or if-else chains
while (true) {
System.out.println("RLE Menu");
System.out.println("--------");
System.out.println("0. Exit");
System.out.println("1. Load File");
System.out.println("2. Load Test Image");
System.out.println("3. Read RLE String");
System.out.println("4. Read RLE Hex String");
System.out.println("5. Read Data Hex String");
System.out.println("6. Display Image");
System.out.println("7. Display RLE String");
System.out.println("8. Display Hex RLE Data");
System.out.println("9. Display Hex Flat Data");
System.out.print("Select a Menu Option: ");
option = in.nextInt();
// 3.1 - option 1
// ConsoleGfx.loadFile(userInput) and you want to store the returned byte[] into imageData array
if (option == 1) {
System.out.println("Enter name of file to load: ");
fileName = in.next();
imageData = ConsoleGfx.loadFile(fileName);
// 3.2 - option 2
// store ConsoleGfx.testImage into the imageData array
} else if (option == 2) {
imageData = ConsoleGfx.testImage;
System.out.println("Test image data loaded.");
// 3.6 - option 6
// display image stored inside of imageData array
} else if (option == 3) {
System.out.println("Enter an RLE string to be decoded: ");
data = in.next();
imageData = stringToRle(data);
} else if (option == 4) {
System.out.println("Enter the hex string holding RLE data: ");
data = in.next();
imageData = stringToData(data);
} else if (option == 5) {
System.out.println("Enter the hex string holding flat data: ");
data = in.next();
imageData = stringToData(data);
} else if (option == 6) {
System.out.print("Displaying image...");
ConsoleGfx.displayImage(imageData);
} else if (option == 7) {
System.out.println("RLE representation: " + toRleString(imageData));
} else if (option == 8) {
String s = toRleString(imageData);
System.out.println("RLE hex values : " + s.replaceAll(":",""));
} else if (option == 9) {
System.out.println("Flat hex values: " + toHexString((decodeRle(imageData))));
} else if (option == 0){
break;
}
else
System.out.println("Error! Invalid input.");
}
}
}