@matfah/

GrayImage FR 2010

Java

No description

fork
loading
Files
  • Main.java
  • GrayImage.java
Main.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
import java.lang.reflect.*;

class Main {
  public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
    
    System.out.println("Testing countWhitePixels...");
    
    for(int i = 0; i < 5; i++)
      testPixels(i);
    
    System.out.println("Done!\n");
    
    System.out.println("Testing processImage...");
    
    for(int i = 0; i < 5; i++)
      testProcess(i);
    
    System.out.println("Done!");
  }
  
  public static void testPixels(int test) throws NoSuchFieldException, IllegalAccessException {
    int[][] pixels;
    int count;
    String description;
    
    if(test == 0) {
      pixels = new int[0][0];
      count = 0;
      description = "0 rows and 0 columns";
    }
    else if(test == 1) {
      pixels = new int[3][5];
      count = 0;
      description = "more columns than rows";
    }
    else if(test == 2) {
      pixels = new int[5][3];
      count = 0;
      description = "more rows than columns";
    }
    else if(test == 3) { //all 1's
      pixels = new int[][] {
        {255, 255, 255, 255},
        {255, 255, 255, 255}
      };
      description = "all white";
      count = 8;
    }
    else {
      pixels = new int[][] {
        {255, 184, 178, 84, 129},
        {84, 255, 255, 130, 84},
        {78, 255, 0, 0, 78},
        {84, 130, 255, 130, 84}
      };
      description = "example in directions";
      count = 5;
    }
    
    GrayImage img = new GrayImage();
    
    Field f = GrayImage.class.getDeclaredField("pixelValues");
    f.setAccessible(true);
    f.set(img, pixels);
    
    boolean success = false;
    String message = "";
    try {
      int result = img.countWhitePixels();
      if(result != count) {
        message = "calcualted value was " + result + ", should be " + count;
      }
      else {
        success = true;
      }
    }
    catch(Exception e) {
      message = e.getClass().getSimpleName();
    }
    
    if(success) System.out.println("  Passed Test " + test);
    else {
      System.out.println("  FAILED Test" + test + ": " + message);
      System.out.println("    " + description);
      System.exit(0);
    }
    
  }
  
  public static void testProcess(int test) throws NoSuchFieldException, IllegalAccessException {
    int[][] pixels;
    int[][] result;
    String description;
    
    if(test == 0) {
      pixels = new int[0][0];
      result = new int[0][0];
      description = "0 rows and 0 columns";
    }
    else if(test == 1) {
      pixels = new int[3][5];
      result = new int[3][5];
      description = "more columns than rows";
    }
    else if(test == 2) {
      pixels = new int[5][3];
      result = new int[5][3];
      description = "more rows than columns";
    }
    else if(test == 3) { //all 1's
      pixels = new int[][] {
        {12, 66, 103, 42, 175, 243},
        {57, 163, 161, 216, 91, 54},
        {3, 27, 30, 3, 172, 76},
        {23, 71, 184, 7, 243, 240},
        {7, 181, 223, 51, 7, 212},
        {173, 63, 7, 168, 100, 132}
      };
      description = "a bunch of random numbers";
      result = new int[][] {
        {0, 63, 0, 0, 175, 243},
        {0, 156, 0, 0, 91, 54},
        {0, 0, 23, 0, 172, 76},
        {16, 0, 84, 0, 243, 240},
        {7, 181, 223, 51, 7, 212},
        {173, 63, 7, 168, 100, 132}
      };
    }
    else {
      pixels = new int[][] {
        {221, 184, 178, 84, 135},
        {84, 255, 255, 130, 84},
        {78, 255, 0, 0, 78},
        {84, 130, 255, 130, 84}
      };
      description = "example in directions";
      result = new int[][] {
        {221, 184, 100, 84, 135},
        {0, 125, 171, 130, 84},
        {78, 255, 0, 0, 78},
        {84, 130, 255, 130, 84}
      };
    }
    
    GrayImage img = new GrayImage();
    
    Field f = GrayImage.class.getDeclaredField("pixelValues");
    f.setAccessible(true);
    f.set(img, pixels);
    
    boolean success = false;
    String message = "";
    try {
      img.processImage();
      if(!equal(pixels, result)) {
        message = "processed value was wrong";
      }
      else {
        success = true;
      }
    }
    catch(Exception e) {
      message = e.getClass().getSimpleName();
    }
    
    if(success) System.out.println("  Passed Test " + test);
    else {
      System.out.println("  FAILED Test" + test + ": " + message);
      System.out.println("    " + description);
      System.exit(0);
    }
    
  }
  
  public static boolean equal(int[][] array1, int[][] array2) {
    if(array1.length == 0 && array2.length == 0)
      return true;
    
    if(array1.length != array2.length || array1[0].length != array2[0].length)
      return false;
      
    for(int i = 0; i < array1.length; i++)
      for(int j = 0; j < array1[0].length; j++)
        if(array1[i][j] != array2[i][j])
          return false;
    return true;
  }
}