-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathArgbBufferedImageSource.java
More file actions
72 lines (62 loc) · 1.42 KB
/
ArgbBufferedImageSource.java
File metadata and controls
72 lines (62 loc) · 1.42 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
package software.coley.bentofx.control.canvas;
import jakarta.annotation.Nonnull;
import java.awt.image.BufferedImage;
import java.util.Arrays;
/**
* ARGB source wrapping a {@link BufferedImage}.
*
* @author Matt Coley
*/
public class ArgbBufferedImageSource implements ArgbSource {
private final BufferedImage image;
private int[] fullArgbCache;
private int hash;
/**
* @param image
* Wrapped image.
*/
public ArgbBufferedImageSource(@Nonnull BufferedImage image) {
this.image = image;
}
@Override
public int getWidth() {
return image.getWidth();
}
@Override
public int getHeight() {
return image.getHeight();
}
@Override
public int getArgb(int x, int y) {
try {
return image.getRGB(x, y);
} catch (Throwable t) {
// Thrown when coordinates are out of bounds.
// Default to transparent black.
return 0;
}
}
@Override
public int[] getArgb(int x, int y, int width, int height) {
try {
return image.getRGB(x, y, width, height, null, 0, width);
} catch (Throwable t) {
// Thrown when coordinates are out of bounds.
return null;
}
}
@Nonnull
@Override
public int[] getArgb() {
// We will likely be using this a bit, so it makes sense to cache the result.
if (fullArgbCache == null)
fullArgbCache = ArgbSource.super.getArgb();
return fullArgbCache;
}
@Override
public int hashCode() {
if (hash == 0)
hash = Arrays.hashCode(getArgb());
return hash;
}
}