-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSegmentedEncoder.java
More file actions
93 lines (87 loc) · 3.75 KB
/
SegmentedEncoder.java
File metadata and controls
93 lines (87 loc) · 3.75 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
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import javax.imageio.IIOException;
import javax.imageio.ImageIO;
// By: Justin Spedding & Andrew Miller
public class SegmentedEncoder extends SegmentedSteganographer {
private ImageWriter imageWriter; // The host image manipulator
protected RandomAccessFile stego; // The stego to encode
protected int[] segmentSizes; // The number of bytes to encode in each segment
/**
* Constructs an encoder object that breaks the work up into multiple segments.
*
* @param hostImagePath
* The path to the host image
* @param stegoPath
* The path to the stego
* @param password
* The password used to store the stego
* @param segments
* The number of segments to break the work up into
* @throws FileNotFoundException
* Throws if the file path is invalid
* @throws IOException
* Throws if there is an IO error
* @throws CannotEncodeException
* Throws if the stego cannot fit in the host image
* @throws ImageOverflowException
* Throws if the internal algorithm tries to write to a pixel that does not exist
*/
public SegmentedEncoder(String hostImagePath, String stegoPath, String password) throws FileNotFoundException, IOException, IIOException, CannotEncodeException, ImageOverflowException {
segments = 100;
imageWriter = new ImageWriter(ImageIO.read(new File(hostImagePath)), password); // Initialize the host image manipulator
File stegoFile = new File(stegoPath);
stego = new RandomAccessFile(stegoFile, "r"); // Initialize the file to be encoded
String fileName = stegoFile.getName(); // Get the name of the file
if (Steganographer.canEncode(imageWriter.getHostImage(), stego, fileName.length())) { // If the file will fit in the image
segmentSizes = new int[segments];
int remainingBytes = (int) stego.length(); // Get the total number of bytes to encode
int segmentLength = (int) (((double) remainingBytes) / ((double) segments)); // Get the length of each segment
for (int i = 0; i < segments - 1; i++) { // Set the byte lengths of all but the last segment
segmentSizes[i] = segmentLength;
remainingBytes -= segmentLength;
}
segmentSizes[segmentSizes.length - 1] = remainingBytes; // Put the remaining bytes in the last segment
currentSegment = 0; // Start with the first segment
ImageRW.writeString(imageWriter, Steganographer.getMarker()); // Mark the image as having a hidden file
ImageRW.writeInt(imageWriter, (int) stego.length()); // Write the size of the file
ImageRW.writeInt(imageWriter, fileName.length()); // Write the length of the file name
ImageRW.writeString(imageWriter, fileName); // Write the file's name
} else {
throw new CannotEncodeException();
}
}
/**
* Processes the next encoding segment
*
* @throws IOException
* Throws if there is an IO error
* @throws ImageOverflowException
* Throws if the internal algorithm tries to write to a pixel that does not exist
* @throws NoSuchSegmentException
* Throws if there are no segments left to process
*/
public void nextSegment() throws IOException, IIOException, ImageOverflowException, NoSuchSegmentException {
if (hasNext()) {
int remainingBytes = segmentSizes[currentSegment];
for (; remainingBytes != 0; remainingBytes--) {
ImageRW.writeByte(imageWriter, (byte) stego.read());
}
currentSegment++;
} else {
throw new NoSuchSegmentException();
}
}
/**
* Returns the encoded image
* If not all of the segments have been completed, this will return an incomplete image.
*
* @return The encoded image, whether it is finished or not
*/
public BufferedImage getEncodedImage() {
return imageWriter.getHostImage();
}
}