/** ImageEncoder - abstract class for writing out an image * * Copyright (C) 1996 by Jef Poskanzer <jef@acme.com>. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * Visit the ACME Labs Java page for up-to-date versions of this and other * fine Java utilities: http://www.acme.com/java/ */ package Acme.JPM.Encoders; import java.util.*; import java.io.*; import java.awt.Image; import java.awt.image.*; /** Abstract class for writing out an image. * <P> * A framework for classes that encode and write out an image in * a particular file format. * <P> * This provides a simplified rendition of the ImageConsumer interface. * It always delivers the pixels as ints in the RGBdefault color model. * It always provides them in top-down left-right order. * If you want more flexibility you can always implement ImageConsumer * directly. * <P> * <A HREF="/resources/classes/Acme/JPM/Encoders/ImageEncoder.java">Fetch the software.</A><BR> * <A HREF="/resources/classes/Acme.tar.gz">Fetch the entire Acme package.</A> * <P> * @see GifEncoder * @see PpmEncoder * @see Acme.JPM.Decoders.ImageDecoder */ public abstract class ImageEncoder implements ImageConsumer { private final static ColorModel rgbModel = ColorModel.getRGBdefault(); /** * Description of the Field */ protected OutputStream out; private ImageProducer producer; private int width = -1; private int height = -1; private int hintflags = 0; private boolean started = false; private boolean encoding; private IOException iox; private Hashtable props = null; private boolean accumulate = false; private int[] accumulator; /** Constructor. * @param img The image to encode. * @param out The stream to write the bytes to. *@exception IOException Description of Exception */ public ImageEncoder(Image img, OutputStream out) throws IOException { this(img.getSource(), out); } /** Constructor. * @param producer The ImageProducer to encode. * @param out The stream to write the bytes to. *@exception IOException Description of Exception */ public ImageEncoder(ImageProducer producer, OutputStream out) throws IOException { this.producer = producer; this.out = out; } // Methods from ImageConsumer. /** * Sets the dimensions attribute of the ImageEncoder object * *@param width The new dimensions value *@param height The new dimensions value */ public void setDimensions(int width, int height) { this.width = width; this.height = height; } /** * Sets the properties attribute of the ImageEncoder object * *@param props The new properties value */ public void setProperties(Hashtable props) { this.props = props; } /** * Sets the colorModel attribute of the ImageEncoder object * *@param model The new colorModel value */ public void setColorModel(ColorModel model) { } /** * Sets the hints attribute of the ImageEncoder object * *@param hintflags The new hints value */ public void setHints(int hintflags) { this.hintflags = hintflags; } /** * Sets the pixels attribute of the ImageEncoder object * *@param x The new pixels value *@param y The new pixels value *@param w The new pixels value *@param h The new pixels value *@param model The new pixels value *@param pixels The new pixels value *@param off The new pixels value *@param scansize The new pixels value */ public void setPixels( int x, int y, int w, int h, ColorModel model, byte[] pixels, int off, int scansize) { int[] rgbPixels = new int[w]; for (int row = 0; row < h; ++row) { int rowOff = off + row * scansize; for (int col = 0; col < w; ++col) { rgbPixels[col] = model.getRGB(pixels[rowOff + col] & 0xff); } try { encodePixelsWrapper(x, y + row, w, 1, rgbPixels, 0, w); } catch (IOException e) { iox = e; stop(); return; } } } /** * Sets the pixels attribute of the ImageEncoder object * *@param x The new pixels value *@param y The new pixels value *@param w The new pixels value *@param h The new pixels value *@param model The new pixels value *@param pixels The new pixels value *@param off The new pixels value *@param scansize The new pixels value */ public void setPixels( int x, int y, int w, int h, ColorModel model, int[] pixels, int off, int scansize) { if (model == rgbModel) { try { encodePixelsWrapper(x, y, w, h, pixels, off, scansize); } catch (IOException e) { iox = e; stop(); return; } } else { int[] rgbPixels = new int[w]; for (int row = 0; row < h; ++row) { int rowOff = off + row * scansize; for (int col = 0; col < w; ++col) { rgbPixels[col] = model.getRGB(pixels[rowOff + col]); } try { encodePixelsWrapper(x, y + row, w, 1, rgbPixels, 0, w); } catch (IOException e) { iox = e; stop(); return; } } } } // Our own methods. /** Call this after initialization to get things going. * *@exception IOException Description of Exception */ public synchronized void encode() throws IOException { encoding = true; iox = null; producer.startProduction(this); while (encoding) { try { wait(); } catch (InterruptedException e) { } } if (iox != null) { throw iox; } } /** * Description of the Method * *@param status Description of Parameter */ public void imageComplete(int status) { //System.out.println("image complete 1"); producer.removeConsumer(this); if (status == ImageConsumer.IMAGEABORTED) { iox = new IOException("image aborted"); } else { try { //System.out.println("image complete 2"); encodeFinish(); //System.out.println("image complete 3"); encodeDone(); } catch (IOException e) { iox = e; } } //System.out.println("image complete 4"); stop(); //System.out.println("image complete!"); } // Methods that subclasses implement. /** Subclasses implement this to initialize an encoding. * *@param w Description of Parameter *@param h Description of Parameter *@exception IOException Description of Exception */ abstract void encodeStart(int w, int h) throws IOException; /** Subclasses implement this to actually write out some bits. They * are guaranteed to be delivered in top-down-left-right order. * One int per pixel, index is row * scansize + off + col, * RGBdefault (AARRGGBB) color model. * *@param x Description of Parameter *@param y Description of Parameter *@param w Description of Parameter *@param h Description of Parameter *@param rgbPixels Description of Parameter *@param off Description of Parameter *@param scansize Description of Parameter *@exception IOException Description of Exception */ abstract void encodePixels( int x, int y, int w, int h, int[] rgbPixels, int off, int scansize) throws IOException; /** Subclasses implement this to finish an encoding. * *@exception IOException Description of Exception */ abstract void encodeDone() throws IOException; /** * Description of the Method * *@param x Description of Parameter *@param y Description of Parameter *@param w Description of Parameter *@param h Description of Parameter *@param rgbPixels Description of Parameter *@param off Description of Parameter *@param scansize Description of Parameter *@exception IOException Description of Exception */ private void encodePixelsWrapper( int x, int y, int w, int h, int[] rgbPixels, int off, int scansize) throws IOException { if (!started) { started = true; encodeStart(width, height); if ((hintflags & TOPDOWNLEFTRIGHT) == 0) { accumulate = true; accumulator = new int[width * height]; } } if (accumulate) { for (int row = 0; row < h; ++row) { System.arraycopy( rgbPixels, row * scansize + off, accumulator, (y + row) * width + x, w); } } else { encodePixels(x, y, w, h, rgbPixels, off, scansize); } } /** * Description of the Method * *@exception IOException Description of Exception */ private void encodeFinish() throws IOException { if (accumulate) { encodePixels(0, 0, width, height, accumulator, 0, width); accumulator = null; accumulate = false; } } /** * Description of the Method */ private synchronized void stop() { encoding = false; notifyAll(); } }
# | Change | User | Description | Committed | |
---|---|---|---|---|---|
#2 | 2735 | andrei_loskutov | sources are now included to binary distribution at http://andrei.gmxhome.de/perforce/index.html | ||
#1 | 2222 | andrei_loskutov |
#13.10.2002 22:16 Revision 1.2.1 1) Documentation task is finished... Yes!!! 2) New feature: copy P4 depot path 3) Some small fixes to menu shortkeys belongs ctrl + c and ctrl + d 4) Clipboard fix because of JDK 1.2 - 1.3 bug 5) Smallest refactoring on some methods. 6) Html help updated |