Class WriteFxImage


  • public final class WriteFxImage
    extends java.lang.Object
    Writes a JavaFx Image into a ByteBuffer or file possible improvements: - use multiple IDAT chunks to use fixed buffer size and limit memory footprint - implement filtering of lines before compression for smaller file sizes - Optionally add tEXT chunks for metadata (EXIF)
    Author:
    Alexander Krimm
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static javafx.scene.image.WritableImage clone​(javafx.scene.image.Image image)
      copy the given Image to a WritableImage
      static void copyImageDataToPixelBuffer​(javafx.scene.image.Image image, int[] uncompressedImageData)  
      static java.nio.ByteBuffer encode​(javafx.scene.image.Image image)
      Encodes a JavaFx image as an RGB png image with fastest (lossless) compression
      static java.nio.ByteBuffer encode​(javafx.scene.image.Image image, java.nio.ByteBuffer byteBuffer, boolean alpha, int compressionLevel, ar.com.hjg.pngj.FilterType filterType)
      Encodes a JavaFx image as an RGB png image.
      static java.nio.ByteBuffer encodeAlt​(javafx.scene.image.Image image, java.nio.ByteBuffer byteBuffer, boolean alpha, int compressionLevel, java.util.Map<java.lang.String,​java.lang.Object> metaInfo)
      Encodes a JavaFx image as an RGB png image.
      static java.nio.ByteBuffer encodePalette​(javafx.scene.image.Image image, java.nio.ByteBuffer byteBuffer, boolean alpha, int compressionLevel, ar.com.hjg.pngj.FilterType filterType, PaletteQuantizer... userPalette)  
      static PaletteQuantizer estimatePalette​(int[] pixelArray, int width, int heigth, boolean alpha, int nColors)  
      static PaletteQuantizer estimatePalette​(javafx.scene.image.Image image, boolean alpha, int nColors)  
      static int getCompressedSizeBound​(int width, int height, boolean alpha)
      Returns the conservative upper bound for the compressed image size.
      static void savePng​(javafx.scene.image.Image image, java.io.File file)
      Saves the given image as a png file.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • clone

        public static javafx.scene.image.WritableImage clone​(javafx.scene.image.Image image)
        copy the given Image to a WritableImage
        Parameters:
        image - the input image
        Returns:
        clone of image
      • copyImageDataToPixelBuffer

        public static void copyImageDataToPixelBuffer​(javafx.scene.image.Image image,
                                                      int[] uncompressedImageData)
      • encode

        public static java.nio.ByteBuffer encode​(javafx.scene.image.Image image)
        Encodes a JavaFx image as an RGB png image with fastest (lossless) compression
        Parameters:
        image - The input image to be encoded
        Returns:
        a byte buffer with the encoded image
        See Also:
        rfc2083
      • encode

        public static java.nio.ByteBuffer encode​(javafx.scene.image.Image image,
                                                 java.nio.ByteBuffer byteBuffer,
                                                 boolean alpha,
                                                 int compressionLevel,
                                                 ar.com.hjg.pngj.FilterType filterType)
        Encodes a JavaFx image as an RGB png image. If you pass in a ByteBuffer to use, please make sure that it has enough capacity to fit the encoded image or handle errors (IndexOutOfBoundsException) accordingly. If you want to be on the safe side, use getCompressedSizeBound(width, height, alpha) to get an upper bound for the output size.
        Parameters:
        image - The input image to be encoded
        byteBuffer - optional byte buffer to store the output in, pass null to return a new one.
        alpha - whether to include alpha information in the image
        compressionLevel - Deflater.BEST_COMPRESSION (9) to Deflater.BEST_SPEED (0)
        filterType - filter as outlines in https://tools.ietf.org/html/rfc2083#section-6 plus some custom additional options implemented in the undelying PNG encode implementation
        Returns:
        a byte buffer with the encoded image
        See Also:
        "https://tools.ietf.org/html/rfc2083"
      • encodeAlt

        public static java.nio.ByteBuffer encodeAlt​(javafx.scene.image.Image image,
                                                    java.nio.ByteBuffer byteBuffer,
                                                    boolean alpha,
                                                    int compressionLevel,
                                                    java.util.Map<java.lang.String,​java.lang.Object> metaInfo)
        Encodes a JavaFx image as an RGB png image. If you pass in a ByteBuffer to use, please make sure that it has enough capacity to fit the encoded image or handle errors (IndexOutOfBoundsException) accordingly. If you want to be on the safe side, use getCompressedSizeBound(width, height, alpha) to get an upper bound for the output size.
        Parameters:
        image - The input image to be encoded
        byteBuffer - optional byte buffer to store the output in, pass null to return a new one.
        alpha - whether to include alpha information in the image
        compressionLevel - Deflater.BEST_COMPRESSION (9) to Deflater.BEST_SPEED (0)
        metaInfo - an optional map which will be filled with debugging information like compression efficiency
        Returns:
        a byte buffer with the encoded image
        See Also:
        "https://tools.ietf.org/html/rfc2083"
      • encodePalette

        public static java.nio.ByteBuffer encodePalette​(javafx.scene.image.Image image,
                                                        java.nio.ByteBuffer byteBuffer,
                                                        boolean alpha,
                                                        int compressionLevel,
                                                        ar.com.hjg.pngj.FilterType filterType,
                                                        PaletteQuantizer... userPalette)
      • estimatePalette

        public static PaletteQuantizer estimatePalette​(javafx.scene.image.Image image,
                                                       boolean alpha,
                                                       int nColors)
      • estimatePalette

        public static PaletteQuantizer estimatePalette​(int[] pixelArray,
                                                       int width,
                                                       int heigth,
                                                       boolean alpha,
                                                       int nColors)
      • getCompressedSizeBound

        public static int getCompressedSizeBound​(int width,
                                                 int height,
                                                 boolean alpha)
        Returns the conservative upper bound for the compressed image size.
        Parameters:
        width - Image width
        height - Image height
        alpha - Alpha enabled
        Returns:
        the upper bound for the size of the resulting png in bytes
        See Also:
        zlib sourcefor deflate upper bound
      • savePng

        public static void savePng​(javafx.scene.image.Image image,
                                   java.io.File file)
                            throws java.io.IOException
        Saves the given image as a png file.
        Parameters:
        image - The image to save
        file - The filename to save the image to.
        Throws:
        java.io.IOException - if the file cannot be written