Java Code Examples for javax.imageio.ImageWriteParam#MODE_DEFAULT

The following examples show how to use javax.imageio.ImageWriteParam#MODE_DEFAULT . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CompressionModeTest.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String args[]) {
    int[] iModes = { ImageWriteParam.MODE_DISABLED,
                     ImageWriteParam.MODE_EXPLICIT,
                     ImageWriteParam.MODE_COPY_FROM_METADATA,
                     ImageWriteParam.MODE_DEFAULT };

    String[] strModes = { "ImageWriteParam.MODE_DISABLED",
                          "ImageWriteParam.MODE_EXPLICIT",
                          "ImageWriteParam.MODE_COPY_FROM_METADATA",
                          "ImageWriteParam.MODE_DEFAULT" };

    for(int i=0; i<iModes.length; i++) {
        System.out.println("Test compression mode "+strModes[i]);
        doTest(iModes[i]);
    }
}
 
Example 2
Source File: RleEncodingTest.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String args[]) throws Exception {
    try {
        int mode = ImageWriteParam.MODE_EXPLICIT;
        String type = "BI_RLE4";
        doTest(type, mode);

        type = "BI_RLE8";
        doTest(type, mode);

        mode = ImageWriteParam.MODE_DEFAULT;
        type = "BI_RLE4";
        doTest(type, mode);

        type = "BI_RLE8";
        doTest(type, mode);

        System.out.println("Test 4bpp image.");
        encodeRLE4Test();

        System.out.println("Test 8bpp image.");
        encodeRLE8Test();
    } catch (IOException e) {
        e.printStackTrace();
        throw new RuntimeException("Unexpected exception. Test failed");
    }
}
 
Example 3
Source File: DisableCompressionTest.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
private static String getModeName(int mode) {
    switch(mode) {
        case ImageWriteParam.MODE_COPY_FROM_METADATA:
            return "MODE_COPY_FROM_METADATA";
        case ImageWriteParam.MODE_DEFAULT:
            return "MODE_DEFAULT";
        case ImageWriteParam.MODE_DISABLED:
            return "MODE_DISABLED";
        case ImageWriteParam.MODE_EXPLICIT:
            return "MODE_EXPLICIT";
        default:
            throw new IllegalArgumentException("Unknown mode: " + mode);
    }
}
 
Example 4
Source File: DisableCompressionTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static String getModeName(int mode) {
    switch(mode) {
        case ImageWriteParam.MODE_COPY_FROM_METADATA:
            return "MODE_COPY_FROM_METADATA";
        case ImageWriteParam.MODE_DEFAULT:
            return "MODE_DEFAULT";
        case ImageWriteParam.MODE_DISABLED:
            return "MODE_DISABLED";
        case ImageWriteParam.MODE_EXPLICIT:
            return "MODE_EXPLICIT";
        default:
            throw new IllegalArgumentException("Unknown mode: " + mode);
    }
}
 
Example 5
Source File: GIFImageWriter.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Writes any extension blocks, the Image Descriptor, and the image data
 *
 * @param iioimage The image and image metadata.
 * @param param The write parameters.
 * @param globalColorTable The Global Color Table.
 * @param sourceBounds The source region.
 * @param destSize The destination dimensions.
 */
private void writeImage(RenderedImage image,
                        GIFWritableImageMetadata imageMetadata,
                        ImageWriteParam param, byte[] globalColorTable,
                        Rectangle sourceBounds, Dimension destSize)
  throws IOException {
    ColorModel colorModel = image.getColorModel();
    SampleModel sampleModel = image.getSampleModel();

    boolean writeGraphicsControlExtension;
    if (imageMetadata == null) {
        // Create default metadata.
        imageMetadata = (GIFWritableImageMetadata)getDefaultImageMetadata(
            new ImageTypeSpecifier(image), param);

        // Set GraphicControlExtension flag only if there is
        // transparency.
        writeGraphicsControlExtension = imageMetadata.transparentColorFlag;
    } else {
        // Check for GraphicControlExtension element.
        NodeList list = null;
        try {
            IIOMetadataNode root = (IIOMetadataNode)
                imageMetadata.getAsTree(IMAGE_METADATA_NAME);
            list = root.getElementsByTagName("GraphicControlExtension");
        } catch(IllegalArgumentException iae) {
            // Should never happen.
        }

        // Set GraphicControlExtension flag if element present.
        writeGraphicsControlExtension =
            list != null && list.getLength() > 0;

        // If progressive mode is not MODE_COPY_FROM_METADATA, ensure
        // the interlacing is set per the ImageWriteParam mode setting.
        if (param != null && param.canWriteProgressive()) {
            if (param.getProgressiveMode() ==
                ImageWriteParam.MODE_DISABLED) {
                imageMetadata.interlaceFlag = false;
            } else if (param.getProgressiveMode() ==
                       ImageWriteParam.MODE_DEFAULT) {
                imageMetadata.interlaceFlag = true;
            }
        }
    }

    // Unset local color table if equal to global color table.
    if (Arrays.equals(globalColorTable, imageMetadata.localColorTable)) {
        imageMetadata.localColorTable = null;
    }

    // Override dimensions
    imageMetadata.imageWidth = destSize.width;
    imageMetadata.imageHeight = destSize.height;

    // Write Graphics Control Extension.
    if (writeGraphicsControlExtension) {
        writeGraphicControlExtension(imageMetadata);
    }

    // Write extension blocks.
    writePlainTextExtension(imageMetadata);
    writeApplicationExtension(imageMetadata);
    writeCommentExtension(imageMetadata);

    // Write Image Descriptor
    int bitsPerPixel =
        getNumBits(imageMetadata.localColorTable == null ?
                   (globalColorTable == null ?
                    sampleModel.getSampleSize(0) :
                    globalColorTable.length/3) :
                   imageMetadata.localColorTable.length/3);
    writeImageDescriptor(imageMetadata, bitsPerPixel);

    // Write image data
    writeRasterData(image, sourceBounds, destSize,
                    param, imageMetadata.interlaceFlag);
}
 
Example 6
Source File: GIFImageWriter.java    From jdk8u_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Writes any extension blocks, the Image Descriptor, and the image data
 *
 * @param iioimage The image and image metadata.
 * @param param The write parameters.
 * @param globalColorTable The Global Color Table.
 * @param sourceBounds The source region.
 * @param destSize The destination dimensions.
 */
