-
Notifications
You must be signed in to change notification settings - Fork 3
/
ChannelShiftGUI.pde
99 lines (85 loc) · 3.89 KB
/
ChannelShiftGUI.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
92
93
94
95
96
97
98
import g4p_controls.*;
// Configs =====================================================================
// Input File ------------------------------------------------------------------
// Default image to load on start
String defaultImgName = "test" + int(random(0,2));
String defaultImgPath = "demo/" + defaultImgName + ".jpg";
// Globals =====================================================================
// Image managers
ImgManager imgManager;
// Sketch state managers
ChannelManager channelManager;
ShiftManager xShiftManager, yShiftManager;
RandomizeManager randomizeManager;
// Advanced option managers
ShiftTypeManager shiftTypeManager;
// Sketch step manager
StepManager stepManager;
// Interface managers
WindowManager windowManager;
// Constants ===================================================================
// String to use for indent in output msgs
String INDENT = " ";
// Helper Methods ==============================================================
// Channel Shift ---------------------------------------------------------------
/**
* Shift and swap color channels
* @param sourceImg The base PImage object to be channel shifted
* @param targetImg The PImage object to apply channel swaps/shifts to
* @param xShift Amount to shift channel horizontally
* @param yShift Amount to shift channel vertically
* @param sourceChannel Color channel to grab from sourceImg (Index into
* CHANNELS)
* @param targetChannel Color channel to insert shifted sourceChannel as in
* targetImg (Index into CHANNELS). Set to the same value as sourceChannel if
* you don't want to swap color channels
*/
void shiftChannel(PImage sourceImg, PImage targetImg, int xShift, int yShift, int sourceChannel, int targetChannel) {
// Get pixels
color[] sourcePixels = sourceImg.pixels;
color[] targetPixels = targetImg.pixels;
// Loop thru rows
for (int y = 0; y < targetImg.height; y++) {
// Loop thru pixels in current row
for (int x = 0; x < targetImg.width; x++) {
int yOffset = mod(shiftTypeManager.calculateShiftOffset(x, y, targetImg.width, targetImg.height, yShift, false), targetImg.height);
int xOffset = mod(shiftTypeManager.calculateShiftOffset(x, y, targetImg.width, targetImg.height, xShift, true), targetImg.width);
// Get source pixel and its RGB vals
int sourceIndex = yOffset * sourceImg.width + xOffset;
color sourcePixel = sourcePixels[sourceIndex];
float[] sourceRGB = new float[]{ red(sourcePixel), green(sourcePixel), blue(sourcePixel) };
// Get target pixel and its RGB vals
int targetIndex = y * targetImg.width + x;
color targetPixel = targetPixels[targetIndex];
float[] targetRGB = new float[]{ red(targetPixel), green(targetPixel), blue(targetPixel) };
// Swap source channel w/ target channel
targetRGB[targetChannel] = sourceRGB[sourceChannel];
// TODO !!! targetRGB[sourceChannel] = sourceRGB[targetChannel] ???
targetPixels[targetIndex] = color(targetRGB[0], targetRGB[1], targetRGB[2]);
}
}
}
// Processing ==================================================================
void setup() {
// Window
size(1,1);
// Initialize managers
imgManager = new ImgManager();
channelManager = new ChannelManager();
xShiftManager = new ShiftManager(true);
yShiftManager = new ShiftManager(false);
shiftTypeManager = new ShiftTypeManager();
randomizeManager = new RandomizeManager();
stepManager = new StepManager(xShiftManager, yShiftManager, channelManager, shiftTypeManager, imgManager);
windowManager = new WindowManager();
// Load image (initializes global PImage objects)
loadImageFile(defaultImgPath, defaultImgName);
// Display controls window
createGUI();
}
void draw() {
windowManager.updatePreview(imgManager.previewImg);
boolean wasImageModified = imgManager.hasPreviewImageBeenModified();
windowManager.updateWindowTitle(wasImageModified);
updateControlsWindow(wasImageModified);
}