KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java2d > demos > Images > ImageOps


1 /*
2  * @(#)ImageOps.java 1.35 07/05/30
3  *
4  * Copyright (c) 2007 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * -Redistribution of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * -Redistribution in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * Neither the name of Sun Microsystems, Inc. or the names of contributors may
17  * be used to endorse or promote products derived from this software without
18  * specific prior written permission.
19  *
20  * This software is provided "AS IS," without a warranty of any kind. ALL
21  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
22  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
24  * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
25  * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
26  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
27  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
28  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
29  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
30  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31  *
32  * You acknowledge that this software is not designed, licensed or intended
33  * for use in the design, construction, operation or maintenance of any
34  * nuclear facility.
35  */

36
37 /*
38  * @(#)ImageOps.java 1.35 07/05/30
39  */

40
41 package java2d.demos.Images;
42
43 import java.awt.*;
44 import java.awt.image.*;
45 import java.awt.event.*;
46 import java.awt.geom.AffineTransform JavaDoc;
47 import javax.swing.*;
48 import javax.swing.event.*;
49 import java2d.ControlsSurface;
50 import java2d.CustomControls;
51
52
53
54 /**
55  * Images drawn using operators such as ConvolveOp LowPass & Sharpen,
56  * LookupOp and RescaleOp.
57  */