private void writeImage(RenderedImage image,
                        GIFWritableImageMetadata imageMetadata,
                        ImageWriteParam param, byte[] globalColorTable,
                        Rectangle sourceBounds, Dimension destSize)
  throws IOException {
    ColorModel colorModel = image.getColorModel();
    SampleModel sampleModel = image.getSampleModel();

    boolean writeGraphicsControlExtension;
    if (imageMetadata == null) {
        // Create default metadata.
        imageMetadata = (GIFWritableImageMetadata)getDefaultImageMetadata(
            new ImageTypeSpecifier(image), param);

        // Set GraphicControlExtension flag only if there is
        // transparency.
        writeGraphicsControlExtension = imageMetadata.transparentColorFlag;
    } else {
        // Check for GraphicControlExtension element.
        NodeList list = null;
        try {
            IIOMetadataNode root = (IIOMetadataNode)
                imageMetadata.getAsTree(IMAGE_METADATA_NAME);
            list = root.getElementsByTagName("GraphicControlExtension");
        } catch(IllegalArgumentException iae) {
            // Should never happen.
        }

        // Set GraphicControlExtension flag if element present.
        writeGraphicsControlExtension =
            list != null && list.getLength() > 0;

        // If progressive mode is not MODE_COPY_FROM_METADATA, ensure
        // the interlacing is set per the ImageWriteParam mode setting.
        if (param != null && param.canWriteProgressive()) {
            if (param.getProgressiveMode() ==
                ImageWriteParam.MODE_DISABLED) {
                imageMetadata.interlaceFlag = false;
            } else if (param.getProgressiveMode() ==
                       ImageWriteParam.MODE_DEFAULT) {
                imageMetadata.interlaceFlag = true;
            }
        }
    }

    // Unset local color table if equal to global color table.
    if (Arrays.equals(globalColorTable, imageMetadata.localColorTable)) {
        imageMetadata.localColorTable = null;
    }

    // Override dimensions
    imageMetadata.imageWidth = destSize.width;
    imageMetadata.imageHeight = destSize.height;

    // Write Graphics Control Extension.
    if (writeGraphicsControlExtension) {
        writeGraphicControlExtension(imageMetadata);
    }

    // Write extension blocks.
    writePlainTextExtension(imageMetadata);
    writeApplicationExtension(imageMetadata);
    writeCommentExtension(imageMetadata);

    // Write Image Descriptor
    int bitsPerPixel =
        getNumBits(imageMetadata.localColorTable == null ?
                   (globalColorTable == null ?
                    sampleModel.getSampleSize(0) :
                    globalColorTable.length/3) :
                   imageMetadata.localColorTable.length/3);
    writeImageDescriptor(imageMetadata, bitsPerPixel);

    // Write image data
    writeRasterData(image, sourceBounds, destSize,
                    param, imageMetadata.interlaceFlag);
}
 
Example 7
Source File: GIFImageWriter.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Writes any extension blocks, the Image Descriptor, and the image data
 *
 * @param iioimage The image and image metadata.
 * @param param The write parameters.
 * @param globalColorTable The Global Color Table.
 * @param sourceBounds The source region.
 * @param destSize The destination dimensions.
 */
private void writeImage(RenderedImage image,
                        GIFWritableImageMetadata imageMetadata,
                        ImageWriteParam param, byte[] globalColorTable,
                        Rectangle sourceBounds, Dimension destSize)
  throws IOException {
    ColorModel colorModel = image.getColorModel();
    SampleModel sampleModel = image.getSampleModel();

    boolean writeGraphicsControlExtension;
    if (imageMetadata == null) {
        // Create default metadata.
        imageMetadata = (GIFWritableImageMetadata)getDefaultImageMetadata(
            new ImageTypeSpecifier(image), param);

        // Set GraphicControlExtension flag only if there is
        // transparency.
        writeGraphicsControlExtension = imageMetadata.transparentColorFlag;
    } else {
        // Check for GraphicControlExtension element.
        NodeList list = null;
        try {
            IIOMetadataNode root = (IIOMetadataNode)
                imageMetadata.getAsTree(IMAGE_METADATA_NAME);
            list = root.getElementsByTagName("GraphicControlExtension");
        } catch(IllegalArgumentException iae) {
            // Should never happen.
        }

        // Set GraphicControlExtension flag if element present.
        writeGraphicsControlExtension =
            list != null && list.getLength() > 0;

        // If progressive mode is not MODE_COPY_FROM_METADATA, ensure
        // the interlacing is set per the ImageWriteParam mode setting.
        if (param != null && param.canWriteProgressive()) {
            if (param.getProgressiveMode() ==
                ImageWriteParam.MODE_DISABLED) {
                imageMetadata.interlaceFlag = false;
            } else if (param.getProgressiveMode() ==
                       ImageWriteParam.MODE_DEFAULT) {
                imageMetadata.interlaceFlag = true;
            }
        }
    }

    // Unset local color table if equal to global color table.
    if (Arrays.equals(globalColorTable, imageMetadata.localColorTable)) {
        imageMetadata.localColorTable = null;
    }

    // Override dimensions
    imageMetadata.imageWidth = destSize.width;
    imageMetadata.imageHeight = destSize.height;

    // Write Graphics Control Extension.
    if (writeGraphicsControlExtension) {
        writeGraphicControlExtension(imageMetadata);
    }

    // Write extension blocks.
    writePlainTextExtension(imageMetadata);
    writeApplicationExtension(imageMetadata);
    writeCommentExtension(imageMetadata);

    // Write Image Descriptor
    int bitsPerPixel =
        getNumBits(imageMetadata.localColorTable == null ?
                   (globalColorTable == null ?
                    sampleModel.getSampleSize(0) :
                    globalColorTable.length/3) :
                   imageMetadata.localColorTable.length/3);
    writeImageDescriptor(imageMetadata, bitsPerPixel);

    // Write image data
    writeRasterData(image, sourceBounds, destSize,
                    param, imageMetadata.interlaceFlag);
}
 
Example 8
Source File: GIFImageWriter.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Writes any extension blocks, the Image Descriptor, and the image data
 *
 * @param iioimage The image and image metadata.
 * @param param The write parameters.
 * @param globalColorTable The Global Color Table.
 * @param sourceBounds The source region.
 * @param destSize The destination dimensions.
 */
