Using Adjustment Layer for PSD Enhancements

Contents
[ ]

Overview

This article explores the editing of adjustment layers in Aspose.PSD for Java. Adjustment layers are a powerful feature in image editing that allow you to make non-destructive changes to your images. Aspose.PSD provides a comprehensive set of adjustment layer classes that you can use to modify various aspects of your images.

To demonstrate the editing of adjustment layers, we will provide a sample code snippet at the end of the page that loads a PSD image and applies different adjustments to its layers.

n the following code snippet, we start by loading the PSD image using the PsdImage.load() method. Then, we set up the options for saving the output PNG files. The PngOptions class allows us to specify the color type for the output image.

Next, we iterate through each layer in the PSD image and check its type using the isAssignable() method. If the layer is of a specific type, we cast it to that type using the cast() method and apply the desired adjustment. For example, we adjust the brightness and contrast of a BrightnessContrastLayer, modify the levels of a LevelsLayer, add a curve point to a CurvesLayer, and so on.

You can add additional code to apply other adjustments to their respective layers. Aspose.PSD provides a wide range of adjustment layer classes, such as ExposureLayer, HueSaturationLayer, ColorBalanceAdjustmentLayer, BlackWhiteAdjustmentLayer, PhotoFilterLayer, ChannelMixerLayer, InvertAdjustmentLayer, PosterizeLayer, ThresholdLayer, SelectiveColorLayer, and more.

Finally, we save the modified image using the save() method of the PsdImage class.

This provides a basic overview of how to edit adjustment layers in Aspose.PSD for Java. You can customize the adjustments according to your requirements and explore the various options available in the API documentation.

Please check the full example below.

Example

public class AdjustmentLayerEnhancementTest {
public static void main(String[] args) {
String sourcePsd = "AllAdjustments.psd";
String outputOrigPng = "AllAdjustments_orig.png";
String outputModPng = "AllAdjustments_mod.png";
PngOptions pngOpt = new PngOptions();
pngOpt.setColorType(PngColorType.TruecolorWithAlpha);
try (PsdImage image = (PsdImage) PsdImage.load(sourcePsd)) {
image.save(outputOrigPng, pngOpt);
Layer[] layers = image.getLayers();
for (Layer layer : layers) {
if (layer instanceof BrightnessContrastLayer) {
BrightnessContrastLayer br = (BrightnessContrastLayer) layer;
br.setBrightness(-br.getBrightness());
br.setContrast(-br.getContrast());
}
if (layer instanceof LevelsLayer) {
LevelsLayer levels = (LevelsLayer) layer;
LevelChannel masterChannel = levels.getMasterChannel();
masterChannel.setOutputShadowLevel((short) 30);
masterChannel.setInputShadowLevel((short) 5);
masterChannel.setInputMidtoneLevel(2);
masterChannel.setOutputHighlightLevel((short) 213);
masterChannel.setInputHighlightLevel((short) 120);
}
if (layer instanceof CurvesLayer) {
CurvesLayer curves = (CurvesLayer) layer;
CurvesContinuousManager manager = (CurvesContinuousManager) curves.getCurvesManager();
manager.addCurvePoint(2, (byte) 150, (byte) 180);
}
if (layer instanceof ExposureLayer) {
ExposureLayer exp = (ExposureLayer) layer;
exp.setExposure((float) (exp.getExposure() + 0.1));
}
if (layer instanceof HueSaturationLayer) {
HueSaturationLayer hue = (HueSaturationLayer) layer;
hue.setHue((short) -15);
hue.setSaturation((short) 30);
}
if (layer instanceof ColorBalanceAdjustmentLayer) {
ColorBalanceAdjustmentLayer colorBal = (ColorBalanceAdjustmentLayer) layer;
colorBal.setMidtonesCyanRedBalance((short) 30);
}
if (layer instanceof BlackWhiteAdjustmentLayer) {
BlackWhiteAdjustmentLayer bw = (BlackWhiteAdjustmentLayer) layer;
bw.setReds(30);
bw.setGreens(25);
bw.setBlues(40);
}
if (layer instanceof PhotoFilterLayer) {
PhotoFilterLayer photoFilter = (PhotoFilterLayer) layer;
photoFilter.setColor(Color.getAzure());
}
if (layer instanceof ChannelMixerLayer) {
ChannelMixerLayer channelMixer = (ChannelMixerLayer) layer;
RgbMixerChannel channel = (RgbMixerChannel) channelMixer.getChannelByIndex(0);
channel.setGreen((short) 120);
channel.setRed((short) 50);
channel.setBlue((short) 70);
channel.setConstant((short) (channel.getConstant() + 10));
}
if (layer instanceof PosterizeLayer) {
PosterizeLayer post = (PosterizeLayer) layer;
post.setLevels((short) 3);
}
if (layer instanceof ThresholdLayer) {
ThresholdLayer threshold = (ThresholdLayer) layer;
threshold.setLevel((short) 15);
}
if (layer instanceof SelectiveColorLayer) {
SelectiveColorLayer selectiveColor = (SelectiveColorLayer) layer;
CmykCorrection correction = new CmykCorrection();
correction.setCyan((short) 25);
correction.setMagenta((short) 10);
correction.setYellow((short) -15);
correction.setBlack((short) 5);
selectiveColor.setCmykCorrection(SelectiveColorsTypes.Cyans, correction);
}
}
image.save(outputModPng, pngOpt);
} catch (Exception e) {
e.printStackTrace();
}
}
}