Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 42 additions & 23 deletions PhotoshopAPI/src/PhotoshopFile/LayerAndMaskInformation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1120,37 +1120,56 @@ void LayerInfo::write(File& document, const FileHeader& header, ProgressCallback
// but we do not bother with that at this point
WriteBinaryData(document, static_cast<int16_t>(m_LayerRecords.size()));

// Loop over the individual layers and compress them while also storing the channel information
// Loop over the individual layers and compress them while also storing the channel information. An exception
// escaping a std::execution::par callable calls std::terminate, so capture the first error
// and rethrow it once every layer has finished.
std::exception_ptr firstError = nullptr;
std::mutex errorMutex;
std::for_each(std::execution::par, m_ChannelImageData.begin(), m_ChannelImageData.end(),
[&](ChannelImageData& channel)
{
// Get a unique index for each of the layers to compress them in random order
const size_t index = &channel - &m_ChannelImageData[0];
callback.setTask("Compressing Layer: " + std::string(m_LayerRecords[index].m_LayerName.getString()));
std::vector<LayerRecords::ChannelInformation> lrChannelInfo;
std::vector<Enum::Compression> lrCompression;

if (header.m_Depth == Enum::BitDepth::BD_8)
{
compressedData[index] = channel.compressData<uint8_t>(header, lrChannelInfo, lrCompression);
}
else if (header.m_Depth == Enum::BitDepth::BD_16)
try
{
compressedData[index] = channel.compressData<uint16_t>(header, lrChannelInfo, lrCompression);
// Get a unique index for each of the layers to compress them in random order
const size_t index = &channel - &m_ChannelImageData[0];
callback.setTask("Compressing Layer: " + std::string(m_LayerRecords[index].m_LayerName.getString()));
std::vector<LayerRecords::ChannelInformation> lrChannelInfo;
std::vector<Enum::Compression> lrCompression;

if (header.m_Depth == Enum::BitDepth::BD_8)
{
compressedData[index] = channel.compressData<uint8_t>(header, lrChannelInfo, lrCompression);
}
else if (header.m_Depth == Enum::BitDepth::BD_16)
{
compressedData[index] = channel.compressData<uint16_t>(header, lrChannelInfo, lrCompression);
}
else if (header.m_Depth == Enum::BitDepth::BD_32)
{
compressedData[index] = channel.compressData<float32_t>(header, lrChannelInfo, lrCompression);
}
else
{
PSAPI_LOG_ERROR("LayerInfo", "Unsupported BitDepth encountered, currently only 8-, 16- and 32-bit files are supported");
}
channelInfos[index] = lrChannelInfo;
channelCompression[index] = lrCompression;
callback.setTask("Compressed Layer: " + std::string(m_LayerRecords[index].m_LayerName.getString()));
callback.increment();
}
else if (header.m_Depth == Enum::BitDepth::BD_32)
catch (...)
{
compressedData[index] = channel.compressData<float32_t>(header, lrChannelInfo, lrCompression);
std::lock_guard<std::mutex> lock(errorMutex);
if (!firstError)
{
firstError = std::current_exception();
}
}
else
{
PSAPI_LOG_ERROR("LayerInfo", "Unsupported BitDepth encountered, currently only 8-, 16- and 32-bit files are supported");
}
channelInfos[index] = lrChannelInfo;
channelCompression[index] = lrCompression;
callback.setTask("Compressed Layer: " + std::string(m_LayerRecords[index].m_LayerName.getString()));
callback.increment();
});
if (firstError)
{
std::rethrow_exception(firstError);
}

// Write the layer records
for (int i = 0; i < m_LayerRecords.size(); ++i)
Expand Down
Loading