private void writeImage(RenderedImage image,
                        GIFWritableImageMetadata imageMetadata,
                        ImageWriteParam param, byte[] globalColorTable,
                        Rectangle sourceBounds, Dimension destSize)
  throws IOException {
    ColorModel colorModel = image.getColorModel();
    SampleModel sampleModel = image.getSampleModel();

    boolean writeGraphicsControlExtension;
    if (imageMetadata == null) {
        // Create default metadata.
        imageMetadata = (GIFWritableImageMetadata)getDefaultImageMetadata(
            new ImageTypeSpecifier(image), param);

        // Set GraphicControlExtension flag only if there is
        // transparency.
        writeGraphicsControlExtension = imageMetadata.transparentColorFlag;
    } else {
        // Check for GraphicControlExtension element.
        NodeList list = null;
        try {
            IIOMetadataNode root = (IIOMetadataNode)
                imageMetadata.getAsTree(IMAGE_METADATA_NAME);
            list = root.getElementsByTagName("GraphicControlExtension");
        } catch(IllegalArgumentException iae) {
            // Should never happen.
        }

        // Set GraphicControlExtension flag if element present.
        writeGraphicsControlExtension =
            list != null && list.getLength() > 0;

        // If progressive mode is not MODE_COPY_FROM_METADATA, ensure
        // the interlacing is set per the ImageWriteParam mode setting.
        if (param != null && param.canWriteProgressive()) {
            if (param.getProgressiveMode() ==
                ImageWriteParam.MODE_DISABLED) {
                imageMetadata.interlaceFlag = false;
            } else if (param.getProgressiveMode() ==
                       ImageWriteParam.MODE_DEFAULT) {
                imageMetadata.interlaceFlag = true;
            }
        }
    }

    // Unset local color table if equal to global color table.
    if (Arrays.equals(globalColorTable, imageMetadata.localColorTable)) {
        imageMetadata.localColorTable = null;
    }

    // Override dimensions
    imageMetadata.imageWidth = destSize.width;
    imageMetadata.imageHeight = destSize.height;

    // Write Graphics Control Extension.
    if (writeGraphicsControlExtension) {
        writeGraphicControlExtension(imageMetadata);
    }

    // Write extension blocks.
    writePlainTextExtension(imageMetadata);
    writeApplicationExtension(imageMetadata);
    writeCommentExtension(imageMetadata);

    // Write Image Descriptor
    int bitsPerPixel =
        getNumBits(imageMetadata.localColorTable == null ?
                   (globalColorTable == null ?
                    sampleModel.getSampleSize(0) :
                    globalColorTable.length/3) :
                   imageMetadata.localColorTable.length/3);
    writeImageDescriptor(imageMetadata, bitsPerPixel);

    // Write image data
    writeRasterData(image, sourceBounds, destSize,
                    param, imageMetadata.interlaceFlag);
}
 
Example 9
Source File: GIFImageWriter.java    From hottub with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Writes any extension blocks, the Image Descriptor, and the image data
 *
 * @param iioimage The image and image metadata.
 * @param param The write parameters.
 * @param globalColorTable The Global Color Table.
 * @param sourceBounds The source region.
 * @param destSize The destination dimensions.
 */
private void writeImage(RenderedImage image,
                        GIFWritableImageMetadata imageMetadata,
                        ImageWriteParam param, byte[] globalColorTable,
                        Rectangle sourceBounds, Dimension destSize)
  throws IOException {
    ColorModel colorModel = image.getColorModel();
    SampleModel sampleModel = image.getSampleModel();

    boolean writeGraphicsControlExtension;
    if (imageMetadata == null) {
        // Create default metadata.
        imageMetadata = (GIFWritableImageMetadata)getDefaultImageMetadata(
            new ImageTypeSpecifier(image), param);

        // Set GraphicControlExtension flag only if there is
        // transparency.
        writeGraphicsControlExtension = imageMetadata.transparentColorFlag;
    } else {
        // Check for GraphicControlExtension element.
        NodeList list = null;
        try {
            IIOMetadataNode root = (IIOMetadataNode)
                imageMetadata.getAsTree(IMAGE_METADATA_NAME);
            list = root.getElementsByTagName("GraphicControlExtension");
        } catch(IllegalArgumentException iae) {
            // Should never happen.
        }

        // Set GraphicControlExtension flag if element present.
        writeGraphicsControlExtension =
            list != null && list.getLength() > 0;

        // If progressive mode is not MODE_COPY_FROM_METADATA, ensure
        // the interlacing is set per the ImageWriteParam mode setting.
        if (param != null && param.canWriteProgressive()) {
            if (param.getProgressiveMode() ==
                ImageWriteParam.MODE_DISABLED) {
                imageMetadata.interlaceFlag = false;
            } else if (param.getProgressiveMode() ==
                       ImageWriteParam.MODE_DEFAULT) {
                imageMetadata.interlaceFlag = true;
            }
        }
    }

    // Unset local color table if equal to global color table.
    if (Arrays.equals(globalColorTable, imageMetadata.localColorTable)) {
        imageMetadata.localColorTable = null;
    }

    // Override dimensions
    imageMetadata.imageWidth = destSize.width;
    imageMetadata.imageHeight = destSize.height;

    // Write Graphics Control Extension.
    if (writeGraphicsControlExtension) {
        writeGraphicControlExtension(imageMetadata);
    }

    // Write extension blocks.
    writePlainTextExtension(imageMetadata);
    writeApplicationExtension(imageMetadata);
    writeCommentExtension(imageMetadata);

    // Write Image Descriptor
    int bitsPerPixel =
        getNumBits(imageMetadata.localColorTable == null ?
                   (globalColorTable == null ?
                    sampleModel.getSampleSize(0) :
                    globalColorTable.length/3) :
                   imageMetadata.localColorTable.length/3);
    writeImageDescriptor(imageMetadata, bitsPerPixel);

    // Write image data
    writeRasterData(image, sourceBounds, destSize,
                    param, imageMetadata.interlaceFlag);
}
 
Example 10
Source File: GIFImageWriter.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Writes any extension blocks, the Image Descriptor, and the image data
 *
 * @param iioimage The image and image metadata.
 * @param param The write parameters.
 * @param globalColorTable The Global Color Table.
 * @param sourceBounds The source region.
 * @param destSize The destination dimensions.
 */
