Notatki wydania Aspose.PSD dla Java 20.6
Contents
[
Hide
]
Ta strona zawiera notatki wydania dla Aspose.PSD dla Java 20.6
Klucz | Podsumowanie | Kategoria |
---|---|---|
PSDJAVA-216 | Wsparcie dla LnkEResource (Zasób warstwy Smart Object) | Funkcja |
PSDJAVA-219 | Wsparcie dla britResource (Zasób warstwy regulacji jasności/kontrastu) | Funkcja |
PSDJAVA-222 | Przeniesienie ustawienia DefaultReplacementFont do klasy ImageOptionsBase | Usprawnienie |
PSDJAVA-217 | Zmiana rozmiaru plików PSD działa nieprawidłowo, jeśli w warstwie korekcji jest maska, która ma puste granice | Błąd |
PSDJAVA-218 | Obraz PSD w trybie RGB 16 bit/na kanał aktualizuje warstwy tylko podglądu | Błąd |
PSDJAVA-220 | Zmiany maski warstwy PSD są odrzucane podczas zapisywania | Błąd |
PSDJAVA-221 | Niepoprawna kolejność warstw po dodaniu grupy warstw do pustej grupy warstw | Błąd |
PSDJAVA-223 | Wyjątek podczas wczytywania określonego pliku PSD ze złożonym zasobem LnkE i adobeStockLicenseState | Błąd |
PSDJAVA-224 | Zapisywanie pliku AI w formacie Jpeg2000 nie działa | Błąd |
PSDJAVA-225 | Grupa warstw z trybem mieszania innych niż Przejście nie jest renderowana | Błąd |
PSDJAVA-226 | Wyjątek NullReference podczas próby konwertowania określonego pliku Psd na obraz | Błąd |
PSDJAVA-227 | OverflowException podczas próby otwarcia określonego pliku Psd | Błąd |
Zmiany w API publicznym
Dodane API:
- M:com.aspose.psd.fileformats.psd.layers.layerresources.linkresources.LiFeDataSource.getFileName
- M:com.aspose.psd.fileformats.psd.layers.layerresources.linkresources.LiFeDataSource.getFileSize
- M:com.aspose.psd.fileformats.psd.layers.layerresources.linkresources.LiFeDataSource.getFullPath
- M:com.aspose.psd.fileformats.psd.layers.layerresources.linkresources.LiFeDataSource.getRelativePath
- M:com.aspose.psd.fileformats.psd.layers.layerresources.linkresources.LiFeDataSource.setAdobeStockId(java.lang.String)
- M:com.aspose.psd.fileformats.psd.layers.layerresources.linkresources.LiFeDataSource.setDate(java.util.Date)
- M:com.aspose.psd.fileformats.psd.layers.layerresources.linkresources.LiFeDataSource.setElementName(java.lang.String)
- M:com.aspose.psd.fileformats.psd.layers.layerresources.linkresources.LiFeDataSource.setElementRef(java.lang.String)
- M:com.aspose.psd.fileformats.psd.layers.layerresources.linkresources.LiFeDataSource.setFileName(java.lang.String)
- M:com.aspose.psd.fileformats.psd.layers.layerresources.linkresources.LiFeDataSource.setFileSize(long)
- M:com.aspose.psd.fileformats.psd.layers.layerresources.linkresources.LiFeDataSource.setFullPath(java.lang.String)
- M:com.aspose.psd.fileformats.psd.layers.layerresources.linkresources.LiFeDataSource.setRelativePath(java.lang.String)
- M:com.aspose.psd.fileformats.psd.layers.layerresources.linkresources.LinkDataSource.getAssetLockedState
- M:com.aspose.psd.fileformats.psd.layers.layerresources.linkresources.LinkDataSource.getAssetModTime
- M:com.aspose.psd.fileformats.psd.layers.layerresources.linkresources.LinkDataSource.getChildDocId
- M:com.aspose.psd.fileformats.psd.layers.layerresources.linkresources.LinkDataSource.getCompId
- M:com.aspose.psd.fileformats.psd.layers.layerresources.linkresources.LinkDataSource.getFileCreator
- M:com.aspose.psd.fileformats.psd.layers.layerresources.linkresources.LinkDataSource.getFileType
- M:com.aspose.psd.fileformats.psd.layers.layerresources.linkresources.LinkDataSource.getLength
- M:com.aspose.psd.fileformats.psd.layers.layerresources.linkresources.LinkDataSource.getOriginalCompId
- M:com.aspose.psd.fileformats.psd.layers.layerresources.linkresources.LinkDataSource.getOriginalFileName
- M:com.aspose.psd.fileformats.psd.layers.layerresources.linkresources.LinkDataSource.getType
- M:com.aspose.psd.fileformats.psd.layers.layerresources.linkresources.LinkDataSource.getUniqueId
- M:com.aspose.psd.fileformats.psd.layers.layerresources.linkresources.LinkDataSource.getVersion
- M:com.aspose.psd.fileformats.psd.layers.layerresources.linkresources.LinkDataSource.hasFileOpenDescriptor
- M:com.aspose.psd.fileformats.psd.layers.layerresources.linkresources.LinkDataSource.isLibraryLink
- M:com.aspose.psd.fileformats.psd.layers.layerresources.linkresources.LinkDataSource.setAssetLockedState(boolean)
- M:com.aspose.psd.fileformats.psd.layers.layerresources.linkresources.LinkDataSource.setAssetModTime(double)
- M:com.aspose.psd.fileformats.psd.layers.layerresources.linkresources.LinkDataSource.setChildDocId(java.lang.String)
- M:com.aspose.psd.fileformats.psd.layers.layerresources.linkresources.LinkDataSource.setCompId(int)
- M:com.aspose.psd.fileformats.psd.layers.layerresources.linkresources.LinkDataSource.setFileCreator(java.lang.String)
- M:com.aspose.psd.fileformats.psd.layers.layerresources.linkresources.LinkDataSource.setFileOpenDescriptor(boolean)
- M:com.aspose.psd.fileformats.psd.layers.layerresources.linkresources.LinkDataSource.setFileType(java.lang.String)
- M:com.aspose.psd.fileformats.psd.layers.layerresources.linkresources.LinkDataSource.setLibraryLink(boolean)
- M:com.aspose.psd.fileformats.psd.layers.layerresources.linkresources.LinkDataSource.setOriginalCompId(int)
- M:com.aspose.psd.fileformats.psd.layers.layerresources.linkresources.LinkDataSource.setOriginalFileName(java.lang.String)
- M:com.aspose.psd.fileformats.psd.layers.layerresources.linkresources.LinkDataSource.setUniqueId(java.util.UUID)
- M:com.aspose.psd.fileformats.psd.layers.layerresources.linkresources.LinkResource.getDataSourceCount
- M:com.aspose.psd.fileformats.psd.layers.layerresources.linkresources.LinkResource.getLength
- M:com.aspose.psd.fileformats.psd.layers.layerresources.linkresources.LinkResource.getPsdVersion
- M:com.aspose.psd.fileformats.psd.layers.layerresources.linkresources.LinkResource.getSignature
- M:com.aspose.psd.fileformats.psd.layers.layerresources.linkresources.LinkResource.isEmpty
- M:com.aspose.psd.fileformats.psd.layers.layerresources.linkresources.LinkResource.save(com.aspose.psd.StreamContainer,int)
- M:com.aspose.psd.fileformats.psd.layers.layerresources.linkresources.Lnk2Resource.#ctor(com.aspose.psd.fileformats.psd.layers.layerresources.linkresources.LinkDataSource[])
- M:com.aspose.psd.fileformats.psd.layers.layerresources.linkresources.Lnk2Resource.getKey
- M:com.aspose.psd.fileformats.psd.layers.layerresources.linkresources.LnkeResource.#ctor
- M:com.aspose.psd.fileformats.psd.layers.layerresources.linkresources.LnkeResource.#ctor(com.aspose.psd.fileformats.psd.layers.layerresources.linkresources.LinkDataSource[])
- M:com.aspose.psd.fileformats.psd.layers.layerresources.linkresources.LnkeResource.getKey
- M:com.aspose.psd.fileformats.psd.layers.layerresources.linkresources.LnkeResource.get_Item(int)
- T:com.aspose.psd.fileformats.psd.layers.layerresources.linkresources.LiFdDataSource
- T:com.aspose.psd.fileformats.psd.layers.layerresources.linkresources.LiFeDataSource
- T:com.aspose.psd.fileformats.psd.layers.layerresources.linkresources.LinkDataSource
- T:com.aspose.psd.fileformats.psd.layers.layerresources.linkresources.LinkDataSourceType
- T:com.aspose.psd.fileformats.psd.layers.layerresources.linkresources.LinkResource
- T:com.aspose.psd.fileformats.psd.layers.layerresources.linkresources.Lnk2Resource
- T:com.aspose.psd.fileformats.psd.layers.layerresources.linkresources.LnkeResource
Usunięte API:
- M:com.aspose.psd.imageloadoptions.PsdLoadOptions.getDefaultReplacementFont
- M:com.aspose.psd.imageloadoptions.PsdLoadOptions.setDefaultReplacementFont(java.lang.String)
Przykłady użycia:
PSDJAVA-216: Wsparcie dla LnkEResource (Zasób warstwy Smart Object)
// Przykład łączenia różnych typów zasobów (obrazy rastrowe, biblioteki CC) z plikiem PSD.
// Rozważana jest również API klasy LnkeResource.
// Klasa przechowująca metody w lokalnym zakresie
class LocalScopeExtension
{
void assertIsTrue(boolean condition)
{
if (!condition)
{
throw new FormatException("ExampleOfLnkEResourceSupport działa nieprawidłowo.");
}
}
void assertAreEqual(Object actual, Object expected)
{
assertIsTrue(actual != null && actual.equals(expected));
}
// Ten przykład demonstruje, jak uzyskać i ustawić właściwości zasobu Photoshop Psd LnkE
// Resource, który zawiera informacje o zewnętrznym pliku połączonym.
void exampleOfLnkEResourceSupport(
String fileName,
int length,
int length2,
int length3,
int length4,
String fullPath,
String date,
double assetModTime,
String childDocId,
boolean locked,
String uid,
String name,
String originalFileName,
String fileType,
long size)
{
String outputPath = "out_" + fileName;
// Wczytaj predefiniowany PSD
PsdImage image = (PsdImage)Image.load(fileName);
try
{
// Szukaj LnkeResource wśród zasobów warstw globalnych
LnkeResource lnkeResource = null;
for (LayerResource resource : image.getGlobalLayerResources())
{
if (resource instanceof LnkeResource)
{
lnkeResource = (LnkeResource)resource;
// Sprawdź właściwości zasobu LnkeResource
assertAreEqual(lnkeResource.getLength(), length);
assertAreEqual(lnkeResource.get_Item(0).getUniqueId(), UUID.fromString(uid));
assertAreEqual(lnkeResource.get_Item(0).getFullPath(), fullPath);
assertAreEqual(new SimpleDateFormat("MM/dd/yyyy HH:mm:ss").format(lnkeResource.get_Item(0).getDate()), date);
assertAreEqual(lnkeResource.get_Item(0).getAssetModTime(), assetModTime);
assertAreEqual(lnkeResource.get_Item(0).getAssetLockedState(), locked);
assertAreEqual(lnkeResource.get_Item(0).getFileName(), name);
assertAreEqual(lnkeResource.get_Item(0).getFileSize(), size);
assertAreEqual(lnkeResource.get_Item(0).getChildDocId(), childDocId);
assertAreEqual(lnkeResource.get_Item(0).getVersion(), 7);
assertAreEqual(lnkeResource.get_Item(0).getFileType().trim(), fileType);
assertAreEqual(lnkeResource.get_Item(0).getFileCreator().trim(), "");
assertAreEqual(lnkeResource.get_Item(0).getOriginalFileName(), originalFileName);
assertAreEqual(lnkeResource.get_Item(0).getCompId(), -1);
assertAreEqual(lnkeResource.get_Item(0).getOriginalCompId(), -1);
assertIsTrue(lnkeResource.get_Item(0).hasFileOpenDescriptor());
assertIsTrue(!lnkeResource.isEmpty());
assertIsTrue(lnkeResource.get_Item(0).getType() == LinkDataSourceType.liFE);
// Zaktualizuj właściwości zasobu LnkeResource
lnkeResource.get_Item(0).setFullPath("file:///C:/Aspose/net/Aspose.Psd/test/testdata/Images/Psd/SmartObjects/rgb8_2x2.png");
assertAreEqual(lnkeResource.getLength(), length2);
lnkeResource.get_Item(0).setFileName("rgb8_2x23.png");
assertAreEqual(lnkeResource.getLength(), length3);
lnkeResource.get_Item(0).setChildDocId(UUID.randomUUID().toString());
assertAreEqual(lnkeResource.getLength(), length4);
lnkeResource.get_Item(0).setDate(new Date());
lnkeResource.get_Item(0).setAssetModTime(Double.MAX_VALUE);
lnkeResource.get_Item(0).setFileSize(Long.MAX_VALUE);
lnkeResource.get_Item(0).setFileType("test");
lnkeResource.get_Item(0).setFileCreator("file");
break;
}
}
// Upewnij się, że LnkeResource jest obsługiwane
assertIsTrue(lnkeResource != null);
// Zapisz kopię wczytanego PSD
image.save(outputPath, new PsdOptions(image));
}
finally
{
image.dispose();
}
// Wczytaj zapisaną kopię
PsdImage image1 = (PsdImage)Image.load(outputPath);
try
{
// Konwertuj PSD na format pliku PNG (z kanałem alfa dla przeźroczystości)
PngOptions pngOptions = new PngOptions();
pngOptions.setColorType(PngColorType.TruecolorWithAlpha);
image1.save(Path.changeExtension(outputPath, "png"), pngOptions);
}
finally
{
image1.dispose();
}
}
}
LocalScopeExtension $ = new LocalScopeExtension();
// Ten przykład pokazuje, jak uzyskać i ustawić właściwości zasobu Psd LnkE, który
// zawiera informacje o zewnętrznie połączonym pliku JPEG.
$.exampleOfLnkEResourceSupport(
"photooverlay_5_new.psd",
0x21c,
0x26c,
0x274,
0x27c,
"file:///C:/Users/cvallejo/Desktop/photo.jpg",
"05/09/2017 22:24:51",
0,
"F062B9DB73E8D124167A4186E54664B0",
false,
"02df245c-36a2-11e7-a9d8-fdb2b61f07a7",
"photo.jpg",
"photo.jpg",
"JPEG",
0x1520d);
// Ten przykład pokazuje, jak uzyskać i ustawić właściwości zasobu PSD LnkE, który
// zawiera informacje o zewnętrznie połączonym pliku PNG.
$.exampleOfLnkEResourceSupport(
"rgb8_2x2_linked.psd",
0x284,
0x290,
0x294,
0x2dc,
"file:///C:/Aspose/net/Aspose.Psd/test/testdata/Issues/PSDNET-491/rgb8_2x2.png",
"04/14/2020 14:23:44",
0,
"",
false,
"5867318f-3174-9f41-abca-22f56a75247e",
"rgb8_2x2.png",
"rgb8_2x2.png",
"png",
0x53);
// Ten przykład pokazuje, jak uzyskać i ustawić właściwości zasobu Psd LnkE, który
// zawiera informacje o zewnętrznie połączonym zasobie bibliotek CC.
$.exampleOfLnkEResourceSupport(
"rgb8_2x2_asset_linked.psd",
0x398,
0x38c,
0x388,
0x3d0,
"CC Libraries Asset “rgb8_2x2_linked/rgb8_2x2” (Funkcja dostępna w Photoshop CC 2015)",
"01/01/0001 00:00:00",
1588890915488.0d,
"",
false,
"ec15f0a8-7f13-a640-b928-7d29c6e9859c",
"rgb8_2x2_linked",
"rgb8_2x2.png",
"png",
0);
PSDJAVA-219: Wsparcie dla britResource (Zasób warstwy regulacji jasności/kontrastu)
// Ten przykład demonstruje, jak programistycznie zmienić zasób warstwy jasności/kontrastu PSD. Jest to niskopoziomowe API Aspose.PSD.
// Możesz korzystać z warstwy jasności/kontrastu za pomocą jej API, co będzie o wiele łatwiejsze,
// ale bezpośrednia edycja zasobów programu PhotoShop daje większą kontrolę nad zawartością pliku PSD---
PSDJAVA-219: Support of britResource (Resource of Brightness/Contrast Adjustment Layer)
<div class="highlight"><pre class="chroma"><code class="language-java" data-lang="java"> <span class="c1">// This Example demonstrates how you can programmatically change the PSD Image
</span><span class="c1"></span>
<span class="c1">// Brightness/Contrast Layer Resource - BritResource. This is a Low-Level Aspose.PSD API.
</span><span class="c1"></span>
<span class="c1">// You can use Brightness/Contrast Layer through its API, which will be much easier,
</span><span class="c1"></span>
<span class="c1">// but direct PhotoShop resource editing gives you more control over the PSD file content.
</span><span class="c1"></span>
<span class="n">String</span> <span class="n">srcPath</span> <span class="o">=</span> <span class="s">"BrightnessContrastPS6.psd"</span><span class="o">;</span>
<span class="n">String</span> <span class="n">dstPath</span> <span class="o">=</span> <span class="s">"BrightnessContrastPS6_output.psd"</span><span class="o">;</span>
<span class="c1">// Load a Photoshop document containing a Brightness / Contrast adjustment layer
</span><span class="c1"></span>
<span class="n">PsdImage</span> <span class="n">psdImage</span> <span class="o">=</span> <span class="o">(</span><span class="n">PsdImage</span><span class="o">)</span><span class="n">Image</span><span class="o">.</span><span class="na">load</span><span class="o">(</span><span class="n">srcPath</span><span class="o">);</span>
<span class="k">try</span>
<span class="o">{</span>
<span class="c1">// Search for BritResource
</span><span class="c1"></span>
<span class="k">for</span> <span class="o">(</span><span class="n">Layer</span> <span class="n">layer</span> <span class="o">:</span> <span class="n">psdImage</span><span class="o">.</span><span class="na">getLayers</span><span class="o">())</span>
<span class="o">{</span>
<span class="k">if</span> <span class="o">(</span><span class="n">layer</span> <span class="k">instanceof</span> <span class="n">BrightnessContrastLayer</span><span class="o">)</span>
<span class="o">{</span>
<span class="k">for</span> <span class="o">(</span><span class="n">LayerResource</span> <span class="n">layerResource</span> <span class="o">:</span> <span class="n">layer</span><span class="o">.</span><span class="na">getResources</span><span class="o">())</span>
<span class="o">{</span>
<span class="k">if</span> <span class="o">(</span><span class="n">layerResource</span> <span class="k">instanceof</span> <span class="n">BritResource</span><span class="o">)</span>
<span class="o">{</span>
<span class="n">BritResource</span> <span class="n">resource</span> <span class="o">=</span> <span class="o">(</span><span class="n">BritResource</span><span class="o">)</span><span class="n">layerResource</span><span class="o">;</span>
<span class="c1">// Verify resource properties
</span><span class="c1"></span>
<span class="k">if</span> <span class="o">(</span><span class="n">resource</span><span class="o">.</span><span class="na">getBrightness</span><span class="o">()</span> <span class="o">!=</span> <span class="o">-</span><span class="n">40</span> <span class="o">||</span>
<span class="n">resource</span><span class="o">.</span><span class="na">getContrast</span><span class="o">()</span> <span class="o">!=</span> <span class="n">10</span> <span class="o">||</span>
<span class="n">resource</span><span class="o">.</span><span class="na">getLabColor</span><span class="o">()</span> <span class="o">||</span>
<span class="n">resource</span><span class="o">.</span><span class="na">getMeanValueForBrightnessAndContrast</span><span class="o">()</span> <span class="o">!=</span> <span class="n">127</span><span class="o">)</span>
<span class="o">{</span>
<span class="k">throw</span> <span class="k">new</span> <span class="n">RuntimeException</span><span class="o">(</span><span class="s">"BritResource was read wrong"</span><span class="o">);</span>
<span class="o">}</span>
<span class="c1">// Update resource properties
</span><span class="c1"></span>
<span class="n">resource</span><span class="o">.</span><span class="na">setBrightness</span><span class="o">((</span><span class="kt">short</span><span class="o">)</span><span class="n">25</span><span class="o">);</span>
<span class="n">resource</span><span class="o">.</span><span class="na">setContrast</span><span class="o">((</span><span class="kt">short</span><span class="o">)-</span><span class="n">14</span><span class="o">);</span>
<span class="n">resource</span><span class="o">.</span><span class="na">setLabColor</span><span class="o">(</span><span class="kc">true</span><span class="o">);</span>
<span class="n">resource</span><span class="o">.</span><span class="na">setMeanValueForBrightnessAndContrast</span><span class="o">((</span><span class="kt">short</span><span class="o">)</span><span class="n">200</span><span class="o">);</span>
<span class="c1">// Save a copy of the updated PSD
</span><span class="c1"></span>
<span class="n">psdImage</span><span class="o">.</span><span class="na">save</span><span class="o">(</span><span class="n">dstPath</span><span class="o">,</span> <span class="k">new</span> <span class="n">PsdOptions</span><span class="o">());</span>
<span class="k">break</span><span class="o">;</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="k">finally</span>
<span class="o">{</span>
<span class="n">psdImage</span><span class="o">.</span><span class="na">dispose</span><span class="o">();</span>
<span class="o">}</span></code></pre></div>
PSDJAVA-217: Resizing PSD files works incorrect if there is a mask in the adjustment layer that has empty bounds
<div class="highlight"><pre class="chroma"><code class="language-java" data-lang="java"> <span class="c1">// An example of resizing an image that contains an adjustment layer mask with empty bounds.
</span><span class="c1"></span>
<span class="c1">// The program loads a predefined PSD just to check whether there are no exceptions.
</span><span class="c1"></span>
<span class="kd">final</span> <span class="kt">int</span> <span class="n">scale</span> <span class="o">=</span> <span class="n">2</span><span class="o">;</span> <span class="c1">// arbitrary coefficient
</span><span class="c1"></span>
<span class="n">String</span><span class="o">[]</span> <span class="n">names</span> <span class="o">=</span> <span class="o">{</span>
<span class="s">"OneRegularAndOneAdjustmentWithVectorAndLayerMask"</span><span class="o">,</span>
<span class="s">"LevelsLayerWithLayerMaskRgb"</span><span class="o">,</span>
<span class="s">"LevelsLayerWithLayerMaskCmyk"</span><span class="o">,</span>
<span class="o">};</span>
<span class="k">for</span> <span class="o">(</span><span class="n">String</span> <span class="n">name</span> <span class="o">:</span> <span class="n">names</span><span class="o">)</span>
<span class="o">{</span>
<span class="n">String</span> <span class="n">srcFilePath</span> <span class="o">=</span> <span class="n">name</span> <span class="o">+</span> <span class="s">".psd"</span><span class="o">;</span>
<span class="n">String</span> <span class="n">dstFilePath</span> <span class="o">=</span> <span class="s">"output_"</span> <span class="o">+</span> <span class="n">srcFilePath</span><span class="o">;</span>
<span class="n">String</span> <span class="n">dstPngFilePath</span> <span class="o">=</span> <span class="s">"output_"</span> <span class="o">+</span> <span class="n">name</span> <span class="o">+</span> <span class="s">".png"</span><span class="o">;</span>
<span class="c1">// Load a predefined PSD containing an adjustment layer mask that has empty bounds
</span><span class="c1"></span>
<span class="n">PsdLoadOptions</span> <span class="n">psdLoadOptions</span> <span class="o">=</span> <span class="k">new</span> <span class="n">PsdLoadOptions</span><span class="o">();</span>
<span class="n">psdLoadOptions</span><span class="o">.</span><span class="na">setLoadEffectsResource</span><span class="o">(</span><span class="kc">true</span><span class="o">);</span>
<span class="n">PsdImage</span> <span class="n">image</span> <span class="o">=</span> <span class="o">(</span><span class="n">PsdImage</span><span class="o">)</span><span class="n">Image</span><span class="o">.</span><span class="na">load</span><span class="o">(</span><span class="n">srcFilePath</span><span class="o">,</span> <span class="n">psdLoadOptions</span><span class="o">);</span>
<span class="k">try</span>
<span class="o">{</span>
<span class="c1">// Resize the image
</span><span class="c1"></span>
<span class="n">image</span><span class="o">.</span><span class="na">resize</span><span class="o">(</span><span class="n">image</span><span class="o">.</span><span class="na">getWidth</span><span class="o">()</span> <span class="o">*</span> <span class="n">scale</span><span class="o">,</span> <span class="n">image</span><span class="o">.</span><span class="na">getHeight</span><span class="o">()</span> <span class="o">*</span> <span class="n">scale</span><span class="o">);</span>
<span class="c1">// Save a copy of the loaded PSD
</span><span class="c1"></span>
<span class="n">image</span><span class="o">.</span><span class="na">save</span><span class="o">(</span><span class="n">dstFilePath</span><span class="o">,</span> <span class="k">new</span> <span class="n">PsdOptions</span><span class="o">());</span>
<span class="c1">// Export PSD to PNG file format (with alpha channel for transparency)
</span><span class="c1"></span>
<span class="n">PngOptions</span> <span class="n">pngOptions</span> <span class="o">=</span> <span class="k">new</span> <span class="n">PngOptions</span><span class="o">();</span>
<span class="n">pngOptions</span><span class="o">.</span><span class="na">setColorType</span><span class="o">(</span><span class="n">PngColorType</span><span class="o">.</span><span class="na">TruecolorWithAlpha</span><span class="o">);</span>
<span class="n">image</span><span class="o">.</span><span class="na">save</span><span class="o">(</span><span class="n">dstPngFilePath</span><span class="o">,</span> <span class="n">pngOptions</span><span class="o">);</span>
<span class="o">}</span>
<span class="k">finally</span>
<span class="o">{</span>
<span class="n">image</span><span class="o">.</span><span class="na">dispose</span><span class="o">();</span>
<span class="o">}</span>
<span class="o">}</span></code></pre></div>
PSDJAVA-218: Psd Image with RGB mode 16 bit/channel updates layers only on preview
<div class="highlight"><pre class="chroma"><code class="language-java" data-lang="java"> <span class="c1">// An example of updating regular layers for a 16-bit RGB image. The program draws something
</span><span class="c1"></span>
<span class="c1">// on each layer just to make sure that the whole layer updates properly.
</span><span class="c1"></span>
<span class="n">String</span> <span class="n">sourceFilePath</span> <span class="o">=</span> <span class="s">"in.psd"</span><span class="o">;</span>
<span class="n">String</span> <span class="n">outputFilePath</span> <span class="o">=</span> <span class="s">"output.psd"</span><span class="o">;</span>
<span class="c1">// Load a predefined PSD in 16-bit RGB mode
</span><span class="c1"></span>
<span class="n">PsdImage</span> <span class="n">image</span> <span class="o">=</span> <span class="o">(</span><span class="n">PsdImage</span><span class="o">)</span><span class="n">Image</span><span class="o">.</span><span class="na">load</span><span class="o">(</span><span class="n">sourceFilePath</span><span class="o">);</span>
<span class="k">try</span>
<span class="o">{</span>
<span class="k">for</span> <span class="o">(</span><span class="n">Layer</span> <span class="n">layer</span> <span class="o">:</span> <span class="n">image</span><span class="o">.</span><span class="na">getLayers</span><span class="o">())</span>
<span class="o">{</span>
<span class="c1">// Draw the layer name and an inner border for the regular layer
</span><span class="c1"></span>
<span class="k">if</span> <span class="o">(!(</span><span class="n">layer</span> <span class="k">instanceof</span> <span class="n">LayerGroup</span><span class="o">)</span> <span class="o">&&</span> <span class="o">!(</span><span class="n">layer</span> <span class="k">instanceof</span> <span class="n">AdjustmentLayer</span><span class="o">)</span> <span class="o">&&</span>
<span class="o">(</span><span class="n">layer</span><span class="o">.</span><span class="na">getWidth</span><span class="o">()</span> <span class="o">></span> <span class="n">100</span><span class="o">)</span> <span class="o">&&</span> <span class="o">(</span><span class="n">layer</span><span class="o">.</span><span class="na">getHeight</span><span class="o">()</span> <span class="o">></span> <span class="n">100</span><span class="o">))</span>
<span class="o">{</span>
<span class="n">Graphics</span> <span class="n">graphics</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Graphics</span><span class="o">(</span><span class="n">layer</span><span class="o">);</span>
<span class="n">graphics</span><span class="o">.</span><span class="na">drawString</span><span class="o">(</span><span class="n">layer</span><span class="o">.</span><span class="na">getName</span><span class="o">(),</span> <span class="k">new</span> <span class="n">Font</span><span class="o">(</span><span class="s">"Arial"</span><span class="o">,</span> <span class="n">10</span><span class="o">),</span>
<span class="k">new</span> <span class="n">SolidBrush</span><span class="o">(</span><span class="n">Color</span><span class="o">.</span><span class="na">getRed</span><span class="o">()),</span> <span class="n">15</span><span class="o">,</span> <span class="n">45</span><span class="o">);</span>
<span class="n">graphics</span><span class="o">.</span><span class="na">drawRectangle</span><span class="o">(</span><span class="k">new</span> <span class="n">Pen</span><span class="o">(</span><span class="n">Color</span><span class="o">.</span><span class="na">getRed</span><span class="o">()),</span> <span class="k">new</span> <span class="n">Rectangle</span><span class="o">(</span><span class="n">30</span><span class="o">,</span> <span class="n">10</span><span class="o">,</span> <span class="n">40</span><span class="o">,</span> <span class="n">80</span><span class="o">));</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="c1">// Save a copy of the loaded PSD
</span><span class="c1"></span>
<span class="n">image</span><span class="o">.</span><span class="na">save</span><span class="o">(</span><span class="n">outputFilePath</span><span class="o">,</span> <span class="k">new</span> <span class="n">PsdOptions</span><span class="o">(</span><span class="n">image</span><span class="o">));</span>
<span class="o">}</span>
<span class="k">finally</span>
<span class="o">{</span>
<span class="n">image</span><span class="o">.</span><span class="na">dispose</span><span class="o">();</span>
<span class="o">}</span></code></pre></div>
PSDJAVA-220: PSD Layer Mask changes are discarded on save
<div class="highlight"><pre class="chroma"><code class="language-java" data-lang="java"> <span class="c1">// A class that keeps methods in the local scope
</span><span class="c1"></span>
<span class="kd">class</span> <span class="nc">LocalScopeExtension</span>
<span class="o">{</span>
<span class="kt">void</span> <span class="nf">assertAreEqual</span><span class="o">(</span><span class="n">Object</span> <span class="n">actual</span><span class="o">,</span> <span class="n">Object</span> <span class="n">expected</span><span class="o">)</span>
<span class="o">{</span>
<span class="k">if</span> <span class="o">(!(</span><span class="n">actual</span> <span class="o">!=</span> <span class="kc">null</span> <span class="o">&&</span> <span class="n">actual</span><span class="o">.</span><span class="na">equals</span><span class="o">(</span><span class="n">expected</span><span class="o">)))</span>
<span class="o">{</span>
<span class="k">throw</span> <span class="k">new</span> <span class="n">FormatException</span><span class="o">(</span><span class="s">"Example works incorrectly."</span><span class="o">);</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="c1">// Gets the int value converted to big-endian bytes order.
</span><span class="c1"></span>
<span class="kt">byte</span><span class="o">[]</span> <span class="nf">getBigEndianBytesInt32</span><span class="o">(</span><span class="kt">int</span> <span class="n">value</span><span class="o">)</span>
<span class="o">{</span>
<span class="kt">byte</span><span class="o">[]</span> <span class="n">bytes</span> <span class="o">=</span> <span class="k">new</span> <span class="kt">byte</span><span class="o">[</span><span class="n">4</span><span class="o">];</span>
<span class="n">bytes</span><span class="o">[</span><span class="n">0</span><span class="o">]</span> <span class="o">=</span> <span class="o">(</span><span class="kt">byte</span><span class="o">)((</span><span class="n">value</span> <span class="o">>></span> <span class="n">24</span><span class="o">)</span> <span class="o">&</span> <span class="n">0x000000FF</span><span class="o">);</span>
<span class="n">bytes</span><span class="o">[</span><span class="n">1</span><span class="o">]</span> <span class="o">=</span> <span class="o">(</span><span class="kt">byte</span><span class="o">)((</span><span class="n">value</span> <span class="o">>></span> <span class="n">16</span><span class="o">)</span> <span class="o">&</span> <span class="n">0x000000FF</span><span class="o">);</span>
<span class="n">bytes</span><span class="o">[</span><span class="n">2</span><span class="o">]</span> <span class="o">=</span> <span class="o">(</span><span class="kt">byte</span><span class="o">)((</span><span class="n">value</span> <span class="o">>></span> <span class="n">8</span><span class="o">)</span> <span class="o">&</span> <span class="n">0x000000FF</span><span class="o">);</span>
<span class="n">bytes</span><span class="o">[</span><span class="n">3</span><span class="o">]</span> <span class="o">=</span> <span class="o">(</span><span class="kt">byte</span><span class="o">)</span><span class="n">value</span><span class="o">;</span>
<span class="k">return</span> <span class="n">bytes</span><span class="o">;</span>
<span class="o">}</span>
<span class="c1">// Gets the value converted from the big endian to Int32.
</span><span class="c1"></span>
<span class="kt">int</span> <span class="nf">fromBigEndianToInt32</span><span class="o">(</span><span class="kt">byte</span><span class="o">[]</span> <span class="n">bytes</span><span class="o">,</span> <span class="kt">int</span> <span class="n">index</span><span class="o">)</span>
<span class="o">{</span>
<span class="k">if</span> <span class="o">(</span><span class="n">bytes</span> <span class="o">==</span> <span class="kc">null</span><span class="o">)</span>
<span class="o">{</span>
<span class="k">throw</span> <span class="k">new</span> <span class="n">ArgumentNullException</span><span class="o">(</span><span class="s">"bytes"</span><span class="o">);</span>
<span class="o">}</span>
<span class="k">if</span> <span class="o">(</span><span class="n">index</span> <span class="o"><</span> <span class="n">0</span> <span class="o">||</span> <span class="n">index</span> <span class="o">+</span> <span class="n">4</span> <span class="o">></span> <span class="n">bytes</span><span class="o">.</span><span class="na">length</span><span class="o">)</span>
<span class="o">{</span>
<span class="k">throw</span> <span class="k">new</span> <span class="n">ArgumentOutOfRangeException</span><span class="o">(</span><span class="s">"index"</span><span class="o">,</span> <span class="s">"The index falls outside the bytes array."</span><span class="o">);</span>
<span class="o">}</span>
<span class="k">return</span> <span class="o">((</span><span class="n">bytes</span><span class="o">[</span><span class="n">index</span><span class="o">]</span> <span class="o">&</span> <span class="n">0xff</span><span class="o">)</span> <span class="o"><<</span> <span class="n">24</span><span class="o">)</span> <span class="o">|</span> <span class="o">((</span><span class="n">bytes</span><span class="o">[</span><span class="n">index</span> <span class="o">+</span> <span class="n">1</span><span class="o">]</span> <span class="o">&</span> <span class="n">0xff</span><span class="o">)</span> <span class="o"><<</span> <span class="n">16</span><span class="o">)</span> <span class="o">|</span>
<span class="o">((</span><span class="n">bytes</span><span class="o">[</span><span class="n">index</span> <span class="o">+</span> <span class="n">2</span><span class="o">]</span> <span class="o">&</span> <span class="n">0xff</span><span class="o">)</span> <span class="o"><<</span> <span class="n">8</span><span class="o">)</span> <span class="o">|</span> <span class="o">(</span><span class="n">bytes</span><span class="o">[</span><span class="n">index</span> <span class="o">+</span> <span class="n">3</span><span class="o">]</span> <span class="o">&</span> <span class="n">0xff</span><span class="o">);</span>
<span class="o">}</span>
<span class="c1">// Gets a raster mask from the layer of a PSD image and saves it to a file
</span><span class="c1"></span>
<span class="kt">void</span> <span class="nf">saveRasterMask</span><span class="o">(</span><span class="n">String</span> <span class="n">maskFilePath</span><span class="o">,</span> <span class="n">Layer</span> <span class="n">layer</span><span class="o">)</span>
<span class="o">{</span>
<span class="n">LayerMaskDataShort</span> <span class="n">maskData</span> <span class="o">=</span> <span class="o">(</span><span class="n">LayerMaskDataShort</span><span class="o">)</span><span class="n">layer</span><span class="o">.</span><span class="na">getLayerMaskData</span><span class="o">();</span>
<span class="n">FileStreamContainer</span> <span class="n">container</span> <span class="o">=</span> <span class="n">FileStreamContainer</span><span class="o">.</span><span class="na">createFileStream</span><span class="o">(</span><span class="n">maskFilePath</span><span class="o">,</span> <span class="kc">false</span><span class="o">);</span>
<span class="k">try</span>
<span class="o">{</span>
<span class="n">container</span><span class="o">.</span><span class="na">write</span><span class="o">(</span><span class="n">getBigEndianBytesInt32</span><span class="o">(</span><span class="n">maskData</span><span class="o">.</span><span class="na">getTop</span><span class="o">()));</span>
<span class="n">container</span><span class="o">.</span><span class="na">write</span><span class="o">(</span><span class="n">getBigEndianBytesInt32</span><span class="o">(</span><span class="n">maskData</span><span class="o">.</span><span class="na">getLeft</span><span class="o">()));</span>
<span class="n">container</span><span class="o">.</span><span class="na">write</span><span class="o">(</span><span class="n">getBigEndianBytesInt32</span><span class="o">(</span><span class="n">maskData</span><span class="o">.</span><span class="na">getBottom</span><span class="o">()));</span>
<span class="n">container</span><span class="o">.</span><span class="na">write</span><span class="o">(</span><span class="n">getBigEndianBytesInt32</span><span class="o">(</span><span class="n">maskData</span><span class="o">.</span><span class="na">getRight</span><span class="o">()));</span>
<span class="n">container</span><span class="o">.</span><span class="na">writeByte</span><span class="o">(</span><span class="n">maskData</span><span class="o">.</span><span class="na">getDefaultColor</span><span class="o">());</span>
<span class="n">container</span><span class="o">.</span><span class="na">writeByte</span><span class="o">((</span><span class="kt">byte</span><span class="o">)</span><span class="n">maskData</span><span class="o">.</span><span class="na">getFlags</span><span class="o">());</span>
<span class="n">container</span><span class="o">.</span><span class="na">write</span><span class="o">(</span><span class="n">getBigEndianBytesInt32</span><span class="o">(</span><span class="n">maskData</span><span class="o">.</span><span class="na">getImageData</span><span class="o">().</span><span class="na">length</span><span class="o">));</span>
<span class="n">container</span><span class="o">.</span><span class="na">write</span><span class="o">(</span><span class="n">maskData</span><span class="o">.</span><span class="na">getImageData</span><span class="o">(),</span> <span class="n">0</span><span class="o">,</span> <span class="n">maskData</span><span class="o">.</span><span class="na">getImageData</span><span class="o">().</span><span class="na">length</span><span class="o">);</span>
<span class="o">}</span>
<span class="k">finally</span>
<span class="o">{</span>
<span class="n">container</span><span class="o">.</span><span class="na">dispose</span><span class="o">();</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="c1">// Adds a raster mask from the file to the layer and saves it the PSD format image
</span><span class="c1"></span>
<span class="kt">void</span> <span class="nf">addRasterMask</span><span class="o">(</span><span class="n">Layer</span> <span class="n">layer</span><span class="o">,</span> <span class="n">String</span> <span class="n">maskSourcePath</span><span class="o">)</span>
<span class="o">{</span>
<span class="n">LayerMaskDataShort</span> <span class="n">maskData</span> <span class="o">=</span> <span class="k">new</span> <span class="n">LayerMaskDataShort</span><span class="o">();</span>
<span class="n">FileStreamContainer</span> <span class="n">container</span> <span class="o">=</span> <span class="n">FileStreamContainer</span><span class="o">.</span><span class="na">openFileStream</span><span class="o">(</span><span class="n">maskSourcePath</span><span class="o">);</span>
<span class="k">try</span>
<span class="o">{</span>
<span class="kt">byte</span><span class="o">[]</span> <span class="n">bytes</span> <span class="o">=</span> <span class="k">new</span> <span class="kt">byte</span><span class="o">[</span><span class="n">22</span><span class="o">];</span>
<span class="n">assertAreEqual</span><span class="o">(</span><span class="n">container</span><span class="o">.</span><span class="na">read</span><span class="o">(</span><span class="n">bytes</span><span class="o">),</span> <span class="n">22</span><span class="o">);</span>
<span class="n">maskData</span><span class="o">.</span><span class="na">setTop</span><span class="o">(</span><span class="n">fromBigEndianToInt32</span><span class="o">(</span><span class="n">bytes</span><span class="o">,</span> <span class="n">0</span><span class="o">));</span>
<span class="n">maskData</span><span class="o">.</span><span class="na">setLeft</span><span class="o">(</span><span class="n">fromBigEndianToInt32</span><span class="o">(</span><span class="n">bytes</span><span class="o">,</span> <span class="n">4</span><span class="o">));</span>
<span class="n">maskData</span><span class="o">.</span><span class="na">setBottom</span><span class="o">(</span><span class="n">fromBigEndianToInt32</span><span class="o">(</span><span class="n">bytes</span><span class="o">,</span> <span class="n">8</span><span class="o">));</span>
<span class="n">maskData</span><span class="o">.</span><span class="na">setRight</span><span class="o">(</span><span class="n">fromBigEndianToInt32</span><span class="o">(</span><span class="n">bytes</span><span class="o">,</span> <span class="n">12</span><span class="o">));</span>
<span class="n">maskData</span><span class="o">.</span><span class="na">setDefaultColor</span><span class="o">(</span><span class="n">bytes</span><span class="o">[</span><span class="n">16</span><span class="o">]);</span>
<span class="n">maskData</span><span class="o">.</span><span class="na">setFlags</span><span class="o">(</span><span class="n">bytes</span><span class="o">[</span><span class="n">17</span><span class="o">]);</span>
<span class="kt">int</span> <span class="n">imageDataLength</span> <span class="o">=</span> <span class="n">fromBigEndianToInt32</span><span class="o">(</span><span class="n">bytes</span><span class="o">,</span> <span class="n">18</span><span class="o">);</span>
<span class="kt">byte</span><span class="o">[]</span> <span class="n">data</span> <span class="o">=</span> <span class="k">new</span> <span class="kt">byte</span><span class="o">[</span><span class="n">imageDataLength</span><span class="o">];</span>
<span class="n">assertAreEqual</span><span class="o">(</span><span class="n">maskData</span><span class="o">.</span><span class="na">getMaskRectangle</span><span class="o">().</span><span class="na">getWidth</span><span class="o">()</span> <span class="o">*</span>
<span class="n">maskData</span><span class="o">.</span><span class="na">getMaskRectangle</span><span class="o">().</span><span class="na">getHeight</span><span class="o">(),</span> <span class="n">imageDataLength</span><span class="o">);</span>
<span class="n">assertAreEqual</span><span class="o">(</span><span class="n">container</span><span class="o">.</span><span class="na">read</span><span class="o">(</span><span class="n">data</span><span class="o">),</span> <span class="n">imageDataLength</span><span class="o">);</span>
<span class="n">maskData</span><span class="o">.</span><span class="na">setImageData</span><span class="o">(</span><span class="n">data</span><span class="o">);</span>
<span class="o">}</span>
<span class="k">finally</span>
<span class="o">{</span>
<span class="n">container</span><span class="o">.</span><span class="na">dispose</span><span class="o">();</span>
<span class="o">}</span>
<span class="c1">// Just adding LayerMaskData is not enough for correct saving because channels are not updated;
</span><span class="c1"></span>
<span class="c1">// layer.setLayerMaskData(mask); // This does not add the mask channel
</span><span class="c1"></span>
<span class="c1">// Add (or update) the mask
</span><span class="c1"></span>
<span class="n">layer</span><span class="o">.</span><span class="na">addLayerMask</span><span class="o">(</span><span class="n">maskData</span><span class="o">);</span> <span class="c1">// But this adds / updates both the mask and channels!
</span><span class="c1"></span>
<span class="o">}</span>
<span class="o">}</span>
<span class="n">LocalScopeExtension</span> <span class="n">$</span> <span class="o">=</span> <span class="k">new</span> <span class="n">LocalScopeExtension</span><span class="o">();</span>
<span class="c1">// This example shows how to get, update, remove and add raster layer masks in the Adobe® Photoshop® file programmatically.
</span><span class="c1"></span>
<span class="n">PngOptions</span> <span class="n">pngOptions</span> <span class="o">=</span> <span class="k">new</span> <span class="n">PngOptions</span><span class="o">();</span>
<span class="n">pngOptions</span><span class="o">.</span><span class="na">setColorType</span><span class="o">(</span><span class="n">PngColorType</span><span class="o">.</span><span class="na">TruecolorWithAlpha</span><span class="o">);</span>
<span class="n">String</span> <span class="n">sourceFilePath</span> <span class="o">=</span> <span class="s">"FourWithMasks.psd"</span><span class="o">;</span>
<span class="n">PsdImage</span> <span class="n">image</span> <span class="o">=</span> <span class="o">(</span><span class="n">PsdImage</span><span class="o">)</span><span class="n">Image</span><span class="o">.</span><span class="na">load</span><span class="o">(</span><span class="n">sourceFilePath</span><span class="o">);</span>
<span class="k">try</span>
<span class="o">{</span>
<span class="n">Layer</span> <span class="n">layer</span> <span class="o">=</span> <span class="n">image</span><span class="o">.</span><span class="na">getLayers</span><span class="o">()[</span><span class="n">2</span><span class="o">];</span>
<span class="c1">// Get a raster mask from the layer and save it to a file
</span><span class="c1"></span>
<span class="n">$</span><span class="o">.</span><span class="na">saveRasterMask</span><span class="o">(</span><span class="s">"FourWithMasks2.msk"</span><span class="o">,</span> <span class="n">layer</span><span class="o">);</span>
<span class="c1">// Change the layer mask (invert) and save the image
</span><span class="c1"></span>
<span class="n">LayerMaskData</span> <span class="n">mask</span> <span class="o">=</span> <span class="n">layer</span><span class="o">.</span><span class="na">getLayerMaskData</span><span class="o">();</span>
<span class="kt">byte</span><span class="o">[]</span> <span class="n">maskData</span> <span class="o">=</span> <span class="n">mask</span><span class="o">.</span><span class="na">getImageData</span><span class="o">();</span>
<span class="k">for</span> <span class="o">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="n">0</span><span class="o">;</span> <span class="n">i</span> <span class="o"><</span> <span class="n">maskData</span><span class="o">.</span><span class="na">length</span><span class="o">;</span> <span class="n">i</span><span class="o">++)</span>
<span class="o">{</span>
<span class="n">maskData</span><span class="o">[</span><span class="n">i</span><span class="o">]</span> <span class="o">=</span> <span class="o">(</span><span class="kt">byte</span><span class="o">)~</span><span class="n">maskData</span><span class="o">[</span><span class="n">i</span><span class="o">];</span>
<span class="o">}</span>
<span class="c1">// Just changing LayerMaskData is enough to effect rendering
</span><span class="c1"></span>
<span class="n">image</span><span class="o">.</span><span class="na">save</span><span class="o">(</span><span class="s">"FourWithMasksUpdated2.png"</span><span class="o">,</span> <span class="n">pngOptions</span><span class="o">);</span>
<span class="c1">// But just changing LayerMaskData is not enough for correct saving because channels are not updated;
</span><span class="c1"></span>
<span class="n">layer</span><span class="o">.</span><span class="na">setLayerMaskData</span><span class="o">(</span><span class="n">mask</span><span class="o">);</span> <span class="c1">// This does not work either
</span><span class="c1"></span>
<span class="n">layer</span><span class="o">.</span><span class="na">addLayerMask</span><span class="o">(</span><span class="n">mask</span><span class="o">);</span> <span class="c1">// But this updates both the mask and channels!
</span><span class="c1"></span>
<span class="n">image</span><span class="o">.</span><span class="na">save</span><span class="o">(</span><span class="s">"FourWithMasksUpdated2.psd"</span><span class="o">);</span>
<span class="c1">// Remove a raster mask from the layer and save the image
</span><span class="c1"></span>
<span class="n">layer</span><span class="o">.</span><span class="na">setLayerMaskData</span><span class="o">(</span><span class="kc">null</span><span class="o">);</span> <span class="c1">// Just removing LayerMaskData is enough to effect rendering but not for saving to PSD format
</span><span class="c1"></span>
<span class="n">image</span><span class="o">.</span><span class="na">save</span><span class="o">(</span><span class="s">"FourWithMasksRemoved2.png"</span><span class="o">,</span> <span class="n">pngOptions</span><span class="o">);</span>
<span class="n">layer</span><span class="o">.</span><span class="na">addLayerMask</span><span class="o">(</span><span class="kc">null</span><span class="o">);</span> <span class="c1">// But this removes both the mask and the mask channel!
</span><span class="c1"></span>
<span class="n">image</span><span class="o">.</span><span class="na">save</span><span class="o">(</span><span class="s">"FourWithMasksRemoved2.psd"</span><span class="o">);</span>
<span class="c1">// Add a raster mask from the file to the layer and save the image
</span><span class="c1"></span>
<span class="n">$</span><span class="o">.</span><span class="na">addRasterMask</span><span class="o">(</span><span class="n">layer</span><span class="o">,</span> <span class="s">"raster.msk"</span><span class="o">);</span>
<span class="n">image</span><span class="o">.</span><span class="na">save</span><span class="o">(</span><span class="s">"FourWithMasksAdded2.png"</span><span class="o">,</span> <span class="n">pngOptions</span><span class="o">);</span>
<span class="n">image</span><span class="o">.</span><span class="na">save</span><span class="o">(</span><span class="s">"FourWithMasksAdded2.psd"</span><span class="o">);</span>
<span class="o">}</span>
<span class="k">finally</span>
<span class="o">{</span>
<span class="n">image</span><span class="o">.</span><span class="na">dispose</span><span class="o">();</span>
<span class="o">}</span></code></pre></div>
PSDJAVA-221: Incorrect Layer Order after you add Layer Group to empty Layer Group
<div class="highlight"><pre class="chroma"><code class="language-java" data-lang="java"> <span class="c1">// This example demonstrates how to add a nested layer group to PSD programmatically.
</span><span class="c1"></span>
<span class="n">String</span> <span class="n">dstPsdPath</span> <span class="o">=</span> <span class="s">"output.psd"</span><span class="o">;</span>
<span class="c1">// Create an image with the size of 1x1 pixels to work with
</span><span class="c1"></span>
<span class="n">PsdImage</span> <span class="n">psdImage</span> <span class="o">=</span> <span class="k">new</span> <span class="n">PsdImage</span><span class="o">(</span><span class="n">1</span><span class="o">,</span> <span class="n">1</span><span class="o">);</span>
<span class="k">try</span>
<span class="o">{</span>
<span class="c1">// Add a parent layer group ("true" means to open the layer group on start)
</span><span class="c1"></span>
<span class="n">LayerGroup</span> <span class="n">group1</span> <span class="o">=</span> <span class="n">psdImage</span><span class="o">.</span><span class="na">addLayerGroup</span><span class="o">(</span><span class="s">"Group 1"</span><span class="o">,</span> <span class="n">0</span><span class="o">,</span> <span class="kc">true</span><span class="o">);</span>
<span class="c1">// Add a nested layer group
</span><span class="c1"></span>
<span class="n">LayerGroup</span> <span class="n">group2</span> <span class="o">=</span> <span class="n">group1</span><span class="o">.</span><span class="na">addLayerGroup</span><span class="o">(</span><span class="s">"Group 2"</span><span class="o">,</span> <span class="n">0</span><span class="o">);</span>
<span class="k">if</span> <span class="o">(</span><span class="n">group1</span><span class="o">.</span><span class="na">getLayers</span><span class="o">().</span><span class="na">length</span> <span class="o">!=</span> <span class="n">2</span><span class="o">)</span>
<span class="o">{</span>
<span class="k">throw</span> <span class="k">new</span> <span class="n">RuntimeException</span><span class="o">(</span><span class="s">"Group 1 must contain two layers of Group 2."</span><span class="o">);</span>
<span class="o">}</span>
<span class="c1">// Verify that there are no exceptions on saving just created layer groups
</span><span class="c1"></span>
<span class="n">psdImage</span><span class="o">.</span><span class="na">save</span><span class="o">(</span><span class="n">dstPsdPath</span><span class="o">);</span>
<span class="o">}</span>
<span class="k">finally</span>
<span class="o">{</span>
<span class="n">psdImage</span><span class="o">.</span><span class="na">dispose</span><span class="o">();</span>
<span class="o">}</span></code></pre></div>