aboutsummaryrefslogtreecommitdiff
path: root/src/test/java/Tests.java
blob: cbb154f97e9c5b029950b886458bc2e6904c880a (plain)
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
import org.junit.Assert;
import org.junit.Test;
import org.junit.internal.InexactComparisonCriteria;

public class Tests {

    // Tolerance used to compare two floating point numbers
    private static final float EPSILON = 0.0001f;

    // Check whether value is equal to expected integer
    private void assertEquals(int expected, int actual) {
        Assert.assertEquals(expected, actual);
    }

    // Check whether value is approximately equal to expected floating point number
    private void assertEquals(float expected, float actual) {
        Assert.assertEquals(expected, actual, EPSILON);
    }

    // Check whether integer array has correct size and values
    private void assertEquals(int[] expected, int[] actual) {
        Assert.assertArrayEquals(expected, actual);
    }

    // Check whether integer two-dimensional array has correct size and values
    private void assertEquals(int[][] expected, int[][] actual) {
        Assert.assertArrayEquals(expected, actual);
    }

    // Check whether floating point two-dimensional array has correct size and similar values
    private void assertEquals(float[][] expected, float[][] actual) {
        new InexactComparisonCriteria(EPSILON).arrayEquals(null, expected, actual);
    }

    // Test based on example in section 3.1 (pixel coding)
    @Test
    public void testColorSinglePixel() {

        // Check RGB components and gray level of this bluish color
        int color = 0b00100000_11000000_11111111;
        assertEquals(0.1254902f, Color.getRed(color));
        assertEquals(0.7529412f, Color.getGreen(color));
        assertEquals(1.0f, Color.getBlue(color));
        assertEquals(0.6261438f, Color.getGray(color));

        // Encode some colors
        assertEquals(0x0000ff, Color.getRGB(0.0f, 0.0f, 1.0f));
        assertEquals(0x7f7f7f, Color.getRGB(0.5f));
        assertEquals(0x0000ff, Color.getRGB(-0.5f, 0, 2));
        assertEquals(0x000000, Color.getRGB(-1.0f));

    }

    // Test based on example in section 3.2 (image coding)
    @Test
    public void testColorWholeImage() {

        // A 2x2 image
        int[][] image = {
                {0x20c0ff, 0x123456},
                {0xffffff, 0x000000}
        };

        // Convert to grayscale
        float[][] gray = {
                {0.62614375f, 0.20392157f},
                {1.0f, 0.0f}
        };
        assertEquals(gray, Color.toGray(image));

        // Convert back to integer representation
        int[][] back = {
                {0x9f9f9f, 0x343434},
                {0xffffff, 0x000000}
        };
        assertEquals(back, Color.toRGB(gray));

    }

    // Test based on example in section 4.1 (image filtering)
    @Test
    public void testFilterConvolution() {

        // A 2x2 grayscale image
        float[][] gray = {
                {0.5f, 1.0f},
                {0.2f, 0.0f}
        };

        // Access some pixels
        assertEquals(0.5f, Filter.at(gray, 0, 0));
        assertEquals(0.2f, Filter.at(gray, 1, 0));
        assertEquals(0.2f, Filter.at(gray, 2, -1));

        // Apply smoothing and Sobel
        assertEquals(new float[][]{
                {0.49f, 0.62f},
                {0.3f, 0.29f}
        }, Filter.smooth(gray));
        assertEquals(new float[][]{
                {1.3f, 1.3f},
                {-0.1f, -0.1f}
        }, Filter.sobelX(gray));
        assertEquals(new float[][]{
                {-1.9f, -3.3f},
                {-1.9f, -3.3f}
        }, Filter.sobelY(gray));
        assertEquals(new float[][]{
                {2.302173f, 3.5468295f},
                {1.9026297f, 3.3015149f}
        }, Filter.sobel(gray));

    }

    // Test based on example in section 5.1 (shortest path)
    @Test
    public void testSeamPath() {

        // Simple graph stored as an adjacency list
        int[][] successors = new int[][]{
            /* 0 -> */ {1, 3},
			/* 1 -> */ {2},
			/* 2 -> */ {},
			/* 3 -> */ {4},
			/* 4 -> */ {1, 5},
			/* 5 -> */ {2}
        };
        float[] costs = new float[]{
			/* 0 : */ 1.0f,
			/* 1 : */ 9.0f,
			/* 2 : */ 2.0f,
			/* 3 : */ 1.0f,
			/* 4 : */ 3.0f,
			/* 5 : */ 1.0f
        };

        // Compute and check shortest path
        int[] vertices = Seam.path(successors, costs, 0, 2);
        assertEquals(new int[]{0, 3, 4, 5, 2}, vertices);

    }

    // Test based on example in section 5.2 (resizing)
    @Test
    public void testSeamResize() {

        // A 3x3 image
        int[][] image = {
                {0x888888, 0x666666, 0xcccccc},
                {0x000000, 0x111111, 0x222222},
                {0xbbbbbb, 0xffffff, 0x666666}
        };
        float[][] gray = Color.toGray(image);

        // Find best seam, using gray level as energy
        int[] seam = Seam.find(gray);
        assertEquals(new int[]{1, 1, 2}, seam);

        // Remove seam
        int[][] resized = Seam.shrink(image, seam);
        assertEquals(new int[][]{
                {0x888888, 0xcccccc},
                {0x000000, 0x222222},
                {0xbbbbbb, 0xffffff}
        }, resized);

    }

}