< prev index next >

src/java.desktop/share/classes/com/sun/imageio/plugins/gif/GIFImageWriter.java

Print this page




  53 import javax.imageio.stream.ImageOutputStream;
  54 import org.w3c.dom.Node;
  55 import org.w3c.dom.NodeList;
  56 import com.sun.imageio.plugins.common.LZWCompressor;
  57 import com.sun.imageio.plugins.common.PaletteBuilder;
  58 import sun.awt.image.ByteComponentRaster;
  59 
  60 public class GIFImageWriter extends ImageWriter {
  61     private static final boolean DEBUG = false; // XXX false for release!
  62 
  63     static final String STANDARD_METADATA_NAME =
  64     IIOMetadataFormatImpl.standardMetadataFormatName;
  65 
  66     static final String STREAM_METADATA_NAME =
  67     GIFWritableStreamMetadata.NATIVE_FORMAT_NAME;
  68 
  69     static final String IMAGE_METADATA_NAME =
  70     GIFWritableImageMetadata.NATIVE_FORMAT_NAME;
  71 
  72     /**
  73      * The <code>output</code> case to an <code>ImageOutputStream</code>.
  74      */
  75     private ImageOutputStream stream = null;
  76 
  77     /**
  78      * Whether a sequence is being written.
  79      */
  80     private boolean isWritingSequence = false;
  81 
  82     /**
  83      * Whether the header has been written.
  84      */
  85     private boolean wroteSequenceHeader = false;
  86 
  87     /**
  88      * The stream metadata of a sequence.
  89      */
  90     private GIFWritableStreamMetadata theStreamMetadata = null;
  91 
  92     /**
  93      * The index of the image being written.


 255         x = x | (x >> 4);
 256         x = x | (x >> 8);
 257         x = x | (x >> 16);
 258         return x + 1;
 259     }
 260 
 261 
 262 
 263     public GIFImageWriter(GIFImageWriterSpi originatingProvider) {
 264         super(originatingProvider);
 265         if (DEBUG) {
 266             System.err.println("GIF Writer is created");
 267         }
 268     }
 269 
 270     public boolean canWriteSequence() {
 271         return true;
 272     }
 273 
 274     /**
 275      * Merges <code>inData</code> into <code>outData</code>. The supplied
 276      * metadata format name is attempted first and failing that the standard
 277      * metadata format name is attempted.
 278      */
 279     private void convertMetadata(String metadataFormatName,
 280                                  IIOMetadata inData,
 281                                  IIOMetadata outData) {
 282         String formatName = null;
 283 
 284         String nativeFormatName = inData.getNativeMetadataFormatName();
 285         if (nativeFormatName != null &&
 286             nativeFormatName.equals(metadataFormatName)) {
 287             formatName = metadataFormatName;
 288         } else {
 289             String[] extraFormatNames = inData.getExtraMetadataFormatNames();
 290 
 291             if (extraFormatNames != null) {
 292                 for (int i = 0; i < extraFormatNames.length; i++) {
 293                     if (extraFormatNames[i].equals(metadataFormatName)) {
 294                         formatName = metadataFormatName;
 295                         break;


 537     }
 538 
 539 
 540     private boolean needToCreateIndex(RenderedImage image) {
 541 
 542         SampleModel sampleModel = image.getSampleModel();
 543         ColorModel colorModel = image.getColorModel();
 544 
 545         return sampleModel.getNumBands() != 1 ||
 546             sampleModel.getSampleSize()[0] > 8 ||
 547             colorModel.getComponentSize()[0] > 8;
 548     }
 549 
 550     /**
 551      * Writes any extension blocks, the Image Descriptor, the image data,
 552      * and optionally the header (Signature and Logical Screen Descriptor)
 553      * and trailer (Block Terminator).
 554      *
 555      * @param writeHeader Whether to write the header.
 556      * @param writeTrailer Whether to write the trailer.
 557      * @param sm The stream metadata or <code>null</code> if
 558      * <code>writeHeader</code> is <code>false</code>.
 559      * @param iioimage The image and image metadata.
 560      * @param p The write parameters.
 561      *
 562      * @throws IllegalArgumentException if the number of bands is not 1.
 563      * @throws IllegalArgumentException if the number of bits per sample is
 564      * greater than 8.
 565      * @throws IllegalArgumentException if the color component size is
 566      * greater than 8.
 567      * @throws IllegalArgumentException if <code>writeHeader</code> is
 568      * <code>true</code> and <code>sm</code> is <code>null</code>.
 569      * @throws IllegalArgumentException if <code>writeHeader</code> is
 570      * <code>false</code> and a sequence is not being written.
 571      */
 572     private void write(boolean writeHeader,
 573                        boolean writeTrailer,
 574                        IIOMetadata sm,
 575                        IIOImage iioimage,
 576                        ImageWriteParam p) throws IOException {
 577         clearAbortRequest();
 578 
 579         RenderedImage image = iioimage.getRenderedImage();
 580 
 581         // Check for ability to encode image.
 582         if (needToCreateIndex(image)) {
 583             image = PaletteBuilder.createIndexedImage(image);
 584             iioimage.setRenderedImage(image);
 585         }
 586 
 587         ColorModel colorModel = image.getColorModel();
 588         SampleModel sampleModel = image.getSampleModel();
 589 
 590         // Determine source region and destination dimensions.




  53 import javax.imageio.stream.ImageOutputStream;
  54 import org.w3c.dom.Node;
  55 import org.w3c.dom.NodeList;
  56 import com.sun.imageio.plugins.common.LZWCompressor;
  57 import com.sun.imageio.plugins.common.PaletteBuilder;
  58 import sun.awt.image.ByteComponentRaster;
  59 
  60 public class GIFImageWriter extends ImageWriter {
  61     private static final boolean DEBUG = false; // XXX false for release!
  62 
  63     static final String STANDARD_METADATA_NAME =
  64     IIOMetadataFormatImpl.standardMetadataFormatName;
  65 
  66     static final String STREAM_METADATA_NAME =
  67     GIFWritableStreamMetadata.NATIVE_FORMAT_NAME;
  68 
  69     static final String IMAGE_METADATA_NAME =
  70     GIFWritableImageMetadata.NATIVE_FORMAT_NAME;
  71 
  72     /**
  73      * The {@code output} case to an {@code ImageOutputStream}.
  74      */
  75     private ImageOutputStream stream = null;
  76 
  77     /**
  78      * Whether a sequence is being written.
  79      */
  80     private boolean isWritingSequence = false;
  81 
  82     /**
  83      * Whether the header has been written.
  84      */
  85     private boolean wroteSequenceHeader = false;
  86 
  87     /**
  88      * The stream metadata of a sequence.
  89      */
  90     private GIFWritableStreamMetadata theStreamMetadata = null;
  91 
  92     /**
  93      * The index of the image being written.


 255         x = x | (x >> 4);
 256         x = x | (x >> 8);
 257         x = x | (x >> 16);
 258         return x + 1;
 259     }
 260 
 261 
 262 
 263     public GIFImageWriter(GIFImageWriterSpi originatingProvider) {
 264         super(originatingProvider);
 265         if (DEBUG) {
 266             System.err.println("GIF Writer is created");
 267         }
 268     }
 269 
 270     public boolean canWriteSequence() {
 271         return true;
 272     }
 273 
 274     /**
 275      * Merges {@code inData} into {@code outData}. The supplied
 276      * metadata format name is attempted first and failing that the standard
 277      * metadata format name is attempted.
 278      */
 279     private void convertMetadata(String metadataFormatName,
 280                                  IIOMetadata inData,
 281                                  IIOMetadata outData) {
 282         String formatName = null;
 283 
 284         String nativeFormatName = inData.getNativeMetadataFormatName();
 285         if (nativeFormatName != null &&
 286             nativeFormatName.equals(metadataFormatName)) {
 287             formatName = metadataFormatName;
 288         } else {
 289             String[] extraFormatNames = inData.getExtraMetadataFormatNames();
 290 
 291             if (extraFormatNames != null) {
 292                 for (int i = 0; i < extraFormatNames.length; i++) {
 293                     if (extraFormatNames[i].equals(metadataFormatName)) {
 294                         formatName = metadataFormatName;
 295                         break;


 537     }
 538 
 539 
 540     private boolean needToCreateIndex(RenderedImage image) {
 541 
 542         SampleModel sampleModel = image.getSampleModel();
 543         ColorModel colorModel = image.getColorModel();
 544 
 545         return sampleModel.getNumBands() != 1 ||
 546             sampleModel.getSampleSize()[0] > 8 ||
 547             colorModel.getComponentSize()[0] > 8;
 548     }
 549 
 550     /**
 551      * Writes any extension blocks, the Image Descriptor, the image data,
 552      * and optionally the header (Signature and Logical Screen Descriptor)
 553      * and trailer (Block Terminator).
 554      *
 555      * @param writeHeader Whether to write the header.
 556      * @param writeTrailer Whether to write the trailer.
 557      * @param sm The stream metadata or {@code null} if
 558      * {@code writeHeader} is {@code false}.
 559      * @param iioimage The image and image metadata.
 560      * @param p The write parameters.
 561      *
 562      * @throws IllegalArgumentException if the number of bands is not 1.
 563      * @throws IllegalArgumentException if the number of bits per sample is
 564      * greater than 8.
 565      * @throws IllegalArgumentException if the color component size is
 566      * greater than 8.
 567      * @throws IllegalArgumentException if {@code writeHeader} is
 568      * {@code true} and {@code sm} is {@code null}.
 569      * @throws IllegalArgumentException if {@code writeHeader} is
 570      * {@code false} and a sequence is not being written.
 571      */
 572     private void write(boolean writeHeader,
 573                        boolean writeTrailer,
 574                        IIOMetadata sm,
 575                        IIOImage iioimage,
 576                        ImageWriteParam p) throws IOException {
 577         clearAbortRequest();
 578 
 579         RenderedImage image = iioimage.getRenderedImage();
 580 
 581         // Check for ability to encode image.
 582         if (needToCreateIndex(image)) {
 583             image = PaletteBuilder.createIndexedImage(image);
 584             iioimage.setRenderedImage(image);
 585         }
 586 
 587         ColorModel colorModel = image.getColorModel();
 588         SampleModel sampleModel = image.getSampleModel();
 589 
 590         // Determine source region and destination dimensions.


< prev index next >