private void writeImage(RenderedImage image,
                        GIFWritableImageMetadata imageMetadata,
                        ImageWriteParam param, byte[] globalColorTable,
                        Rectangle sourceBounds, Dimension destSize)
  throws IOException {
    ColorModel colorModel = image.getColorModel();
    SampleModel sampleModel = image.getSampleModel();

    boolean writeGraphicsControlExtension;
    if (imageMetadata == null) {
        // Create default metadata.
        imageMetadata = (GIFWritableImageMetadata)getDefaultImageMetadata(
            new ImageTypeSpecifier(image), param);

        // Set GraphicControlExtension flag only if there is
        // transparency.
        writeGraphicsControlExtension = imageMetadata.transparentColorFlag;
    } else {
        // Check for GraphicControlExtension element.
        NodeList list = null;
        try {
            IIOMetadataNode root = (IIOMetadataNode)
                imageMetadata.getAsTree(IMAGE_METADATA_NAME);
            list = root.getElementsByTagName("GraphicControlExtension");
        } catch(IllegalArgumentException iae) {
            // Should never happen.
        }

        // Set GraphicControlExtension flag if element present.
        writeGraphicsControlExtension =
            list != null && list.getLength() > 0;

        // If progressive mode is not MODE_COPY_FROM_METADATA, ensure
        // the interlacing is set per the ImageWriteParam mode setting.
        if (param != null && param.canWriteProgressive()) {
            if (param.getProgressiveMode() ==
                ImageWriteParam.MODE_DISABLED) {
                imageMetadata.interlaceFlag = false;
            } else if (param.getProgressiveMode() ==
                       ImageWriteParam.MODE_DEFAULT) {
                imageMetadata.interlaceFlag = true;
            }
        }
    }

    // Unset local color table if equal to global color table.
    if (Arrays.equals(globalColorTable, imageMetadata.localColorTable)) {
        imageMetadata.localColorTable = null;
    }

    // Override dimensions
    imageMetadata.imageWidth = destSize.width;
    imageMetadata.imageHeight = destSize.height;

    // Write Graphics Control Extension.
    if (writeGraphicsControlExtension) {
        writeGraphicControlExtension(imageMetadata);
    }

    // Write extension blocks.
    writePlainTextExtension(imageMetadata);
    writeApplicationExtension(imageMetadata);
    writeCommentExtension(imageMetadata);

    // Write Image Descriptor
    int bitsPerPixel =
        getNumBits(imageMetadata.localColorTable == null ?
                   (globalColorTable == null ?
                    sampleModel.getSampleSize(0) :
                    globalColorTable.length/3) :
                   imageMetadata.localColorTable.length/3);
    writeImageDescriptor(imageMetadata, bitsPerPixel);

    // Write image data
    writeRasterData(image, sourceBounds, destSize,
                    param, imageMetadata.interlaceFlag);
}
 
Example 11
Source File: GIFImageWriter.java    From jdk8u-dev-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Writes any extension blocks, the Image Descriptor, and the image data
 *
 * @param iioimage The image and image metadata.
 * @param param The write parameters.
 * @param globalColorTable The Global Color Table.
 * @param sourceBounds The source region.
 * @param destSize The destination dimensions.
 */
private void writeImage(RenderedImage image,
                        GIFWritableImageMetadata imageMetadata,
                        ImageWriteParam param, byte[] globalColorTable,
                        Rectangle sourceBounds, Dimension destSize)
  throws IOException {
    ColorModel colorModel = image.getColorModel();
    SampleModel sampleModel = image.getSampleModel();

    boolean writeGraphicsControlExtension;
    if (imageMetadata == null) {
        // Create default metadata.
        imageMetadata = (GIFWritableImageMetadata)getDefaultImageMetadata(
            new ImageTypeSpecifier(image), param);

        // Set GraphicControlExtension flag only if there is
        // transparency.
        writeGraphicsControlExtension = imageMetadata.transparentColorFlag;
    } else {
        // Check for GraphicControlExtension element.
        NodeList list = null;
        try {
            IIOMetadataNode root = (IIOMetadataNode)
                imageMetadata.getAsTree(IMAGE_METADATA_NAME);
            list = root.getElementsByTagName("GraphicControlExtension");
        } catch(IllegalArgumentException iae) {
            // Should never happen.
        }

        // Set GraphicControlExtension flag if element present.
        writeGraphicsControlExtension =
            list != null && list.getLength() > 0;

        // If progressive mode is not MODE_COPY_FROM_METADATA, ensure
        // the interlacing is set per the ImageWriteParam mode setting.
        if (param != null && param.canWriteProgressive()) {
            if (param.getProgressiveMode() ==
                ImageWriteParam.MODE_DISABLED) {
                imageMetadata.interlaceFlag = false;
            } else if (param.getProgressiveMode() ==
                       ImageWriteParam.MODE_DEFAULT) {
                imageMetadata.interlaceFlag = true;
            }
        }
    }

    // Unset local color table if equal to global color table.
    if (Arrays.equals(globalColorTable, imageMetadata.localColorTable)) {
        imageMetadata.localColorTable = null;
    }

    // Override dimensions
    imageMetadata.imageWidth = destSize.width;
    imageMetadata.imageHeight = destSize.height;

    // Write Graphics Control Extension.
    if (writeGraphicsControlExtension) {
        writeGraphicControlExtension(imageMetadata);
    }

    // Write extension blocks.
    writePlainTextExtension(imageMetadata);
    writeApplicationExtension(imageMetadata);
    writeCommentExtension(imageMetadata);

    // Write Image Descriptor
    int bitsPerPixel =
        getNumBits(imageMetadata.localColorTable == null ?
                   (globalColorTable == null ?
                    sampleModel.getSampleSize(0) :
                    globalColorTable.length/3) :
                   imageMetadata.localColorTable.length/3);
    writeImageDescriptor(imageMetadata, bitsPerPixel);

    // Write image data
    writeRasterData(image, sourceBounds, destSize,
                    param, imageMetadata.interlaceFlag);
}
 
Example 12
Source File: GIFImageWriter.java    From jdk1.8-source-analysis with Apache License 2.0 4 votes vote down vote up
/**
 * Writes any extension blocks, the Image Descriptor, and the image data
 *
 * @param iioimage The image and image metadata.
 * @param param The write parameters.
 * @param globalColorTable The Global Color Table.
 * @param sourceBounds The source region.
 * @param destSize The destination dimensions.
 */
