-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageManipulator.java
More file actions
65 lines (58 loc) · 2.07 KB
/
Copy pathImageManipulator.java
File metadata and controls
65 lines (58 loc) · 2.07 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
import java.awt.image.BufferedImage;
import java.util.Random;
// By: Justin Spedding & Andrew Miller
public abstract class ImageManipulator {
protected BufferedImage hostImage; // The host file to write to
protected int x; // The x coordinate of the current pixel
protected int y; // The y coordinate of the current pixel
protected int rgb; // The color to access: 2 = red, 1 = green, 0 = blue
protected int currentBit; // The least significant bit to access, 0 = last bit in byte, 7 = first bit in byte
protected int index; // The index of the current pixel in the pixel array
protected int[] pixelArray; // The order in which to access pixels
protected ImageManipulator(BufferedImage hostImage, String password) {
this.hostImage = hostImage;
generatePixelArray(password.hashCode());
index = 0;
rgb = 0;
currentBit = 0;
x = pixelArray[index] % hostImage.getWidth();
y = pixelArray[index] / hostImage.getWidth();
}
private void generatePixelArray(int seed) {
pixelArray = new int[hostImage.getHeight() * hostImage.getWidth()]; // Create an array of indexes to all the pixels
for (int i = pixelArray.length - 1; i >= 0; i--) {
pixelArray[i] = i; // Add all of the indexes in order
}
Random random = new Random(seed);
for (int i = pixelArray.length - 1; i != 0; i--) { // Shuffle the array
int randomIndex = random.nextInt(i + 1);
int temp = pixelArray[i];
pixelArray[i] = pixelArray[randomIndex];
pixelArray[randomIndex] = temp;
}
}
protected void nextPixel() throws ImageOverflowException {
index++;
if (index >= pixelArray.length) { // Go to next rgb offset if necessary
index = 0;
rgb++;
if (rgb > 2) { // Go to next least significant pixel if necessary
rgb = 0;
currentBit++;
if (currentBit > 7) { // Throw exception if there is no next pixel
throw new ImageOverflowException();
}
}
}
x = pixelArray[index] % hostImage.getWidth();
y = pixelArray[index] / hostImage.getWidth();
}
/**
* Returns the host image
*
* @return The host image
*/
public BufferedImage getHostImage() {
return hostImage;
}
}