-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShowImage.java
More file actions
276 lines (216 loc) · 5.98 KB
/
Copy pathShowImage.java
File metadata and controls
276 lines (216 loc) · 5.98 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
import java.io.*;
import java.awt.*;
import java.awt.image.*;
import java.applet.*;
public class ShowImage extends Applet {
MemoryImageSource imgsrc;
Image lennaImage;
Image FFTedImage;
private Panel subPanel = new Panel();
int[] pixels;
private final static String FILE_NAME = "e:\\corso\\mc\\lenna.bin";
private final static int IMAGE_WIDTH = 256;
private
int[][] load_matrix( String fileName )
{
FileInputStream fis = null;
try {
fis = new FileInputStream( fileName );
}
catch( FileNotFoundException e )
{
System.out.println( "Input file not found: " + fileName );
}
byte row[] = new byte[ IMAGE_WIDTH ];
int img[][] = new int[ IMAGE_WIDTH ] [ IMAGE_WIDTH ];
try {
for( int i=0; i < 256; i++ ) {
fis.read( row );
// Because byte type in Java in signed by default and it's impossible
// to change its behavious i have to add 128 in order to shift them into the range [0, 255]
for( int j=0; j < IMAGE_WIDTH; j++ ) {
img[i][j] = row[j];
if (img[i][j] < 0 ) img[i][j] += 256;
}
}
}
catch( IOException e )
{
System.out.println( "Unexpected IO error: " + e );
return null;
}
finally
{
try{ fis.close(); } catch( IOException e) {};
}
return img;
}
private
int[] fromMatrixToArray( int[][] mat )
{
int pixels[] = new int[ IMAGE_WIDTH * IMAGE_WIDTH ];
for(int i=0; i < IMAGE_WIDTH; i++ )
for(int j=0; j < IMAGE_WIDTH; j++ )
{
// int pixel = 256 - mat[j][i];
int pixel = mat[j][i];
pixels [ i * IMAGE_WIDTH + j ] = (255 << 24) | (pixel << 16) | (pixel << 8) | pixel;;
// pixels [ i * IMAGE_WIDTH + j ] = (pixel << 24);
}
return pixels;
}
public
String getAppletInfo() {
return "Show PlayBoy cover's girl.";
}
final static
void gbcon(GridBagLayout gridbag, GridBagConstraints c, Component cp, Panel p) {
gridbag.setConstraints(cp, c);
p.add(cp);
}
public
double [][][] applyFFT( int[][] in )
{
double out[][][] = new double[2] [ IMAGE_WIDTH ][ IMAGE_WIDTH ];
double re[] = new double[ IMAGE_WIDTH ];
double im[] = new double[ IMAGE_WIDTH ];
// First row by row
for( int i=0; i < IMAGE_WIDTH; i++ ) {
for( int j=0; j < IMAGE_WIDTH; j++ ) {
re[j] = in[i][j];
im[j] = 0;
}
FT.FFT( 8, re, im );
// FT.DFT( 1, IMAGE_WIDTH, re, im );
for( int j=0; j < IMAGE_WIDTH; j++ ) {
out[0][i][j] = re[j];
out[1][i][j] = im[j];
}
}
// and now column by columns
for( int i=0; i < IMAGE_WIDTH; i++ ) {
for( int j=0; j < IMAGE_WIDTH; j++ ) {
re[j] = out[0][j][i];
im[j] = out[1][j][i];
}
FT.FFT( 8, re, im );
// FT.DFT( 1, IMAGE_WIDTH, re, im );
for( int j=0; j < IMAGE_WIDTH; j++ ) {
out[0][j][i] = re[j];
out[1][j][i] = im[j];
}
}
return out;
}
private
int [][] normalizeColours( double [][][] in )
{
int img[][] = new int[ IMAGE_WIDTH ][ IMAGE_WIDTH ];
int white = 0;
int black = 0;
int min = 1000;
int max = -1000;
for( int i=0; i < IMAGE_WIDTH; i++ ) {
for( int j=0; j < IMAGE_WIDTH; j++ ) {
/*
if (in[0][i][j] < 0 ) {
img[i][j] = (byte)0;
white++;
// System.out.println("["+i+","+j+"] - re:"+in[0][i][j]+",i:"+in[1][i][j]);
}
else
if (in[0][i][j] > 255 ) {
img[i][j] = (byte)255;
black++;
}
else
img[i][j] = (byte)in[0][i][j];
*/
img[i][j] = (int)(in[0][i][j]);
// System.out.println("["+i+","+j+"]: "+img[i][j]);
/*
if ( in[0][i][j] < min ) min = (int)in[0][i][j];
else
if ( in[0][i][j] > max ) max = (int)in[0][i][j];
*/
if ( img[i][j] < min ) min = (int)img[i][j];
else
if ( img[i][j] > max ) max = (int)img[i][j];
}
// System.out.println();
}
/*
for( int j=0; j < IMAGE_WIDTH; j++ )
System.out.println("iFFT[255,"+j+"]: "+img[255][j]);
*/
System.out.print( "Total: [" + IMAGE_WIDTH * IMAGE_WIDTH + "] White: [" + white + "] Black: [" + black + "]" );
System.out.println(" Min: ["+min+"] Max:["+max+"]");
return img;
}
public
double [][] [] applyInverseFFT( double[][][] in )
{
double out[][][] = new double [2] [ IMAGE_WIDTH ][ IMAGE_WIDTH ];
double re[] = new double[ IMAGE_WIDTH ];
double im[] = new double[ IMAGE_WIDTH ];
// First row by row
for( int i=0; i < IMAGE_WIDTH; i++ ) {
for( int j=0; j < IMAGE_WIDTH; j++ ) {
re[j] = in[0][i][j];
im[j] = in[1][i][j];
}
FT.iFFT( 8, re, im );
// FT.DFT( -1, IMAGE_WIDTH, re, im );
for( int j=0; j < IMAGE_WIDTH; j++ ) {
out[0][i][j] = re[j];
out[1][i][j] = im[j];
}
}
// and now column by columns
for( int i=0; i < IMAGE_WIDTH; i++ ) {
for( int j=0; j < IMAGE_WIDTH; j++ ) {
re[j] = out[0][j][i];
im[j] = out[1][j][i];
}
FT.iFFT( 8, re, im );
// FT.DFT( -1, IMAGE_WIDTH, re, im );
for( int j=0; j < IMAGE_WIDTH; j++ ) {
out[0][j][i] = re[j];
out[1][j][i] = re[j];
}
}
return out;
}
public void init() {
int[][] lenna = load_matrix( FILE_NAME );
// pixels = fromMatrixToArray( img );
// Let's print the last line of the original image
/*
for( int j=0; j < IMAGE_WIDTH; j++ )
System.out.println("lenna[255,"+j+"]: "+lenna[255][j]);
*/
double[][][] FFTImg = applyFFT( lenna );
double[][][] iFFTImg = applyInverseFFT( FFTImg );
int[][] img2 = normalizeColours(iFFTImg);
pixels = fromMatrixToArray( img2 );
imgsrc = new MemoryImageSource(IMAGE_WIDTH, IMAGE_WIDTH, fromMatrixToArray(lenna), 0, IMAGE_WIDTH);
lennaImage = createImage(imgsrc);
imgsrc = new MemoryImageSource(IMAGE_WIDTH, IMAGE_WIDTH, pixels, 0, IMAGE_WIDTH);
FFTedImage = createImage(imgsrc);
resize(256, 600);
}
public void start() {
}
public synchronized void stop() {
notify();
}
public void paint(Graphics g) {
// Draw the animated image
g.drawImage(lennaImage, 0, 0, this);
g.drawImage(FFTedImage, 0, 300, this);
}
public static
void main(String args[]) {
AppletFrame.startApplet("ShowImage", "Lenna", args);
}
}