private void writeImage(RenderedImage image,
                        GIFWritableImageMetadata imageMetadata,
                        ImageWriteParam param, byte[] globalColorTable,
                        Rectangle sourceBounds, Dimension destSize)
  throws IOException {
    ColorModel colorModel = image.getColorModel();
    SampleModel sampleModel = image.getSampleModel();

    boolean writeGraphicsControlExtension;
    if (imageMetadata == null) {
        // Create default metadata.
        imageMetadata = (GIFWritableImageMetadata)getDefaultImageMetadata(
            new ImageTypeSpecifier(image), param);

        // Set GraphicControlExtension flag only if there is
        // transparency.
        writeGraphicsControlExtension = imageMetadata.transparentColorFlag;
    } else {
        // Check for GraphicControlExtension element.
        NodeList list = null;
        try {
            IIOMetadataNode root = (IIOMetadataNode)
                imageMetadata.getAsTree(IMAGE_METADATA_NAME);
            list = root.getElementsByTagName("GraphicControlExtension");
        } catch(IllegalArgumentException iae) {
            // Should never happen.
        }

        // Set GraphicControlExtension flag if element present.
        writeGraphicsControlExtension =
            list != null && list.getLength() > 0;

        // If progressive mode is not MODE_COPY_FROM_METADATA, ensure
        // the interlacing is set per the ImageWriteParam mode setting.
        if (param != null && param.canWriteProgressive()) {
            if (param.getProgressiveMode() ==
                ImageWriteParam.MODE_DISABLED) {
                imageMetadata.interlaceFlag = false;
            } else if (param.getProgressiveMode() ==
                       ImageWriteParam.MODE_DEFAULT) {
                imageMetadata.interlaceFlag = true;
            }
        }
    }

    // Unset local color table if equal to global color table.
    if (Arrays.equals(globalColorTable, imageMetadata.localColorTable)) {
        imageMetadata.localColorTable = null;
    }

    // Override dimensions
    imageMetadata.imageWidth = destSize.width;
    imageMetadata.imageHeight = destSize.height;

    // Write Graphics Control Extension.
    if (writeGraphicsControlExtension) {
        writeGraphicControlExtension(imageMetadata);
    }

    // Write extension blocks.
    writePlainTextExtension(imageMetadata);
    writeApplicationExtension(imageMetadata);
    writeCommentExtension(imageMetadata);

    // Write Image Descriptor
    int bitsPerPixel =
        getNumBits(imageMetadata.localColorTable == null ?
                   (globalColorTable == null ?
                    sampleModel.getSampleSize(0) :
                    globalColorTable.length/3) :
                   imageMetadata.localColorTable.length/3);
    writeImageDescriptor(imageMetadata, bitsPerPixel);

    // Write image data
    writeRasterData(image, sourceBounds, destSize,
                    param, imageMetadata.interlaceFlag);
}
 
Example 13
Source File: GIFImageWriter.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Writes any extension blocks, the Image Descriptor, and the image data
 *
 * @param image The image.
 * @param imageMetadata The image metadata.
 * @param param The write parameters.
 * @param globalColorTable The Global Color Table.
 * @param sourceBounds The source region.
 * @param destSize The destination dimensions.
 */
private void writeImage(RenderedImage image,
                        GIFWritableImageMetadata imageMetadata,
                        ImageWriteParam param, byte[] globalColorTable,
                        Rectangle sourceBounds, Dimension destSize)
  throws IOException {
    ColorModel colorModel = image.getColorModel();
    SampleModel sampleModel = image.getSampleModel();

    boolean writeGraphicsControlExtension;
    if (imageMetadata == null) {
        // Create default metadata.
        imageMetadata = (GIFWritableImageMetadata)getDefaultImageMetadata(
            new ImageTypeSpecifier(image), param);

        // Set GraphicControlExtension flag only if there is
        // transparency.
        writeGraphicsControlExtension = imageMetadata.transparentColorFlag;
    } else {
        // Check for GraphicControlExtension element.
        NodeList list = null;
        try {
            IIOMetadataNode root = (IIOMetadataNode)
                imageMetadata.getAsTree(IMAGE_METADATA_NAME);
            list = root.getElementsByTagName("GraphicControlExtension");
        } catch(IllegalArgumentException iae) {
            // Should never happen.
        }

        // Set GraphicControlExtension flag if element present.
        writeGraphicsControlExtension =
            list != null && list.getLength() > 0;

        // If progressive mode is not MODE_COPY_FROM_METADATA, ensure
        // the interlacing is set per the ImageWriteParam mode setting.
        if (param != null && param.canWriteProgressive()) {
            if (param.getProgressiveMode() ==
                ImageWriteParam.MODE_DISABLED) {
                imageMetadata.interlaceFlag = false;
            } else if (param.getProgressiveMode() ==
                       ImageWriteParam.MODE_DEFAULT) {
                imageMetadata.interlaceFlag = true;
            }
        }
    }

    // Unset local color table if equal to global color table.
    if (Arrays.equals(globalColorTable, imageMetadata.localColorTable)) {
        imageMetadata.localColorTable = null;
    }

    // Override dimensions
    imageMetadata.imageWidth = destSize.width;
    imageMetadata.imageHeight = destSize.height;

    // Write Graphics Control Extension.
    if (writeGraphicsControlExtension) {
        writeGraphicControlExtension(imageMetadata);
    }

    // Write extension blocks.
    writePlainTextExtension(imageMetadata);
    writeApplicationExtension(imageMetadata);
    writeCommentExtension(imageMetadata);

    // Write Image Descriptor
    int bitsPerPixel =
        getNumBits(imageMetadata.localColorTable == null ?
                   (globalColorTable == null ?
                    sampleModel.getSampleSize(0) :
                    globalColorTable.length/3) :
                   imageMetadata.localColorTable.length/3);
    writeImageDescriptor(imageMetadata, bitsPerPixel);

    // Write image data
    writeRasterData(image, sourceBounds, destSize,
                    param, imageMetadata.interlaceFlag);
}
 
Example 14
Source File: GIFImageWriter.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
/**
 * Writes any extension blocks, the Image Descriptor, and the image data
 *
 * @param image The image.
 * @param imageMetadata The image metadata.
 * @param param The write parameters.
 * @param globalColorTable The Global Color Table.
 * @param sourceBounds The source region.
 * @param destSize The destination dimensions.
 */
private void writeImage(RenderedImage image,
                        GIFWritableImageMetadata imageMetadata,
                        ImageWriteParam param, byte[] globalColorTable,
                        Rectangle sourceBounds, Dimension destSize)
  throws IOException {
    ColorModel colorModel = image.getColorModel();
    SampleModel sampleModel = image.getSampleModel();

    boolean writeGraphicsControlExtension;
    if (imageMetadata == null) {
        // Create default metadata.
        imageMetadata = (GIFWritableImageMetadata)getDefaultImageMetadata(
            new ImageTypeSpecifier(image), param);

        // Set GraphicControlExtension flag only if there is
        // transparency.
        writeGraphicsControlExtension = imageMetadata.transparentColorFlag;
    } else {
        // Check for GraphicControlExtension element.
        NodeList list = null;
        try {
            IIOMetadataNode root = (IIOMetadataNode)
                imageMetadata.getAsTree(IMAGE_METADATA_NAME);
            list = root.getElementsByTagName("GraphicControlExtension");
        } catch(IllegalArgumentException iae) {
            // Should never happen.
        }

        // Set GraphicControlExtension flag if element present.
        writeGraphicsControlExtension =
            list != null && list.getLength() > 0;

        // If progressive mode is not MODE_COPY_FROM_METADATA, ensure
        // the interlacing is set per the ImageWriteParam mode setting.
        if (param != null && param.canWriteProgressive()) {
            if (param.getProgressiveMode() ==
                ImageWriteParam.MODE_DISABLED) {
                imageMetadata.interlaceFlag = false;
            } else if (param.getProgressiveMode() ==
                       ImageWriteParam.MODE_DEFAULT) {
                imageMetadata.interlaceFlag = true;
            }
        }
    }

    // Unset local color table if equal to global color table.
    if (Arrays.equals(globalColorTable, imageMetadata.localColorTable)) {
        imageMetadata.localColorTable = null;
    }

    // Override dimensions
    imageMetadata.imageWidth = destSize.width;
    imageMetadata.imageHeight = destSize.height;

    // Write Graphics Control Extension.
    if (writeGraphicsControlExtension) {
        writeGraphicControlExtension(imageMetadata);
    }

    // Write extension blocks.
    writePlainTextExtension(imageMetadata);
    writeApplicationExtension(imageMetadata);
    writeCommentExtension(imageMetadata);

    // Write Image Descriptor
    int bitsPerPixel =
        getNumBits(imageMetadata.localColorTable == null ?
                   (globalColorTable == null ?
                    sampleModel.getSampleSize(0) :
                    globalColorTable.length/3) :
                   imageMetadata.localColorTable.length/3);
    writeImageDescriptor(imageMetadata, bitsPerPixel);

    // Write image data
    writeRasterData(image, sourceBounds, destSize,
                    param, imageMetadata.interlaceFlag);
}
 
