-
Notifications
You must be signed in to change notification settings - Fork 0
/
LinkedListImage.java
766 lines (655 loc) · 22.6 KB
/
LinkedListImage.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
import java.util.*;
import java.io.*;
class Node{
protected int element;
protected Node next;
public Node(){
element = 0;
next = null;
}
public Node(int e , Node n){
element = e;
next = n;
}
public int getElement(){
return element;
}
public void setElement(int d){
element =d;
}
public Node getNext() {
return next;
}
public void setNext(Node m){
next = m;
}
}
class Linkedlist{
public Node head;
public Node tail;
public int size;
public Linkedlist(){
head = null;
tail = null;
size = 0;
}
public int size(){
return size;
}
public boolean isEmpty(){
return size == 0;
}
public int first(){
return head.getElement();
}
public int last(){
return tail.getElement();
}
public void addFirst(int e){
size++;
if (head == null) {
head.setElement(e);
head.setNext(null);
}
else{
Node newNode = new Node(e,head);
head = newNode;
}
}
public void addLast(int e){
Node newest = new Node(e,null);
if(isEmpty()){
head = newest;
tail = head;
}
else{
tail.setNext(newest);
tail = newest;
}
size++;
}
}
public class LinkedListImage implements CompressedImageInterface {
public Linkedlist[] a;
public int[] nobp;
public int height;
public int width;
public LinkedListImage(String filename) {
//you need to implement this
//throw new java.lang.UnsupportedOperationException("Not implemented yet.");
try {
FileInputStream file = new FileInputStream(filename);
Scanner x = new Scanner(file);
height = x.nextInt();
width = x.nextInt();
Linkedlist[] comp = new Linkedlist[height + 1];
int[] compx = new int[height];
a = comp;
nobp = compx;
comp[0] = new Linkedlist();
comp[0].addLast(height);
comp[0].addLast(width);
for (int i = 1; i < height + 1; i++) {
comp[i] = new Linkedlist();
Linkedlist compnew = new Linkedlist();
for (int j = 0; j < width; j++) {
int b = x.nextInt();
if (b == 0) {
compx[i-1]++;
if (j == width - 1) {
compnew.addLast(j);
if (compnew.size() == 1) {
comp[i].addLast(compnew.first());
comp[i].addLast(compnew.first());
} else if (compnew.size() > 1) {
comp[i].addLast(compnew.first());
comp[i].addLast(compnew.last());
}
compnew = new Linkedlist();
} else {
compnew.addLast(j);
}
}
else {
if (compnew.size() == 1) {
comp[i].addLast(compnew.first());
comp[i].addLast(compnew.last());
} else if (compnew.size() > 1) {
comp[i].addLast(compnew.first());
comp[i].addLast(compnew.last());
} else {
continue;
}
compnew = new Linkedlist();
}
}
comp[i].addLast(-1);
}
} catch (FileNotFoundException e) {
System.out.println("FILE NOT FOUNDDD!");
}
} // DONE
public LinkedListImage(boolean[][] grid, int w, int h) // DONE
{
height = h;
width = w;
Linkedlist[] comp = new Linkedlist[height + 1];
int[] compx = new int[height];
a = comp;
nobp = compx;
comp[0] = new Linkedlist();
comp[0].addLast(height);
comp[0].addLast(width);
for (int i = 1; i < height + 1; i++) {
comp[i] = new Linkedlist();
Linkedlist compnew = new Linkedlist();
for (int j = 0; j < width; j++) {
int b;
if (grid[i-1][j]) {
b=1;
}
else{
b=0;
}
if (b == 0) {
compx[i-1]++;
if (j == width - 1) {
compnew.addLast(j);
if (compnew.size() == 1) {
comp[i].addLast(compnew.first());
comp[i].addLast(compnew.first());
} else if (compnew.size() > 1) {
comp[i].addLast(compnew.first());
comp[i].addLast(compnew.last());
}
compnew = new Linkedlist();
} else {
compnew.addLast(j);
}
}
else {
if (compnew.size() == 1) {
comp[i].addLast(compnew.first());
comp[i].addLast(compnew.last());
} else if (compnew.size() > 1) {
comp[i].addLast(compnew.first());
comp[i].addLast(compnew.last());
} else {
continue;
}
compnew = new Linkedlist();
}
}
comp[i].addLast(-1);
}
}
public boolean getPixelValue(int x, int y) throws PixelOutOfBoundException
{
//you need to implement this
//throw new java.lang.UnsupportedOperationException("Not implemented yet.");
if (x<height && y<width && x>=0 && y>=0) {
Node ptr = a[x + 1].head;
int cntr = 1;
for (int i = 0; i < a[x + 1].size() - 1; i++) {
if (cntr == 1)
cntr--;
else
cntr++;
if (ptr.getElement() == y)
return false;
else if (y < ptr.getElement() && cntr == 0) {
return true;
} else if (y > ptr.getElement() && cntr == 0 && y <= (ptr.getNext()).getElement()) {
return false;
} else if (y > ptr.getElement() && cntr == 1 && y < (ptr.getNext()).getElement()) {
return true;
}
ptr = ptr.getNext();
}
return true;
}
else{
throw new PixelOutOfBoundException("Check the input");
}
}
public void setPixelValue(int x, int y, boolean val) throws PixelOutOfBoundException
{
//you need to implement this
//throw new java.lang.UnsupportedOperationException("Not implemented yet.");
if (x<height && y<width && x>=0 && y>=0) {
if (getPixelValue(x, y) == val) {
return;
}
int[] arr = new int[width];
Node ptr = a[x + 1].head;
Node temp = new Node();
int cntr = 2;
if (val) {
nobp[x]--;
} else
nobp[x]++;
if (a[x + 1].size() > 1) {
for (int i = 0; i < a[x + 1].size() - 1; i = i + 1) {
if (cntr == 1)
cntr--;
else if (cntr == 0)
cntr++;
if (cntr == 1) {
for (int j = temp.getElement() + 1; j < ptr.getElement(); j++) {
arr[j] = 1;
}
} else if (cntr == 0) {
for (int j = temp.getElement(); j <= ptr.getElement(); j++) {
arr[j] = 0;
}
} else {
for (int j = 0; j < ptr.getElement(); j++) {
arr[j] = 1;
}
cntr = 1;
}
temp = ptr;
ptr = ptr.getNext();
if (ptr.getElement() == -1) {
for (int j = temp.getElement() + 1; j < width; j++) {
arr[j] = 1;
}
break;
}
}
} else {
for (int i = 0; i < width; i++) {
arr[i] = 1;
}
}
if (val)
arr[y] = 1;
else
arr[y] = 0;
Linkedlist compnew = new Linkedlist();
Linkedlist link = new Linkedlist();
for (int j = 0; j < width; j++) {
if (arr[j] == 0) {
//nobp[j]++;
if (j == width - 1) {
compnew.addLast(j);
if (compnew.size() == 1) {
link.addLast(compnew.first());
link.addLast(compnew.first());
} else if (compnew.size() > 1) {
link.addLast(compnew.first());
link.addLast(compnew.last());
}
compnew = new Linkedlist();
} else {
compnew.addLast(j);
}
} else {
if (compnew.size() == 1) {
link.addLast(compnew.first());
link.addLast(compnew.last());
} else if (compnew.size() > 1) {
link.addLast(compnew.first());
link.addLast(compnew.last());
} else {
continue;
}
compnew = new Linkedlist();
}
}
link.addLast(-1);
a[x + 1] = link;
//nobp=nob;
}
else{
throw new PixelOutOfBoundException("Check the input");
}
}
public int[] numberOfBlackPixels() { return nobp;
}
public void invert()
{
//you need to implement this
//throw new java.lang.UnsupportedOperationException("Not implemented yet.");
try {
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
this.setPixelValue(i, j, !this.getPixelValue(i, j));
}
nobp[i] = width - nobp[i];
}
}
catch (PixelOutOfBoundException e){
System.out.println("XOXO");
}
}
public void performAnd(CompressedImageInterface img) throws BoundsMismatchException
{
LinkedListImage imglist=(LinkedListImage)img;
try{
if(height==imglist.height && width==imglist.width) {
int[] arr = new int[height];
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
this.setPixelValue(i, j, this.getPixelValue(i, j) && img.getPixelValue(i, j));
if (!this.getPixelValue(i, j)) {
arr[i]++;
}
}
}
nobp = arr;
}
else{
throw new BoundsMismatchException("Check the size of image");
}
}
catch (PixelOutOfBoundException e){
System.out.println("XOxo");
}
}
public void performOr(CompressedImageInterface img) throws BoundsMismatchException
{
LinkedListImage imglist=(LinkedListImage)img;
try{
if(height==imglist.height && width==imglist.width) {
int[] arr = new int[height];
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
this.setPixelValue(i, j, this.getPixelValue(i, j) || img.getPixelValue(i, j));
if (!this.getPixelValue(i, j)) {
arr[i]++;
}
}
}
nobp = arr;
}
else{
throw new BoundsMismatchException("Check the size of image");
}
}
catch (PixelOutOfBoundException e){
System.out.println("XOxo");
}
}
public void performXor(CompressedImageInterface img) throws BoundsMismatchException
{
LinkedListImage imglist=(LinkedListImage)img;
try{
if(height==imglist.height && width==imglist.width) {
int[] arr = new int[height];
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
this.setPixelValue(i, j, this.getPixelValue(i, j) ^ img.getPixelValue(i, j));
if (!this.getPixelValue(i, j)) {
arr[i]++;
}
}
}
nobp = arr;
}
else{
throw new BoundsMismatchException("Check the size of image");
}
}
catch (PixelOutOfBoundException e){
System.out.println("XOxo");
}
}
public String toStringUnCompressed()
{
//you need to implement this
//throw new java.lang.UnsupportedOperationException("Not implemented yet.");
String res = "";
res+=height;
res+=" ";
res+=width;
for (int x = 0 ; x< height ;x++){
int[] arr=new int[width];
Node ptr = a[x+1].head;
Node temp = new Node();
int cntr=2;
if (a[x+1].size()>1) {
for (int i = 0; i < a[x + 1].size() - 1; i=i+1) {
if (cntr == 1)
cntr--;
else if (cntr == 0)
cntr++;
if (cntr == 1) {
for (int j = temp.getElement() + 1; j < ptr.getElement(); j++) {
arr[j] = 1;
}
}
else if (cntr == 0){
for (int j = temp.getElement(); j <= ptr.getElement(); j++) {
arr[j] = 0;
}
}
else {
for (int j = 0; j < ptr.getElement(); j++) {
arr[j] = 1;
}
cntr = 1;
}
temp = ptr;
ptr = ptr.getNext();
if (ptr.getElement() == -1)
{
for (int j = temp.getElement()+1;j<width;j++){
arr[j]=1;
}
break;
}
}
}
else{
for (int i = 0; i <width ; i++) {
arr[i]=1;
}
}
res+=",";
for (int j = 0; j <width ; j++) {
res+=" ";res+=String.valueOf(arr[j]);
}
}
return res;
}
public String toStringCompressed()
{
//you need to implement this
//throw new java.lang.UnsupportedOperationException("Not implemented yet.");
String res = "";
res+=height;
res+=" ";
res+=width;
res+=", ";
int i =1;
while(i<=height-1){
Node ref = a[i].head;
while(ref.getNext()!=null){
int h=ref.getElement();
res+=h;
res+=" ";
ref=ref.getNext();
}
res+="-1";
res+=',';
res+=" ";
i++;
}
Node ref = a[height].head;
while(ref.getNext()!=null){
int h = ref.getElement();
res+=h;
res+=" ";
ref=ref.getNext();
}
res += "-1";
return res;
}
public static void main(String[] args) {
// testing all methods here :
boolean success = true;
// check constructor from file
CompressedImageInterface img1 = new LinkedListImage("sampleInputFile.txt");
// check toStringCompressed
String img1_compressed = img1.toStringCompressed();
String img_ans = "16 16, -1, 5 7 -1, 3 7 -1, 2 7 -1, 2 2 6 7 -1, 6 7 -1, 6 7 -1, 4 6 -1, 2 4 -1, 2 3 14 15 -1, 2 2 13 15 -1, 11 13 -1, 11 12 -1, 10 11 -1, 9 10 -1, 7 9 -1";
success = success && (img_ans.equals(img1_compressed));
if (!success)
{
System.out.println("Constructor (file) or toStringCompressed ERROR");
return;
}
// check getPixelValue
boolean[][] grid = new boolean[16][16];
for (int i = 0; i < 16; i++)
for (int j = 0; j < 16; j++)
{
try
{
grid[i][j] = img1.getPixelValue(i, j);
}
catch (PixelOutOfBoundException e)
{
System.out.println("Errorrrrrrrr");
}
}
// check constructor from grid
CompressedImageInterface img2 = new LinkedListImage(grid, 16, 16);
String img2_compressed = img2.toStringCompressed();
success = success && (img2_compressed.equals(img_ans));
if (!success)
{
System.out.println("Constructor (array) or toStringCompressed ERROR");
return;
}
// check Xor
try
{
img1.performXor(img2);
}
catch (BoundsMismatchException e)
{
System.out.println("Errorrrrrrrr");
}
for (int i = 0; i < 16; i++)
for (int j = 0; j < 16; j++)
{
try
{
success = success && (!img1.getPixelValue(i,j));
}
catch (PixelOutOfBoundException e)
{
System.out.println("Errorrrrrrrr");
}
}
if (!success)
{
System.out.println("performXor or getPixelValue ERROR");
return;
}
// check setPixelValue
for (int i = 0; i < 16; i++)
{
try
{
img1.setPixelValue(i, 0, true);
}
catch (PixelOutOfBoundException e)
{
System.out.println("Errorrrrrrrr");
}
}
// check numberOfBlackPixels
int[] img1_black = img1.numberOfBlackPixels();
success = success && (img1_black.length == 16);
for (int i = 0; i < 16 && success; i++)
success = success && (img1_black[i] == 15);
if (!success)
{
System.out.println("setPixelValue or numberOfBlackPixels ERROR");
return;
}
// check invert
img1.invert();
for (int i = 0; i < 16; i++)
{
try
{
success = success && !(img1.getPixelValue(i, 0));
}
catch (PixelOutOfBoundException e)
{
System.out.println("Errorrrrrrrr");
}
}
if (!success)
{
System.out.println("invert or getPixelValue ERROR");
return;
}
// check Or
try
{
img1.performOr(img2);
}
catch (BoundsMismatchException e)
{
System.out.println("Errorrrrrrrr");
}
for (int i = 0; i < 16; i++)
for (int j = 0; j < 16; j++)
{
try
{
success = success && img1.getPixelValue(i,j);
}
catch (PixelOutOfBoundException e)
{
System.out.println("Errorrrrrrrr");
}
}
if (!success)
{
System.out.println("performOr or getPixelValue ERROR");
return;
}
// check And
try
{
img1.performAnd(img2);
}
catch (BoundsMismatchException e)
{
System.out.println("Errorrrrrrrr");
}
for (int i = 0; i < 16; i++)
for (int j = 0; j < 16; j++)
{
try
{
success = success && (img1.getPixelValue(i,j) == img2.getPixelValue(i,j));
}
catch (PixelOutOfBoundException e)
{
System.out.println("Errorrrrrrrr");
}
}
if (!success)
{
System.out.println("performAnd or getPixelValue ERROR");
return;
}
// check toStringUnCompressed
String img_ans_uncomp = "16 16, 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1, 1 1 1 1 1 0 0 0 1 1 1 1 1 1 1 1, 1 1 1 0 0 0 0 0 1 1 1 1 1 1 1 1, 1 1 0 0 0 0 0 0 1 1 1 1 1 1 1 1, 1 1 0 1 1 1 0 0 1 1 1 1 1 1 1 1, 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1, 1 1 1 1 1 1 0 0 1 1 1 1 1 1 1 1, 1 1 1 1 0 0 0 1 1 1 1 1 1 1 1 1, 1 1 0 0 0 1 1 1 1 1 1 1 1 1 1 1, 1 1 0 0 1 1 1 1 1 1 1 1 1 1 0 0, 1 1 0 1 1 1 1 1 1 1 1 1 1 0 0 0, 1 1 1 1 1 1 1 1 1 1 1 0 0 0 1 1, 1 1 1 1 1 1 1 1 1 1 1 0 0 1 1 1, 1 1 1 1 1 1 1 1 1 1 0 0 1 1 1 1, 1 1 1 1 1 1 1 1 1 0 0 1 1 1 1 1, 1 1 1 1 1 1 1 0 0 0 1 1 1 1 1 1";
success = success && (img1.toStringUnCompressed().equals(img_ans_uncomp)) && (img2.toStringUnCompressed().equals(img_ans_uncomp));
if (!success)
{
System.out.println("toStringUnCompressed ERROR");
return;
}
else
System.out.println("ALL TESTS SUCCESSFUL! YAYY!");
}
}