Content

Written by Esben Elfving   
Tuesday, 08 January 2013 17:21

Creating PSD files with layers

 

This article will show how to work with and create layered images using the latest version of the MDKGraphicsEngine (MDK) SDK. In production environments layered images can be very useful in the areas of copyright protection and the ability to embed extra image data into a processed image without necessarily having to "destroy" the output image. Embedding a copyright notice either as a text string or a logo into a saved PSD image as an extra layer can be done using MDK. Each additional layer can be saved with custom values for opacity, blending mode and layer name. It is also possible to add a layer with visibility disabled and thereby having this particular layer ignored when the final composite image is generated during saving. All embedded layers can be opened in and handled by image processing software which supports PSD files and layers.

The MDK 2.6 Imaging API supports all blending modes known from Adobe PhotoshopTM and they can be used both to do a direct blend of two images before saving and as a layer blending mode. For a detailed explaination of the different supported blending modes please take a look at the following link.

For all sample code used in this article error handling, color space and dimension checks are left out for simplicity. These checks should be added to the code for safe execution in real world applications. Sample code in this article uses C# syntax but MDK supports all managed .NET languages and direct unmanaged C/C++.

All samples will be using the following four images. Click each image to see it in original size.

Image used as background layer Image used as layer 1 Image used as layer 2 MDK Logo

 

The first sample shows how to add a simple logo or watermark to an image. This sample does not make use of layers but is shown to give the reader an idea of how blending of layers is done during composition of PSD images. The sample code blends the MDK Logo into the background image using the Multiply blending mode. The Multiply blending mode ignores the color white and is therefore perfectly suited for this purpose.

//first we open the image which is the blend target
MDK.Image image = new MDK.Image(@"media00.tif", MDK.GraphicsEngine.GetFileformat(@"media00.tif"));
// next open the blend source image
MDK.Image blend = new MDK.Image(@"MDKLogo.png", MDK.GraphicsEngine.GetFileformat(@"MDKLogo.png"));
// blend the source image onto the target image using Multiply blend function and opacity at 100%
// Align at top-left position
image.Blend(blend, MDK.BlendFunction.Multiply, 100, MDK.Alignment.TopLeft);
// we can now Dispose the blend source as it is not used anymore
blend.Dispose();
// save the blended image as a PNG image
image.SaveToFile(@"BlendImages.png", MDK.FileFormat.PNG);
// and finally dispose the last image
image.Dispose();

The output image with the MDK logo shown in the top-left position looks like this:

 

The second sample is a bit more complicated and uses all four sample images. The first image will be used as the background image onto which the other three sample images will be added as layers. Image two (the cup) is added as layer 1 but is disabled and therefore not blended into the composite image. Image three (the high-rise) is blended onto the background using Darken blend mode and with an opacity of 50%. The MDK Logo is added last using the Multiply blend mode with an opacity of 40%.

 

//Enable support for PSD layers
MDK.GraphicsEngine.GetSystemProperties().SetBooleanProperty(MDK.BooleanProperty.WritePhotoshopLayerData, true);
// open all four images
MDK.Image backgroundImage = new MDK.Image(@"media00.tif", MDK.GraphicsEngine.GetFileformat(@"media00.tif"));
MDK.Image media1Image = new MDK.Image(@"media01.tif", MDK.GraphicsEngine.GetFileformat(@"media01.tif"));
MDK.Image media2Image = new MDK.Image(@"media02.tif", MDK.GraphicsEngine.GetFileformat(@"media02.tif"));
MDK.Image logoImage = new MDK.Image(@"MDKLogo.png", MDK.GraphicsEngine.GetFileformat(@"MDKLogo.png"));
// Add the images: media1Image, media2Image and logoImage as layers to the background image
backgroundImage.GetMetadata().AddLayerFromImage(media1Image);
backgroundImage.GetMetadata().AddLayerFromImage(media2Image);
backgroundImage.GetMetadata().AddLayerFromImage(logoImage);
// the three images has now been copied into the background image and may be disposed (saves memory)
media1Image.Dispose();
media2Image.Dispose();
logoImage.Dispose();
// change name for the background image. Index 0 is always the background image onto which the
// other images will be blended.
MDK.Layer bgLayer = backgroundImage.GetMetadata().GetLayerAtIndex(0);
bgLayer.Name = "Background Layer";
bgLayer.Save();
// the media1Image will be added and given a name but with visibility disabled
MDK.Layer disabledLayer = backgroundImage.GetMetadata().GetLayerAtIndex(1);
disabledLayer.Visible = false;
disabledLayer.Name = @"Disabled";
disabledLayer.Save();
// layer at index 2 will be configured to use the Darken blend mode and with opacity 50%
MDK.Layer darkenLayer = backgroundImage.GetMetadata().GetLayerAtIndex(2);
darkenLayer.Name = @"Darken 50%";
darkenLayer.BlendFunction = MDK.BlendFunction.Darken;
darkenLayer.Opacity = 50;
darkenLayer.Save();
// The fourth layer is the MDK Logo added with Multiply blend function and 40% opacity
MDK.Layer logoLayer = backgroundImage.GetMetadata().GetLayerAtIndex(3);
logoLayer.Name = @"Middleware Denmark";
logoLayer.BlendFunction = MDK.BlendFunction.Multiply;
logoLayer.Opacity = 40;
logoLayer.Save();
// and last: save the PSD image
backgroundImage.SaveToFile(@"AddLayers.psd", MDK.FileFormat.PSD);

 

The resulting PSD image can be downloaded here

The composite part of AddLayers.psd saved as a PNG image looks like this:

The above samples show only a subset of all the features MDK offers in the area of blending and working with layers.

[the end..]

Last Updated on Saturday, 08 June 2013 15:26
 
 
Copyright © 2013, Middleware Denmark ApS