Example 15
Source File: GIFImageWriter.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Writes any extension blocks, the Image Descriptor, and the image data
 *
 * @param iioimage The image and image metadata.
 * @param param The write parameters.
 * @param globalColorTable The Global Color Table.
 * @param sourceBounds The source region.
 * @param destSize The destination dimensions.
 */
private void writeImage(RenderedImage image,
                        GIFWritableImageMetadata imageMetadata,
                        ImageWriteParam param, byte[] globalColorTable,
                        Rectangle sourceBounds, Dimension destSize)
  throws IOException {
    ColorModel colorModel = image.getColorModel();
    SampleModel sampleModel = image.getSampleModel();

    boolean writeGraphicsControlExtension;
    if (imageMetadata == null) {
        // Create default metadata.
        imageMetadata = (GIFWritableImageMetadata)getDefaultImageMetadata(
            new ImageTypeSpecifier(image), param);

        // Set GraphicControlExtension flag only if there is
        // transparency.
        writeGraphicsControlExtension = imageMetadata.transparentColorFlag;
    } else {
        // Check for GraphicControlExtension element.
        NodeList list = null;
        try {
            IIOMetadataNode root = (IIOMetadataNode)
                imageMetadata.getAsTree(IMAGE_METADATA_NAME);
            list = root.getElementsByTagName("GraphicControlExtension");
        } catch(IllegalArgumentException iae) {
            // Should never happen.
        }

        // Set GraphicControlExtension flag if element present.
        writeGraphicsControlExtension =
            list != null && list.getLength() > 0;

        // If progressive mode is not MODE_COPY_FROM_METADATA, ensure
        // the interlacing is set per the ImageWriteParam mode setting.
        if (param != null && param.canWriteProgressive()) {
            if (param.getProgressiveMode() ==
                ImageWriteParam.MODE_DISABLED) {
                imageMetadata.interlaceFlag = false;
            } else if (param.getProgressiveMode() ==
                       ImageWriteParam.MODE_DEFAULT) {
                imageMetadata.interlaceFlag = true;
            }
        }
    }

    // Unset local color table if equal to global color table.
    if (Arrays.equals(globalColorTable, imageMetadata.localColorTable)) {
        imageMetadata.localColorTable = null;
    }

    // Override dimensions
    imageMetadata.imageWidth = destSize.width;
    imageMetadata.imageHeight = destSize.height;

    // Write Graphics Control Extension.
    if (writeGraphicsControlExtension) {
        writeGraphicControlExtension(imageMetadata);
    }

    // Write extension blocks.
    writePlainTextExtension(imageMetadata);
    writeApplicationExtension(imageMetadata);
    writeCommentExtension(imageMetadata);

    // Write Image Descriptor
    int bitsPerPixel =
        getNumBits(imageMetadata.localColorTable == null ?
                   (globalColorTable == null ?
                    sampleModel.getSampleSize(0) :
                    globalColorTable.length/3) :
                   imageMetadata.localColorTable.length/3);
    writeImageDescriptor(imageMetadata, bitsPerPixel);

    // Write image data
    writeRasterData(image, sourceBounds, destSize,
                    param, imageMetadata.interlaceFlag);
}
 
Example 16
Source File: GIFImageWriter.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Writes any extension blocks, the Image Descriptor, and the image data
 *
 * @param iioimage The image and image metadata.
 * @param param The write parameters.
 * @param globalColorTable The Global Color Table.
 * @param sourceBounds The source region.
 * @param destSize The destination dimensions.
 */
private void writeImage(RenderedImage image,
                        GIFWritableImageMetadata imageMetadata,
                        ImageWriteParam param, byte[] globalColorTable,
                        Rectangle sourceBounds, Dimension destSize)
  throws IOException {
    ColorModel colorModel = image.getColorModel();
    SampleModel sampleModel = image.getSampleModel();

    boolean writeGraphicsControlExtension;
    if (imageMetadata == null) {
        // Create default metadata.
        imageMetadata = (GIFWritableImageMetadata)getDefaultImageMetadata(
            new ImageTypeSpecifier(image), param);

        // Set GraphicControlExtension flag only if there is
        // transparency.
        writeGraphicsControlExtension = imageMetadata.transparentColorFlag;
    } else {
        // Check for GraphicControlExtension element.
        NodeList list = null;
        try {
            IIOMetadataNode root = (IIOMetadataNode)
                imageMetadata.getAsTree(IMAGE_METADATA_NAME);
            list = root.getElementsByTagName("GraphicControlExtension");
        } catch(IllegalArgumentException iae) {
            // Should never happen.
        }

        // Set GraphicControlExtension flag if element present.
        writeGraphicsControlExtension =
            list != null && list.getLength() > 0;

        // If progressive mode is not MODE_COPY_FROM_METADATA, ensure
        // the interlacing is set per the ImageWriteParam mode setting.
        if (param != null && param.canWriteProgressive()) {
            if (param.getProgressiveMode() ==
                ImageWriteParam.MODE_DISABLED) {
                imageMetadata.interlaceFlag = false;
            } else if (param.getProgressiveMode() ==
                       ImageWriteParam.MODE_DEFAULT) {
                imageMetadata.interlaceFlag = true;
            }
        }
    }

    // Unset local color table if equal to global color table.
    if (Arrays.equals(globalColorTable, imageMetadata.localColorTable)) {
        imageMetadata.localColorTable = null;
    }

    // Override dimensions
    imageMetadata.imageWidth = destSize.width;
    imageMetadata.imageHeight = destSize.height;

    // Write Graphics Control Extension.
    if (writeGraphicsControlExtension) {
        writeGraphicControlExtension(imageMetadata);
    }

    // Write extension blocks.
    writePlainTextExtension(imageMetadata);
    writeApplicationExtension(imageMetadata);
    writeCommentExtension(imageMetadata);

    // Write Image Descriptor
    int bitsPerPixel =
        getNumBits(imageMetadata.localColorTable == null ?
                   (globalColorTable == null ?
                    sampleModel.getSampleSize(0) :
                    globalColorTable.length/3) :
                   imageMetadata.localColorTable.length/3);
    writeImageDescriptor(imageMetadata, bitsPerPixel);

    // Write image data
    writeRasterData(image, sourceBounds, destSize,
                    param, imageMetadata.interlaceFlag);
}
 
