RasterCachedImage

Inheritance: java.lang.Object, com.aspose.imaging.DisposableObject, com.aspose.imaging.DataStreamSupporter, com.aspose.imaging.Image, com.aspose.imaging.RasterImage

public abstract class RasterCachedImage extends RasterImage

Represents a raster image supporting raster graphics operations. This image caches pixel data when required.

Methods

Method Description
isCached() Gets a value indicating whether image data is cached currently.
cacheData() Caches the data and ensures no additional data loading will be performed from the underlying DataStreamSupporter.DataStreamContainer.
resize(int newWidth, int newHeight, int resizeType) Resizes the image.
resize(int newWidth, int newHeight, ImageResizeSettings settings) Resizes the image.
rotateFlip(int rotateFlipType) Rotates, flips, or rotates and flips the image.
rotate(float angle, boolean resizeProportionally, Color backgroundColor) Rotate image around the center.
crop(Rectangle rectangle) Cropping the image.
dither(int ditheringMethod, int bitsCount, IColorPalette customPalette) Performs dithering on the current image.
grayscale() Transformation of an image to its grayscale representation
binarizeFixed(byte threshold) Binarization of an image with predefined threshold
binarizeOtsu() Binarization of an image with Otsu thresholding
binarizeBradley(double brightnessDifference, int windowSize) Binarization of an image using Bradley’s adaptive thresholding algorithm using the integral image thresholding
binarizeBradley(double brightnessDifference) Binarization of an image using Bradley’s adaptive thresholding algorithm using the integral image thresholding
adjustBrightness(int brightness) Adjust of a brightness for image.
adjustContrast(float contrast) Image contrasting
adjustGamma(float gammaRed, float gammaGreen, float gammaBlue) Gamma-correction of an image.
adjustGamma(float gamma) Gamma-correction of an image.

Example: The following example transforms a colored raster cached image to its grayscale representation.

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();
}

isCached()

public boolean isCached()

Gets a value indicating whether image data is cached currently.

Returns: boolean - 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.

Example: The following example shows how raster image caching affects performance. 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(int newWidth, int newHeight, int resizeType)

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

Resizes the image.

Parameters:

Parameter Type Description
newWidth int The new width.
newHeight int The new height.
resizeType int The resize type.

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(int newWidth, int newHeight, ImageResizeSettings settings)

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

Resizes the image.

Parameters:

Parameter Type Description
newWidth int The new width.
newHeight int The new height.
settings ImageResizeSettings The resize settings.

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(int rotateFlipType)

public void rotateFlip(int rotateFlipType)

Rotates, flips, or rotates and flips the image.

Parameters:

Parameter Type Description
rotateFlipType int The rotating flip type.

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(float angle, boolean resizeProportionally, Color backgroundColor)

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

Rotate image around the center.

Parameters:

Parameter Type Description
angle float The rotation angle in degrees. Positive values will rotate clockwise.
resizeProportionally boolean 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 Color of the background.

crop(Rectangle rectangle)

public void crop(Rectangle rectangle)

Cropping the image.

Parameters:

Parameter Type Description
rectangle Rectangle The rectangle.

Example: The following example crops a raster cached image. 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(int ditheringMethod, int bitsCount, IColorPalette customPalette)

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

Performs dithering on the current image.

Parameters:

Parameter Type Description
ditheringMethod int The dithering method.
bitsCount int The final bits count for dithering.
customPalette IColorPalette The custom palette for dithering.

grayscale()

public void grayscale()

Transformation of an image to its grayscale representation

Example: The following example transforms a colored raster cached image to its grayscale representation. 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(byte threshold)

public void binarizeFixed(byte threshold)

Binarization of an image with predefined threshold

Parameters:

Parameter Type Description
threshold byte Threshold value. If corresponding gray value of a pixel is greater than threshold, a value of 255 will be assigned to it, 0 otherwise.

Example: The following example binarizes a raster cached image with the predefined threshold. 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

Example: The following example binarizes a raster cached image with Otsu thresholding. 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(double brightnessDifference, int windowSize)

public void binarizeBradley(double brightnessDifference, int windowSize)

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

Parameters:

Parameter Type Description
brightnessDifference double The brightness difference between pixel and the average of an s x s window of pixels centered around this pixel.
windowSize int The size of s x s window of pixels centered around this pixel

Example: The following example binarizes a raster cached image with Bradley’s adaptive thresholding algorithm with the specified window size. 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(double brightnessDifference)

public void binarizeBradley(double brightnessDifference)

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

Parameters:

Parameter Type Description
brightnessDifference double The brightness difference between pixel and the average of an s x s window of pixels centered around this pixel.

Example: The following example binarizes a raster cached image with Bradley’s adaptive thresholding algorithm. 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(int brightness)

public void adjustBrightness(int brightness)

Adjust of a brightness for image.

Parameters:

Parameter Type Description
brightness int Brightness value.

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(float contrast)

public void adjustContrast(float contrast)

Image contrasting

Parameters:

Parameter Type Description
contrast float Contrast value (in range [-100; 100])

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(float gammaRed, float gammaGreen, float gammaBlue)

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

Gamma-correction of an image.

Parameters:

Parameter Type Description
gammaRed float Gamma for red channel coefficient
gammaGreen float Gamma for green channel coefficient
gammaBlue float Gamma for blue channel coefficient

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(float gamma)

public void adjustGamma(float gamma)

Gamma-correction of an image.

Parameters:

Parameter Type Description
gamma float Gamma for red, green and blue channels coefficient

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();
}