Packages

 

com.aspose.imaging

Interfaces

Classes

Exceptions

com.aspose.imaging

Class ColorPaletteHelper



  • public final class ColorPaletteHelper
    extends Object

    Helper class for color palettes manipulation.

    • Method Detail

      • createMonochrome

        public static IColorPalette createMonochrome()

        Creates a monochrome color palette containing 2 colors only.

        Returns:
        Color palette for monochrome images.
      • create4Bit

        public static IColorPalette create4Bit()

        Creates the 4 bit color palette.

        Returns:
        The 4 bit color palette.
      • create4BitGrayscale

        public static IColorPalette create4BitGrayscale(boolean minIsWhite)

        Creates the 4 bit grayscale palette.

        Parameters:
        minIsWhite - if set to true the palette starts with white color, otherwise it starts with black color.
        Returns:
        The 4 bit grayscale palette.
      • create8Bit

        public static IColorPalette create8Bit()

        Creates the 8 bit color palette.

        Returns:
        The 8 bit color palette.
      • create8BitGrayscale

        public static IColorPalette create8BitGrayscale(boolean minIsWhite)

        Creates the 8 bit grayscale palette.

        Parameters:
        minIsWhite - if set to true the palette starts with white color, otherwise it starts with black color.
        Returns:
        The 8 bit grayscale palette.
        Code example:

        The following example creates a palettized grayscale BMP image and then saves it to a file.


        String dir = "c:\\temp\\";
        
        com.aspose.imaging.imageoptions.BmpOptions createOptions = new com.aspose.imaging.imageoptions.BmpOptions();
        
        // Save to a file
        createOptions.setSource(new com.aspose.imaging.sources.FileCreateSource(dir + "output.palette8bit.bmp", false));
        
        // Use 8 bits per pixel to reduce the size of the output image.
        createOptions.setBitsPerPixel(8);
        
        // Set the standard 8-bit grayscale color palette which covers all grayscale colors.
        // If the processed image contains only grayscale colors, then its palettized version
        // is visually indistinguishable from a non-palletized one.
        createOptions.setPalette(com.aspose.imaging.ColorPaletteHelper.create8BitGrayscale(false));
        
        // Save without compression.
        // You can also use RLE-8 compression to reduce the size of the output image.
        createOptions.setCompression(com.aspose.imaging.fileformats.bmp.BitmapCompression.Rgb);
        
        // Set the horizontal and vertical resolution to 96 dpi.
        createOptions.setResolutionSettings(new com.aspose.imaging.ResolutionSetting(96.0, 96.0));
        
        // Create a BMP image of 100 x 100 px and save it to a file.
        com.aspose.imaging.Image image = com.aspose.imaging.Image.create(createOptions, 100, 100);
        try {
            com.aspose.imaging.Graphics graphics = new com.aspose.imaging.Graphics(image);
        
            com.aspose.imaging.brushes.LinearGradientBrush gradientBrush = new com.aspose.imaging.brushes.LinearGradientBrush(
                    new com.aspose.imaging.Point(0, 0),
                    new com.aspose.imaging.Point(image.getWidth(), image.getHeight()),
                    com.aspose.imaging.Color.getBlack(),
                    com.aspose.imaging.Color.getWhite());
        
            // Fill the image with a grayscale gradient
            graphics.fillRectangle(gradientBrush, image.getBounds());
        
            image.save();
        } finally {
            image.dispose();
        }
        

      • getCloseImagePalette

        public static IColorPalette getCloseImagePalette(RasterImage image,
                                                         int entriesCount)

        Gets color palette from raster image (palletizes image) in case the image does not have one. In case palette exists it will be used instead performing calculations.

        Parameters:
        image - The raster image.
        entriesCount - The desired entries count.
        Returns:
        The color palette which starts with the most frequent colors from the image and contains entriesCount entries.
        Code example:

        The following example shows how to palletize a BMP image to reduce its output size.


        
        // Create a BMP image 100 x 100 px.
        com.aspose.imaging.fileformats.bmp.BmpImage bmpImage = new com.aspose.imaging.fileformats.bmp.BmpImage(100, 100);
        try {
            // The linear gradient from the left-top to the right-bottom corner of the image.
            com.aspose.imaging.brushes.LinearGradientBrush brush =
                    new com.aspose.imaging.brushes.LinearGradientBrush(
                            new com.aspose.imaging.Point(0, 0),
                            new com.aspose.imaging.Point(bmpImage.getWidth(), bmpImage.getHeight()),
                            com.aspose.imaging.Color.getRed(),
                            com.aspose.imaging.Color.getGreen());
        
            // Fill the entire image with the linear gradient brush.
            com.aspose.imaging.Graphics gr = new com.aspose.imaging.Graphics(bmpImage);
            gr.fillRectangle(brush, bmpImage.getBounds());
        
            // Get the closest 8-bit color palette which covers as many pixels as possible, so that a palettized image
            // is almost visually indistinguishable from a non-palletized one.
            com.aspose.imaging.IColorPalette palette = com.aspose.imaging.ColorPaletteHelper.getCloseImagePalette(bmpImage, 256);
        
            // 8-bit palette contains at most 256 colors.
            com.aspose.imaging.imageoptions.BmpOptions saveOptions = new com.aspose.imaging.imageoptions.BmpOptions();
            saveOptions.setPalette(palette);
            saveOptions.setBitsPerPixel(8);
        
            java.io.ByteArrayOutputStream stream = new java.io.ByteArrayOutputStream();
            try {
                bmpImage.save(stream, saveOptions);
                System.out.println("The palettized image size is " + stream.size() + " bytes.");
            } finally {
                stream.close();
            }
        
            stream = new java.io.ByteArrayOutputStream();
            try {
                bmpImage.save(stream);
                System.out.println("The non-palettized image size is " + stream.size() + " bytes.");
            } finally {
                stream.close();
            }
        } finally {
            bmpImage.dispose();
        }
        
        // The output looks like this:
        // The palettized image size is 11078 bytes.
        // The non-palettized image size is 40054 bytes.
        

      • getCloseImagePalette

        public static IColorPalette getCloseImagePalette(RasterImage image,
                                                         Rectangle destBounds,
                                                         int entriesCount)

        Gets color palette from raster image (palletizes image) in case the image does not have one. In case palette exists it will be used instead performing calculations.

        Parameters:
        image - The raster image.
        destBounds - The destination image bounds.
        entriesCount - The desired entries count.
        Returns:
        The color palette which starts with the most frequent colors from the image and contains entriesCount entries.
      • getUniformColorPalette

        public static ColorPalette getUniformColorPalette(RasterImage image)

        Get uniform 256 color palette.

        Parameters:
        image - The image.
        Returns:
        The ColorPalette.
      • getDownscalePalette

        public static ColorPalette getDownscalePalette(RasterImage image)

        Get 256 color palette, composed from upper bits of initial image color values.

        Parameters:
        image - The image.
        Returns:
        The ColorPalette.