-
Notifications
You must be signed in to change notification settings - Fork 3
/
images.pde
92 lines (73 loc) · 2.7 KB
/
images.pde
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
import java.util.Arrays;
// =============================================================================
// Globals related to working PImage objects
// =============================================================================
// Manager =====================================================================
// TODO store previous image objects to allow for undo?
/**
* Manage the source, target, and preview PImage objects
*/
public class ImgManager {
// Source, modified, and preview images
public PImage sourceImg, targetImg, previewImg;
// Width/height vars for easy access
public int imgWidth, imgHeight;
// Use resulting image as the source for next iteration upon confirming step
public boolean recursiveIteration;
public ImgManager() {
this.imgWidth = this.imgHeight = 0;
this.recursiveIteration = true;
}
// Getter/Setter
public void loadImageFile(String path) {
// Initialize images
this.sourceImg = loadImage(path);
this.targetImg = sourceImg.copy();
this.previewImg = sourceImg.copy();
// This seems to fix a bug where the recursive option doesn't do anything
this.sourceImg.loadPixels();
this.targetImg.loadPixels();
this.previewImg.loadPixels();
// Update width/height vars
this.imgWidth = this.sourceImg.width;
this.imgHeight = this.sourceImg.height;
}
public void toggleRecursiveIteration(boolean recursive) { this.recursiveIteration = recursive; }
// Confirm Current Step
public void confirmStep() {
// Update targetImg to match preview
this.copyPreviewToTarget();
// If recursive, sourceImg.pixels = targetImg.pixels
if (this.recursiveIteration)
this.copyTargetPixelsToSource();
}
// Image Utility Methods
// TODO: doc, better names?
public void savePreviewImg(String path) {
this.previewImg.save(path);
}
public void copyTargetToPreview() {
this.previewImg = this.targetImg.copy();
this.previewImg.loadPixels();
}
public void copyPreviewToTarget() {
this.targetImg = this.previewImg.copy();
this.targetImg.loadPixels();
}
public void updatePreview() {
this.previewImg.updatePixels();
}
// For recursive iterations
public void copyTargetPixelsToSource() {
this.sourceImg.pixels = this.targetImg.pixels;
this.sourceImg.updatePixels();
}
public boolean hasPreviewImageBeenModified() {
return !Arrays.equals(this.previewImg.pixels, this.targetImg.pixels);
}
}
// Event Handlers ==============================================================
// Recursive Checkbox ----------------------------------------------------------
public void recursiveCheckbox_click(GCheckbox source, GEvent event) {
imgManager.toggleRecursiveIteration(source.isSelected());
}