Example: Extended Signature (C++ only)#
This example covers creation of a LayeredFile (A Photoshop file) using the ‘extended’ signature, do note that this is almost never necessary and Example: Create simple document is the preferred (and encouraged) way to create files.
Here we read the PhotoshopFile structure directly instead of through the LayeredFile which gives us a view into the objects as they are parsed from photoshop. This is primarily for debugging or if you wish to understand how the structures are parsed
Relevant documentation links:
/*
This is the ModifyLayerStructure example but instead of using the simplified read and write signature we use the extend
one to give us direct access to the PhotoshopFile. This is a lot more verbose than the alternative presented in the other examples
The preferred method is always to interact through the LayeredFile
struct rather than directly with the low level PhotoshopAPI::PhotoshopFile although that is also possible but requires a deep understanding of the
underlying structure whereas LayeredFile abstracts the implementation details.
*/
#include "PhotoshopAPI.h"
#include <unordered_map>
#include <vector>
int main()
{
using namespace NAMESPACE_PSAPI;
auto inputFile = File("./LayeredFile.psd");
auto psDocumentPtr = std::make_unique<PhotoshopFile>();
// The PhotoshopFile struct requires a progress callback to be passed which will be
// edited by the write function, to see an example of how to interact with this callback please refer
// to the "ProgressCallbacks" example
ProgressCallback callback{};
psDocumentPtr->read(inputFile, callback);
// In this case we already know the bit depth but otherwise one could use the PhotoshopFile.m_Header.m_Depth
// variable on the PhotoshopFile to figure it out programmatically
LayeredFile<bpp8_t> layeredFile = { std::move(psDocumentPtr), "./LayeredFile.psd" };
// The Structure of the photoshop file we just opened is
// 'Group'
// 'NestedGroup'
// 'NestedImageLayer'
// 'ImageLayer'
//
// and we now want to move the 'NestedGroup' and all of its children to the scene root
// as well as delete 'ImageLayer'. It is advised to do the restructuring in steps
// as shown below to avoid trying to access a layer which no longer exists
// By not specifying a second parameter to move_layer() we tell the function to move it to the scene root
// we could however also move it under another group by passing that group as a second parameter
layeredFile.move_layer("Group/NestedGroup");
layeredFile.remove_layer("Group/ImageLayer");
// This is the alternative signature which expects a layer ptr instead of a path.
// It is useful if we already have the layer ptr from another operation like extracting data and then want to move
// it etc.
/*
auto nestedGroupLayer = layeredFile.findLayer("Group/NestedGroup");
if (nestedGroupLayer)
{
layeredFile.moveLayer(nestedGroupLayer);
}
auto imageLayer = layeredFile.findLayer();
if (imageLayer)
{
layeredFile.removeLayer(imageLayer);
}
*/
// We can now convert back to a PhotoshopFile and write out to disk
File::FileParams params = File::FileParams();
params.doRead = false;
params.forceOverwrite = true;
auto outputFile = File("./RearrangedFile.psd", params);
auto psOutDocumentPtr = layered_to_photoshop(std::move(layeredFile), "./RearrangedFile.psd");
// We pass the same callback into the write function, clearing of data will
// be handled internally
psOutDocumentPtr->write(outputFile, callback);
}