-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPure.java
More file actions
83 lines (72 loc) · 3.08 KB
/
Pure.java
File metadata and controls
83 lines (72 loc) · 3.08 KB
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
/*
* Copyright (c) 2021 the original author or authors.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
public class Pure {
public static void main(String[] args) {
int warmup = 100;
int runs = 1000;
int width = 1920;
int height = 1080;
int channels = 1;
boolean normalize = true;
int kernelSize = 5;
double[][] averageKernel = new double[kernelSize][kernelSize];
for (int x = 0; x < kernelSize; x++) {
for (int y = 0; y < kernelSize; y++) {
averageKernel[x][y] = 1.0 / ((double) (kernelSize * kernelSize));
}
}
short[][][] image = new short[height][width][channels];
short cnt = 0;
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
for (int c = 0; c < channels; c++) {
image[y][x][c] = cnt++;
if (cnt > 255) {
cnt = 0;
}
}
}
}
for (int r = 0; r < runs + warmup; r++) {
long before = System.currentTimeMillis();
short[][][] result = new short[height][width][channels];
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
for (int c = 0; c < channels; c++) {
double sum = 0.0;
double maskSum = 0.0;
int yRadius = (kernelSize - 1) / 2;
for (int yOffset = -yRadius; yOffset <= yRadius; yOffset++) {
int xRadius = (kernelSize - 1) / 2;
for (int xOffset = -xRadius; xOffset <= xRadius; xOffset++) {
int nbX = x + xOffset;
int nbY = y + yOffset;
if ((nbX >= 0) && (nbX < width) && (nbY >= 0) && (nbY < height)) {
sum += result[nbY][nbX][0] * averageKernel[yOffset + yRadius][xOffset + xRadius];
maskSum += averageKernel[yOffset + yRadius][xOffset + xRadius];
} //range check
} // xOffset
} // yOffset
if (normalize) {
if (maskSum == 0) {
throw new IllegalStateException("KernelSum is 0 but normalizing is active");
}
sum *= 1.0 / maskSum;
} // normalize
result[y][x][c] = (short) sum;
}
}
}
long after = System.currentTimeMillis();
if(r > warmup - 1){
System.out.println("" + (after - before));
}
}
}
}