Example 17
Source File: GIFImageWriter.java    From JDKSourceCode1.8 with MIT License 4 votes vote down vote up
/**
 * Writes any extension blocks, the Image Descriptor, and the image data
 *
 * @param iioimage The image and image metadata.
 * @param param The write parameters.
 * @param globalColorTable The Global Color Table.
 * @param sourceBounds The source region.
 * @param destSize The destination dimensions.
 */
private void writeImage(RenderedImage image,
                        GIFWritableImageMetadata imageMetadata,
                        ImageWriteParam param, byte[] globalColorTable,
                        Rectangle sourceBounds, Dimension destSize)
  throws IOException {
    ColorModel colorModel = image.getColorModel();
    SampleModel sampleModel = image.getSampleModel();

    boolean writeGraphicsControlExtension;
    if (imageMetadata == null) {
        // Create default metadata.
        imageMetadata = (GIFWritableImageMetadata)getDefaultImageMetadata(
            new ImageTypeSpecifier(image), param);

        // Set GraphicControlExtension flag only if there is
        // transparency.
        writeGraphicsControlExtension = imageMetadata.transparentColorFlag;
    } else {
        // Check for GraphicControlExtension element.
        NodeList list = null;
        try {
            IIOMetadataNode root = (IIOMetadataNode)
                imageMetadata.getAsTree(IMAGE_METADATA_NAME);
            list = root.getElementsByTagName("GraphicControlExtension");
        } catch(IllegalArgumentException iae) {
            // Should never happen.
        }

        // Set GraphicControlExtension flag if element present.
        writeGraphicsControlExtension =
            list != null && list.getLength() > 0;

        // If progressive mode is not MODE_COPY_FROM_METADATA, ensure
        // the interlacing is set per the ImageWriteParam mode setting.
        if (param != null && param.canWriteProgressive()) {
            if (param.getProgressiveMode() ==
                ImageWriteParam.MODE_DISABLED) {
                imageMetadata.interlaceFlag = false;
            } else if (param.getProgressiveMode() ==
                       ImageWriteParam.MODE_DEFAULT) {
                imageMetadata.interlaceFlag = true;
            }
        }
    }

    // Unset local color table if equal to global color table.
    if (Arrays.equals(globalColorTable, imageMetadata.localColorTable)) {
        imageMetadata.localColorTable = null;
    }

    // Override dimensions
    imageMetadata.imageWidth = destSize.width;
    imageMetadata.imageHeight = destSize.height;

    // Write Graphics Control Extension.
    if (writeGraphicsControlExtension) {
        writeGraphicControlExtension(imageMetadata);
    }

    // Write extension blocks.
    writePlainTextExtension(imageMetadata);
    writeApplicationExtension(imageMetadata);
    writeCommentExtension(imageMetadata);

    // Write Image Descriptor
    int bitsPerPixel =
        getNumBits(imageMetadata.localColorTable == null ?
                   (globalColorTable == null ?
                    sampleModel.getSampleSize(0) :
                    globalColorTable.length/3) :
                   imageMetadata.localColorTable.length/3);
    writeImageDescriptor(imageMetadata, bitsPerPixel);

    // Write image data
    writeRasterData(image, sourceBounds, destSize,
                    param, imageMetadata.interlaceFlag);
}
 
Example 18
Source File: GIFImageWriter.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Writes any extension blocks, the Image Descriptor, and the image data
 *
 * @param iioimage The image and image metadata.
 * @param param The write parameters.
 * @param globalColorTable The Global Color Table.
 * @param sourceBounds The source region.
 * @param destSize The destination dimensions.
 */
private void writeImage(RenderedImage image,
                        GIFWritableImageMetadata imageMetadata,
                        ImageWriteParam param, byte[] globalColorTable,
                        Rectangle sourceBounds, Dimension destSize)
  throws IOException {
    ColorModel colorModel = image.getColorModel();
    SampleModel sampleModel = image.getSampleModel();

    boolean writeGraphicsControlExtension;
    if (imageMetadata == null) {
        // Create default metadata.
        imageMetadata = (GIFWritableImageMetadata)getDefaultImageMetadata(
            new ImageTypeSpecifier(image), param);

        // Set GraphicControlExtension flag only if there is
        // transparency.
        writeGraphicsControlExtension = imageMetadata.transparentColorFlag;
    } else {
        // Check for GraphicControlExtension element.
        NodeList list = null;
        try {
            IIOMetadataNode root = (IIOMetadataNode)
                imageMetadata.getAsTree(IMAGE_METADATA_NAME);
            list = root.getElementsByTagName("GraphicControlExtension");
        } catch(IllegalArgumentException iae) {
            // Should never happen.
        }

        // Set GraphicControlExtension flag if element present.
        writeGraphicsControlExtension =
            list != null && list.getLength() > 0;

        // If progressive mode is not MODE_COPY_FROM_METADATA, ensure
        // the interlacing is set per the ImageWriteParam mode setting.
        if (param != null && param.canWriteProgressive()) {
            if (param.getProgressiveMode() ==
                ImageWriteParam.MODE_DISABLED) {
                imageMetadata.interlaceFlag = false;
            } else if (param.getProgressiveMode() ==
                       ImageWriteParam.MODE_DEFAULT) {
                imageMetadata.interlaceFlag = true;
            }
        }
    }

    // Unset local color table if equal to global color table.
    if (Arrays.equals(globalColorTable, imageMetadata.localColorTable)) {
        imageMetadata.localColorTable = null;
    }

    // Override dimensions
    imageMetadata.imageWidth = destSize.width;
    imageMetadata.imageHeight = destSize.height;

    // Write Graphics Control Extension.
    if (writeGraphicsControlExtension) {
        writeGraphicControlExtension(imageMetadata);
    }

    // Write extension blocks.
    writePlainTextExtension(imageMetadata);
    writeApplicationExtension(imageMetadata);
    writeCommentExtension(imageMetadata);

    // Write Image Descriptor
    int bitsPerPixel =
        getNumBits(imageMetadata.localColorTable == null ?
                   (globalColorTable == null ?
                    sampleModel.getSampleSize(0) :
                    globalColorTable.length/3) :
                   imageMetadata.localColorTable.length/3);
    writeImageDescriptor(imageMetadata, bitsPerPixel);

    // Write image data
    writeRasterData(image, sourceBounds, destSize,
                    param, imageMetadata.interlaceFlag);
}
 
