-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageRW.java
More file actions
165 lines (154 loc) · 4.49 KB
/
Copy pathImageRW.java
File metadata and controls
165 lines (154 loc) · 4.49 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// By: Justin Spedding & Andrew Miller
public class ImageRW {
/**
* Writes a byte to an image
*
* @param imageWriter
* The image writer to use
* @param byteToWrite
* The byte to write
* @throws ImageOverflowException
* Throws if the internal algorithm tries to write to a pixel that does not exist
*/
public static void writeByte(ImageWriter imageWriter, byte byteToWrite) throws ImageOverflowException {
for (int i = 7; i >= 0; i--) {
imageWriter.writeBit((byteToWrite >> i) & 1);
}
}
/**
* Writes a char to an image
*
* @param imageWriter
* The image writer to use
* @param charToWrite
* The char to write
* @throws ImageOverflowException
* Throws if the internal algorithm tries to write to a pixel that does not exist
*/
public static void writeChar(ImageWriter image, char charToWrite) throws ImageOverflowException {
for (int i = 15; i >= 0; i--) {
image.writeBit((charToWrite >> i) & 1);
}
}
/**
* Writes an int to an image
*
* @param imageWriter
* The image writer to use
* @param intToWrite
* The int to write
* @throws ImageOverflowException
* Throws if the internal algorithm tries to write to a pixel that does not exist
*/
public static void writeInt(ImageWriter imageWriter, int intToWrite) throws ImageOverflowException {
for (int i = 31; i >= 0; i--) {
imageWriter.writeBit((intToWrite >> i) & 1);
}
}
/**
* Writes a long to an image
*
* @param imageWriter
* The image writer to use
* @param longToWrite
* The long to write
* @throws ImageOverflowException
* Throws if the internal algorithm tries to write to a pixel that does not exist
*/
public static void writeLong(ImageWriter imageWriter, long longToWrite) throws ImageOverflowException {
for (int i = 63; i >= 0; i--) {
imageWriter.writeBit((int) (longToWrite >> i) & 1);
}
}
/**
* Writes a string to an image
*
* @param imageWriter
* The image writer to use
* @param stringToWrite
* The string to write
* @throws ImageOverflowException
* Throws if the internal algorithm tries to write to a pixel that does not exist
*/
public static void writeString(ImageWriter imageWriter, String stringToWrite) throws ImageOverflowException {
char[] c = stringToWrite.toCharArray();
for (int i = 0; i < c.length; i++) {
writeChar(imageWriter, c[i]);
}
}
/**
* Reads a byte from an image
*
* @param imageReader
* The image reader to use
* @throws ImageOverflowException
* Throws if the internal algorithm tries to write to a pixel that does not exist
*/
public static byte readByte(ImageReader imageReader) throws ImageOverflowException {
int output = 0;
for (int i = 0; i < 8; i++) {
output = output * 2 + imageReader.readBit();
}
return (byte) output;
}
/**
* Reads a char from an image
*
* @param imageReader
* The image reader to use
* @throws ImageOverflowException
* Throws if the internal algorithm tries to write to a pixel that does not exist
*/
public static char readChar(ImageReader imageReader) throws ImageOverflowException {
int output = 0;
for (int i = 0; i < 16; i++) {
output = output * 2 + imageReader.readBit();
}
return (char) output;
}
/**
* Reads an int from an image
*
* @param imageReader
* The image reader to use
* @throws ImageOverflowException
* Throws if the internal algorithm tries to write to a pixel that does not exist
*/
public static int readInt(ImageReader imageReader) throws ImageOverflowException {
int output = 0;
for (int i = 0; i < 32; i++) {
output = output * 2 + imageReader.readBit();
}
return output;
}
/**
* Reads a long from an image
*
* @param imageReader
* The image reader to use
* @throws ImageOverflowException
* Throws if the internal algorithm tries to write to a pixel that does not exist
*/
public static long readLong(ImageReader imageReader) throws ImageOverflowException {
long output = 0;
for (int i = 0; i < 64; i++) {
output = output * 2 + imageReader.readBit();
}
return output;
}
/**
* Reads a string from an image
*
* @param imageReader
* The image reader to use
* @throws ImageOverflowException
* Throws if the internal algorithm tries to write to a pixel that does not exist
*/
public static String readString(ImageReader imageReader, int length) throws ImageOverflowException {
String output = "";
for (int i = 0; i < length; i++) {
output = output + readChar(imageReader);
}
return output;
}
}