-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageStore.java
More file actions
95 lines (82 loc) · 2.48 KB
/
ImageStore.java
File metadata and controls
95 lines (82 loc) · 2.48 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
84
85
86
87
88
89
90
91
92
93
94
import java.util.*;
import processing.core.PImage;
import processing.core.PApplet;
import java.io.File;
import java.io.FileNotFoundException;
public final class ImageStore
{
private static final int KEYED_IMAGE_MIN = 5;
private Map<String, List<PImage>> images;
private List<PImage> defaultImages;
public ImageStore(PImage defaultImage)
{
this.images = new HashMap<>();
defaultImages = new LinkedList<>();
defaultImages.add(defaultImage);
}
public List<PImage> get(String key)
{
return images.getOrDefault(key, defaultImages);
}
public static void loadImages(Scanner in, ImageStore imageStore,
int tile_width, int tile_height, PApplet sketch)
throws FileNotFoundException
{
while (in.hasNextLine())
{
processImageLine(imageStore.images, in.nextLine(), sketch);
}
}
private static void processImageLine(Map<String, List<PImage>> images,
String line, PApplet sketch)
{
String[] attrs = line.split("\\s");
if (attrs.length >= 2)
{
String key = attrs[0];
PImage img = sketch.loadImage(attrs[1]);
if (img != null && img.width != -1)
{
List<PImage> imgs = getImages(images, key);
imgs.add(img);
if (attrs.length >= KEYED_IMAGE_MIN)
{
int r = Integer.parseInt(attrs[2]);
int g = Integer.parseInt(attrs[3]);
int b = Integer.parseInt(attrs[4]);
setAlpha(img, sketch.color(r, g, b), 0);
}
}
}
}
private static List<PImage> getImages(Map<String, List<PImage>> images,
String key)
{
List<PImage> imgs = images.get(key);
if (imgs == null)
{
imgs = new LinkedList<>();
images.put(key, imgs);
}
return imgs;
}
private static final int COLOR_MASK = 0xffffff;
// Called with color for which alpha should be set and alpha value.
//PImage img = setAlpha(loadImage("wyvern1.bmp"), color(255, 255, 255), 0));
private static PImage setAlpha(PImage img, int maskColor, int alpha)
{
int alphaValue = alpha << 24;
int nonAlpha = maskColor & COLOR_MASK;
img.format = PApplet.ARGB;
img.loadPixels();
for (int i = 0; i < img.pixels.length; i++)
{
if ((img.pixels[i] & COLOR_MASK) == nonAlpha)
{
img.pixels[i] = alphaValue | nonAlpha;
}
}
img.updatePixels();
return img;
}
}