58 public class ImageOps extends ControlsSurface implements ChangeListener {
59
60     protected JSlider slider1, slider2;
61     private static String JavaDoc imgName[] = { "bld.jpg", "boat.png" };
62     private static BufferedImage img[] = new BufferedImage[imgName.length];
63     private static String JavaDoc opsName[] = {
64               "Threshold", "RescaleOp" ,"Invert", "Yellow Invert", "3x3 Blur",
65               "3x3 Sharpen", "3x3 Edge", "5x5 Edge"};
66     private static BufferedImageOp biop[] = new BufferedImageOp[opsName.length];
67     private static int rescaleFactor = 128;
68     private static float rescaleOffset = 0;
69     private static int low = 100, high = 200;
70     private int opsIndex, imgIndex;
71  
72     static {
73         thresholdOp(low, high);
74         int i = 1;
75         biop[i++] = new RescaleOp(1.0f, 0, null);
76         byte invert[] = new byte[256];
77         byte ordered[] = new byte[256];
78         for (int j = 0; j < 256 ; j++) {
79             invert[j] = (byte) (256-j);
80             ordered[j] = (byte) j;
81         }
82         biop[i++] = new LookupOp(new ByteLookupTable(0,invert), null);
83         byte[][] yellowInvert = new byte[][] { invert, invert, ordered };
84         biop[i++] = new LookupOp(new ByteLookupTable(0,yellowInvert), null);
85         int dim[][] = {{3,3}, {3,3}, {3,3}, {5,5}};
86         float data[][] = { {0.1f, 0.1f, 0.1f, // 3x3 blur
87
0.1f, 0.2f, 0.1f,
88                             0.1f, 0.1f, 0.1f},
89                            {-1.0f, -1.0f, -1.0f, // 3x3 sharpen
90
-1.0f, 9.0f, -1.0f,
91                             -1.0f, -1.0f, -1.0f},
92                            { 0.f, -1.f, 0.f, // 3x3 edge
93
-1.f, 5.f, -1.f,
94                              0.f, -1.f, 0.f},
95                            {-1.0f, -1.0f, -1.0f, -1.0f, -1.0f, // 5x5 edge
96
-1.0f, -1.0f, -1.0f, -1.0f, -1.0f,
97                             -1.0f, -1.0f, 24.0f, -1.0f, -1.0f,
98                             -1.0f, -1.0f, -1.0f, -1.0f, -1.0f,
99                             -1.0f, -1.0f, -1.0f, -1.0f, -1.0f}};
100         for (int j = 0; j < data.length; j++, i++) {
101             biop[i] = new ConvolveOp(new Kernel(dim[j][0],dim[j][1],data[j]));
102         }
103     }
104
105
106     public ImageOps() {
107         setDoubleBuffered(true);
108         setBackground(Color.white);
109         for (int i = 0; i < imgName.length; i++) {
110             Image image = getImage(imgName[i]);
111             int iw = image.getWidth(this);
112             int ih = image.getHeight(this);
113             img[i] = new BufferedImage(iw, ih, BufferedImage.TYPE_INT_RGB);
114             img[i].createGraphics().drawImage(image,0,0,null);
115         }
116         slider1 = new JSlider(JSlider.VERTICAL, 0, 255, low);
117         slider1.setPreferredSize(new Dimension(15, 100));
118         slider1.addChangeListener(this);
119         slider2 = new JSlider(JSlider.VERTICAL, 0, 255, high);
120         slider2.setPreferredSize(new Dimension(15, 100));
121         slider2.addChangeListener(this);
122         setControls(new Component[]{new DemoControls(this),slider1,slider2});
123         setConstraints(new String JavaDoc[] {
124             BorderLayout.NORTH, BorderLayout.WEST, BorderLayout.EAST });
125     }
126
127
128     public static void thresholdOp(int low, int high) {
129         byte threshold[] = new byte[256];
130         for (int j = 0; j < 256 ; j++) {
131             if (j > high) {
132                 threshold[j] = (byte) 255;
133             } else if (j < low) {
134                 threshold[j] = (byte) 0;
135             } else {
136                 threshold[j] = (byte) j;
137             }
138         }
139         biop[0] = new LookupOp(new ByteLookupTable(0,threshold), null);
140     }
141
142
143     public void render(int w, int h, Graphics2D g2) {
144         int iw = img[imgIndex].getWidth(null);
145         int ih = img[imgIndex].getHeight(null);
146         AffineTransform JavaDoc oldXform = g2.getTransform();
147         g2.scale(((double)w) / iw, ((double)h) / ih);
148         g2.drawImage(img[imgIndex], biop[opsIndex], 0, 0);
149         g2.setTransform(oldXform);
150     }
151
152
153     public void stateChanged(ChangeEvent e) {
154         if (e.getSource().equals(slider1)) {
155             if (opsIndex == 0) {
156                 thresholdOp(slider1.getValue(), high);
157             } else {
158                 rescaleFactor = slider1.getValue();
159                 biop[1] = new RescaleOp((float)rescaleFactor/128.0f, rescaleOffset, null);
160             }
161         } else {
162             if (opsIndex == 0) {
163                 thresholdOp(low, slider2.getValue());
164             } else {
165                 rescaleOffset = (float) slider2.getValue();
166                 biop[1] = new RescaleOp((float)rescaleFactor/128.0f, rescaleOffset, null);
167             }
168
169         }
170         repaint();
171     }
172
173
174     public static void main(String JavaDoc s[]) {
175         createDemoFrame(new ImageOps());
176     }
177
178
179     static class DemoControls extends CustomControls implements ActionListener {
180
181         ImageOps demo;
182         JComboBox imgCombo, opsCombo;
183         Font font = new Font("serif", Font.PLAIN, 10);
184
185         public DemoControls(ImageOps demo) {
186             super(demo.name);
187             this.demo = demo;
188             add(imgCombo = new JComboBox());
189             imgCombo.setFont(font);
190             for (String JavaDoc name : ImageOps.imgName) {
191                 imgCombo.addItem(name);
192             }
193             imgCombo.addActionListener(this);
194             add(opsCombo = new JComboBox());
195             opsCombo.setFont(font);
196             for (String JavaDoc name : ImageOps.opsName) {
197                 opsCombo.addItem(name);
198             }
199             opsCombo.addActionListener(this);
200         }
201
202
203         public void actionPerformed(ActionEvent e) {
204             if (e.getSource().equals(opsCombo)) {
205                 demo.opsIndex = opsCombo.getSelectedIndex();
206                 if (demo.opsIndex == 0) {
207                     demo.slider1.setValue(demo.low);
208                     demo.slider2.setValue(demo.high);
209                     demo.slider1.setEnabled(true);
210                     demo.slider2.setEnabled(true);
211                 } else if (demo.opsIndex == 1) {
212                     demo.slider1.setValue(demo.rescaleFactor);
213                     demo.slider2.setValue((int) demo.rescaleOffset);
214                     demo.slider1.setEnabled(true);
215                     demo.slider2.setEnabled(true);
216                 } else {
217                     demo.slider1.setEnabled(false);
218                     demo.slider2.setEnabled(false);
219                 }
220             } else if (e.getSource().equals(imgCombo)) {
221                 demo.imgIndex = imgCombo.getSelectedIndex();
222             }
223             demo.repaint(10);
224         }
225
226
227         public Dimension getPreferredSize() {
228             return new Dimension(200,39);
229         }
230
231
232         public void run() {
233             try { thread.sleep(1111); } catch (Exception JavaDoc e) { return; }
234             Thread JavaDoc me = Thread.currentThread();
235             while (thread == me) {
236                 for (int i = 0; i < ImageOps.imgName.length; i++) {
237                     imgCombo.setSelectedIndex(i);
238                     for (int j = 0; j < ImageOps.opsName.length; j++) {
239                         opsCombo.setSelectedIndex(j);
240                         if (j <= 1) {
241                             for (int k = 50; k <= 200; k+=10) {
242                                 demo.slider1.setValue(k);
243                                 try {
244                                     thread.sleep(200);
245                                 } catch (InterruptedException JavaDoc e) { return; }
246                             }
247                         }
248                         try {
249                             thread.sleep(4444);
250                         } catch (InterruptedException JavaDoc e) { return; }
251                     }
252                 }
253             }
254             thread = null;
255         }
256     } // End DemoControls
257
} // End ImageOps
258
Popular Tags