Packages

 

com.aspose.imaging

Interfaces

Classes

Exceptions

com.aspose.imaging

Class RasterCachedImage

    • Method Detail

      • isCached

        public boolean isCached()

        Gets a value indicating whether image data is cached currently.

        Specified by:
        isCached in class DataStreamSupporter
        Returns:
        true if image data is cached; otherwise, false.
      • cacheData

        public void cacheData()

        Caches the data and ensures no additional data loading will be performed from the underlying DataStreamSupporter.DataStreamContainer.

        Specified by:
        cacheData in class DataStreamSupporter
        Code example:

        The following example shows how raster image caching affects performance. In general case, reading cached data is performed faster than reading non-cached data.


        String dir = "c:\\temp\\";
        
        // Load an image from a PNG file.
        com.aspose.imaging.RasterCachedImage image = (com.aspose.imaging.RasterCachedImage) com.aspose.imaging.Image.load(dir + "sample.png");
        try {
            // Cache all pixel data so that no additional data loading will be performed from the underlying data stream
            image.cacheData();
        
            long startTime = System.currentTimeMillis();
        
            // Reading all pixels is pretty fast.
            for (int y = 0; y < image.getHeight(); y++) {
                for (int x = 0; x < image.getWidth(); x++) {
                    int color = image.getArgb32Pixel(x, y);
                }
            }
        
            long stopTime = System.currentTimeMillis();
            long elapsedMilliseconds = stopTime - startTime;
            System.out.println("Reading all cached pixels took " + elapsedMilliseconds + " ms.");
        } finally {
            image.dispose();
        }
        
        // Load an image from a PNG file
        image = (com.aspose.imaging.RasterCachedImage) com.aspose.imaging.Image.load(dir + "sample.png");
        try {
            long startTime = System.currentTimeMillis();
        
            // Reading all pixels is not as fast as when caching
            for (int y = 0; y < image.getHeight(); y++) {
                for (int x = 0; x < image.getWidth(); x++) {
                    int color = image.getArgb32Pixel(x, y);
                }
            }
        
            long stopTime = System.currentTimeMillis();
            long elapsedMilliseconds = stopTime - startTime;
            System.out.println("Reading all pixels without preliminary caching took " + elapsedMilliseconds + " ms.");
        } finally {
            image.dispose();
        }
        
        // The output may look like this:
        //Reading all cached pixels took 2923 ms.
        //    java.lang.OutOfMemoryError
        //at com.aspose.imaging.internal.G.be.b(Unknown Source)
        //at com.aspose.imaging.internal.G.be.a(Unknown Source)
        //at com.aspose.imaging.internal.G.be.a(Unknown Source)
        //at com.aspose.imaging.internal.G.be.a(Unknown Source)
        //at com.aspose.imaging.internal.G.aB.a(Unknown Source)
        //at com.aspose.imaging.RasterImage.a(Unknown Source)
        //at com.aspose.imaging.RasterImage.getArgb32Pixel(Unknown Source)
        //at com.aspose.examples.ExamplesTest.Test(ExamplesTest.java:54)
        

      • resize

        public void resize(int newWidth,
                           int newHeight,
                           int resizeType)

        Resizes the image.

        Overrides:
        resize in class RasterImage
        Parameters:
        newWidth - The new width.
        newHeight - The new height.
        resizeType - The resize type.
        Code example:

        This example loads a raster cached image and resizes it using various resizing methods.


        String dir = "c:\\temp\\";
        
        com.aspose.imaging.RasterCachedImage image = (com.aspose.imaging.RasterCachedImage) com.aspose.imaging.Image.load(dir + "sample.png");
        try {
            // Scale up by 2 times using Nearest Neighbour resampling.
            image.resize(image.getWidth() * 2, image.getHeight() * 2, com.aspose.imaging.ResizeType.NearestNeighbourResample);
        
            // Save to PNG with default options.
            image.save(dir + "upsample.nearestneighbour.png", new com.aspose.imaging.imageoptions.PngOptions());
        } finally {
            image.dispose();
        }
        
        image = (com.aspose.imaging.RasterCachedImage) com.aspose.imaging.Image.load(dir + "sample.png");
        try {
            // Scale down by 2 times using Nearest Neighbour resampling.
            image.resize(image.getWidth() / 2, image.getHeight() / 2, com.aspose.imaging.ResizeType.NearestNeighbourResample);
        
            // Save to PNG with default options.
            image.save(dir + "downsample.nearestneighbour.png", new com.aspose.imaging.imageoptions.PngOptions());
        } finally {
            image.dispose();
        }
        
        image = (com.aspose.imaging.RasterCachedImage) com.aspose.imaging.Image.load(dir + "sample.png");
        try {
            // Scale up by 2 times using Bilinear resampling.
            image.resize(image.getWidth() * 2, image.getHeight() * 2, com.aspose.imaging.ResizeType.BilinearResample);
        
            // Save to PNG with default options.
            image.save(dir + "upsample.bilinear.png", new com.aspose.imaging.imageoptions.PngOptions());
        } finally {
            image.dispose();
        }
        
        image = (com.aspose.imaging.RasterCachedImage) com.aspose.imaging.Image.load(dir + "sample.png");
        try {
            // Scale down by 2 times using Bilinear resampling.
            image.resize(image.getWidth() / 2, image.getHeight() / 2, com.aspose.imaging.ResizeType.BilinearResample);
        
            // Save to PNG with default options.
            image.save(dir + "downsample.bilinear.png", new com.aspose.imaging.imageoptions.PngOptions());
        } finally {
            image.dispose();
        }
        

      • resize

        public void resize(int newWidth,
                           int newHeight,
                           ImageResizeSettings settings)

        Resizes the image.

        Overrides:
        resize in class RasterImage
        Parameters:
        newWidth - The new width.
        newHeight - The new height.
        settings - The resize settings.
        Code example:

        This example loads a raster cached image and resizes it using various resizing settings.


        String dir = "c:\\temp\\";
        
        com.aspose.imaging.ImageResizeSettings resizeSettings = new com.aspose.imaging.ImageResizeSettings();
        
        // The adaptive algorithm based on weighted and blended rational function and lanczos3 interpolation.
        resizeSettings.setMode(com.aspose.imaging.ResizeType.AdaptiveResample);
        
        // The small rectangular filter
        resizeSettings.setFilterType(com.aspose.imaging.ImageFilterType.SmallRectangular);
        
        // The number of colors in the palette.
        resizeSettings.setEntriesCount(256);
        
        // The color quantization is not used
        resizeSettings.setColorQuantizationMethod(com.aspose.imaging.ColorQuantizationMethod.None);
        
        // The euclidian method
        resizeSettings.setColorCompareMethod(com.aspose.imaging.ColorCompareMethod.Euclidian);
        
        com.aspose.imaging.RasterCachedImage image = (com.aspose.imaging.RasterCachedImage) com.aspose.imaging.Image.load(dir + "sample.png");
        try {
            // Scale down by 2 times using adaptive resampling.
            image.resize(image.getWidth() / 2, image.getHeight() / 2, resizeSettings);
            image.save(dir + "downsample.adaptive.png", new com.aspose.imaging.imageoptions.PngOptions());
        } finally {
            image.dispose();
        }
        

      • rotateFlip

        public void rotateFlip(int rotateFlipType)

        Rotates, flips, or rotates and flips the image.

        Specified by:
        rotateFlip in class Image
        Parameters:
        rotateFlipType - The rotate flip type.
        See Also:
        RotateFlipType
        Code example:

        This example loads a raster cached image, rotates it by 90 degrees clockwise and optionally flips the image horizontally and(or) vertically.


        String dir = "c:\\temp\\";
        
        // This is a helper class.
        class LocalHelper {
            // Gets a string representation of the rotate flip type.
            public String rotateFlipTypeToString(int rotateFilpType) {
                switch (rotateFilpType) {
                    case com.aspose.imaging.RotateFlipType.RotateNoneFlipNone:
                        return "RotateNoneFlipNone";
                    case com.aspose.imaging.RotateFlipType.Rotate90FlipNone:
                        return "Rotate90FlipNone";
                    case com.aspose.imaging.RotateFlipType.Rotate180FlipNone:
                        return "Rotate180FlipNone";
                    case com.aspose.imaging.RotateFlipType.Rotate270FlipNone:
                        return "Rotate270FlipNone";
                    case com.aspose.imaging.RotateFlipType.RotateNoneFlipX:
                        return "RotateNoneFlipX";
                    case com.aspose.imaging.RotateFlipType.Rotate90FlipX:
                        return "Rotate90FlipX";
                    case com.aspose.imaging.RotateFlipType.Rotate180FlipX:
                        return "Rotate180FlipX";
                    case com.aspose.imaging.RotateFlipType.Rotate270FlipX:
                        return "Rotate270FlipX";
                    case com.aspose.imaging.RotateFlipType.RotateNoneFlipY:
                        return "RotateNoneFlipY";
                    case com.aspose.imaging.RotateFlipType.Rotate90FlipY:
                        return "Rotate90FlipY";
                    case com.aspose.imaging.RotateFlipType.Rotate180FlipY:
                        return "Rotate180FlipY";
                    case com.aspose.imaging.RotateFlipType.Rotate270FlipY:
                        return "Rotate270FlipY";
                    case com.aspose.imaging.RotateFlipType.RotateNoneFlipXY:
                        return "RotateNoneFlipXY";
                    case com.aspose.imaging.RotateFlipType.Rotate90FlipXY:
                        return "Rotate90FlipXY";
                    case com.aspose.imaging.RotateFlipType.Rotate180FlipXY:
                        return "Rotate180FlipXY";
                    case com.aspose.imaging.RotateFlipType.Rotate270FlipXY:
                        return "Rotate270FlipXY";
                    default:
                        throw new java.lang.IllegalArgumentException("rotateFlipType");
                }
            }
        }
        
        // Here is the main example
        int[] rotateFlipTypes = new int[]
                {
                        com.aspose.imaging.RotateFlipType.Rotate90FlipNone,
                        com.aspose.imaging.RotateFlipType.Rotate90FlipX,
                        com.aspose.imaging.RotateFlipType.Rotate90FlipXY,
                        com.aspose.imaging.RotateFlipType.Rotate90FlipY,
                };
        
        LocalHelper localHelper = new LocalHelper();
        for (int rotateFlipType : rotateFlipTypes) {
            // Rotate, flip and save to the output file.
            com.aspose.imaging.RasterCachedImage image = (com.aspose.imaging.RasterCachedImage) com.aspose.imaging.Image.load(dir + "sample.bmp");
            try {
                image.rotateFlip(rotateFlipType);
                image.save(dir + "sample." + localHelper.rotateFlipTypeToString(rotateFlipType) + ".bmp");
            } finally {
                image.dispose();
            }
        }
        

      • rotate

        public void rotate(float angle,
                           boolean resizeProportionally,
                           Color backgroundColor)

        Rotate image around the center.

        Overrides:
        rotate in class RasterImage
        Parameters:
        angle - The rotate angle in degrees. Positive values will rotate clockwise.
        resizeProportionally - if set to true you will have your image size changed according to rotated rectangle (corner points) projections in other case that leaves dimensions untouched and only internal image contents are rotated.
        backgroundColor - Color of the background.
      • crop

        public void crop(Rectangle rectangle)

        Cropping the image.

        Overrides:
        crop in class RasterImage
        Parameters:
        rectangle - The rectangle.
        Code example:

        The following example crops a raster cached image. The cropping area is be specified via com.aspose.imaging.Rectangle.


        String dir = "c:\\temp\\";
        
        com.aspose.imaging.Image image = com.aspose.imaging.Image.load(dir + "sample.png");
        try {
            com.aspose.imaging.RasterCachedImage rasterImage = (com.aspose.imaging.RasterCachedImage) image;
        
            // Crop the image. The cropping area is the rectangular central area of the image.
            int width = rasterImage.getWidth();
            int height = rasterImage.getHeight();
            com.aspose.imaging.Rectangle area = new com.aspose.imaging.Rectangle(width / 4, height / 4, width / 2, height / 2);
            rasterImage.crop(area);
        
            // Save the cropped image to PNG
            rasterImage.save(dir + "sample.Crop.png");
        } finally {
            image.dispose();
        }
        

      • dither

        public void dither(int ditheringMethod,
                           int bitsCount,
                           IColorPalette customPalette)

        Performs dithering on the current image.

        Specified by:
        dither in class RasterImage
        Parameters:
        ditheringMethod - The dithering method.
        bitsCount - The final bits count for dithering.
        customPalette - The custom palette for dithering.
      • grayscale

        public void grayscale()

        Transformation of an image to its grayscale representation

        Overrides:
        grayscale in class RasterImage
        Code example:

        The following example transforms a colored raster cached image to its grayscale representation. Grayscale images are composed exclusively of shades of gray and carry only intensity information.


        String dir = "c:\\temp\\";
        
        com.aspose.imaging.Image image = com.aspose.imaging.Image.load(dir + "sample.png");
        try {
            com.aspose.imaging.RasterCachedImage rasterImage = (com.aspose.imaging.RasterCachedImage) image;
        
            rasterImage.grayscale();
            rasterImage.save(dir + "sample.Grayscale.png");
        } finally {
            image.dispose();
        }
        

      • binarizeFixed

        public void binarizeFixed(byte threshold)

        Binarization of an image with predefined threshold

        Overrides:
        binarizeFixed in class RasterImage
        Parameters:
        threshold - Threshold value. If corresponding gray value of a pixel is greater than threshold, a value of 255 will be assigned to it, 0 otherwise.
        Code example:

        The following example binarizes a raster cached image with the predefined threshold. Binarized images contain only 2 colors - black and white.


        String dir = "c:\\temp\\";
        
        com.aspose.imaging.Image image = com.aspose.imaging.Image.load(dir + "sample.png");
        try {
            com.aspose.imaging.RasterCachedImage rasterImage = (com.aspose.imaging.RasterCachedImage) image;
        
            // Binarize the image with a threshold value of 127.
            // If a corresponding gray value of a pixel is greater than 127, a value of 255 will be assigned to it, 0 otherwise.
            rasterImage.binarizeFixed((byte) 127);
            rasterImage.save(dir + "sample.BinarizeFixed.png");
        } finally {
            image.dispose();
        }
        

      • binarizeOtsu

        public void binarizeOtsu()

        Binarization of an image with Otsu thresholding

        Overrides:
        binarizeOtsu in class RasterImage
        Code example:

        The following example binarizes a raster cached image with Otsu thresholding. Binarized images contain only 2 colors - black and white.


        String dir = "c:\\temp\\";
        
        com.aspose.imaging.Image image = com.aspose.imaging.Image.load(dir + "sample.png");
        try {
            com.aspose.imaging.RasterCachedImage rasterImage = (com.aspose.imaging.RasterCachedImage) image;
        
            // Binarize the image with Otsu thresholding.
            rasterImage.binarizeOtsu();
            rasterImage.save(dir + "sample.BinarizeOtsu.png");
        } finally {
            image.dispose();
        }
        

      • binarizeBradley

        public void binarizeBradley(double brightnessDifference,
                                    int windowSize)

        Binarization of an image using Bradley's adaptive thresholding algorithm using the integral image thresholding

        Overrides:
        binarizeBradley in class RasterImage
        Parameters:
        brightnessDifference - The brightness difference between pixel and the average of an s x s window of pixels centered around this pixel.
        windowSize - The size of s x s window of pixels centered around this pixel
        Code example:

        The following example binarizes a raster cached image with Bradley's adaptive thresholding algorithm with the specified window size. Binarized images contain only 2 colors - black and white.


        String dir = "c:\\temp\\";
        
        com.aspose.imaging.Image image = com.aspose.imaging.Image.load(dir + "sample.png");
        try {
            com.aspose.imaging.RasterCachedImage rasterImage = (com.aspose.imaging.RasterCachedImage) image;
        
            // Binarize the image with a brightness difference of 5.
            // The brightness is a difference between a pixel and the average of an 10 x 10 window of pixels centered around this pixel.
            rasterImage.binarizeBradley(5, 10);
            rasterImage.save(dir + "sample.BinarizeBradley5_10x10.png");
        } finally {
            image.dispose();
        }
        

      • binarizeBradley

        public void binarizeBradley(double brightnessDifference)

        Binarization of an image using Bradley's adaptive thresholding algorithm using the integral image thresholding

        Overrides:
        binarizeBradley in class RasterImage
        Parameters:
        brightnessDifference - The brightness difference between pixel and the average of an s x s window of pixels centered around this pixel.
        Code example:

        The following example binarizes a raster cached image with Bradley's adaptive thresholding algorithm. Binarized images contain only 2 colors - black and white.


        String dir = "c:\\temp\\";
        
        com.aspose.imaging.Image image = com.aspose.imaging.Image.load(dir + "sample.png");
        try {
            com.aspose.imaging.RasterCachedImage rasterImage = (com.aspose.imaging.RasterCachedImage) image;
        
            // Binarize the image with a brightness difference of 5.
            // The brightness is a difference between a pixel and the average of an s x s window of pixels centered around this pixel.
            // The size of window will be calibrated automatically.
            rasterImage.binarizeBradley(5);
            rasterImage.save(dir + "sample.BinarizeBradley5.png");
        } finally {
            image.dispose();
        }
        

      • adjustBrightness

        public void adjustBrightness(int brightness)

        Adjust of a brightness for image.

        Overrides:
        adjustBrightness in class RasterImage
        Parameters:
        brightness - Brightness value.
        Code example:

        The following example performs brightness correction of a raster cached image.


        String dir = "c:\\temp\\";
        
        com.aspose.imaging.Image image = com.aspose.imaging.Image.load(dir + "sample.png");
        try {
            com.aspose.imaging.RasterCachedImage rasterImage = (com.aspose.imaging.RasterCachedImage) image;
        
            // Set the brightness value. The accepted values of brightness are in the range [-255, 255].
            rasterImage.adjustBrightness(50);
            rasterImage.save(dir + "sample.AdjustBrightness.png");
        } finally {
            image.dispose();
        }
        

      • adjustContrast

        public void adjustContrast(float contrast)

        Image contrasting

        Overrides:
        adjustContrast in class RasterImage
        Parameters:
        contrast - Contrast value (in range [-100; 100])
        Code example:

        The following example performs contrast correction of a raster cached image.


        String dir = "c:\\temp\\";
        
        com.aspose.imaging.Image image = com.aspose.imaging.Image.load(dir + "sample.png");
        try {
            com.aspose.imaging.RasterCachedImage rasterImage = (com.aspose.imaging.RasterCachedImage) image;
        
            // Set the contrast value. The accepted values of contrast are in the range [-100f, 100f].
            rasterImage.adjustContrast(50);
            rasterImage.save(dir + "sample.AdjustContrast.png");
        } finally {
            image.dispose();
        }
        

      • adjustGamma

        public void adjustGamma(float gammaRed,
                                float gammaGreen,
                                float gammaBlue)

        Gamma-correction of an image.

        Overrides:
        adjustGamma in class RasterImage
        Parameters:
        gammaRed - Gamma for red channel coefficient
        gammaGreen - Gamma for green channel coefficient
        gammaBlue - Gamma for blue channel coefficient
        Code example:

        The following example performs gamma-correction of a raster cached image applying different coefficients for color components.


        String dir = "c:\\temp\\";
        
        com.aspose.imaging.Image image = com.aspose.imaging.Image.load(dir + "sample.png");
        try {
            com.aspose.imaging.RasterCachedImage rasterImage = (com.aspose.imaging.RasterCachedImage) image;
        
            // Set individual gamma coefficients for red, green and blue channels.
            rasterImage.adjustGamma(1.5f, 2.5f, 3.5f);
            rasterImage.save(dir + "sample.AdjustGamma.png");
        } finally {
            image.dispose();
        }
        

      • adjustGamma

        public void adjustGamma(float gamma)

        Gamma-correction of an image.

        Overrides:
        adjustGamma in class RasterImage
        Parameters:
        gamma - Gamma for red, green and blue channels coefficient
        Code example:

        The following example performs gamma-correction of a raster cached image.


        String dir = "c:\\temp\\";
        
        com.aspose.imaging.Image image = com.aspose.imaging.Image.load(dir + "sample.png");
        try {
            com.aspose.imaging.RasterCachedImage rasterImage = (com.aspose.imaging.RasterCachedImage) image;
        
            // Set gamma coefficient for red, green and blue channels.
            rasterImage.adjustGamma(2.5f);
            rasterImage.save(dir + "sample.AdjustGamma.png");
        } finally {
            image.dispose();
        }