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: }
|