📄 gifimagewriter.java
字号:
} if (imageType == null) { throw new IllegalArgumentException("imageType == null!"); } GIFWritableImageMetadata im = (GIFWritableImageMetadata)getDefaultImageMetadata(imageType, param); // Save interlace flag state. boolean isProgressive = im.interlaceFlag; convertMetadata(IMAGE_METADATA_NAME, inData, im); // Undo change to interlace flag if not MODE_COPY_FROM_METADATA. if (param != null && param.canWriteProgressive() && param.getProgressiveMode() != param.MODE_COPY_FROM_METADATA) { im.interlaceFlag = isProgressive; } return im; } public void endWriteSequence() throws IOException { if (stream == null) { throw new IllegalStateException("output == null!"); } if (!isWritingSequence) { throw new IllegalStateException("prepareWriteSequence() was not invoked!"); } writeTrailer(); resetLocal(); } public IIOMetadata getDefaultImageMetadata(ImageTypeSpecifier imageType, ImageWriteParam param) { GIFWritableImageMetadata imageMetadata = new GIFWritableImageMetadata(); // Image dimensions SampleModel sampleModel = imageType.getSampleModel(); Rectangle sourceBounds = new Rectangle(sampleModel.getWidth(), sampleModel.getHeight()); Dimension destSize = new Dimension(); computeRegions(sourceBounds, destSize, param); imageMetadata.imageWidth = destSize.width; imageMetadata.imageHeight = destSize.height; // Interlacing if (param != null && param.canWriteProgressive() && param.getProgressiveMode() == ImageWriteParam.MODE_DISABLED) { imageMetadata.interlaceFlag = false; } else { imageMetadata.interlaceFlag = true; } // Local color table ColorModel colorModel = imageType.getColorModel(); imageMetadata.localColorTable = createColorTable(colorModel, sampleModel); // Transparency if (colorModel instanceof IndexColorModel) { int transparentIndex = ((IndexColorModel)colorModel).getTransparentPixel(); if (transparentIndex != -1) { imageMetadata.transparentColorFlag = true; imageMetadata.transparentColorIndex = transparentIndex; } } return imageMetadata; } public IIOMetadata getDefaultStreamMetadata(ImageWriteParam param) { GIFWritableStreamMetadata streamMetadata = new GIFWritableStreamMetadata(); streamMetadata.version = "89a"; return streamMetadata; } public ImageWriteParam getDefaultWriteParam() { return new GIFImageWriteParam(getLocale()); } public void prepareWriteSequence(IIOMetadata streamMetadata) throws IOException { if (stream == null) { throw new IllegalStateException("Output is not set."); } resetLocal(); // Save the possibly converted stream metadata as an instance variable. if (streamMetadata == null) { this.theStreamMetadata = (GIFWritableStreamMetadata)getDefaultStreamMetadata(null); } else { this.theStreamMetadata = new GIFWritableStreamMetadata(); convertMetadata(STREAM_METADATA_NAME, streamMetadata, theStreamMetadata); } this.isWritingSequence = true; } public void reset() { super.reset(); resetLocal(); } /** * Resets locally defined instance variables. */ private void resetLocal() { this.isWritingSequence = false; this.wroteSequenceHeader = false; this.theStreamMetadata = null; this.imageIndex = 0; } public void setOutput(Object output) { super.setOutput(output); if (output != null) { if (!(output instanceof ImageOutputStream)) { throw new IllegalArgumentException("output is not an ImageOutputStream"); } this.stream = (ImageOutputStream)output; this.stream.setByteOrder(ByteOrder.LITTLE_ENDIAN); } else { this.stream = null; } } public void write(IIOMetadata sm, IIOImage iioimage, ImageWriteParam p) throws IOException { if (stream == null) { throw new IllegalStateException("output == null!"); } if (iioimage == null) { throw new IllegalArgumentException("iioimage == null!"); } if (iioimage.hasRaster()) { throw new UnsupportedOperationException("canWriteRasters() == false!"); } resetLocal(); GIFWritableStreamMetadata streamMetadata; if (sm == null) { streamMetadata = (GIFWritableStreamMetadata)getDefaultStreamMetadata(p); } else { streamMetadata = (GIFWritableStreamMetadata)convertStreamMetadata(sm, p); } write(true, true, streamMetadata, iioimage, p); } public void writeToSequence(IIOImage image, ImageWriteParam param) throws IOException { if (stream == null) { throw new IllegalStateException("output == null!"); } if (image == null) { throw new IllegalArgumentException("image == null!"); } if (image.hasRaster()) { throw new UnsupportedOperationException("canWriteRasters() == false!"); } if (!isWritingSequence) { throw new IllegalStateException("prepareWriteSequence() was not invoked!"); } write(!wroteSequenceHeader, false, theStreamMetadata, image, param); if (!wroteSequenceHeader) { wroteSequenceHeader = true; } this.imageIndex++; } private boolean needToCreateIndex(RenderedImage image) { SampleModel sampleModel = image.getSampleModel(); ColorModel colorModel = image.getColorModel(); return sampleModel.getNumBands() != 1 || sampleModel.getSampleSize()[0] > 8 || colorModel.getComponentSize()[0] > 8; } /** * Writes any extension blocks, the Image Descriptor, the image data, * and optionally the header (Signature and Logical Screen Descriptor) * and trailer (Block Terminator). * * @param writeHeader Whether to write the header. * @param writeTrailer Whether to write the trailer. * @param sm The stream metadata or <code>null</code> if * <code>writeHeader</code> is <code>false</code>. * @param iioimage The image and image metadata. * @param p The write parameters. * * @throws IllegalArgumentException if the number of bands is not 1. * @throws IllegalArgumentException if the number of bits per sample is * greater than 8. * @throws IllegalArgumentException if the color component size is * greater than 8. * @throws IllegalArgumentException if <code>writeHeader</code> is * <code>true</code> and <code>sm</code> is <code>null</code>. * @throws IllegalArgumentException if <code>writeHeader</code> is * <code>false</code> and a sequence is not being written. */ private void write(boolean writeHeader, boolean writeTrailer, IIOMetadata sm, IIOImage iioimage, ImageWriteParam p) throws IOException { clearAbortRequest(); RenderedImage image = iioimage.getRenderedImage(); // Check for ability to encode image. if (needToCreateIndex(image)) { image = PaletteBuilder.createIndexedImage(image); iioimage.setRenderedImage(image); } ColorModel colorModel = image.getColorModel(); SampleModel sampleModel = image.getSampleModel(); // Determine source region and destination dimensions. Rectangle sourceBounds = new Rectangle(image.getMinX(), image.getMinY(), image.getWidth(), image.getHeight()); Dimension destSize = new Dimension(); computeRegions(sourceBounds, destSize, p); // Convert any provided image metadata. GIFWritableImageMetadata imageMetadata = null; if (iioimage.getMetadata() != null) { imageMetadata = new GIFWritableImageMetadata(); convertMetadata(IMAGE_METADATA_NAME, iioimage.getMetadata(), imageMetadata); // Converted rgb image can use palette different from global. // In order to avoid color artefacts we want to be sure we use // appropriate palette. For this we initialize local color table // from current color and sample models. // At this point we can guarantee that local color table can be // build because image was already converted to indexed or // gray-scale representations if (imageMetadata.localColorTable == null) { imageMetadata.localColorTable = createColorTable(colorModel, sampleModel); // in case of indexed image we should take care of // transparent pixels if (colorModel instanceof IndexColorModel) { IndexColorModel icm = (IndexColorModel)colorModel; int index = icm.getTransparentPixel(); imageMetadata.transparentColorFlag = (index != -1); if (imageMetadata.transparentColorFlag) { imageMetadata.transparentColorIndex = index; } /* NB: transparentColorFlag might have not beed reset for greyscale images but explicitly reseting it here is potentially not right thing to do until we have way to find whether current value was explicitly set by the user. */ } } } // Global color table values. byte[] globalColorTable = null; // Write the header (Signature+Logical Screen Descriptor+ // Global Color Table). if (writeHeader) { if (sm == null) { throw new IllegalArgumentException("Cannot write null header!"); } GIFWritableStreamMetadata streamMetadata = (GIFWritableStreamMetadata)sm; // Set the version if not set. if (streamMetadata.version == null) { streamMetadata.version = "89a"; } // Set the Logical Screen Desriptor if not set. if (streamMetadata.logicalScreenWidth == GIFMetadata.UNDEFINED_INTEGER_VALUE) { streamMetadata.logicalScreenWidth = destSize.width; } if (streamMetadata.logicalScreenHeight == GIFMetadata.UNDEFINED_INTEGER_VALUE) { streamMetadata.logicalScreenHeight = destSize.height;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -