public abstract class RasterImage extends Image implements IRasterImageArgb32PixelLoader, com.aspose.imaging_internal.IPixelsSaver
Represents a raster image supporting raster graphics operations.
This example shows how to load pixel information in an array of colors, manipulates the array and set it back to the image.
String dir = "c:\\temp\\"; // Create an instance of GifOptions and set its various properties including the Source property com.aspose.imaging.imageoptions.GifOptions gifOptions = new com.aspose.imaging.imageoptions.GifOptions(); gifOptions.setSource(new com.aspose.imaging.sources.FileCreateSource(dir + "output.gif", false)); // Create an instance of Image com.aspose.imaging.RasterImage image = (com.aspose.imaging.RasterImage) com.aspose.imaging.Image.create(gifOptions, 500, 500); try { // Get the pixels of image by specifying the area as image boundary com.aspose.imaging.Color[] pixels = image.loadPixels(image.getBounds()); // Loop over the array and sets color of alternative indexed pixel for (int index = 0; index < pixels.length; index++) { if (index % 2 == 0) { // Set the indexed pixel color to yellow pixels[index] = com.aspose.imaging.Color.getYellow(); } else { // Set the indexed pixel color to blue pixels[index] = com.aspose.imaging.Color.getBlue(); } } // Apply the pixel changes to the image image.savePixels(image.getBounds(), pixels); // Save all changes. image.save(); } finally { image.dispose(); }
Modifier and Type | Method and Description |
---|---|
void |
adjustBrightness(int brightness)
Adjust of a brightness for image.
|
void |
adjustContrast(float contrast)
Image contrasting
|
void |
adjustGamma(float gamma)
Gamma-correction of an image.
|
void |
adjustGamma(float gammaRed,
float gammaGreen,
float gammaBlue)
Gamma-correction of an image.
|
void |
binarizeBradley(double brightnessDifference)
Binarization of an image using Bradley's adaptive thresholding algorithm using the integral image thresholding
|
void |
binarizeBradley(double brightnessDifference,
int windowSize)
Binarization of an image using Bradley's adaptive thresholding algorithm using the integral image thresholding
|
void |
binarizeFixed(byte threshold)
Binarization of an image with predefined threshold
|
void |
binarizeOtsu()
Binarization of an image with Otsu thresholding
|
void |
crop(int leftShift,
int rightShift,
int topShift,
int bottomShift)
Crop image with shifts.
|
void |
crop(Rectangle rectangle)
Crops the specified rectangle.
|
void |
dither(int ditheringMethod,
int bitsCount)
Performs dithering on the current image.
|
abstract void |
dither(int ditheringMethod,
int bitsCount,
IColorPalette customPalette)
Performs dithering on the current image.
|
void |
filter(Rectangle rectangle,
FilterOptionsBase options)
Filters the specified rectangle.
|
int |
getArgb32Pixel(int x,
int y)
Gets an image 32-bit ARGB pixel.
|
int[] |
getDefaultArgb32Pixels(Rectangle rectangle)
Gets the default 32-bit ARGB pixels array.
|
void |
getDefaultPixels(Rectangle rectangle,
IPartialArgb32PixelLoader partialPixelLoader)
Gets the default pixels array using partial pixel loader.
|
void |
getDefaultRawData(Rectangle rectangle,
IPartialRawDataLoader partialRawDataLoader,
RawDataSettings rawDataSettings)
Gets the default raw data array using partial pixel loader.
|
byte[] |
getDefaultRawData(Rectangle rectangle,
RawDataSettings rawDataSettings)
Gets the default raw data array.
|
double |
getHorizontalResolution()
Gets or sets the horizontal resolution, in pixels per inch, of this
RasterImage . |
float |
getImageOpacity()
Gets opacity of this image.
|
Date |
getModifyDate(boolean useDefault)
Gets the date and time the resource image was last modified.
|
Color |
getPixel(int x,
int y)
Gets an image pixel.
|
boolean |
getPremultiplyComponents()
Gets or sets a value indicating whether the image components must be premultiplied.
|
IColorConverter |
getRawCustomColorConverter()
Gets or sets the custom color converter
|
PixelDataFormat |
getRawDataFormat()
Gets the raw data format.
|
RawDataSettings |
getRawDataSettings()
Gets the current raw data settings.
|
int |
getRawFallbackIndex()
Gets or sets the fallback index to use when palette index is out of bounds
|
IIndexedColorConverter |
getRawIndexedColorConverter()
Gets or sets the indexed color converter
|
int |
getRawLineSize()
Gets the raw line size in bytes.
|
float |
getSkewAngle()
Gets the skew angle.
|
Color |
getTransparentColor()
Gets the image transparent color.
|
boolean |
getUpdateXmpData()
Gets or sets a value indicating whether to update the XMP metadata.
|
boolean |
getUseRawData()
Gets or sets a value indicating whether to use raw data loading when the raw data loading is available.
|
double |
getVerticalResolution()
Gets or sets the vertical resolution, in pixels per inch, of this
RasterImage . |
XmpPacketWrapper |
getXmpData()
Gets or sets the XMP metadata.
|
void |
grayscale()
Transformation of an image to its grayscale representation
|
boolean |
hasAlpha()
Gets a value indicating whether this instance has alpha.
|
boolean |
hasTransparentColor()
Gets a value indicating whether image has transparent color.
|
boolean |
isRawDataAvailable()
Gets a value indicating whether raw data loading is available.
|
int[] |
loadArgb32Pixels(Rectangle rectangle)
Loads 32-bit ARGB pixels.
|
long[] |
loadArgb64Pixels(Rectangle rectangle)
Loads 64-bit ARGB pixels.
|
int[] |
loadCmyk32Pixels(Rectangle rectangle)
Loads pixels in CMYK format.
|
CmykColor[] |
loadCmykPixels(Rectangle rectangle)
Deprecated.
use instead loadCmyk32Pixels(Rectangle)
|
void |
loadPartialArgb32Pixels(Rectangle rectangle,
IPartialArgb32PixelLoader partialPixelLoader)
Loads 32-bit ARGB pixels partially by packs.
|
void |
loadPartialPixels(Rectangle desiredRectangle,
IPartialPixelLoader pixelLoader)
Loads pixels partially by packs.
|
Color[] |
loadPixels(Rectangle rectangle)
Loads pixels.
|
void |
loadRawData(Rectangle rectangle,
RawDataSettings rawDataSettings,
IPartialRawDataLoader rawDataLoader)
Loads raw image data using the partial processing mechanism.
|
void |
loadRawData(Rectangle rectangle,
Rectangle destImageBounds,
RawDataSettings rawDataSettings,
IPartialRawDataLoader rawDataLoader)
Loads raw data.
|
void |
normalizeAngle()
Normalizes the angle.
|
void |
normalizeAngle(boolean resizeProportionally,
Color backgroundColor)
Normalizes the angle.
|
int[] |
readArgb32ScanLine(int scanLineIndex)
Reads the whole scan line by the specified scan line index.
|
Color[] |
readScanLine(int scanLineIndex)
Reads the whole scan line by the specified scan line index.
|
void |
replaceColor(Color oldColor,
byte oldColorDiff,
Color newColor)
Replaces one color to another with allowed difference and preserves original alpha value to save smooth edges.
|
void |
replaceColor(int oldColorArgb,
byte oldColorDiff,
int newColorArgb)
Replaces one color to another with allowed difference and preserves original alpha value to save smooth edges.
|
void |
replaceNonTransparentColors(Color newColor)
Replaces all non-transparent colors with new color and preserves original alpha value to save smooth edges.
|
void |
replaceNonTransparentColors(int newColorArgb)
Replaces all non-transparent colors with new color and preserves original alpha value to save smooth edges.
|
void |
resize(int newWidth,
int newHeight,
ImageResizeSettings settings)
Resizes the image with extended options.
|
void |
resize(int newWidth,
int newHeight,
int resizeType)
Resizes the image.
|
void |
rotate(float angle)
Rotate image around the center.
|
void |
rotate(float angle,
boolean resizeProportionally,
Color backgroundColor)
Rotate image around the center.
|
void |
saveArgb32Pixels(Rectangle rectangle,
int[] pixels)
Saves the 32-bit ARGB pixels.
|
void |
saveCmyk32Pixels(Rectangle rectangle,
int[] pixels)
Saves the pixels.
|
void |
saveCmykPixels(Rectangle rectangle,
CmykColor[] pixels)
Deprecated.
use instead saveCmyk32Pixels(Rectangle, int[])
|
void |
savePixels(Rectangle rectangle,
Color[] pixels)
Saves the pixels.
|
void |
saveRawData(byte[] data,
int dataOffset,
Rectangle rectangle,
RawDataSettings rawDataSettings)
Saves the raw data.
|
void |
setArgb32Pixel(int x,
int y,
int argb32Color)
Sets an image 32-bit ARGB pixel for the specified position.
|
void |
setHorizontalResolution(double value)
Gets or sets the horizontal resolution, in pixels per inch, of this
RasterImage . |
void |
setPalette(IColorPalette palette,
boolean updateColors)
Sets the image palette.
|
void |
setPixel(int x,
int y,
Color color)
Sets an image pixel for the specified position.
|
void |
setPremultiplyComponents(boolean value)
Gets or sets a value indicating whether the image components must be premultiplied.
|
void |
setRawCustomColorConverter(IColorConverter value)
Gets or sets the custom color converter
|
void |
setRawFallbackIndex(int value)
Gets or sets the fallback index to use when palette index is out of bounds
|
void |
setRawIndexedColorConverter(IIndexedColorConverter value)
Gets or sets the indexed color converter
|
void |
setResolution(double dpiX,
double dpiY)
Sets the resolution for this
RasterImage . |
void |
setTransparentColor(boolean value)
Gets a value indicating whether image has transparent color.
|
void |
setTransparentColor(Color value)
Gets the image transparent color.
|
void |
setUpdateXmpData(boolean value)
Gets or sets a value indicating whether to update the XMP metadata.
|
void |
setUseRawData(boolean value)
Gets or sets a value indicating whether to use raw data loading when the raw data loading is available.
|
void |
setVerticalResolution(double value)
Gets or sets the vertical resolution, in pixels per inch, of this
RasterImage . |
void |
setXmpData(XmpPacketWrapper value)
Gets or sets the XMP metadata.
|
BufferedImage |
toBitmap()
Converts raster image to the bitmap.
|
void |
writeArgb32ScanLine(int scanLineIndex,
int[] argb32Pixels)
Writes the whole scan line to the specified scan line index.
|
void |
writeScanLine(int scanLineIndex,
Color[] pixels)
Writes the whole scan line to the specified scan line index.
|
canLoad, canLoad, canLoad, canLoad, canSave, create, getBackgroundColor, getBitsPerPixel, getBounds, getBufferSizeHint, getContainer, getDefaultOptions, getFileFormat, getFileFormat, getFileFormat, getFittingRectangle, getFittingRectangle, getHeight, getInterruptMonitor, getOriginalOptions, getPalette, getProgressEventHandler, getProgressEventHandlerInfo, getProportionalHeight, getProportionalWidth, getSize, getWidth, hasBackgroundColor, isAutoAdjustPalette, load, load, load, load, load, load, resize, resizeHeightProportionally, resizeHeightProportionally, resizeHeightProportionally, resizeWidthProportionally, resizeWidthProportionally, resizeWidthProportionally, rotateFlip, save, save, save, save, save, save, save, setAutoAdjustPalette, setBackgroundColor, setBackgroundColor, setBufferSizeHint, setInterruptMonitor, setPalette
cacheData, getDataStreamContainer, isCached, save, save, save, save
close, dispose, getDisposed
public boolean getPremultiplyComponents()
Gets or sets a value indicating whether the image components must be premultiplied.
true
if the image components must be premultiplied; otherwise, false
.public void setPremultiplyComponents(boolean value)
Gets or sets a value indicating whether the image components must be premultiplied.
value
- true
if the image components must be premultiplied; otherwise, false
.The following example creates a new raster image, saves the specified semi-transparent pixels, then loads those pixels and gets final colors in the premultiplied form.
int imageWidth = 3; int imageHeight = 2; com.aspose.imaging.Color[] colors = new com.aspose.imaging.Color[] { com.aspose.imaging.Color.fromArgb(127, 255, 0, 0), com.aspose.imaging.Color.fromArgb(127, 0, 255, 0), com.aspose.imaging.Color.fromArgb(127, 0, 0, 255), com.aspose.imaging.Color.fromArgb(127, 255, 255, 0), com.aspose.imaging.Color.fromArgb(127, 255, 0, 255), com.aspose.imaging.Color.fromArgb(127, 0, 255, 255), }; com.aspose.imaging.imageoptions.PngOptions createOptions = new com.aspose.imaging.imageoptions.PngOptions(); createOptions.setSource(new com.aspose.imaging.sources.StreamSource(new com.aspose.imaging.system.io.MemoryStream(), true)); createOptions.setColorType(com.aspose.imaging.fileformats.png.PngColorType.TruecolorWithAlpha); com.aspose.imaging.Image image = com.aspose.imaging.Image.create(createOptions, imageWidth, imageHeight); try { com.aspose.imaging.RasterImage rasterImage = (com.aspose.imaging.RasterImage) image; // Save pixels for the whole image. rasterImage.savePixels(rasterImage.getBounds(), colors); // The pixels are stored in the original image in the non-premultiplied form. // Need to specify the corresponding option explicitly to obtain premultiplied color components. // The premultiplied color components are calculated by the formulas: // red = original_red * alpha / 255; // green = original_green * alpha / 255; // blue = original_blue * alpha / 255; rasterImage.setPremultiplyComponents(true); com.aspose.imaging.Color[] premultipliedColors = rasterImage.loadPixels(rasterImage.getBounds()); for (int i = 0; i < colors.length; i++) { System.out.println("Original color: " + colors[i].toString()); System.out.println("Premultiplied color: " + premultipliedColors[i].toString()); } } finally { image.dispose(); }
public boolean getUseRawData()
Gets or sets a value indicating whether to use raw data loading when the raw data loading is available.
true
if use raw data loading when the raw data loading is available.; otherwise, false
.public void setUseRawData(boolean value)
Gets or sets a value indicating whether to use raw data loading when the raw data loading is available.
value
- true
if use raw data loading when the raw data loading is available.; otherwise, false
.public boolean getUpdateXmpData()
Gets or sets a value indicating whether to update the XMP metadata.
true
if update the XMP metadata; otherwise, false
.public void setUpdateXmpData(boolean value)
Gets or sets a value indicating whether to update the XMP metadata.
value
- true
if update the XMP metadata; otherwise, false
.public XmpPacketWrapper getXmpData()
Gets or sets the XMP metadata.
public void setXmpData(XmpPacketWrapper value)
Gets or sets the XMP metadata.
value
- The XMP metadata.public IIndexedColorConverter getRawIndexedColorConverter()
Gets or sets the indexed color converter
public void setRawIndexedColorConverter(IIndexedColorConverter value)
Gets or sets the indexed color converter
value
- The indexed color converterpublic IColorConverter getRawCustomColorConverter()
Gets or sets the custom color converter
public void setRawCustomColorConverter(IColorConverter value)
Gets or sets the custom color converter
value
- The custom color converterpublic int getRawFallbackIndex()
Gets or sets the fallback index to use when palette index is out of bounds
public void setRawFallbackIndex(int value)
Gets or sets the fallback index to use when palette index is out of bounds
value
- The fallback index to use when palette index is out of boundspublic RawDataSettings getRawDataSettings()
IRasterImageRawDataLoader
Gets the current raw data settings. Note when using these settings the data loads without conversion.
getRawDataSettings
in interface IRasterImageRawDataLoader
public PixelDataFormat getRawDataFormat()
Gets the raw data format.
The following example loads raster images and prints information about raw data format and alpha channel.
// The image files to load. String[] fileNames = new String[] { "c:\\temp\\sample.bmp", "c:\\temp\\alpha.png", }; for (String fileName : fileNames) { com.aspose.imaging.Image image = com.aspose.imaging.Image.load(fileName); try { com.aspose.imaging.RasterImage rasterImage = (com.aspose.imaging.RasterImage) image; System.out.println( "ImageFile=" + fileName + " FileFormat=" + rasterImage.getRawDataFormat() + " HasAlpha=" + rasterImage.hasAlpha()); } finally { image.dispose(); } } // The output may look like this: // ImageFile=c:\temp\sample.bmp FileFormat=Rgb24Bpp, used channels: 8,8,8 HasAlpha=false // ImageFile=c:\temp\alpha.png FileFormat=RGBA32Bpp, used channels: 8,8,8,8 HasAlpha=true
public int getRawLineSize()
Gets the raw line size in bytes.
public boolean isRawDataAvailable()
Gets a value indicating whether raw data loading is available.
isRawDataAvailable
in interface IRasterImageRawDataLoader
true
if this raw data loading is available; otherwise, false
.public double getHorizontalResolution()
Gets or sets the horizontal resolution, in pixels per inch, of this RasterImage
.
Note by default this value is always 96 since different platforms cannot return the screen resolution. You may consider using the SetResolution method for updating both resolution values in single call.
The following example shows how to set horizontal/vertical resolution of a raster image.
String dir = "c:\\temp\\"; com.aspose.imaging.Image image = com.aspose.imaging.Image.load(dir + "sample.jpg"); try { com.aspose.imaging.RasterImage rasterImage = (com.aspose.imaging.RasterImage) image; // Get horizontal and vertical resolution of the image double horizontalResolution = rasterImage.getHorizontalResolution(); double verticalResolution = rasterImage.getVerticalResolution(); System.out.println("The horizontal resolution, in pixels per inch: " + horizontalResolution); System.out.println("The vertical resolution, in pixels per inch: " + verticalResolution); if (horizontalResolution != 96.0 || verticalResolution != 96.0) { // Use the SetResolution method for updating both resolution values in a single call. System.out.println("Set resolution values to 96 dpi"); rasterImage.setResolution(96.0, 96.0); System.out.println("The horizontal resolution, in pixels per inch: " + rasterImage.getHorizontalResolution()); System.out.println("The vertical resolution, in pixels per inch: " + rasterImage.getVerticalResolution()); } // The output may look like this: // The horizontal resolution, in pixels per inch: 300.0 // The vertical resolution, in pixels per inch: 300.0 // Set resolution values to 96 dpi // The horizontal resolution, in pixels per inch: 96.0 // The vertical resolution, in pixels per inch: 96.0 } finally { image.dispose(); }
public void setHorizontalResolution(double value)
Gets or sets the horizontal resolution, in pixels per inch, of this RasterImage
.
value
- The horizontal resolution.
Note by default this value is always 96 since different platforms cannot return the screen resolution. You may consider using the SetResolution method for updating both resolution values in single call.
public double getVerticalResolution()
Gets or sets the vertical resolution, in pixels per inch, of this RasterImage
.
Note by default this value is always 96 since different platforms cannot return the screen resolution. You may consider using the SetResolution method for updating both resolution values in single call.
The following example shows how to set horizontal/vertical resolution of a raster image.
String dir = "c:\\temp\\"; com.aspose.imaging.Image image = com.aspose.imaging.Image.load(dir + "sample.jpg"); try { com.aspose.imaging.RasterImage rasterImage = (com.aspose.imaging.RasterImage) image; // Get horizontal and vertical resolution of the image double horizontalResolution = rasterImage.getHorizontalResolution(); double verticalResolution = rasterImage.getVerticalResolution(); System.out.println("The horizontal resolution, in pixels per inch: " + horizontalResolution); System.out.println("The vertical resolution, in pixels per inch: " + verticalResolution); if (horizontalResolution != 96.0 || verticalResolution != 96.0) { // Use the SetResolution method for updating both resolution values in a single call. System.out.println("Set resolution values to 96 dpi"); rasterImage.setResolution(96.0, 96.0); System.out.println("The horizontal resolution, in pixels per inch: " + rasterImage.getHorizontalResolution()); System.out.println("The vertical resolution, in pixels per inch: " + rasterImage.getVerticalResolution()); } // The output may look like this: // The horizontal resolution, in pixels per inch: 300.0 // The vertical resolution, in pixels per inch: 300.0 // Set resolution values to 96 dpi // The horizontal resolution, in pixels per inch: 96.0 // The vertical resolution, in pixels per inch: 96.0 } finally { image.dispose(); }
public void setVerticalResolution(double value)
Gets or sets the vertical resolution, in pixels per inch, of this RasterImage
.
value
- The vertical resolution.
Note by default this value is always 96 since different platforms cannot return the screen resolution. You may consider using the SetResolution method for updating both resolution values in single call.
public boolean hasTransparentColor()
Gets a value indicating whether image has transparent color.
public void setTransparentColor(boolean value)
Gets a value indicating whether image has transparent color.
public boolean hasAlpha()
Gets a value indicating whether this instance has alpha.
true
if this instance has alpha; otherwise, false
.The following example loads raster images and prints information about raw data format and alpha channel.
// The image files to load. String[] fileNames = new String[] { "c:\\temp\\sample.bmp", "c:\\temp\\alpha.png", }; for (String fileName : fileNames) { com.aspose.imaging.Image image = com.aspose.imaging.Image.load(fileName); try { com.aspose.imaging.RasterImage rasterImage = (com.aspose.imaging.RasterImage) image; System.out.println( "ImageFile=" + fileName + " FileFormat=" + rasterImage.getRawDataFormat() + " HasAlpha=" + rasterImage.hasAlpha()); } finally { image.dispose(); } } // The output may look like this: // ImageFile=c:\temp\sample.bmp FileFormat=Rgb24Bpp, used channels: 8,8,8 HasAlpha=false // ImageFile=c:\temp\alpha.png FileFormat=RGBA32Bpp, used channels: 8,8,8,8 HasAlpha=true
public Color getTransparentColor()
Gets the image transparent color.
public void setTransparentColor(Color value)
Gets the image transparent color.
public float getImageOpacity()
Gets opacity of this image.
public Date getModifyDate(boolean useDefault)
Gets the date and time the resource image was last modified.
useDefault
- if set to true
uses the information from FileInfo as default value.public void dither(int ditheringMethod, int bitsCount)
Performs dithering on the current image.
ditheringMethod
- The dithering method.bitsCount
- The final bits count for dithering.The following example loads a raster image and performs threshold and floyd dithering using different palette depth.
String dir = "c:\\temp\\"; com.aspose.imaging.Image image = com.aspose.imaging.Image.load(dir + "sample.png"); try { com.aspose.imaging.RasterImage rasterImage = (com.aspose.imaging.RasterImage) image; // Perform threshold dithering using 4-bit color palette which contains 16 colors. // The more bits specified the higher quality and the bigger size of the output image. // Note that only 1-bit, 4-bit and 8-bit palettes are supported at the moment. rasterImage.dither(com.aspose.imaging.DitheringMethod.ThresholdDithering, 4); rasterImage.save(dir + "sample.ThresholdDithering4.png"); } finally { image.dispose(); } image = com.aspose.imaging.Image.load(dir + "sample.png"); try { com.aspose.imaging.RasterImage rasterImage = (com.aspose.imaging.RasterImage) image; // Perform floyd dithering using 1-bit color palette which contains only 2 colors - black and white. // The more bits specified the higher quality and the bigger size of the output image. // Note that only 1-bit, 4-bit and 8-bit palettes are supported at the moment. rasterImage.dither(com.aspose.imaging.DitheringMethod.FloydSteinbergDithering, 1); rasterImage.save(dir + "sample.FloydSteinbergDithering1.png"); } finally { image.dispose(); }
public abstract void dither(int ditheringMethod, int bitsCount, IColorPalette customPalette)
Performs dithering on the current image.
ditheringMethod
- The dithering method.bitsCount
- The final bits count for dithering.customPalette
- The custom palette for dithering.public void getDefaultPixels(Rectangle rectangle, IPartialArgb32PixelLoader partialPixelLoader)
Gets the default pixels array using partial pixel loader.
rectangle
- The rectangle to get pixels for.partialPixelLoader
- The partial pixel loader.public void getDefaultRawData(Rectangle rectangle, IPartialRawDataLoader partialRawDataLoader, RawDataSettings rawDataSettings)
Gets the default raw data array using partial pixel loader.
rectangle
- The rectangle to get pixels for.partialRawDataLoader
- The partial raw data loader.rawDataSettings
- The raw data settings.public int[] getDefaultArgb32Pixels(Rectangle rectangle)
Gets the default 32-bit ARGB pixels array.
rectangle
- The rectangle to get pixels for.public byte[] getDefaultRawData(Rectangle rectangle, RawDataSettings rawDataSettings)
Gets the default raw data array.
rectangle
- The rectangle to get raw data for.rawDataSettings
- The raw data settings.public int getArgb32Pixel(int x, int y)
Gets an image 32-bit ARGB pixel.
x
- The pixel x location.y
- The pixel y location.The following example loads a raster image and obtains the color of an arbitrary pixel represented as a 32-bit integer value.
com.aspose.imaging.Image image = com.aspose.imaging.Image.load("c:\\temp\\sample.png"); try { com.aspose.imaging.RasterImage rasterImage = (com.aspose.imaging.RasterImage) image; // Get an integer representation of the color of the top-left pixel of the image. int color = rasterImage.getArgb32Pixel(0, 0); // To obtain the values of the individual color components, shift the color value by a corresponding number of bits int alpha = (color >> 24) & 0xff; int red = (color >> 16) & 0xff; int green = (color >> 8) & 0xff; int blue = (color >> 0) & 0xff; System.out.println("The color of the pixel(0,0) is A=" + alpha + ",R=" + red + ",G=" + green + ",B=" + blue); } finally { image.dispose(); } // The output may look like this: // The color of the pixel(0,0) is A=255,R=0,G=0,B=0
public Color getPixel(int x, int y)
Gets an image pixel.
x
- The pixel x location.y
- The pixel y location.The following example loads a raster image and obtains the color of an arbitrary pixel.
com.aspose.imaging.Image image = com.aspose.imaging.Image.load("c:\\temp\\sample.png"); try { com.aspose.imaging.RasterImage rasterImage = (com.aspose.imaging.RasterImage) image; // Get the color of the top-left pixel of the image. com.aspose.imaging.Color color = rasterImage.getPixel(0, 0); // Obtain the values of the individual color components int alpha = color.getA(); int red = color.getR(); int green = color.getG(); int blue = color.getB(); System.out.println("The color of the pixel(0,0) is A=" + alpha + ",R=" + red + ",G=" + green + ",B=" + blue); } finally { image.dispose(); }
public void setArgb32Pixel(int x, int y, int argb32Color)
Sets an image 32-bit ARGB pixel for the specified position.
x
- The pixel x location.y
- The pixel y location.argb32Color
- The 32-bit ARGB pixel for the specified position.The following example loads a raster image, and sets the color of an arbitrary pixel.
com.aspose.imaging.Image image = com.aspose.imaging.Image.load("c:\\temp\\sample.png"); try { com.aspose.imaging.RasterImage rasterImage = (com.aspose.imaging.RasterImage) image; // Sets the color of the top-left pixel. rasterImage.setArgb32Pixel(0, 0, com.aspose.imaging.Color.getAqua().toArgb()); // Another way is to pass an instance of the com.aspose.imaging.Color directly rasterImage.setPixel(0, 0, com.aspose.imaging.Color.getAqua()); } finally { image.dispose(); }
public void setPixel(int x, int y, Color color)
Sets an image pixel for the specified position.
x
- The pixel x location.y
- The pixel y location.color
- The pixel color for the specified position.The following example loads a raster image, and sets the color of an arbitrary pixel.
com.aspose.imaging.Image image = com.aspose.imaging.Image.load("c:\\temp\\sample.png"); try { com.aspose.imaging.RasterImage rasterImage = (com.aspose.imaging.RasterImage) image; // Sets the color of the top-left pixel. rasterImage.setArgb32Pixel(0, 0, com.aspose.imaging.Color.getAqua().toArgb()); // Another way is to pass an instance of the com.aspose.imaging.Color directly rasterImage.setPixel(0, 0, com.aspose.imaging.Color.getAqua()); } finally { image.dispose(); }
public Color[] readScanLine(int scanLineIndex)
Reads the whole scan line by the specified scan line index.
scanLineIndex
- Zero based index of the scan line.public int[] readArgb32ScanLine(int scanLineIndex)
Reads the whole scan line by the specified scan line index.
scanLineIndex
- Zero based index of the scan line.public void writeScanLine(int scanLineIndex, Color[] pixels)
Writes the whole scan line to the specified scan line index.
scanLineIndex
- Zero based index of the scan line.pixels
- The pixel colors array to write.public void writeArgb32ScanLine(int scanLineIndex, int[] argb32Pixels)
Writes the whole scan line to the specified scan line index.
scanLineIndex
- Zero based index of the scan line.argb32Pixels
- The 32-bit ARGB colors array to write.public final void loadPartialArgb32Pixels(Rectangle rectangle, IPartialArgb32PixelLoader partialPixelLoader)
Loads 32-bit ARGB pixels partially by packs.
loadPartialArgb32Pixels
in interface IRasterImageArgb32PixelLoader
rectangle
- The desired rectangle.partialPixelLoader
- The 32-bit ARGB pixel loader.The following example shows how to load and process pixels of a raster image using your own partial processor. For example, consider a problem of counting of fully transparent pixels of an image. In order to count transparent pixels using partial loading mechanism, a separate class TransparentArgb32PixelCounter implementing com.aspose.imaging.IPartialArgb32PixelLoader is introduced.
// First, implement com.aspose.imaging.IPartialArgb32PixelLoader to count all fully transparent pixels. /** Counts the number of fully transparent pixels with alpha channel value of 0. */ class TransparentArgb32PixelCounter implements com.aspose.imaging.IPartialArgb32PixelLoader { /** * The number of fully transparent pixels. */ private int count; /** * Gets the number of fully transparent pixels. */ public int getCount() { return this.count; } /** * <p>Processes the loaded pixels. This method is called back every time when a new portion of pixels is loaded.</p> * * * @param pixelsRectangle The pixels rectangle. * @param pixels The 32-bit ARGB pixels. * @param start The start pixels point. * @param end The end pixels point. */ public void process(com.aspose.imaging.Rectangle pixelsRectangle, int[] pixels, com.aspose.imaging.Point start, com.aspose.imaging.Point end) { for (int pixel : pixels) { int alpha = (pixel >> 24) & 0xff; if (alpha == 0) { this.count++; } } } } // Here is an example of using the counter. com.aspose.imaging.Image image = com.aspose.imaging.Image.load("c:\\temp\\alpha.png"); try { com.aspose.imaging.RasterImage rasterImage = (com.aspose.imaging.RasterImage) image; // Create an instance of the com.aspose.imaging.IPartialArgb32PixelLoader and pass it to the com.aspose.imaging.RasterImage.LoadPartialArgb32Pixels TransparentArgb32PixelCounter counter = new TransparentArgb32PixelCounter(); // Load pixels for the whole image. Any rectangular part of the image can be specified as the first parameter of the com.aspose.imaging.RasterImage.loadPartialArgb32Pixels method. rasterImage.loadPartialArgb32Pixels(rasterImage.getBounds(), counter); System.out.println("The number of fully transparent pixels is " + counter.getCount()); System.out.println("The total number of pixels is " + (image.getWidth() * image.getHeight())); } finally { image.dispose(); } // The output may look like this: // The number of fully transparent pixels is 55157 // The total number of pixels is 120400
public void loadPartialPixels(Rectangle desiredRectangle, IPartialPixelLoader pixelLoader)
Loads pixels partially by packs.
desiredRectangle
- The desired rectangle.pixelLoader
- The pixel loader.The following example shows how to load and process pixels of a raster image using your own partial processor. For example, consider a problem of counting of fully transparent pixels of an image. In order to count transparent using partial loading mechanism, a separate class TransparentPixelCounter implementing com.aspose.imaging.IPartialPixelLoader is introduced.
// First, implement com.aspose.imaging.IPartialPixelLoader to count all fully transparent pixels. /** Counts the number of fully transparent pixels with alpha channel value of 0. */ class TransparentPixelCounter implements com.aspose.imaging.IPartialPixelLoader { /** * The number of fully transparent pixels. */ private int count; /** * Gets the number of fully transparent pixels. */ public int getCount() { return this.count; } /** * <p>Processes the loaded pixels. This method is called back every time when a new portion of pixels is loaded.</p> * * @param pixelsRectangle The pixels rectangle. * @param pixels The 32-bit ARGB pixels. * @param start The start pixels point. * @param end The end pixels point. */ public void process(com.aspose.imaging.Rectangle pixelsRectangle, com.aspose.imaging.Color[] pixels, com.aspose.imaging.Point start, com.aspose.imaging.Point end) { for (com.aspose.imaging.Color pixel : pixels) { if (pixel.getA() == 0) { this.count++; } } } } // Here is an example of using the counter. com.aspose.imaging.Image image = com.aspose.imaging.Image.load("c:\\temp\\alpha.png"); try { com.aspose.imaging.RasterImage rasterImage = (com.aspose.imaging.RasterImage) image; // Create an instance of the com.aspose.imaging.IPartialPixelLoader and pass it to the com.aspose.imaging.RasterImage.loadPartialPixels TransparentPixelCounter counter = new TransparentPixelCounter(); // Load pixels for the whole image. Any rectangular part of the image can be specified as the first parameter of the com.aspose.imaging.RasterImage.loadPartialPixels method. rasterImage.loadPartialPixels(rasterImage.getBounds(), counter); System.out.println("The number of fully transparent pixels is " + counter.getCount()); System.out.println("The total number of pixels is " + (image.getWidth() * image.getHeight())); } finally { image.dispose(); } // The output may look like this: // The number of fully transparent pixels is 55157 // The total number of pixels is 120400
public int[] loadArgb32Pixels(Rectangle rectangle)
Loads 32-bit ARGB pixels.
rectangle
- The rectangle to load pixels from.The following example shows how to load and process pixels of a raster image. The pixels are represented as 32-bit integer values. For example, consider a problem of counting of fully transparent pixels of an image.
com.aspose.imaging.Image image = com.aspose.imaging.Image.load("c:\\temp\\alpha.png"); try { com.aspose.imaging.RasterImage rasterImage = (com.aspose.imaging.RasterImage) image; // Load pixels for the whole image. Any rectangular part of the image can be specified as a parameter of the com.aspose.imaging.RasterImage.loadArgb32Pixels method. int[] pixels = rasterImage.loadArgb32Pixels(rasterImage.getBounds()); int count = 0; for (int pixel : pixels) { int alpha = (pixel >> 24) & 0xff; if (alpha == 0) { count++; } } System.out.println("The number of fully transparent pixels is " + count); System.out.println("The total number of pixels is " + (image.getWidth() * image.getHeight())); } finally { image.dispose(); }
public long[] loadArgb64Pixels(Rectangle rectangle)
Loads 64-bit ARGB pixels.
rectangle
- The rectangle to load pixels from.The following example shows how to load and process pixels of a raster image. The pixels are represented as 64-bit integer values. For example, consider a problem of counting of fully transparent pixels of an image.
com.aspose.imaging.Image image = com.aspose.imaging.Image.load("c:\\temp\\16rgba.png"); try { com.aspose.imaging.RasterImage rasterImage = (com.aspose.imaging.RasterImage) image; // Load pixels for the whole image. Any rectangular part of the image can be specified as a parameter of the com.aspose.imaging.RasterImage.loadArgb64Pixels method. // Note that the image itself must have 16 bits per sample, because com.aspose.imaging.RasterImage.loadArgb64Pixels doesn't work with 8 bit per sample. // In order to work with 8 bits per sample please use the good old com.aspose.imaging.RasterImage.loadArgb32Pixels method. long[] pixels = rasterImage.loadArgb64Pixels(rasterImage.getBounds()); int count = 0; for (long pixel : pixels) { // Note that all color components including alpha are represented by 16-bit values, so their allowed values are in the range [0, 63535]. long alpha = (pixel >> 48) & 0xffff; if (alpha == 0) { count++; } } System.out.println("The number of fully transparent pixels is " + count); System.out.println("The total number of pixels is " + (image.getWidth() * image.getHeight())); } finally { image.dispose(); }
public Color[] loadPixels(Rectangle rectangle)
Loads pixels.
rectangle
- The rectangle to load pixels from.The following example shows how to load and process pixels of a raster image. For example, consider a problem of counting of fully transparent pixels of an image.
com.aspose.imaging.Image image = com.aspose.imaging.Image.load("c:\\temp\\alpha.png"); try { com.aspose.imaging.RasterImage rasterImage = (com.aspose.imaging.RasterImage) image; // Load pixels for the whole image. Any rectangular part of the image can be specified as a parameter of the Aspose.Imaging.RasterImage.LoadPixels method. com.aspose.imaging.Color[] pixels = rasterImage.loadPixels(rasterImage.getBounds()); int count = 0; for (com.aspose.imaging.Color pixel : pixels) { if (pixel.getA() == 0) { count++; } } System.out.println("The number of fully transparent pixels is " + count); System.out.println("The total number of pixels is " + (image.getWidth() * image.getHeight())); } finally { image.dispose(); }
@Deprecated public CmykColor[] loadCmykPixels(Rectangle rectangle)
Loads pixels in CMYK format.
This method is deprecated. Please use more effective the loadCmyk32Pixels(Rectangle)
method.
rectangle
- The rectangle to load pixels from.public int[] loadCmyk32Pixels(Rectangle rectangle)
Loads pixels in CMYK format.
rectangle
- The rectangle to load pixels from.public void loadRawData(Rectangle rectangle, RawDataSettings rawDataSettings, IPartialRawDataLoader rawDataLoader)
Loads raw image data using the partial processing mechanism.
loadRawData
in interface IRasterImageRawDataLoader
rectangle
- The desired rectangular area of the image to load data from.rawDataSettings
- The raw data settings.rawDataLoader
- The raw data loader.The following example shows how to extract pixels from the raw image data using RawDataSettings. For example, consider a problem of counting of fully transparent pixels of an image.
// First, implement a counter. In case of raw data, the counter may look like this: /** Counts the number of fully transparent pixels with alpha channel value of 0. */ class TransparentPixelRawDataCounter implements com.aspose.imaging.IPartialRawDataLoader { /** * The number of fully transparent pixels. */ private int count; /** * The raw data settings of the loaded image. */ private com.aspose.imaging.RawDataSettings rawDataSettings; /** * Gets the number of fully transparent pixels. */ public int getCount() { return this.count; } /** * <p>Initializes a new instance of the <see TransparentPixelRawDataCounter /> class.</p> * * @param settings The raw data settings allow to extract color components from raw data. */ public TransparentPixelRawDataCounter(com.aspose.imaging.RawDataSettings settings) { this.rawDataSettings = settings; this.count = 0; } /** * <p>Processes the loaded raw data. This method is called back every time when a new portion of raw data is loaded.</p> * * @param dataRectangle The raw data rectangle. * @param data The raw data. * @param start The start data point. * @param end The end data point. */ public void process(com.aspose.imaging.Rectangle dataRectangle, byte[] data, com.aspose.imaging.Point start, com.aspose.imaging.Point end)// throws java.lang.Exception { int[] channelBits = this.rawDataSettings.getPixelDataFormat().getChannelBits(); // Only simple formats are considered here to simplify the code. // Let's consider only images with 8 bits per sample. for (int i = 0; i < channelBits.length; i++) { if (channelBits[i] != 8) { throw new java.lang.UnsupportedOperationException(); } } switch (this.rawDataSettings.getPixelDataFormat().getPixelFormat()) { case com.aspose.imaging.PixelFormat.Rgb: case com.aspose.imaging.PixelFormat.Bgr: { if (channelBits.length == 4) { // ARGB for (int i = 0; i < data.length; i += 4) { // The alpha channel is stored last, after the color components. if (data[i + 3] == 0) { this.count++; } } } } break; case com.aspose.imaging.PixelFormat.Grayscale: { if (channelBits.length == 2) { // Grayscale Alpha for (int i = 0; i < data.length; i += 2) { // The alpha channel is stored last, after the color components. if (data[i + 1] == 0) { this.count++; } } } } break; default: throw new java.lang.IllegalArgumentException("PixelFormat"); } } /** * <p>Processes the loaded raw data. This method is called back every time when a new portion of raw data is loaded.</p> * * * @param dataRectangle The raw data rectangle. * @param data The raw data. * @param start The start data point. * @param end The end data point. * @param loadOptions The load options. */ public void process(com.aspose.imaging.Rectangle dataRectangle, byte[] data, com.aspose.imaging.Point start, com.aspose.imaging.Point end, com.aspose.imaging.LoadOptions loadOptions) { this.process(dataRectangle, data, start, end); } } // Here is the main example of using the counter com.aspose.imaging.Image image = com.aspose.imaging.Image.load("c:\\temp\\alpha.png"); try { com.aspose.imaging.RasterImage rasterImage = (com.aspose.imaging.RasterImage) image; com.aspose.imaging.RawDataSettings settings = rasterImage.getRawDataSettings(); TransparentPixelRawDataCounter rawDataLoader = new TransparentPixelRawDataCounter(settings); // Load pixels for the whole image. Any rectangular part of the image can be specified as a parameter of the Aspose.Imaging.RasterImage.LoadRawData method. rasterImage.loadRawData(rasterImage.getBounds(), settings, rawDataLoader); System.out.println("The number of fully transparent pixels is " + rawDataLoader.getCount()); System.out.println("The total number of pixels is " + (image.getWidth() * image.getHeight())); } finally { image.dispose(); } // The output may look like this: // The number of fully transparent pixels is 55157 // The total number of pixels is 120400
public void loadRawData(Rectangle rectangle, Rectangle destImageBounds, RawDataSettings rawDataSettings, IPartialRawDataLoader rawDataLoader)
Loads raw data.
rectangle
- The rectangle to load raw data from.destImageBounds
- The dest image bounds.rawDataSettings
- The raw data settings to use for loaded data. Note if data is not in the format specified then data conversion will be performed.rawDataLoader
- The raw data loader.public void saveRawData(byte[] data, int dataOffset, Rectangle rectangle, RawDataSettings rawDataSettings)
Saves the raw data.
data
- The raw data.dataOffset
- The starting raw data offset.rectangle
- The raw data rectangle.rawDataSettings
- The raw data settings the data is in.public void saveArgb32Pixels(Rectangle rectangle, int[] pixels)
Saves the 32-bit ARGB pixels.
saveArgb32Pixels
in interface com.aspose.imaging_internal.IPixelsSaver
rectangle
- The rectangle to save pixels to.pixels
- The 32-bit ARGB pixels array.The following example fills the central area of a raster image with black pixels using the com.aspose.imaging.RasterImage.saveArgb32Pixels method.
String dir = "c:\\temp\\"; com.aspose.imaging.Image image = com.aspose.imaging.Image.load(dir + "sample.png"); try { com.aspose.imaging.RasterImage rasterImage = (com.aspose.imaging.RasterImage) image; // The black square int[] pixels = new int[(rasterImage.getWidth() / 2) * (rasterImage.getHeight() / 2)]; for (int i = 0; i < pixels.length; i++) { pixels[i] = com.aspose.imaging.Color.getBlack().toArgb(); } // Draw the black square at the center of the image. com.aspose.imaging.Rectangle area = new com.aspose.imaging.Rectangle(rasterImage.getWidth() / 4, rasterImage.getHeight() / 4, rasterImage.getWidth() / 2, rasterImage.getHeight() / 2); rasterImage.saveArgb32Pixels(area, pixels); rasterImage.save(dir + "sample.SaveArgb32Pixels.png"); } finally { image.dispose(); }
public void savePixels(Rectangle rectangle, Color[] pixels)
Saves the pixels.
rectangle
- The rectangle to save pixels to.pixels
- The pixels array.The following example fills the central area of a raster image with black pixels using the com.aspose.imaging.RasterImage.savePixels method.
String dir = "c:\\temp\\"; com.aspose.imaging.Image image = com.aspose.imaging.Image.load(dir + "sample.png"); try { com.aspose.imaging.RasterImage rasterImage = (com.aspose.imaging.RasterImage) image; // The black square com.aspose.imaging.Color[] pixels = new com.aspose.imaging.Color[(rasterImage.getWidth() / 2) * (rasterImage.getHeight() / 2)]; for (int i = 0; i < pixels.length; i++) { pixels[i] = com.aspose.imaging.Color.getBlack(); } // Draw the black square at the center of the image. com.aspose.imaging.Rectangle area = new com.aspose.imaging.Rectangle(rasterImage.getWidth() / 4, rasterImage.getHeight() / 4, rasterImage.getWidth() / 2, rasterImage.getHeight() / 2); rasterImage.savePixels(area, pixels); rasterImage.save(dir + "sample.SavePixels.png"); } finally { image.dispose(); }
public BufferedImage toBitmap()
Converts raster image to the bitmap.
The following example converts a BMP image to a native Java bitmap.
com.aspose.imaging.Image image = com.aspose.imaging.Image.load("c:\\temp\\sample.bmp"); try { com.aspose.imaging.fileformats.bmp.BmpImage bmpImage = (com.aspose.imaging.fileformats.bmp.BmpImage) image; java.awt.image.BufferedImage bitmap = bmpImage.toBitmap(); // Process the native Java bitmap. } finally { image.dispose(); }
@Deprecated public void saveCmykPixels(Rectangle rectangle, CmykColor[] pixels)
Saves the pixels.
This method is deprecated. Please use more effective the saveCmyk32Pixels(Rectangle, int[])
method.
rectangle
- The rectangle to save pixels to.pixels
- The CMYK pixels array.public void saveCmyk32Pixels(Rectangle rectangle, int[] pixels)
Saves the pixels.
rectangle
- The rectangle to save pixels to.pixels
- The CMYK pixels presented as the 32-bit integer values.The following example fills the central area of a raster image with black pixels using the com.aspose.imaging.RasterImage.saveCmyk32Pixels method.
String dir = "c:\\temp\\"; com.aspose.imaging.Image image = com.aspose.imaging.Image.load(dir + "sample.png"); try { com.aspose.imaging.RasterImage rasterImage = (com.aspose.imaging.RasterImage) image; // Get an integer representation of black in the CMYK color space. int blackCmyk = com.aspose.imaging.CmykColorHelper.toCmyk(com.aspose.imaging.Color.getBlack()); // The black square. int[] pixels = new int[(rasterImage.getWidth() / 2) * (rasterImage.getHeight() / 2)]; for (int i = 0; i < pixels.length; i++) { pixels[i] = blackCmyk; } // Draw the black square at the center of the image. com.aspose.imaging.Rectangle area = new com.aspose.imaging.Rectangle(rasterImage.getWidth() / 4, rasterImage.getHeight() / 4, rasterImage.getWidth() / 2, rasterImage.getHeight() / 2); rasterImage.saveCmyk32Pixels(area, pixels); rasterImage.save(dir + "sample.SaveCmyk32Pixels.png"); } finally { image.dispose(); }
public void setResolution(double dpiX, double dpiY)
Sets the resolution for this RasterImage
.
dpiX
- The horizontal resolution, in dots per inch, of the RasterImage
.dpiY
- The vertical resolution, in dots per inch, of the RasterImage
.The following example shows how to set horizontal/vertical resolution of a raster image.
String dir = "c:\\temp\\"; com.aspose.imaging.Image image = com.aspose.imaging.Image.load(dir + "sample.jpg"); try { com.aspose.imaging.RasterImage rasterImage = (com.aspose.imaging.RasterImage) image; // Get horizontal and vertical resolution of the image double horizontalResolution = rasterImage.getHorizontalResolution(); double verticalResolution = rasterImage.getVerticalResolution(); System.out.println("The horizontal resolution, in pixels per inch: " + horizontalResolution); System.out.println("The vertical resolution, in pixels per inch: " + verticalResolution); if (horizontalResolution != 96.0 || verticalResolution != 96.0) { // Use the SetResolution method for updating both resolution values in a single call. System.out.println("Set resolution values to 96 dpi"); rasterImage.setResolution(96.0, 96.0); System.out.println("The horizontal resolution, in pixels per inch: " + rasterImage.getHorizontalResolution()); System.out.println("The vertical resolution, in pixels per inch: " + rasterImage.getVerticalResolution()); } // The output may look like this: // The horizontal resolution, in pixels per inch: 300.0 // The vertical resolution, in pixels per inch: 300.0 // Set resolution values to 96 dpi // The horizontal resolution, in pixels per inch: 96.0 // The vertical resolution, in pixels per inch: 96.0 } finally { image.dispose(); }
public void setPalette(IColorPalette palette, boolean updateColors)
Sets the image palette.
setPalette
in class Image
palette
- The palette to set.updateColors
- if set to true
colors will be updated according to the new palette; otherwise color indexes remain unchanged. Note that unchanged indexes may crash the image on loading if some indexes have no corresponding palette entries.public void resize(int newWidth, int newHeight, int resizeType)
Resizes the image.
resize
in class Image
newWidth
- The new width.newHeight
- The new height.resizeType
- The resize type.This example loads a raster image and resizes it using various resizing methods.
String dir = "c:\\temp\\"; com.aspose.imaging.RasterImage image = (com.aspose.imaging.RasterImage) com.aspose.imaging.Image.load(dir + "sample.gif"); try { // Scale up by 2 times using Nearest Neighbour resampling. image.resize(image.getWidth() * 2, image.getHeight() * 2, com.aspose.imaging.ResizeType.NearestNeighbourResample); image.save(dir + "upsample.nearestneighbour.gif"); } finally { image.dispose(); } image = (com.aspose.imaging.RasterImage) com.aspose.imaging.Image.load(dir + "sample.gif"); try { // Scale down by 2 times using Nearest Neighbour resampling. image.resize(image.getWidth() / 2, image.getHeight() / 2, com.aspose.imaging.ResizeType.NearestNeighbourResample); image.save(dir + "downsample.nearestneighbour.gif"); } finally { image.dispose(); } image = (com.aspose.imaging.RasterImage) com.aspose.imaging.Image.load(dir + "sample.gif"); try { // Scale up by 2 times using Bilinear resampling. image.resize(image.getWidth() * 2, image.getHeight() * 2, com.aspose.imaging.ResizeType.BilinearResample); image.save(dir + "upsample.bilinear.gif"); } finally { image.dispose(); } image = (com.aspose.imaging.RasterImage) com.aspose.imaging.Image.load(dir + "sample.gif"); try { // Scale down by 2 times using Bilinear resampling. image.resize(image.getWidth() / 2, image.getHeight() / 2, com.aspose.imaging.ResizeType.BilinearResample); image.save(dir + "downsample.bilinear.gif"); } finally { image.dispose(); }
public void resize(int newWidth, int newHeight, ImageResizeSettings settings)
Resizes the image with extended options.
resize
in class Image
newWidth
- The new width.newHeight
- The new height.settings
- The resize settings.This example loads a raster 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.RasterImage image = (com.aspose.imaging.RasterImage) com.aspose.imaging.Image.load(dir + "sample.gif"); try { // Scale down by 2 times using adaptive resampling. image.resize(image.getWidth() / 2, image.getHeight() / 2, resizeSettings); image.save(dir + "downsample.adaptive.gif"); } finally { image.dispose(); }
public void crop(Rectangle rectangle)
Crops the specified rectangle.
rectangle
- The rectangle.The following example crops a raster 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.RasterImage rasterImage = (com.aspose.imaging.RasterImage) image; // Crop the image. The cropping area is the rectangular central area of the image. com.aspose.imaging.Rectangle area = new com.aspose.imaging.Rectangle(rasterImage.getWidth() / 4, rasterImage.getHeight() / 4, rasterImage.getWidth() / 2, rasterImage.getHeight() / 2); rasterImage.crop(area); // Save the cropped image to PNG rasterImage.save(dir + "sample.Crop.png"); } finally { image.dispose(); }
public void binarizeFixed(byte threshold)
Binarization of an image with predefined threshold
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.The following example binarizes a raster 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.RasterImage rasterImage = (com.aspose.imaging.RasterImage) 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(); }
public void binarizeOtsu()
Binarization of an image with Otsu thresholding
The following example binarizes a raster 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.RasterImage rasterImage = (com.aspose.imaging.RasterImage) image; // Binarize the image with Otsu thresholding. rasterImage.binarizeOtsu(); rasterImage.save(dir + "sample.BinarizeOtsu.png"); } finally { image.dispose(); }
public void binarizeBradley(double brightnessDifference)
Binarization of an image using Bradley's adaptive thresholding algorithm using the integral image thresholding
brightnessDifference
- The brightness difference between pixel and the average of an s x s window of pixels centered around this pixel.public void binarizeBradley(double brightnessDifference, int windowSize)
Binarization of an image using Bradley's adaptive thresholding algorithm using the integral image thresholding
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 pixelThe following example binarizes a raster 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.RasterImage rasterImage = (com.aspose.imaging.RasterImage) 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(); }
public void grayscale()
Transformation of an image to its grayscale representation
The following example transforms a colored raster 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.RasterImage rasterImage = (com.aspose.imaging.RasterImage) image; rasterImage.grayscale(); rasterImage.save(dir + "sample.Grayscale.png"); } finally { image.dispose(); }
public void adjustBrightness(int brightness)
Adjust of a brightness for image.
brightness
- Brightness value.The following example performs brightness correction of an image.
String dir = "c:\\temp\\"; com.aspose.imaging.Image image = com.aspose.imaging.Image.load(dir + "sample.png"); try { com.aspose.imaging.RasterImage rasterImage = (com.aspose.imaging.RasterImage) 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(); }
public void adjustContrast(float contrast)
Image contrasting
contrast
- Contrast value (in range [-100; 100])The following example performs contrast correction of an image.
String dir = "c:\\temp\\"; com.aspose.imaging.Image image = com.aspose.imaging.Image.load(dir + "sample.png"); try { com.aspose.imaging.RasterImage rasterImage = (com.aspose.imaging.RasterImage) 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(); }
public void adjustGamma(float gammaRed, float gammaGreen, float gammaBlue)
Gamma-correction of an image.
gammaRed
- Gamma for red channel coefficientgammaGreen
- Gamma for green channel coefficientgammaBlue
- Gamma for blue channel coefficientThe following example performs gamma-correction of an 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.RasterImage rasterImage = (com.aspose.imaging.RasterImage) 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(); }
public void adjustGamma(float gamma)
Gamma-correction of an image.
gamma
- Gamma for red, green and blue channels coefficientThe following example performs gamma-correction of an image.
String dir = "c:\\temp\\"; com.aspose.imaging.Image image = com.aspose.imaging.Image.load(dir + "sample.png"); try { com.aspose.imaging.RasterImage rasterImage = (com.aspose.imaging.RasterImage) image; // Set gamma coefficient for red, green and blue channels. rasterImage.adjustGamma(2.5f); rasterImage.save(dir + "sample.AdjustGamma.png"); } finally { image.dispose(); }
public void crop(int leftShift, int rightShift, int topShift, int bottomShift)
Crop image with shifts.
leftShift
- The left shift.rightShift
- The right shift.topShift
- The top shift.bottomShift
- The bottom shift.The following example crops a raster image. The cropping area is specified via Left, Top, Right, Bottom margins.
String dir = "c:\\temp\\"; com.aspose.imaging.Image image = com.aspose.imaging.Image.load(dir + "sample.png"); try { com.aspose.imaging.RasterImage rasterImage = (com.aspose.imaging.RasterImage) image; // Crop again. Set a margin of 10% of the image size. int horizontalMargin = rasterImage.getWidth() / 10; int verticalMargin = rasterImage.getHeight() / 10; rasterImage.crop(horizontalMargin, horizontalMargin, verticalMargin, verticalMargin); // Save the cropped image to PNG. rasterImage.save(dir + "sample.Crop.png"); } finally { image.dispose(); }
public void rotate(float angle, boolean resizeProportionally, Color backgroundColor)
Rotate image around the center.
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.com.aspose.ms.System.NotImplementedException
public void rotate(float angle)
Rotate image around the center.
angle
- The rotate angle in degrees. Positive values will rotate clockwise.public final float getSkewAngle()
Gets the skew angle. This method is applicable to scanned text documents, to determine the skew angle when scanning.
public final void normalizeAngle()
Normalizes the angle.
This method is applicable to scanned text documents to get rid of the skewed scan.
This method uses RasterImage.getSkewAngle()
and RasterImage.rotate(float)
methods.
public void normalizeAngle(boolean resizeProportionally, Color backgroundColor)
Normalizes the angle.
This method is applicable to scanned text documents to get rid of the skewed scan.
This method uses RasterImage.getSkewAngle()
and RasterImage.rotate(float, boolean, Color)
methods.
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.Skew is an artifact that might appear during document scanning process when the text/images of the document get rotated at a slight angle. It can have various causes but the most common is that the paper get misplaced during a scan. Therefore, deskew is the process of detecting and fixing this issue on scanned files(i.e. bitmap) so deskewed documents will have the text/images correctly and horizontally adjusted.
String dir = "c:\\aspose.imaging\\issues\\java\\1461\\"; String inputFilePath = dir + "skewed.png"; String outputFilePath = dir + "skewed.out.png"; // Get rid of the skewed scan with default parameters com.aspose.imaging.RasterImage image = (com.aspose.imaging.RasterImage) com.aspose.imaging.Image.load(inputFilePath); try { // Deskew image.normalizeAngle(false /*do not resize*/, com.aspose.imaging.Color.getLightGray() /*background color*/); image.save(outputFilePath); } finally { image.close(); }
public void filter(Rectangle rectangle, FilterOptionsBase options)
Filters the specified rectangle.
rectangle
- The rectangle.options
- The options.The following example applies various types of filters to a raster image.
String dir = "c:\\temp\\"; com.aspose.imaging.Image image = com.aspose.imaging.Image.load(dir + "sample.png"); try { com.aspose.imaging.RasterImage rasterImage = (com.aspose.imaging.RasterImage) image; // Apply a median filter with a rectangle size of 5 to the entire image. rasterImage.filter(rasterImage.getBounds(), new com.aspose.imaging.imagefilters.filteroptions.MedianFilterOptions(5)); rasterImage.save(dir + "sample.MedianFilter.png"); } finally { image.dispose(); } image = com.aspose.imaging.Image.load(dir + "sample.png"); try { com.aspose.imaging.RasterImage rasterImage = (com.aspose.imaging.RasterImage) image; // Apply a bilateral smoothing filter with a kernel size of 5 to the entire image. rasterImage.filter(rasterImage.getBounds(), new com.aspose.imaging.imagefilters.filteroptions.BilateralSmoothingFilterOptions(5)); rasterImage.save(dir + "sample.BilateralSmoothingFilter.png"); } finally { image.dispose(); } image = com.aspose.imaging.Image.load(dir + "sample.png"); try { com.aspose.imaging.RasterImage rasterImage = (com.aspose.imaging.RasterImage) image; // Apply a Gaussian blur filter with a radius of 5 and a sigma value of 4.0 to the entire image. rasterImage.filter(rasterImage.getBounds(), new com.aspose.imaging.imagefilters.filteroptions.GaussianBlurFilterOptions(5, 4.0)); rasterImage.save(dir + "sample.GaussianBlurFilter.png"); } finally { image.dispose(); } image = com.aspose.imaging.Image.load(dir + "sample.png"); try { com.aspose.imaging.RasterImage rasterImage = (com.aspose.imaging.RasterImage) image; // Apply a Gauss-Wiener filter with a radius of 5 and a smooth value of 4.0 to the entire image. rasterImage.filter(rasterImage.getBounds(), new com.aspose.imaging.imagefilters.filteroptions.GaussWienerFilterOptions(5, 4.0)); rasterImage.save(dir + "sample.GaussWienerFilter.png"); } finally { image.dispose(); } image = com.aspose.imaging.Image.load(dir + "sample.png"); try { com.aspose.imaging.RasterImage rasterImage = (com.aspose.imaging.RasterImage) image; // Apply a motion wiener filter with a length of 5, a smooth value of 4.0 and an angle of 90.0 degrees to the entire image. rasterImage.filter(rasterImage.getBounds(), new com.aspose.imaging.imagefilters.filteroptions.MotionWienerFilterOptions(10, 1.0, 90.0)); rasterImage.save(dir + "sample.MotionWienerFilter.png"); } finally { image.dispose(); } image = com.aspose.imaging.Image.load(dir + "sample.png"); try { com.aspose.imaging.RasterImage rasterImage = (com.aspose.imaging.RasterImage) image; // Apply a sharpen filter with a kernel size of 5 and a sigma value of 4.0 to the entire image. rasterImage.filter(rasterImage.getBounds(), new com.aspose.imaging.imagefilters.filteroptions.SharpenFilterOptions(5, 4.0)); rasterImage.save(dir + "sample.SharpenFilter.png"); } finally { image.dispose(); }
public void replaceColor(Color oldColor, byte oldColorDiff, Color newColor)
Replaces one color to another with allowed difference and preserves original alpha value to save smooth edges.
oldColor
- Old color to be replaced.oldColorDiff
- Allowed difference in old color to be able to widen replaced color tone.newColor
- New color to replace old color with.public void replaceColor(int oldColorArgb, byte oldColorDiff, int newColorArgb)
Replaces one color to another with allowed difference and preserves original alpha value to save smooth edges.
oldColorArgb
- Old color ARGB value to be replaced.oldColorDiff
- Allowed difference in old color to be able to widen replaced color tone.newColorArgb
- New color ARGB value to replace old color with.public void replaceNonTransparentColors(Color newColor)
Replaces all non-transparent colors with new color and preserves original alpha value to save smooth edges. Note: if you use it on images without transparency, all colors will be replaced with a single one.
newColor
- New color to replace non transparent colors with.public void replaceNonTransparentColors(int newColorArgb)
Replaces all non-transparent colors with new color and preserves original alpha value to save smooth edges. Note: if you use it on images without transparency, all colors will be replaced with a single one.
newColorArgb
- New color ARGB value to replace non transparent colors with.