-
Notifications
You must be signed in to change notification settings - Fork 0
/
Test.java
867 lines (767 loc) · 30.2 KB
/
Test.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
/* Test.java */
/**
* The Test class is a program that tests the functionality of the PixImage
* and RunLengthEncoding classes.
*
* @author Jonathan Shewchuk and Joel Galenson
*/
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Random;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
public class Test {
// This should be true when we generate
private static final boolean WRITE_MODE = false;
// The maximum number of rows/columns/lines we should print.
// Change this to Integer.MAX_VALUE to print everything.
private static int MAX_PRINT_SIZE = 5;
// Handle reading and writing the good versions.
// We can't serialize PixImages and RLEs because it gives away
// our implementation and the names conflict, so
// we convert to/from strings/ints.
private static ObjectInputStream is;
private static ObjectOutputStream os;
private static void init() throws FileNotFoundException, IOException {
File file = new File("data.gz");
if (WRITE_MODE) {
if (file.exists()) // Let's make it hard to overwrite the data.
throw new RuntimeException("Please delete " + file.getAbsolutePath());
os = new ObjectOutputStream(
new GZIPOutputStream(new FileOutputStream(file)));
} else
is = new ObjectInputStream(
new GZIPInputStream(new FileInputStream(file)));
}
/**
* colorToString() returns a length-2 hex string (00 through FF), given
* a value in the range [0, 256).
*/
private static String colorToString(int color) {
return String.format("%2s", Integer.toHexString(color).toUpperCase())
.replace(' ', '0');
}
private static String stringOfPixImage(PixImage image) {
StringBuffer sb =
new StringBuffer(image.getWidth() * image.getHeight() * 3 * 2);
for (int y = 0; y < image.getHeight(); y++) {
for (int x = 0; x < image.getWidth(); x++) {
sb.append(colorToString(image.getRed(x, y)));
sb.append(colorToString(image.getGreen(x, y)));
sb.append(colorToString(image.getBlue(x, y)));
}
}
return sb.toString();
}
private static short getNextColor(InputStream strStream, byte[] bytes)
throws IOException {
strStream.read(bytes);
return Short.parseShort(new String(bytes), 16);
}
private static BufferedImage imageOfString(int width, int height,
String imageStr)
throws IOException {
BufferedImage image =
new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
InputStream strStream = new ByteArrayInputStream(imageStr.getBytes());
byte[] bytes = new byte[2];
for (int y = 0; y < image.getHeight(); y++) {
for (int x = 0; x < image.getWidth(); x++) {
image.setRGB(x, y, new Color(getNextColor(strStream, bytes),
getNextColor(strStream, bytes),
getNextColor(strStream, bytes)).getRGB());
}
}
return image;
}
// If WRITE_MODE then image is our solution; otherwise it is the student's.
private static BufferedImage readOrWriteNextImage(PixImage image)
throws IOException, ClassNotFoundException {
if (WRITE_MODE) {
os.writeInt(image.getWidth());
os.writeInt(image.getHeight());
os.writeObject(stringOfPixImage(image));
return ImageUtils.pixImage2buffer(image);
} else {
return imageOfString(is.readInt(), is.readInt(),
(String) is.readObject());
}
}
private static ArrayList<int[]> rleToList(RunLengthEncoding rle) {
ArrayList<int[]> runs = new ArrayList<int[]>();
for (RunIterator it = rle.iterator(); it.hasNext(); )
runs.add(it.next());
return runs;
}
private static void encodeRLE(RunLengthEncoding rle, ObjectOutputStream os)
throws IOException {
os.writeInt(rle.getWidth());
os.writeInt(rle.getHeight());
ArrayList<int[]> runs = rleToList(rle);
os.writeInt(runs.size());
for (int[] run: runs)
os.writeObject(run);
}
private static ArrayList<int[]> decodeRLE(ObjectInputStream os)
throws IOException, ClassNotFoundException {
int width = os.readInt();
int height = os.readInt();
int numRuns = os.readInt();
ArrayList<int[]> runs = new ArrayList<int[]>(numRuns);
for (int i = 0; i < numRuns; i++) {
runs.add((int[])os.readObject());
}
return runs;
}
// If WRITE_MODE then image is our solution; otherwise it is the students'.
private static ArrayList<int[]> readOrWriteNextRLE(RunLengthEncoding rle)
throws IOException, ClassNotFoundException {
if (WRITE_MODE) {
encodeRLE(rle, os);
return rleToList(rle);
} else {
return decodeRLE(is);
}
}
// Stuff from old Test.java. Many names are unchanged.
/**
* Default parameters.
*/
private static final int iBlur = 11;
private static final int jBlur = 15;
private static final int iterBlur = 5;
private static PixImage randomImage(int i, int j) {
/**
* Visit each cell (in a roundabout order); randomly pick a color.
*/
PixImage image = new PixImage(i, j);
Random random = new Random(0); // Create a "Random" object with seed 0
int x = 0;
int y = 0;
for (int xx = 0; xx < i; xx++) {
x = (x + 78887) % i; // This will visit every x-coordinate once
for (int yy = 0; yy < j; yy++) {
y = (y + 78887) % j; // This will visit every y-coordinate once
image.setPixel(x, y, (short) random.nextInt(256),
(short) random.nextInt(256),
(short) random.nextInt(256));
}
}
return image;
}
private static int clamp(int coord, int max) {
int d = MAX_PRINT_SIZE / 2;
coord -= MAX_PRINT_SIZE / 2;
if (coord + MAX_PRINT_SIZE > max)
coord = max - MAX_PRINT_SIZE;
if (coord < 0)
coord = 0;
return coord;
}
private static void printHorizBound(int width, int printWidth,
int startX) {
if (startX > 0) // Handle an extra column of "..."s at the beginning.
System.out.print("----");
for (int x = 0; x < printWidth; x++)
System.out.print("-------");
if (printWidth < width) // Handle an extra column of "..."s at the end.
System.out.print("----");
System.out.println("-");
}
private static void printEllipsisRow(int width, int printWidth,
int startX) {
System.out.print("|");
if (startX > 0) // Print an extra column of "..."s at the beginning.
System.out.print("...,");
for (int x = 0; x < printWidth; x++)
System.out.print(" ... ");
if (printWidth < width) // Print an extra column of "..."s at the end.
System.out.print("...");
System.out.println("|");
}
private static void paint(BufferedImage image, int[] firstDiff) {
/* Draw the image. */
int printWidth = Math.min(image.getWidth(), MAX_PRINT_SIZE);
int printHeight = Math.min(image.getHeight(), MAX_PRINT_SIZE);
int startX = 0, startY = 0;
if (firstDiff != null) {
startX = clamp(firstDiff[0], image.getWidth());
startY = clamp(firstDiff[1], image.getHeight());
}
if (startX != 0 || startY != 0)
System.out.println("Starting to print the image from (" + startX + "," + startY + ").");
// Print out the top boundary.
printHorizBound(image.getWidth(), printWidth, startX);
// Print out a row of "..."s if we're not starting from the first row.
if (startY > 0)
printEllipsisRow(image.getWidth(), printWidth, startX);
for (int y = 0; y < printHeight; y++) {
System.out.print("|");
if (startX > 0) // Print an extra column of "..."s at the beginning.
System.out.print("...,");
for (int x = 0; x < printWidth; x++) {
if (x > 0)
System.out.print(",");
Color color = new Color(image.getRGB(startX + x, startY + y));
System.out.print(colorToString(color.getRed()) +
colorToString(color.getGreen()) +
colorToString(color.getBlue()));
}
if (printWidth < image.getWidth()) // Print an extra column of "..."s at the end.
System.out.print(",...");
System.out.println("|");
}
// Print out a row of "..."s if we're not ending at the last row.
if (printHeight < image.getHeight())
printEllipsisRow(image.getWidth(), printWidth, startX);
// Print out the bottom boundary.
printHorizBound(image.getWidth(), printWidth, startX);
}
private static void paint(PixImage image, int[] firstDiff) {
paint(ImageUtils.pixImage2buffer(image), firstDiff);
}
private static int[] findFirstDiff(BufferedImage image1, PixImage image2,
int border) {
if (image1.getWidth() != image2.getWidth() ||
image1.getHeight() != image2.getHeight()) {
return new int[] { -1, -1 };
}
BufferedImage bImage2 = ImageUtils.pixImage2buffer(image2);
for (int y = border; y < image1.getHeight() - border; y++) {
for (int x = border; x < image1.getWidth() - border; x++) {
if (image1.getRGB(x, y) != bImage2.getRGB(x, y)) {
return new int[] { x, y };
}
}
}
return null;
}
private static boolean innerEqual(BufferedImage image1, PixImage image2,
int border) {
return findFirstDiff(image1, image2, border) == null;
}
private static boolean equal(BufferedImage image1, PixImage image2) {
return innerEqual(image1, image2, 0);
}
private static void diffImages(BufferedImage goodImage, PixImage studentPixImage) {
BufferedImage studentImage = ImageUtils.pixImage2buffer(studentPixImage);
int diffCount = 0;
System.out.println("The difference is:");
if (studentImage.getWidth() != goodImage.getWidth()) {
System.out.println("The width is " + studentImage.getWidth() + " but should be " + goodImage.getWidth());
diffCount++;
}
if (studentImage.getHeight() != goodImage.getHeight()) {
System.out.println("The height is " + studentImage.getHeight() + " but should be " + goodImage.getHeight());
diffCount++;
}
for (int y = 0; y < goodImage.getHeight() && y < studentImage.getHeight(); y++) {
for (int x = 0; x < goodImage.getWidth() && x < studentImage.getWidth(); x++) {
if (goodImage.getRGB(x, y) != studentImage.getRGB(x, y)) {
if (diffCount == MAX_PRINT_SIZE) {
System.out.println(" ...");
return;
}
System.out.println(" (" + x + "," + y + ") is " + new Color(studentImage.getRGB(x, y)) + " but should be " + new Color(goodImage.getRGB(x, y)));
diffCount++;
}
}
}
}
private static void printPixImages(PixImage studentImage,
BufferedImage goodImage) {
int[] firstDiff = findFirstDiff(goodImage, studentImage, 0);
System.out.println("The correct current image is:");
paint(goodImage, firstDiff);
System.out.println("Your PixImage is:");
paint(studentImage, firstDiff);
diffImages(goodImage, studentImage);
}
private static void printPixImages(PixImage studentImage,
BufferedImage goodImage,
BufferedImage JRSOld) {
System.out.println("The previous image was:");
paint(JRSOld, findFirstDiff(goodImage, studentImage, 0));
printPixImages(studentImage, goodImage);
}
private static void printRLE(ArrayList<int[]> runs) {
System.out.println(" Here is the correct encoding:");
for (int[] run: runs) {
System.out.println(run[0] + "x[red=" + run[1] + ",green=" + run[2] +
",blue=" + run[3] + "]");
}
}
private static void runTests() throws FileNotFoundException, IOException,
ClassNotFoundException {
BufferedImage goodImage;
BufferedImage prevImage;
PixImage studentImage;
int blurInitScore = 1;
int blurInnerScore = 1;
int blurBoundaryScore = 1;
int blurMultiScore = 1;
int sobelInitScore = 1;
int sobelInnerScore = 2;
int sobelBoundaryScore = 1;
System.out.println("Beginning Part I.");
System.out.println("Performing " + iterBlur + " boxBlur(1) calls, then " +
"one boxBlur(3) calls on a " + iBlur + "x" + jBlur +
" image.");
/**
* Create a random image to blur.
*/
studentImage = randomImage(iBlur, jBlur);
goodImage = readOrWriteNextImage(studentImage);
/**
* Plot and compare our image with the student's.
*/
if (!equal(goodImage, studentImage)) {
// The student's image is incorrect after initialization.
System.out.println("Your initial PixImage is incorrect.");
printPixImages(studentImage, goodImage);
// Student loses all four blur points.
blurInitScore = 0;
blurInnerScore = 0;
blurBoundaryScore = 0;
blurMultiScore = 0;
}
/**
* Perform multiple steps of blurring.
*
* Note: These loops ensure that we always call readOrWriteNext* the
* same number of times even if the tests fail.
*/
for (int x = 1; x <= iterBlur; x++) {
// Perform one blurring step in the student's image and my image.
prevImage = goodImage;
studentImage = studentImage.boxBlur(1);
goodImage = readOrWriteNextImage(studentImage);
// studentImage.setPixel(0, 3, (short) 2, (short) 5, (short) 7);
// Plot and compare them.
if (blurInnerScore > 0 && !equal(goodImage, studentImage)) {
// The student's image is incorrect. Check if it's only a problem
// at the boundaries.
boolean innerEq = innerEqual(goodImage, studentImage, x);
// Print error message, but only if this is the first time.
if (blurBoundaryScore > 0) {
blurBoundaryScore = 0;
blurMultiScore = 0;
// Draw previous image; correct new image; student's new image.
System.out.println("Your image is incorrect after " + x +
"step(s) of boxBlur(1).");
printPixImages(studentImage, goodImage, prevImage);
if (innerEq) {
System.out.println(
"(The problem seems to be only at the boundaries.)");
} else {
blurInnerScore = 0;
}
} else if (!innerEq) {
// Control should only get here if bugs near the boundary were
// encountered on a previous iteration, but bugs in the
// interior first surfaced on this iteration.
blurInnerScore = 0;
System.out.println(
"Your image's interior is incorrect after blurring step " + x +
".");
printPixImages(studentImage, goodImage, prevImage);
}
}
}
// Perform multiple blurring steps in the student's image and my image.
prevImage = goodImage;
studentImage = studentImage.boxBlur(3);
goodImage = readOrWriteNextImage(studentImage);
// Plot and compare them.
if (blurMultiScore > 0 && !equal(goodImage, studentImage)) {
// The student's image is incorrect.
blurMultiScore = 0;
// Draw previous image; correct new image; student's new image.
System.out.println("Your image is incorrect after boxBlur(3).");
}
if (blurMultiScore == 1) {
System.out.println(" Test successful.");
}
System.out.println();
System.out.println("Performing a sobelEdges call on feathers.tiff.");
/**
* Create a random image to perform edge detection on.
*/
try {
System.out.println(" Reading feathers.tiff.");
studentImage = ImageUtils.readTIFFPix("feathers.tiff");
} catch (IllegalArgumentException e) {
System.out.println("Cannot read feathers.tiff. Exiting.");
System.exit(1);
// studentImage = new PixImage(1, 1);
}
goodImage = readOrWriteNextImage(studentImage);
/**
* Plot and compare our image with the student's.
*/
if (!equal(goodImage, studentImage)) {
// The student's image is incorrect after initialization.
System.out.println("Your initial PixImage is incorrect.");
printPixImages(studentImage, goodImage);
// Student loses all four Sobel points.
sobelInitScore = 0;
sobelInnerScore = 0;
sobelBoundaryScore = 0;
}
/**
* Perform Sobel edge detection in my image and the student's image.
*/
prevImage = goodImage;
studentImage = studentImage.sobelEdges();
goodImage = readOrWriteNextImage(studentImage);
// Plot and compare them.
if (sobelInnerScore > 0 && !equal(goodImage, studentImage)) {
sobelBoundaryScore = 0;
// Draw previous image; correct new image; student's new image.
System.out.println("Your image is incorrect after sobelEdges.");
printPixImages(studentImage, goodImage, prevImage);
// Check if it's only a problem at the boundaries.
if (innerEqual(goodImage, studentImage, 1)) {
System.out.println(
"(The problem seems to be only at the boundaries.)");
} else {
sobelInnerScore = 0;
}
}
if (sobelBoundaryScore > 0) {
System.out.println(" Test successful.");
}
int parti = blurInitScore + blurInnerScore + blurBoundaryScore +
blurMultiScore + sobelInitScore + sobelInnerScore + sobelBoundaryScore;
System.out.println();
System.out.println("Total Part I score: " + parti + " out of 8.");
System.out.println("Total Autogradable score so far: " + parti +
" out of 8.");
System.out.println();
System.out.println("Beginning Part II.");
System.out.println("Performing a 4x4 RunLengthEncoding-to-PixImage test.");
int readBackScore = 2;
int toPixImageScore = 3;
System.out.println(" Calling the six-parameter constructor.");
int[] rr = {0, 1, 2, 3, 4, 5};
int[] rg = {20, 18, 16, 14, 12, 10};
int[] rb = {42, 42, 42, 137, 137, 137};
int[] rl = {3, 2, 5, 1, 1, 4};
RunLengthEncoding rle1 = new RunLengthEncoding(4, 4, rr, rg, rb, rl);
ArrayList<int[]> jrle1 = readOrWriteNextRLE(rle1);
System.out.println(" Reading back the encoding through nextRun.");
RunIterator rle1it = rle1.iterator();
for (int i = 0; i < rl.length; i++) {
if (!rle1it.hasNext()) {
System.out.println(" Run # " + i +
" missing. (Runs are indexed from zero.)");
System.out.println(" (In other words, your nextRun() is " +
"returning null when it shouldn't.)");
printRLE(jrle1);
readBackScore = 0;
break;
}
int[] ts = rle1it.next();
if ((ts[0] != rl[i]) || (ts[1] != rr[i]) || (ts[2] != rg[i])
|| (ts[3] != rb[i])) {
System.out.println(" Run # " + i + " should be " + rl[i] + ", " +
rr[i] + ", " + rg[i] + ", " + rb[i] +
". (Runs are indexed from zero.)");
System.out.println(" Instead, it's " + ts[0] + ", " + ts[1] + ", "
+ ts[2] + ", " + ts[3]);
printRLE(jrle1);
readBackScore = 0;
break;
}
}
if ((readBackScore == 1) && (rle1it.hasNext())) {
System.out.println(" Your hasNext() is failing to return false when" +
" the runs run out.");
printRLE(jrle1);
readBackScore = 0;
}
System.out.println(" Calling toPixImage.");
PixImage o1 = rle1.toPixImage();
BufferedImage jo1 = readOrWriteNextImage(o1);
if (!equal(jo1, o1)) {
printPixImages(o1, jo1);
toPixImageScore = 0;
}
int partii = readBackScore + toPixImageScore;
if (partii == 5) {
System.out.println(" Test successful.");
}
System.out.println();
System.out.println("Total Part II score: " + partii + " out of 5.");
System.out.println("Total Autogradable score so far: " +
(parti + partii) + " out of 13.");
System.out.println();
System.out.println("Beginning Part III.");
System.out.println("Run-length encoding a PixImage.");
int toRLEScore = 3;
int backToPixImageScore = 1;
try {
System.out.println(" Reading highcontrast.tiff.");
studentImage = ImageUtils.readTIFFPix("highcontrast.tiff");
} catch (IllegalArgumentException e) {
System.out.println("Cannot read highcontrast.tiff. Exiting.");
System.exit(1);
// studentImage = new PixImage(1, 1);
}
goodImage = readOrWriteNextImage(studentImage);
System.out.println(" Calling the one-parameter RunLengthEncoding " +
"constructor.");
RunLengthEncoding rle2 = new RunLengthEncoding(studentImage);
ArrayList<int[]> jrle2 = readOrWriteNextRLE(rle2);
int i = 0;
RunIterator rle2it = rle2.iterator();
for (int[] jts2: jrle2) {
if (!rle2it.hasNext()) {
System.out.println(" Run # " + i +
" missing. (Runs are indexed from zero.)");
System.out.println(" (In other words, your nextRun() is " +
"returning null when it shouldn't.)");
toRLEScore = 0;
break;
}
int[] ts2 = rle2it.next();
if ((jts2[0] != ts2[0]) || (jts2[1] != ts2[1]) || (jts2[2] != ts2[2])
|| (jts2[3] != ts2[3])) {
System.out.println(" Run # " + i + " should be " + jts2[0] +
", " + jts2[1] + ", " + jts2[2] + ", " + jts2[3] + "."
+ " (Runs indexed from zero.)");
System.out.println(" Instead, it's " + ts2[0] + ", " + ts2[1] + ", "
+ ts2[2] + ", " + ts2[3]);
toRLEScore = 0;
break;
}
i++;
}
if ((toRLEScore == 1) && (rle2it.hasNext())) {
System.out.println(" Your nextRun is failing to return null when" +
" the runs run out.");
System.out.println(" Here is the correct image.");
paint(goodImage, null);
toRLEScore = 0;
}
System.out.println(" Converting back to a PixImage.");
PixImage o2j = rle2.toPixImage();
if (!equal(goodImage, o2j)) {
printPixImages(o2j, goodImage);
backToPixImageScore = 0;
}
int partiii = toRLEScore + backToPixImageScore;
if (partiii == 4) {
System.out.println(" Test successful.");
}
System.out.println();
System.out.println("Total Part III score: " + partiii + " out of 4.");
System.out.println("Total Autogradable score so far: " +
(parti + partii + partiii) + " out of 17.");
System.out.println();
System.out.println("Beginning Part IV.");
System.out.println("Setting pixels in your 4x4 run-length encoding" +
" (from Part II).");
int addScore1 = 1;
int addScore2 = 1;
System.out.println(" Setting (3, 2) to [red=5, green=10, blue=136].");
rle1.setPixel(3, 2, (short) 5, (short) 10, (short) 136);
jrle1 = readOrWriteNextRLE(rle1);
i = 0;
rle1it = rle1.iterator();
for (int[] jts1: jrle1) {
if (!rle1it.hasNext()) {
System.out.println(" Run # " + i +
" missing. (Runs are indexed from zero.)");
System.out.println(" (In other words, your nextRun() is " +
"returning null when it shouldn't.)");
System.out.println(" Here is your encoding:\n" + rle1);
printRLE(jrle1);
addScore1 = 0;
break;
}
int[] ts1 = rle1it.next();
if ((jts1[0] != ts1[0]) || (jts1[1] != ts1[1]) || (jts1[2] != ts1[2])
|| (jts1[3] != ts1[3])) {
System.out.println(" Run # " + i + " should be " + jts1[0] +
", " + jts1[1] + ", " + jts1[2] + ", " + jts1[3] + "."
+ " (Runs indexed from zero.)");
System.out.println(" Instead, it's " + ts1[0] + ", " + ts1[1] + ", "
+ ts1[2] + ", " + ts1[3]);
System.out.println(" Here is your encoding:\n" + rle1);
printRLE(jrle1);
addScore1 = 0;
break;
}
i++;
}
System.out.println(" Setting (0, 3) to [red=5, green=10, blue=136].");
rle1.setPixel(0, 3, (short) 5, (short) 10, (short) 136);
jrle1 = readOrWriteNextRLE(rle1);
i = 0;
rle1it = rle1.iterator();
for (int[] jts1: jrle1) {
if (!rle1it.hasNext()) {
System.out.println(" Run # " + i +
" missing. (Runs are indexed from zero.)");
System.out.println(" (In other words, your nextRun() is " +
"returning null when it shouldn't.)");
System.out.println(" Here is your encoding:\n" + rle1);
printRLE(jrle1);
addScore1 = 0;
break;
}
int[] ts1 = rle1it.next();
if ((jts1[0] != ts1[0]) || (jts1[1] != ts1[1]) || (jts1[2] != ts1[2])
|| (jts1[3] != ts1[3])) {
System.out.println(" Run # " + i + " should be " + jts1[0] +
", " + jts1[1] + ", " + jts1[2] + ", " + jts1[3] + "."
+ " (Runs indexed from zero.)");
System.out.println(" Instead, it's " + ts1[0] + ", " + ts1[1] + ", "
+ ts1[2] + ", " + ts1[3]);
System.out.println(" Here is your encoding:\n" + rle1);
printRLE(jrle1);
addScore1 = 0;
break;
}
i++;
}
System.out.println(" Setting (1, 0) to [red=1, green=18, blue=42].");
rle1.setPixel(1, 0, (short) 1, (short) 18, (short) 42);
jrle1 = readOrWriteNextRLE(rle1);
i = 0;
rle1it = rle1.iterator();
for (int[] jts1: jrle1) {
if (!rle1it.hasNext()) {
System.out.println(" Run # " + i +
" missing. (Runs are indexed from zero.)");
System.out.println(" (In other words, your nextRun() is " +
"returning null when it shouldn't.)");
System.out.println(" Here is your encoding:\n" + rle1);
printRLE(jrle1);
addScore2 = 0;
break;
}
int[] ts1 = rle1it.next();
if ((jts1[0] != ts1[0]) || (jts1[1] != ts1[1]) || (jts1[2] != ts1[2])
|| (jts1[3] != ts1[3])) {
System.out.println(" Run # " + i + " should be " + jts1[0] +
", " + jts1[1] + ", " + jts1[2] + ", " + jts1[3] + "."
+ " (Runs indexed from zero.)");
System.out.println(" Instead, it's " + ts1[0] + ", " + ts1[1] + ", "
+ ts1[2] + ", " + ts1[3]);
System.out.println(" Here is your encoding:\n" + rle1);
printRLE(jrle1);
addScore2 = 0;
break;
}
i++;
}
System.out.println(" Setting (2, 0) to [red=1, green=18, blue=42].");
rle1.setPixel(2, 0, (short) 1, (short) 18, (short) 42);
jrle1 = readOrWriteNextRLE(rle1);
i = 0;
rle1it = rle1.iterator();
for (int[] jts1: jrle1) {
if (!rle1it.hasNext()) {
System.out.println(" Run # " + i +
" missing. (Runs are indexed from zero.)");
System.out.println(" (In other words, your nextRun() is " +
"returning null when it shouldn't.)");
System.out.println(" Here is your encoding:\n" + rle1);
printRLE(jrle1);
addScore2 = 0;
break;
}
int[] ts1 = rle1it.next();
if ((jts1[0] != ts1[0]) || (jts1[1] != ts1[1]) || (jts1[2] != ts1[2])
|| (jts1[3] != ts1[3])) {
System.out.println(" Run # " + i + " should be " + jts1[0] +
", " + jts1[1] + ", " + jts1[2] + ", " + jts1[3] + "."
+ " (Runs indexed from zero.)");
System.out.println(" Instead, it's " + ts1[0] + ", " + ts1[1] + ", "
+ ts1[2] + ", " + ts1[3]);
System.out.println(" Here is your encoding:\n" + rle1);
printRLE(jrle1);
addScore2 = 0;
break;
}
i++;
}
System.out.println(" Setting (0, 0) to [red=1, green=18, blue=42].");
rle1.setPixel(0, 0, (short) 1, (short) 18, (short) 42);
jrle1 = readOrWriteNextRLE(rle1);
i = 0;
rle1it = rle1.iterator();
for (int[] jts1: jrle1) {
if (!rle1it.hasNext()) {
System.out.println(" Run # " + i +
" missing. (Runs are indexed from zero.)");
System.out.println(" (In other words, your nextRun() is " +
"returning null when it shouldn't.)");
System.out.println(" Here is your encoding:\n" + rle1);
printRLE(jrle1);
addScore2 = 0;
break;
}
int[] ts1 = rle1it.next();
if ((jts1[0] != ts1[0]) || (jts1[1] != ts1[1]) || (jts1[2] != ts1[2])
|| (jts1[3] != ts1[3])) {
System.out.println(" Run # " + i + " should be " + jts1[0] +
", " + jts1[1] + ", " + jts1[2] + ", " + jts1[3] + "."
+ " (Runs indexed from zero.)");
System.out.println(" Instead, it's " + ts1[0] + ", " + ts1[1] + ", "
+ ts1[2] + ", " + ts1[3]);
System.out.println(" Here is your encoding:\n" + rle1);
printRLE(jrle1);
addScore2 = 0;
break;
}
i++;
}
int partiv = addScore1 + addScore2;
if (partiv == 2) {
System.out.println(" Test successful.");
}
System.out.println();
System.out.println("Total Part IV score: " + partiv + " out of 2.");
System.out.println("Total Autogradable score: " +
(parti + partii + partiii + partiv) + " out of 19.");
System.out.println();
System.out.println("(Note: 1 hand-graded point will be for your check()" +
" method.)");
}
public static void main(String[] args) {
try {
init();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
runTests();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (WRITE_MODE) {
os.close();
} else {
is.close();
}
} catch (IOException e) {
}
}
}
}