Packages

 

com.aspose.psd.fileformats.psd

Class PsdImage

  • All Implemented Interfaces:
    IObjectWithBounds, IRasterImageArgb32PixelLoader, IRasterImageRawDataLoader


    public final class PsdImage
    extends RasterCachedImage

    Defines the PsdImage class that provides the ability to load, edit, save PSD files as well as update properties, add watermarks, perform graphics operations or convert one file format to another. Aspose.PSD supports import as a layer and export to the following formats: Png, Jpeg, Jpeg2000, Gif, Bmp, Tiff, Psd, Psb along with export to Pdf with selectable text

    Code example:

    An example of support of layer masks for layer groups. The program loads and saves PSD to different output formats without throwing exceptions.


    String srcFile = "psdnet595.psd";
    String outputPng = "output.png";
    String outputPsd = "output.psd";
                
    // Load a predefined PSD containing layer masks for layer groups
    PsdImage input = (PsdImage)Image.load(srcFile);
    try
    {
        // Convert loaded PSD to PNG
        input.save(outputPng, new PngOptions());
        // Save a copy of the PSD
        input.save(outputPsd);
    }
    finally
    {
        input.dispose();
    }
    

    • Field Detail

      • DefaultVersion

        public static final int DefaultVersion

        The default PSD version.

        See Also:
        Constant Field Values
    • Constructor Detail

      • PsdImage

        public PsdImage(String path)

        Initializes a new instance of the PsdImage class from specified path from raster image (not psd image in path). Used to initialize psd image with default parameters - Color mode - rgb, 4 channels, 8 bit per channel, Compression - Raw.

        Parameters:
        path - The path to load pixel and palette data from and initialize with.
      • PsdImage

        public PsdImage(String path,
                        short colorMode,
                        short channelBitDepth,
                        short channels,
                        int psdVersion,
                        short compression)

        Initializes a new instance of the PsdImage class from specified path from raster image (not psd image in path) with constructor parameters.

        Parameters:
        path - The path to load pixel and palette data from and initialize with.
        colorMode - The color mode.
        channelBitDepth - The PSD bit depth per channel.
        channels - The PSD channels count.
        psdVersion - The PSD version.
        compression - The compression to use.
      • PsdImage

        public PsdImage(InputStream stream)

        Initializes a new instance of the PsdImage class from specified path from raster image (not psd image in stream). Used to initialize psd image with default parameters - Color mode - rgb, 4 channels, 8 bit per channel, Compression - Raw.

        Parameters:
        stream - The stream to load pixel and palette data from and initialize with.
      • PsdImage

        public PsdImage(InputStream stream,
                        short colorMode,
                        short channelBitDepth,
                        short channels,
                        int psdVersion,
                        short compression)

        Initializes a new instance of the PsdImage class from specified path from raster image (not psd image in stream) with constructor parameters.

        Parameters:
        stream - The stream to load pixel and palette data from and initialize with.
        colorMode - The color mode.
        channelBitDepth - The PSD bit depth per channel.
        channels - The PSD channels count.
        psdVersion - The PSD version.
        compression - The compression to use.
      • PsdImage

        public PsdImage(RasterImage rasterImage)

        Initializes a new instance of the PsdImage class from existing raster image (not psd image) with RGB color mode with 4 channels 8 bit/channel and no compression.

        Parameters:
        rasterImage - The image to load pixel and palette data from and initialize with.
      • PsdImage

        public PsdImage(RasterImage rasterImage,
                        short colorMode,
                        short channelBitDepth,
                        short channels,
                        int psdVersion,
                        short compression)

        Initializes a new instance of the PsdImage class from existing raster image (not psd image) with constructor parameters.

        Parameters:
        rasterImage - The image to load pixel and palette data from and initialize with.
        colorMode - The color mode.
        channelBitDepth - The PSD bit depth per channel.
        channels - The PSD channels count.
        psdVersion - The PSD version.
        compression - The compression to use.
      • PsdImage

        public PsdImage(int width,
                        int height)

        Initializes a new instance of the PsdImage class with specified width and height. Used to initialize empty psd image.

        Parameters:
        width - The image width.
        height - The image height.
      • PsdImage

        public PsdImage(int width,
                        int height,
                        IColorPalette colorPalette,
                        short colorMode,
                        short channelBitDepth,
                        short channels,
                        int psdVersion,
                        short compression)

        Initializes a new instance of the PsdImage class with specified width,height, paletter, color mode, channels count and channels bit-length and specified compression mode parameters. Used to initialize empty psd image.

        Parameters:
        width - The image width.
        height - The image height.
        colorPalette - The color palette.
        colorMode - The color mode.
        channelBitDepth - The PSD bit depth per channel.
        channels - The PSD channels count.
        psdVersion - The PSD version.
        compression - The compression to use.
    • Method Detail

      • setXmpData

        public void setXmpData(XmpPacketWrapper value)

        Gets or sets the XMP metadata.

        Value: The XMP metadata.
        Overrides:
        setXmpData in class RasterImage
        Parameters:
        value - The XMP metadata.
      • getFileFormat

        public long getFileFormat()

        Gets a value of file format

        Overrides:
        getFileFormat in class Image
      • hasAlpha

        public boolean hasAlpha()

        Gets or sets the vertical resolution, in pixels per inch, of this RasterImage.

        Value: true if this instance has alpha; otherwise, false.
        Overrides:
        hasAlpha in class RasterImage
        Returns:
        true if this instance has alpha; otherwise, false.
      • getVerticalResolution

        public double getVerticalResolution()

        Gets or sets the vertical resolution, in pixels per inch, of this PsdImage.

        Overrides:
        getVerticalResolution in class RasterImage
        Returns:
        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.

        Throws:
        PsdImageException - ResolutionInfo resource not found and can not set proper resolution Value: The vertical resolution.


        Default value for PSD is 72, so if ResolutionInfoResource wasn't found, this value is returned.
      • setVerticalResolution

        public void setVerticalResolution(double value)

        Gets or sets the vertical resolution, in pixels per inch, of this PsdImage.

        Overrides:
        setVerticalResolution in class RasterImage
        Parameters:
        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.

        Throws:
        PsdImageException - ResolutionInfo resource not found and can not set proper resolution Value: The vertical resolution.


        Default value for PSD is 72, so if ResolutionInfoResource wasn't found, this value is returned.
      • getHorizontalResolution

        public double getHorizontalResolution()

        Gets or sets the horizontal resolution, in pixels per inch, of this PsdImage.

        Overrides:
        getHorizontalResolution in class RasterImage
        Returns:
        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.

        Throws:
        PsdImageException - ResolutionInfo resource not found and can not set proper resolution Value: The horizontal resolution.


        Default value for PSD is 72, so if ResolutionInfoResource wasn't found, this value is returned.
      • setHorizontalResolution

        public void setHorizontalResolution(double value)

        Gets or sets the horizontal resolution, in pixels per inch, of this PsdImage.

        Overrides:
        setHorizontalResolution in class RasterImage
        Parameters:
        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.

        Throws:
        PsdImageException - ResolutionInfo resource not found and can not set proper resolution Value: The horizontal resolution.


        Default value for PSD is 72, so if ResolutionInfoResource wasn't found, this value is returned.
      • getRgbColorProfile

        public final StreamSource getRgbColorProfile()

        Gets or sets the RGB color profile for CMYK PSD images. Must be in pair with CmykColorProfile for correct color conversion.

        Value: The RGB color profile.
      • setRgbColorProfile

        public final void setRgbColorProfile(StreamSource value)

        Gets or sets the RGB color profile for CMYK PSD images. Must be in pair with CmykColorProfile for correct color conversion.

        Value: The RGB color profile.
      • getCmykColorProfile

        public final StreamSource getCmykColorProfile()

        Gets or sets the CMYK color profile for CMYK PSD images. Must be in pair with RgbColorProfile for correct color conversion.

        Value: The CMYK color profile.
      • setCmykColorProfile

        public final void setCmykColorProfile(StreamSource value)

        Gets or sets the CMYK color profile for CMYK PSD images. Must be in pair with RgbColorProfile for correct color conversion.

        Value: The CMYK color profile.
      • getGrayColorProfile

        public final StreamSource getGrayColorProfile()

        Gets or sets the GRAY (monochrome) color profile for Grayscale PSD images.

        Value: The GRAY (monochrome) color profile.
      • setGrayColorProfile

        public final void setGrayColorProfile(StreamSource value)

        The GRAY (monochrome) color profile for Grayscale PSD images.

        Value: The GRAY (monochrome) color profile.
      • getColorMode

        public final short getColorMode()

        Gets or sets the color mode.

        Value: The color mode.
      • setColorMode

        public final void setColorMode(short value)

        Gets or sets the color mode.

        Value: The color mode.
      • getCompression

        public final short getCompression()

        Gets the compression method.

        Value: The compression.
      • getChannelsCount

        public final int getChannelsCount()

        Gets the PSD channels count.

        Value: The PSD channels count.
      • getBitsPerChannel

        public final int getBitsPerChannel()

        Gets the bits per channel.

        Value: The bits per channel.
      • getImageResources

        public final ResourceBlock[] getImageResources()

        Gets or sets the PSD image resources.

        Value: The PSD image resources.
      • setImageResources

        public final void setImageResources(ResourceBlock[] value)

        Gets or sets the PSD image resources.

        Value: The PSD image resources.
      • getVersion

        public final int getVersion()

        Gets or sets the version.

        Value: The version.
      • setVersion

        public final void setVersion(int value)

        Gets or sets the version.

        Value: The version.
      • getWidth

        public int getWidth()

        Gets the image width.

        Value: The image width.
        Specified by:
        getWidth in interface IObjectWithBounds
        Specified by:
        getWidth in class Image
        Returns:
        The image width.
      • getHeight

        public int getHeight()

        Gets the image height.

        Value: The image height.
        Specified by:
        getHeight in interface IObjectWithBounds
        Specified by:
        getHeight in class Image
        Returns:
        The image height.
      • getBitsPerPixel

        public int getBitsPerPixel()

        Gets the image bits per pixel count.

        Value: The image bits per pixel count.
        Specified by:
        getBitsPerPixel in class Image
        Returns:
        The image bits per pixel count.
      • getLayers

        public final Layer[] getLayers()

        Gets or sets the PSD layers.

        Value: The PSD layers.


        Note that if there are no layers the other related information within layer and mask information section will not be preserved (layer masks, resources and etc).
      • setLayers

        public final void setLayers(Layer[] value)

        Gets or sets the PSD layers.

        Value: The PSD layers.


        Note that if there are no layers the other related information within layer and mask information section will not be preserved (layer masks, resources and etc).
      • getGlobalLayerResources

        public final LayerResource[] getGlobalLayerResources()

        Gets or sets the global layer resources.

        Value: The global layer resources.
      • setGlobalLayerResources

        public final void setGlobalLayerResources(LayerResource[] value)

        Gets or sets the global layer resources.

        Value: The global layer resources.
      • getGlobalLayerMaskInfo

        public final GlobalLayerMaskInfo getGlobalLayerMaskInfo()

        Gets the global layer mask info.

      • hasTransparencyData

        public final boolean hasTransparencyData()

        Gets or sets a value indicating whether first alpha channel contains the transparency data for the merged result when specifying layers data.

        Value: true if first alpha channel contains the transparency data for the merged result when specifying layers data; otherwise, false.
      • setTransparencyData

        public final void setTransparencyData(boolean value)

        Gets or sets a value indicating whether first alpha channel contains the transparency data for the merged result when specifying layers data.

        Value: true if first alpha channel contains the transparency data for the merged result when specifying layers data; otherwise, false.
      • getActiveLayer

        public final Layer getActiveLayer()

        Gets or sets the active layer.

        Throws:
        TiffImageException - There is no active layer and no layers in image.
        PsdImageException - The active layer cannot be set as it belongs to another image. Value: The active layer.
      • setActiveLayer

        public final void setActiveLayer(Layer value)

        Gets or sets the active layer.

        Throws:
        TiffImageException - There is no active layer and no layers in image.
        PsdImageException - The active layer cannot be set as it belongs to another image. Value: The active layer.
      • isFlatten

        public final boolean isFlatten()

        Gets a value indicating whether psd image is flattened.

        Value: true if this instance is flatten; otherwise, false.
      • getGlobalAngle

        public final int getGlobalAngle()

        Gets or sets the global angle.

      • setGlobalAngle

        public final void setGlobalAngle(int value)

        The global angle.

      • getLinkedLayersManager

        public final LinkedLayersManager getLinkedLayersManager()

        Gets the linked layers manager.

      • getSmartObjectProvider

        public final SmartObjectProvider getSmartObjectProvider()

        Gets the smart object provider.

        Value: The smart object provider.
      • addLayer

        public final void addLayer(Layer layer)

        Adds the layer.

        Parameters:
        layer - The layer.
        Code example:

        An example of drawing on a layer created by no-argument constructor. It adds a manually initialized layer to the document and draws a couple of simple figures on it then saves a changed copy of the document as a new PSD file.


        String outPsdFilePath = "output.psd";
                    
        int width = 100;
        int height = 100;
                    
        // Create a new document of the appropriate size
        PsdImage image = new PsdImage(width, height);
        try
        {
            // Add a layer created by no-argument constructor to the document
            Layer layer = new Layer();
            layer.setBottom(height);
            layer.setRight(width);
            image.addLayer(layer);
                    
            Graphics graphic = new Graphics(layer);
                    
            // Clear the canvas and paint it up with a yellow color
            graphic.clear(Color.getYellow());
                    
            // Draw a rectangle using the Pen tool
            graphic.drawRectangle(new Pen(Color.getRed()), new Rectangle(30, 10, 40, 80));
                    
            // Draw another rectangle using Solid Brush with a Blue color
            graphic.drawRectangle(new Pen(new SolidBrush(Color.getBlue())), new Rectangle(10, 30, 80, 40));
                    
            // Save a copy of the loaded PSD file including the changes on the specified path
            image.save(outPsdFilePath);
        }
        finally
        {
            image.dispose();
        }
        

      • mergeLayers

        public final Layer mergeLayers(Layer bottomLayer,
                                       Layer topLayer)

        Merges the layers.

        Parameters:
        bottomLayer - The bottom layer.
        topLayer - The top layer.
        Returns:
        Bottom layer after the merge
      • flattenImage

        public final void flattenImage()

        Flattens all layers.

      • addRegularLayer

        public final Layer addRegularLayer()

        Adds a new regular layer.

        Returns:
        Created regular layer.
      • addTextLayer

        public final TextLayer addTextLayer(String text,
                                            Rectangle rect)

        Adds a new Text layer.

        Parameters:
        text - The layer's text.
        rect - The layer's rectangle.
        Returns:
        Created text layer.
      • addCurvesAdjustmentLayer

        public final CurvesLayer addCurvesAdjustmentLayer()

        Adds the Curves Adjustment layer.

        Returns:
        Created CurvesLayer Layer
      • addExposureAdjustmentLayer

        public final ExposureLayer addExposureAdjustmentLayer()
      • addExposureAdjustmentLayer

        public final ExposureLayer addExposureAdjustmentLayer(float exposure)
      • addExposureAdjustmentLayer

        public final ExposureLayer addExposureAdjustmentLayer(float exposure,
                                                              float offset)
      • addExposureAdjustmentLayer

        public final ExposureLayer addExposureAdjustmentLayer(float exposure,
                                                              float offset,
                                                              float gammaCorrection)

        Adds the exposure adjustment layer.

        Parameters:
        exposure - The exposure.
        offset - The offset.
        gammaCorrection - The gamma correction.
        Returns:
        Created Exposure Adjustment Layer
      • addHueSaturationAdjustmentLayer

        public final HueSaturationLayer addHueSaturationAdjustmentLayer()

        Adds the hue/saturation adjustment layer.

        Returns:
        A newly created hue/saturation layer.
      • addColorBalanceAdjustmentLayer

        public final ColorBalanceAdjustmentLayer addColorBalanceAdjustmentLayer()

        Adds the color balance adjustment layer.

        Returns:
        A newly created color balance layer.
      • addLevelsAdjustmentLayer

        public final LevelsLayer addLevelsAdjustmentLayer()

        Adds the Levels adjustment layer.

        Returns:
        A newly created Levels layer
      • addPhotoFilterLayer

        public final PhotoFilterLayer addPhotoFilterLayer(Color color)

        Adds the photofilter layer.

        Parameters:
        color - The color.
        Returns:
        Created PhotoFilter Layer
      • addChannelMixerAdjustmentLayer

        public final ChannelMixerLayer addChannelMixerAdjustmentLayer()

        Adds the channel mixer adjustment layer with default parameters

        Returns:
        Added Channel Mixer Layer
      • addBrightnessContrastAdjustmentLayer

        public final BrightnessContrastLayer addBrightnessContrastAdjustmentLayer(int brightness,
                                                                                  int contrast)

        Adds the brightness/contrast adjustment layer.

        Parameters:
        brightness - The brightness.
        contrast - The contrast.
        Returns:
        Created brightness/contrast layer
      • addInvertAdjustmentLayer

        public final InvertAdjustmentLayer addInvertAdjustmentLayer()

        Adds an invert adjustment layer.

        Returns:
        The created invert layer
      • addBlackWhiteAdjustmentLayer

        public final BlackWhiteAdjustmentLayer addBlackWhiteAdjustmentLayer()

        Adds the black white adjustment layer.

        Returns:
        The created black white adjustment layer.
      • addLayerGroup

        public final LayerGroup addLayerGroup(String groupName,
                                              int index,
                                              boolean startBehaviour)

        Adds the layer group.

        Parameters:
        groupName - Name of the group.
        index - The index of the layer to insert after.
        startBehaviour - if set to true [start behaviour] than group will be in open state on start up, otherwise in minimized state.
        Returns:
        Opening group layer
        Throws:
        PsdImageException - Index must be in bounds of Layers count
      • rotate

        public void rotate(float angle)

        Rotate image around the center.

        Overrides:
        rotate in class RasterImage
        Parameters:
        angle - The rotate angle in degrees. Positive values will rotate clockwise.
      • rotate

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

        Rotate image around the center.

        Overrides:
        rotate in class RasterCachedImage
        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 RasterCachedImage
        Parameters:
        rectangle - The rectangle.
      • convert

        public final void convert(PsdOptions newOptions)

        Converts this image format to the one specified in options.

        Parameters:
        newOptions - The new options.
      • resizeHeightProportionally

        public void resizeHeightProportionally(int newHeight,
                                               ImageResizeSettings settings)

        Resizes the height proportionally.

        Overrides:
        resizeHeightProportionally in class Image
        Parameters:
        newHeight - The new height.
        settings - The image resize settings.
      • resizeHeightProportionally

        public void resizeHeightProportionally(int newHeight,
                                               int resizeType)

        Resizes the height proportionally.

        Overrides:
        resizeHeightProportionally in class Image
        Parameters:
        newHeight - The new height.
        resizeType - Type of the resize.
      • resizeWidthProportionally

        public void resizeWidthProportionally(int newWidth,
                                              ImageResizeSettings settings)

        Resizes the width proportionally.

        Overrides:
        resizeWidthProportionally in class Image
        Parameters:
        newWidth - The new width.
        settings - The image resize settings.
      • resizeWidthProportionally

        public void resizeWidthProportionally(int newWidth,
                                              int resizeType)

        Resizes the width proportionally.

        Overrides:
        resizeWidthProportionally in class Image
        Parameters:
        newWidth - The new width.
        resizeType - Type of the resize.
      • dither

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

        Performs dithering on the current image.

        Overrides:
        dither in class RasterCachedImage
        Parameters:
        ditheringMethod - The dithering method.
        bitsCount - The final bits count for dithering.
        customPalette - The custom palette for dithering.
      • 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 RasterCachedImage
        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
      • 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 RasterCachedImage
        Parameters:
        brightnessDifference - The brightness difference between pixel and the average of an s x s window of pixels centered around this pixel.
      • adjustBrightness

        public void adjustBrightness(int brightness)

        Adjust of a brightness for image.

        Overrides:
        adjustBrightness in class RasterCachedImage
        Parameters:
        brightness - Brightness value.
      • adjustGamma

        public void adjustGamma(float gamma)

        Gamma-correction of an image.

        Overrides:
        adjustGamma in class RasterCachedImage
        Parameters:
        gamma - Gamma for red, green and blue channels coefficient
      • adjustContrast

        public void adjustContrast(float contrast)

        Image contrasting

        Overrides:
        adjustContrast in class RasterCachedImage
        Parameters:
        contrast - Contrast value (in range [-100; 100])
      • adjustGamma

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

        Gamma-correction of an image.

        Overrides:
        adjustGamma in class RasterCachedImage
        Parameters:
        gammaRed - Gamma for red channel coefficient
        gammaGreen - Gamma for green channel coefficient
        gammaBlue - Gamma for blue channel coefficient
      • binarizeFixed

        public void binarizeFixed(byte threshold)

        Binarization of an image with predefined threshold

        Overrides:
        binarizeFixed in class RasterCachedImage
        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.
      • grayscale

        public void grayscale()

        Transformation of an image to its grayscale representation

        Overrides:
        grayscale in class RasterCachedImage
      • filter

        public void filter(Rectangle rectangle,
                           FilterOptionsBase options)

        Filters the specified rectangle.

        Overrides:
        filter in class RasterImage
        Parameters:
        rectangle - The rectangle.
        options - The options.
      • replaceColor

        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.

        Overrides:
        replaceColor in class RasterImage
        Parameters:
        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.
      • replaceNonTransparentColors

        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.

        Overrides:
        replaceNonTransparentColors in class RasterImage
        Parameters:
        newColorArgb - New color ARGB value to replace non transparent colors with.