Example 19
Source File: GIFImageWriter.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Writes any extension blocks, the Image Descriptor, and the image data
 *
 * @param iioimage The image and image metadata.
 * @param param The write parameters.
 * @param globalColorTable The Global Color Table.
 * @param sourceBounds The source region.
 * @param destSize The destination dimensions.
 */
private void writeImage(RenderedImage image,
                        GIFWritableImageMetadata imageMetadata,
                        ImageWriteParam param, byte[] globalColorTable,
                        Rectangle sourceBounds, Dimension destSize)
  throws IOException {
    ColorModel colorModel = image.getColorModel();
    SampleModel sampleModel = image.getSampleModel();

    boolean writeGraphicsControlExtension;
    if (imageMetadata == null) {
        // Create default metadata.
        imageMetadata = (GIFWritableImageMetadata)getDefaultImageMetadata(
            new ImageTypeSpecifier(image), param);

        // Set GraphicControlExtension flag only if there is
        // transparency.
        writeGraphicsControlExtension = imageMetadata.transparentColorFlag;
    } else {
        // Check for GraphicControlExtension element.
        NodeList list = null;
        try {
            IIOMetadataNode root = (IIOMetadataNode)
                imageMetadata.getAsTree(IMAGE_METADATA_NAME);
            list = root.getElementsByTagName("GraphicControlExtension");
        } catch(IllegalArgumentException iae) {
            // Should never happen.
        }

        // Set GraphicControlExtension flag if element present.
        writeGraphicsControlExtension =
            list != null && list.getLength() > 0;

        // If progressive mode is not MODE_COPY_FROM_METADATA, ensure
        // the interlacing is set per the ImageWriteParam mode setting.
        if (param != null && param.canWriteProgressive()) {
            if (param.getProgressiveMode() ==
                ImageWriteParam.MODE_DISABLED) {
                imageMetadata.interlaceFlag = false;
            } else if (param.getProgressiveMode() ==
                       ImageWriteParam.MODE_DEFAULT) {
                imageMetadata.interlaceFlag = true;
            }
        }
    }

    // Unset local color table if equal to global color table.
    if (Arrays.equals(globalColorTable, imageMetadata.localColorTable)) {
        imageMetadata.localColorTable = null;
    }

    // Override dimensions
    imageMetadata.imageWidth = destSize.width;
    imageMetadata.imageHeight = destSize.height;

    // Write Graphics Control Extension.
    if (writeGraphicsControlExtension) {
        writeGraphicControlExtension(imageMetadata);
    }

    // Write extension blocks.
    writePlainTextExtension(imageMetadata);
    writeApplicationExtension(imageMetadata);
    writeCommentExtension(imageMetadata);

    // Write Image Descriptor
    int bitsPerPixel =
        getNumBits(imageMetadata.localColorTable == null ?
                   (globalColorTable == null ?
                    sampleModel.getSampleSize(0) :
                    globalColorTable.length/3) :
                   imageMetadata.localColorTable.length/3);
    writeImageDescriptor(imageMetadata, bitsPerPixel);

    // Write image data
    writeRasterData(image, sourceBounds, destSize,
                    param, imageMetadata.interlaceFlag);
}
 
Example 20
Source File: GIFImageWriter.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Writes any extension blocks, the Image Descriptor, and the image data
 *
 * @param iioimage The image and image metadata.
 * @param param The write parameters.
 * @param globalColorTable The Global Color Table.
 * @param sourceBounds The source region.
 * @param destSize The destination dimensions.
 */
private void writeImage(RenderedImage image,
                        GIFWritableImageMetadata imageMetadata,
                        ImageWriteParam param, byte[] globalColorTable,
                        Rectangle sourceBounds, Dimension destSize)
  throws IOException {
    ColorModel colorModel = image.getColorModel();
    SampleModel sampleModel = image.getSampleModel();

    boolean writeGraphicsControlExtension;
    if (imageMetadata == null) {
        // Create default metadata.
        imageMetadata = (GIFWritableImageMetadata)getDefaultImageMetadata(
            new ImageTypeSpecifier(image), param);

        // Set GraphicControlExtension flag only if there is
        // transparency.
        writeGraphicsControlExtension = imageMetadata.transparentColorFlag;
    } else {
        // Check for GraphicControlExtension element.
        NodeList list = null;
        try {
            IIOMetadataNode root = (IIOMetadataNode)
                imageMetadata.getAsTree(IMAGE_METADATA_NAME);
            list = root.getElementsByTagName("GraphicControlExtension");
        } catch(IllegalArgumentException iae) {
            // Should never happen.
        }

        // Set GraphicControlExtension flag if element present.
        writeGraphicsControlExtension =
            list != null && list.getLength() > 0;

        // If progressive mode is not MODE_COPY_FROM_METADATA, ensure
        // the interlacing is set per the ImageWriteParam mode setting.
        if (param != null && param.canWriteProgressive()) {
            if (param.getProgressiveMode() ==
                ImageWriteParam.MODE_DISABLED) {
                imageMetadata.interlaceFlag = false;
            } else if (param.getProgressiveMode() ==
                       ImageWriteParam.MODE_DEFAULT) {
                imageMetadata.interlaceFlag = true;
            }
        }
    }

    // Unset local color table if equal to global color table.
    if (Arrays.equals(globalColorTable, imageMetadata.localColorTable)) {
        imageMetadata.localColorTable = null;
    }

    // Override dimensions
    imageMetadata.imageWidth = destSize.width;
    imageMetadata.imageHeight = destSize.height;

    // Write Graphics Control Extension.
    if (writeGraphicsControlExtension) {
        writeGraphicControlExtension(imageMetadata);
    }

    // Write extension blocks.
    writePlainTextExtension(imageMetadata);
    writeApplicationExtension(imageMetadata);
    writeCommentExtension(imageMetadata);

    // Write Image Descriptor
    int bitsPerPixel =
        getNumBits(imageMetadata.localColorTable == null ?
                   (globalColorTable == null ?
                    sampleModel.getSampleSize(0) :
                    globalColorTable.length/3) :
                   imageMetadata.localColorTable.length/3);
    writeImageDescriptor(imageMetadata, bitsPerPixel);

    // Write image data
    writeRasterData(image, sourceBounds, destSize,
                    param, imageMetadata.interlaceFlag);
}