KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)JPEGFlip.java 1.27 06/08/29
3  *
4  * Copyright (c) 2006 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  * @(#)JPEGFlip.java 1.27 06/08/29
39  */

40
41 package java2d.demos.Images;
42
43 import static java.awt.Color JavaDoc.*;
44 import java.awt.*;
45 import com.sun.image.codec.jpeg.*;
46 import java.awt.image.BufferedImage JavaDoc;
47 import java.awt.image.DataBuffer JavaDoc;
48 import java.awt.geom.GeneralPath JavaDoc;
49 import java.io.*;
50 import java2d.Surface;
51
52
53 /**
54  * Render a filled star & duke into a BufferedImage, save the BufferedImage
55  * as a JPEG, display the BufferedImage, using the decoded JPEG BufferedImage
56  * DataBuffer flip the elements, display the JPEG flipped BufferedImage.
57  */

58 public class JPEGFlip extends Surface {
59
60     private static Image JavaDoc img;
61
62     public JPEGFlip() {
63         setBackground(WHITE);
64         img = getImage("duke.gif");
65     }
66
67
68     public void render(int w, int h, Graphics2D g2) {
69
70         int hh = h/2;
71
72         BufferedImage JavaDoc bi = new BufferedImage JavaDoc(w, hh, BufferedImage.TYPE_INT_RGB);
73         Graphics2D big = bi.createGraphics();
74
75         // .. use rendering hints from J2DCanvas ..
76
big.setRenderingHints(g2.getRenderingHints());
77
78         big.setBackground(getBackground());
79         big.clearRect(0, 0, w, hh);
80
81         big.setColor(GREEN.darker());
82         GeneralPath JavaDoc p = new GeneralPath JavaDoc(GeneralPath.WIND_NON_ZERO);
83         p.moveTo(- w / 2.0f, - hh / 8.0f);
84         p.lineTo(+ w / 2.0f, - hh / 8.0f);
85         p.lineTo(- w / 4.0f, + hh / 2.0f);
86         p.lineTo(+ 0.0f, - hh / 2.0f);
87         p.lineTo(+ w / 4.0f, + hh / 2.0f);
88         p.closePath();
89         big.translate(w/2, hh/2);
90         big.fill(p);
91
92         int iw = img.getWidth(this);
93         int ih = img.getHeight(this);
94         if (hh < ih * 1.5)
95             ih = (int) (ih * ((hh / (ih*1.5))));
96         big.drawImage(img, -img.getWidth(this)/2, -ih/2, iw, ih, this);
97
98         g2.drawImage(bi, 0, 0, this);
99         g2.setFont(new Font("Dialog", Font.PLAIN, 10));
100         g2.setColor(BLACK);
101         g2.drawString("BufferedImage", 4, 12);
102
103
104         BufferedImage JavaDoc bi1 = null;
105
106         try {
107             // To write the jpeg to a file uncomment the File* lines and
108
// comment out the ByteArray*Stream lines.
109
//File file = new File("images", "test.jpg");
110
//FileOutputStream out = new FileOutputStream(file);
111
ByteArrayOutputStream out = new ByteArrayOutputStream();
112             JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
113             JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
114             param.setQuality(1.0f, false);
115             encoder.setJPEGEncodeParam(param);
116             encoder.encode(bi);
117
118             //FileInputStream in = new FileInputStream(file);
119
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
120             JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(in);
121             bi1 = decoder.decodeAsBufferedImage();
122         } catch (Exception JavaDoc ex) {
123             g2.setColor(RED);
124             g2.drawString("write permissions on images/test.jpg?", 5, hh*2-5);
125             return;
126         }
127
128         if (bi1 == null) {
129             g2.setColor(RED);
130             g2.drawString("decodeAsBufferedImage=null", 5, hh*2-5);
131             return;
132         }
133
134         BufferedImage JavaDoc bi2 = new BufferedImage JavaDoc(bi1.getWidth(),bi1.getHeight(),bi1.getType());
135         DataBuffer JavaDoc db1 = bi1.getRaster().getDataBuffer();
136         DataBuffer JavaDoc db2 = bi2.getRaster().getDataBuffer();
137
138         for (int i = db1.getSize()-1, j = 0; i >= 0; --i, j++) {
139             db2.setElem(j, db1.getElem(i));
140         }
141
142         g2.drawImage(bi2, 0, hh, this);
143
144         g2.drawString("JPEGImage Flipped", 4, hh*2-4);
145         g2.drawLine(0, hh, w, hh);
146     }
147
148
149     public static void main(String JavaDoc s[]) {
150         createDemoFrame(new JPEGFlip());
151     }
152 }
153
Popular Tags