diff --git a/PhotoshopAPI/src/PhotoshopFile/LayerAndMaskInformation.cpp b/PhotoshopAPI/src/PhotoshopFile/LayerAndMaskInformation.cpp index 5b747ae2..3f5253b3 100644 --- a/PhotoshopAPI/src/PhotoshopFile/LayerAndMaskInformation.cpp +++ b/PhotoshopAPI/src/PhotoshopFile/LayerAndMaskInformation.cpp @@ -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(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 lrChannelInfo; - std::vector lrCompression; - - if (header.m_Depth == Enum::BitDepth::BD_8) - { - compressedData[index] = channel.compressData(header, lrChannelInfo, lrCompression); - } - else if (header.m_Depth == Enum::BitDepth::BD_16) + try { - compressedData[index] = channel.compressData(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 lrChannelInfo; + std::vector lrCompression; + + if (header.m_Depth == Enum::BitDepth::BD_8) + { + compressedData[index] = channel.compressData(header, lrChannelInfo, lrCompression); + } + else if (header.m_Depth == Enum::BitDepth::BD_16) + { + compressedData[index] = channel.compressData(header, lrChannelInfo, lrCompression); + } + else if (header.m_Depth == Enum::BitDepth::BD_32) + { + compressedData[index] = channel.compressData(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(header, lrChannelInfo, lrCompression); + std::lock_guard 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)