-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStegoDisplay.java
More file actions
202 lines (164 loc) · 6.31 KB
/
Copy pathStegoDisplay.java
File metadata and controls
202 lines (164 loc) · 6.31 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/**
* Write a description of class StegoDisplay here.
*
* @author (your name)
* @version (a version number or a date)
*/
import java.lang.Object;
import java.awt.*;
import java.io.File;
import javax.swing.*;
import java.util.*;
import squint.*;
public class StegoDisplay extends GUIManager
{
private ImageViewer cover;
private ImageViewer secret;
private ImageViewer encrypted;
private JSplitPane split;
private JPanel bigPanel;
private JTextArea theTextArea;
private JButton encodeButton;
private JButton decodeButton;
private JButton encryptButton;
private JButton decryptButton;
private JButton clearTextButton;
private JButton clearImageButton;
private JPanel buttonPanel;
private JButton whichButton;
public StegoDisplay()
{
this.createWindow(1000,600);
contentPane.setLayout(new BorderLayout());
cover = new ImageViewer();
secret = new ImageViewer();
encrypted = new ImageViewer();
theTextArea = new JTextArea();
split = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
split.setTopComponent(theTextArea);
split.setBottomComponent(secret);
bigPanel = new JPanel();
bigPanel.setLayout(new GridLayout(1,3));
bigPanel.add(cover);
bigPanel.add(split);
bigPanel.add(encrypted);
contentPane.add(bigPanel, BorderLayout.CENTER);
encodeButton = new JButton("Encode");
encodeButton.setActionCommand("Encode");
decodeButton = new JButton("Decode");
decodeButton.setActionCommand("Decode");
encryptButton = new JButton("Encrypt");
encryptButton.setActionCommand("Encrypt");
decryptButton = new JButton("Decrypt");
decryptButton.setActionCommand("Decrypt");
clearTextButton = new JButton("Clear Text");
clearTextButton.setActionCommand("Clear Text");
clearImageButton = new JButton("Clear Image");
clearImageButton.setActionCommand("Clear Image");
buttonPanel = new JPanel();
buttonPanel.add(encodeButton);
buttonPanel.add(decodeButton);
buttonPanel.add(encryptButton);
buttonPanel.add(decryptButton);
buttonPanel.add(clearTextButton);
buttonPanel.add(clearImageButton);
contentPane.add(buttonPanel, BorderLayout.SOUTH);
split.setDividerLocation(0.5);
}
public void buttonClicked( JButton whichButton )
{
String actionCommand = whichButton.getActionCommand();
if(actionCommand.equals(clearImageButton.getActionCommand()))
{
secret.clear();
}else if(actionCommand.equals(clearTextButton.getActionCommand()))
{
theTextArea.setText("");
}else if(actionCommand.equals(encryptButton.getActionCommand()))
{
SImage coverImage = cover.getImage();
SImage secretImage = secret.getImage() ;
//to test
//System.out.print(secretImage);
//secret.setImage(secretImage);
SImage encryptImage;
if(coverImage.getWidth() == secretImage.getWidth() && coverImage.getHeight() == secretImage.getHeight())
{
encryptImage = ImageFilter.addImages(ImageFilter.clearLowerBit(coverImage),ImageFilter.scaleImage(secretImage,1,128));
}else
{
int[][] pixel = new int[coverImage.getWidth()][coverImage.getHeight()];
for (int x = 0; x < coverImage.getWidth(); ++x)
{
for(int y = 0; y < coverImage.getHeight(); ++y)
{
pixel[x][y] = 0;
}
}
int[][] secretPixel = secretImage.getPixelArray();
int minWidth = Math.min(coverImage.getWidth(),secretImage.getWidth());
int minHeight = Math.min(coverImage.getHeight(),secretImage.getHeight());
for (int x = 0; x < minWidth; ++x)
{
for(int y = 0; y < minHeight; ++y)
{
pixel[x][y] = secretPixel[x][y];
}
}
secretImage = new SImage(pixel);
encryptImage = ImageFilter.addImages(ImageFilter.clearLowerBit(coverImage), ImageFilter.scaleImage(secretImage,1,128));
}
encrypted.setImage(encryptImage);
}else if(actionCommand.equals(decryptButton.getActionCommand()))
{
SImage oldImage = encrypted.getImage();
SImage newImage = ImageFilter.clearLowerBit(oldImage);
SImage newSecretImage = ImageFilter.subtractImage(oldImage,newImage);
newSecretImage = ImageFilter.scaleImage(newSecretImage,128,1);
secret.setImage(newSecretImage);
}else if(actionCommand.equals(encodeButton.getActionCommand()))
{
SImage coverImage = cover.getImage();
SImage secretImage = convert2Image(StringConverter.convert2Bits(theTextArea.getText()),
coverImage.getWidth(), coverImage.getHeight());
secret.setImage(secretImage);
}else if(actionCommand.equals(decodeButton.getActionCommand()))
{
String text = StringConverter.convert2String(convert2Bits(secret.getImage()));
theTextArea.setText(text);
}
}
public SImage convert2Image(int[] bits, int width, int height)
{
SImage image;
int[][] pixel = new int[width][height];
for (int x = 0; x < width; ++x)
{
for (int y = 0; y < height; ++y)
{
if (height * x + y >= bits.length)
{
pixel[x][y] = 0;
} else {
pixel[x][y] = bits[height * x + y] * 128;
}
}
}
return new SImage(pixel);
}
private int[] convert2Bits(SImage image)
{
int width = image.getWidth();
int height = image.getHeight();
int[][] pixel = image.getPixelArray();
int[] bits = new int[width * height];
for (int x = 0; x < width; ++x)
{
for (int y = 0; y < height; ++y)
{
bits[height * x + y] = pixel[x][y] / 128;
}
}
return bits;
}
}