home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / JBuilder8.iso / Solaris / resource / jre / demo / jfc / Java2D / src / java2d / demos / Images / JPEGFlip.java < prev    next >
Encoding:
Java Source  |  2002-09-06  |  5.4 KB  |  151 lines

  1. /*
  2.  * Copyright (c) 2002 Sun Microsystems, Inc. All  Rights Reserved.
  3.  * 
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  * 
  8.  * -Redistributions of source code must retain the above copyright
  9.  *  notice, this list of conditions and the following disclaimer.
  10.  * 
  11.  * -Redistribution in binary form must reproduct the above copyright
  12.  *  notice, this list of conditions and the following disclaimer in
  13.  *  the documentation and/or other materials provided with the distribution.
  14.  * 
  15.  * Neither the name of Sun Microsystems, Inc. or the names of contributors
  16.  * may be used to endorse or promote products derived from this software
  17.  * without specific prior written permission.
  18.  * 
  19.  * This software is provided "AS IS," without a warranty of any kind. ALL
  20.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
  21.  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
  22.  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT
  23.  * BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT
  24.  * OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS
  25.  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
  26.  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
  27.  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
  28.  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN
  29.  * IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  30.  * 
  31.  * You acknowledge that Software is not designed, licensed or intended for
  32.  * use in the design, construction, operation or maintenance of any nuclear
  33.  * facility.
  34.  */
  35.  
  36. /*
  37.  * @(#)JPEGFlip.java    1.22 02/06/13
  38.  */
  39.  
  40. package java2d.demos.Images;
  41.  
  42. import java.awt.*;
  43. import com.sun.image.codec.jpeg.*;
  44. import java.awt.image.BufferedImage;
  45. import java.awt.image.DataBuffer;
  46. import java.awt.geom.GeneralPath;
  47. import java.io.*;
  48. import java2d.Surface;
  49.  
  50.  
  51. /**
  52.  * Render a filled star & duke into a BufferedImage, save the BufferedImage
  53.  * as a JPEG, display the BufferedImage, using the decoded JPEG BufferedImage
  54.  * DataBuffer flip the elements, display the JPEG flipped BufferedImage.
  55.  */
  56. public class JPEGFlip extends Surface {
  57.  
  58.     private static Image img;
  59.  
  60.     public JPEGFlip() {
  61.         setBackground(Color.white);
  62.         img = getImage("duke.gif");
  63.     }
  64.  
  65.  
  66.     public void render(int w, int h, Graphics2D g2) {
  67.  
  68.         int hh = h/2;
  69.  
  70.         BufferedImage bi = new BufferedImage(w, hh, BufferedImage.TYPE_INT_RGB);
  71.         Graphics2D big = bi.createGraphics();
  72.  
  73.         // .. use rendering hints from J2DCanvas ..
  74.         big.setRenderingHints(g2.getRenderingHints());
  75.  
  76.         big.setBackground(getBackground());
  77.         big.clearRect(0, 0, w, hh);
  78.  
  79.         big.setColor(Color.green.darker());
  80.         GeneralPath p = new GeneralPath(GeneralPath.WIND_NON_ZERO);
  81.         p.moveTo(- w / 2.0f, - hh / 8.0f);
  82.         p.lineTo(+ w / 2.0f, - hh / 8.0f);
  83.         p.lineTo(- w / 4.0f, + hh / 2.0f);
  84.         p.lineTo(+     0.0f, - hh / 2.0f);
  85.         p.lineTo(+ w / 4.0f, + hh / 2.0f);
  86.         p.closePath();
  87.         big.translate(w/2, hh/2);
  88.         big.fill(p);
  89.  
  90.         int iw = img.getWidth(this);
  91.         int ih = img.getHeight(this);
  92.         if (hh < ih * 1.5)
  93.             ih = (int) (ih * ((hh / (ih*1.5))));
  94.         big.drawImage(img, -img.getWidth(this)/2, -ih/2, iw, ih, this);
  95.  
  96.         g2.drawImage(bi, 0, 0, this);
  97.         g2.setFont(new Font("Dialog", Font.PLAIN, 10));
  98.         g2.setColor(Color.black);
  99.         g2.drawString("BufferedImage", 4, 12);
  100.  
  101.  
  102.         BufferedImage bi1 = null;
  103.  
  104.         try {
  105.             // To write the jpeg to a file uncomment the File* lines and
  106.             // comment out the ByteArray*Stream lines.
  107.             //File file = new File("images", "test.jpg");
  108.             //FileOutputStream out = new FileOutputStream(file);
  109.             ByteArrayOutputStream out = new ByteArrayOutputStream();
  110.             JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
  111.             JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bi);
  112.             param.setQuality(1.0f, false);
  113.             encoder.setJPEGEncodeParam(param);
  114.             encoder.encode(bi);
  115.  
  116.             //FileInputStream in = new FileInputStream(file);
  117.             ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
  118.             JPEGImageDecoder decoder = JPEGCodec.createJPEGDecoder(in);
  119.             bi1 = decoder.decodeAsBufferedImage();
  120.         } catch (Exception ex) {
  121.             g2.setColor(Color.red);
  122.             g2.drawString("write permissions on images/test.jpg?", 5, hh*2-5);
  123.             return;
  124.         }
  125.  
  126.         if (bi1 == null) {
  127.             g2.setColor(Color.red);
  128.             g2.drawString("decodeAsBufferedImage=null", 5, hh*2-5);
  129.             return;
  130.         }
  131.  
  132.         BufferedImage bi2 = new BufferedImage(bi1.getWidth(),bi1.getHeight(),bi1.getType());
  133.         DataBuffer db1 = bi1.getRaster().getDataBuffer();
  134.         DataBuffer db2 = bi2.getRaster().getDataBuffer();
  135.  
  136.         for (int i = db1.getSize()-1, j = 0; i >= 0; --i, j++) {
  137.             db2.setElem(j, db1.getElem(i));
  138.         }
  139.  
  140.         g2.drawImage(bi2, 0, hh, this);
  141.  
  142.         g2.drawString("JPEGImage Flipped", 4, hh*2-4);
  143.         g2.drawLine(0, hh, w, hh);
  144.     }
  145.  
  146.  
  147.     public static void main(String s[]) {
  148.         createDemoFrame(new JPEGFlip());
  149.     }
  150. }
  151.