/Users/lyon/j4p/src/ip/hak/HaarLiftingFrame.java
|
1 package ip.hak;
2
3 import java.awt.*;
4 import java.awt.event.ActionEvent;
5 import java.awt.event.ActionListener;
6 import java.awt.image.ColorModel;
7 import java.awt.image.MemoryImageSource;
8 import java.awt.image.PixelGrabber;
9 import java.io.FileOutputStream;
10 import java.io.IOException;
11 import java.io.ObjectOutputStream;
12 import java.util.zip.ZipEntry;
13 import java.util.zip.ZipOutputStream;
14
15 public class HaarLiftingFrame extends ip.gui.frames.ClosableFrame implements ActionListener {
16 MenuBar mb = new MenuBar();
17 Menu fMenu = new Menu("File");
18 MenuItem open_MI, quit_MI;
19 Image img = null,xfimg[];
20 String fname = null,dir = null;
21 RecieveFrame rf;
22 short r[][],g[][],b[][];
23 Button svButton,sdButton;
24 int w, h;
25
26
27 public static void main(String args[]) {
28 HaarLiftingFrame hlf = new HaarLiftingFrame("Haar Lifting Frame");
29 hlf.setVisible(true);
30 }
31
32 public HaarLiftingFrame(String title) {
33 super(title);
34 init();
35 }
36
37 public void init() {
38 setLayout(null);
39 int c = 203;
40 Color mc = new Color(c, c, c);
41 setBackground(mc);
42 setSize(380, 320);
43 open_MI = new MenuItem("Open Image File..");
44 fMenu.add(open_MI);
45 open_MI.addActionListener(this);
46 quit_MI = new MenuItem("Quit");
47 fMenu.add(quit_MI);
48 quit_MI.addActionListener(this);
49 mb.add(fMenu);
50 setMenuBar(mb);
51
52 svButton = new Button("Save");
53 svButton.setSize(50, 20);
54 svButton.setLocation(300, 100);
55 add(svButton);
56 svButton.setEnabled(false);
57 svButton.addActionListener(this);
58
59 sdButton = new Button("Send");
60 sdButton.setSize(50, 20);
61 sdButton.setLocation(300, 140);
62 add(sdButton);
63 sdButton.setEnabled(false);
64 sdButton.addActionListener(this);
65 }
66
67 public void openFile() {
68 FileDialog fd = new FileDialog(this, "Select image file", FileDialog.LOAD);
69 fd.setVisible(true);
70 fname = fd.getFile();
71 if (fname == null) {
72 dir = null;
73 return;
74 }
75 dir = fd.getDirectory();
76 openImage(dir + fname);
77 }
78
79 public void openImage(String fn) {
80 img = Toolkit.getDefaultToolkit().getImage(dir + fname);
81 repaint();
82 waitForImage(this, img);
83 image2Short(img);
84
85 if (rf != null)
86 rf.dispose();
87 rf = new RecieveFrame("Recieve Frame", w, h);
88 n = 2;
89 ip.transforms.Lifting.forwardHaar(r);
90 ip.transforms.Lifting.forwardHaar(g);
91 ip.transforms.Lifting.forwardHaar(b);
92
93 svButton.setEnabled(true);
94 sdButton.setEnabled(true);
95 }
96
97 public void waitForImage(Component component, Image image) {
98 MediaTracker tracker = new MediaTracker(component);
99 try {
100 tracker.addImage(image, 0);
101 tracker.waitForID(0);
102 if (!tracker.checkID(0))
103 System.out.println("Load failure!");
104 } catch (InterruptedException e) {
105 }
106 }
107
108 public void image2Short(Image im) {
109 w = im.getWidth(this);
110 h = im.getHeight(this);
111 r = new short[w][h];
112 g = new short[w][h];
113 b = new short[w][h];
114
115 int pels[] = new int[w * h];
116 ColorModel cm = ColorModel.getRGBdefault();
117
118 PixelGrabber grabber = new PixelGrabber(im, 0, 0, w, h, pels, 0, w);
119
120 try {
121 grabber.grabPixels();
122 } catch (InterruptedException e) {
123 }
124 ;
125
126 int i = 0;
127 for (int x = 0; x < w; x++)
128 for (int y = 0; y < h; y++) {
129 i = x + y * w;
130 b[x][y] = (short) cm.getBlue(pels[i]);
131 g[x][y] = (short) cm.getGreen(pels[i]);
132 r[x][y] = (short) cm.getRed(pels[i]);
133 }
134 }
135
136 public Image short2Image(int wi, int he) {
137 Toolkit tk = Toolkit.getDefaultToolkit();
138 ColorModel cm = ColorModel.getRGBdefault();
139
140 int pels[] = new int[wi * he];
141 for (int x = 0; x < wi; x++)
142 for (int y = 0; y < he; y++) {
143 pels[x + y * wi] = 0xff000000 | (r[x][y] << 16)
144 | (g[x][y] << 8)
145 | b[x][y];
146 }
147 Image i = tk.createImage(new MemoryImageSource(wi, he, cm, pels, 0, wi));
148 return i;
149 }
150
151 public void paint(Graphics g) {
152 Rectangle rec = getBounds();
153 if (img == null) {
154 g.drawRect(20, rec.height - 276, 256, 256);
155 return;
156 }
157
158 g.drawImage(img, 20, rec.height - 276, 256, 256, this);
159
160 }
161
162 int n = 1;
163
164 public void send() {
165 rf.initArrays();
166 rf.sendData(r, g, b, n);
167 n *= 2;
168 if (n > h) {
169 sdButton.setEnabled(false);
170 sdButton.transferFocus();
171 }
172 }
173
174 public void saveFile() {
175 FileDialog fd = new FileDialog(this, "Save as lzc file", FileDialog.SAVE);
176 fd.setFile("images.lzc");
177 fd.setVisible(true);
178 String fname = fd.getFile();
179 if (fname != null) {
180 String dir = fd.getDirectory();
181 saveFile(dir + fname);
182 }
183 }
184
185 public void saveFile(String fn) {
186 try {
187 FileOutputStream fos = new FileOutputStream(fn);
188 ZipOutputStream zos = new ZipOutputStream(fos);
189 ObjectOutputStream oos;
190 int sn = 0;
191 int en = 1;
192 int num = 0;
193 while (en <= h) {
194 ZipEntry ze = new ZipEntry(makeName(num++));
195 zos.putNextEntry(ze);
196 oos = new ObjectOutputStream(zos);
197 saveObject(oos, sn, en);
198 sn = en;
199 en *= 2;
200 }
201
202 zos.finish();
203 zos.close();
204 System.out.println("Save done...");
205 } catch (IOException e) {
206 System.out.println(e);
207 }
208 }
209
210 public String makeName(int i) {
211 String name1 = "Datafile";
212 String name2 = null;
213 if (i < 10)
214 name2 = "00" + i + ".dat";
215 else if (i < 100)
216 name2 = "0" + i + ".dat";
217 else
218 name2 = "" + i + ".dat";
219
220 return name1 + name2;
221 }
222
223 public void saveObject(ObjectOutputStream oos, int sind, int eind) {
224 int size = eind - sind;
225 short rt[][] = new short[w][size];
226 short gt[][] = new short[w][size];
227 short bt[][] = new short[w][size];
228
229 for (int x = 0; x < w; x++)
230 for (int y = sind,si = 0; y < eind; y++, si++) {
231 rt[x][si] = r[x][y];
232 gt[x][si] = g[x][y];
233 bt[x][si] = b[x][y];
234 }
235 try {
236 oos.writeObject(rt);
237 oos.writeObject(gt);
238 oos.writeObject(bt);
239 } catch (IOException e) {
240 System.out.println(e);
241 }
242
243 }
244
245 public void actionPerformed(ActionEvent e) {
246 if (e.getSource() == open_MI) {
247 openFile();
248 return;
249 }
250
251 if (e.getSource() == quit_MI) {
252
253 dispose();
254 return;
255 }
256 if (e.getSource() == svButton) {
257 saveFile();
258 return;
259 }
260 if (e.getSource() == sdButton) {
261 send();
262 return;
263 }
264 }
265 }