Source Code Cross Referenced for ImageOutput.java in  » Science » Cougaar12_4 » org » cougaar » demo » mandelbrot » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Science » Cougaar12_4 » org.cougaar.demo.mandelbrot.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.cougaar.demo.mandelbrot.util;
002:
003:        import java.io.File;
004:        import java.io.IOException;
005:        import java.io.OutputStream;
006:        import java.awt.Color;
007:        import java.awt.Graphics;
008:        import java.awt.Image;
009:        import java.awt.image.BufferedImage;
010:        import java.awt.image.RenderedImage;
011:        import javax.imageio.ImageIO;
012:        import javax.swing.JComponent;
013:        import javax.swing.JFrame;
014:
015:        /**
016:         * Image output utilities.
017:         */
018:        public final class ImageOutput {
019:
020:            private ImageOutput() {
021:            }
022:
023:            /**
024:             * Write the image data as a JPEG.
025:             *
026:             * @see #writeImage(int,int,byte[],Palette,String,OutputStream) write the
027:             * data using the default palette and "jpg" format.
028:             */
029:            public static void writeJPG(int width, int height, byte[] data,
030:                    OutputStream out) {
031:                writeImage(width, height, data, null, "jpg", out);
032:            }
033:
034:            /**
035:             * Display the image data in a popup Swing UI.
036:             *
037:             * @see #createImage(int,int,byte[],Palette) creates the image
038:             * @see #displayImage(Image) creates the popup UI, calls "System.exit" when
039:             * the window is closed.
040:             */
041:            public static void displayImage(int width, int height, byte[] data) {
042:                try {
043:                    Image image = createImage(width, height, data, null);
044:                    displayImage(image);
045:                } catch (Exception e) {
046:                    if (e instanceof  RuntimeException) {
047:                        throw (RuntimeException) e;
048:                    }
049:                    throw new RuntimeException("Unable to display image", e);
050:                }
051:            }
052:
053:            /**
054:             * Write image data to an output stream.
055:             *
056:             * @param width the data width
057:             * @param height the data height
058:             * @param data an array with length at least (width * height)
059:             * @param palette optional color palette, defaults to the default paleete
060:             * @param format the image format, e.g. "png" or "jpg"
061:             * @param out the output stream to write the image to
062:             */
063:            public static void writeImage(int width, int height, byte[] data,
064:                    Palette palette, String format, OutputStream out) {
065:                try {
066:                    Image image = createImage(width, height, data, palette);
067:                    writeImage(image, format, out);
068:                } catch (Exception e) {
069:                    if (e instanceof  RuntimeException) {
070:                        throw (RuntimeException) e;
071:                    }
072:                    throw new RuntimeException("Unable to write image", e);
073:                }
074:            }
075:
076:            /**
077:             * Create an image from the given data.
078:             *
079:             * @return an Image
080:             * @see #writeImage(Image,String,OutputStream) write the image to a stream
081:             * @see #displayImage(Image) create a simple popup UI that displays the image
082:             */
083:            public static Image createImage(int width, int height, byte[] data,
084:                    Palette palette) {
085:                return createImage(width, height, width, height, data, palette);
086:            }
087:
088:            private static Image createImage(int image_width, int image_height,
089:                    int data_width, int data_height, byte[] data,
090:                    Palette palette) {
091:                BufferedImage bufferedImage = new BufferedImage(image_width,
092:                        image_height, BufferedImage.TYPE_INT_RGB);
093:                Graphics g = bufferedImage.createGraphics();
094:
095:                paint(g, image_width, image_height, data_width, data_height,
096:                        data, palette);
097:
098:                g.dispose();
099:                return bufferedImage;
100:            }
101:
102:            private static void paint(Graphics g, int image_width,
103:                    int image_height, int data_width, int data_height,
104:                    byte[] data, Palette palette) {
105:                int height = Math.min(data_height, image_height);
106:                int width = Math.min(data_width, image_width);
107:
108:                Palette p = palette;
109:                if (p == null) {
110:                    p = new Palette();
111:                }
112:
113:                for (int col = 0; col < height; col++) {
114:                    int col_offset = col * data_width;
115:                    for (int row = 0; row < width; row++) {
116:                        int n = col_offset + row;
117:                        byte b = data[n];
118:                        int i = (int) b & 0xFF;
119:                        Color ci = (Color) p.get(i);
120:                        g.setColor(ci);
121:                        g.fillRect(row, col, 1, 1);
122:                    }
123:                }
124:            }
125:
126:            /**
127:             * Write an image to a file.
128:             *
129:             * @param filename a name ending in either ".jpg" or ".png"
130:             */
131:            public static void writeImage(Image image, String filename)
132:                    throws IOException {
133:                int sep = filename.lastIndexOf('.');
134:                String format = filename.substring(sep + 1).toLowerCase();
135:                if (!"jpg".equals(format) && !"png".equals(format)) {
136:                    throw new IllegalArgumentException("Unknown image format: "
137:                            + filename);
138:                }
139:
140:                File file = new File(filename);
141:                ImageIO.write(((RenderedImage) image), format, file);
142:            }
143:
144:            /**
145:             * Write an image to an output stream.
146:             *
147:             * @param format a supported {@link ImageIO} format, such as "jpg" or "png".
148:             */
149:            public static void writeImage(Image image, String format,
150:                    OutputStream out) throws IOException {
151:                ImageIO.write(((RenderedImage) image), format, out);
152:            }
153:
154:            /**
155:             * Pop-up a simple frame that display the image and calls {@link System#exit}
156:             * when the "close" box is clicked.
157:             * <p>
158:             * This is primarily intended as example code and for debugging purposes.
159:             */
160:            public static void displayImage(final Image image) {
161:                Runnable runner = new Runnable() {
162:                    public void run() {
163:                        JComponent component = new JComponent() {
164:                            public void paint(Graphics g) {
165:                                g.drawImage(image, 0, 0, this );
166:                            }
167:                        };
168:                        JFrame frame = new JFrame();
169:                        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
170:                        frame.getContentPane().add(component);
171:                        frame.setSize(image.getWidth(null), image
172:                                .getHeight(null));
173:                        frame.setVisible(true);
174:                    }
175:                };
176:                (new Thread(runner)).start();
177:            }
178:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.