/
FieldUtils.java
61 lines (52 loc) · 2.01 KB
/
FieldUtils.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
package msi.gama.common.util;
import java.awt.image.BufferedImage;
import java.awt.image.PixelGrabber;
import msi.gama.metamodel.shape.GamaPoint;
import msi.gama.runtime.IScope;
import msi.gama.util.file.GamaImageFile;
import msi.gama.util.file.IFieldMatrixProvider;
import msi.gaml.species.ISpecies;
public class FieldUtils {
private static final double[] DNULL = new double[0];
public static double[] buildDoubleArrayFrom(final IScope scope, final Object object, final GamaPoint dim) {
if (object instanceof IFieldMatrixProvider) {
IFieldMatrixProvider provider = (IFieldMatrixProvider) object;
dim.x = provider.getCols(scope);
dim.y = provider.getRows(scope);
return provider.getFieldData(scope);
}
// Special case for grid species
if (object instanceof ISpecies) {
ISpecies species = (ISpecies) object;
if (species.isGrid())
return buildDoubleArrayFrom(scope, species.getPopulation(scope).getTopology().getPlaces(), dim);
}
return DNULL;
}
public static float[] buildFloatArrayFrom(final IScope scope, final Object object, final GamaPoint dim) {
if (object instanceof GamaImageFile) return buildFloatArrayFrom(scope, (GamaImageFile) object, dim);
return toFloats(buildDoubleArrayFrom(scope, object, dim));
}
private static float[] buildFloatArrayFrom(final IScope scope, final GamaImageFile file, final GamaPoint dim) {
BufferedImage image = file.getImage(scope, true);
dim.x = image.getWidth();
dim.y = image.getHeight();
final float[] values = new float[(int) (dim.x * dim.y)];
int[] pixels = new int[values.length];
PixelGrabber pgb = new PixelGrabber(image, 0, 0, (int) dim.x, (int) dim.y, pixels, 0, (int) dim.x);
try {
pgb.grabPixels();
} catch (InterruptedException e) {}
for (int i = 0; i < values.length; ++i) {
values[i] = pixels[i] & 255;
}
return values;
}
public static float[] toFloats(final double[] array) {
float[] result = new float[array.length];
for (int i = 0; i < array.length; ++i) {
result[i] = (float) array[i];
}
return result;
}
}