From 06c5e31761df1c9e7a9edc272c232a286de34dad Mon Sep 17 00:00:00 2001 From: heavenstone Date: Wed, 19 Jul 2023 15:37:33 +0900 Subject: [PATCH 1/2] Implemented ParseColorModeDataSection --- src/Psd/PsdColorModeDataSection.h | 2 ++ src/Psd/PsdParseColorModeDataSection.cpp | 40 ++++++++++++++++++++---- src/Psd/PsdParseColorModeDataSection.h | 2 +- 3 files changed, 37 insertions(+), 7 deletions(-) diff --git a/src/Psd/PsdColorModeDataSection.h b/src/Psd/PsdColorModeDataSection.h index 9f6a067..f853c2f 100644 --- a/src/Psd/PsdColorModeDataSection.h +++ b/src/Psd/PsdColorModeDataSection.h @@ -11,6 +11,8 @@ PSD_NAMESPACE_BEGIN /// \brief A struct representing the information extracted from the Color Mode Data section. struct ColorModeDataSection { + uint8_t* colorData; + uint32_t sizeOfColorData; }; PSD_NAMESPACE_END diff --git a/src/Psd/PsdParseColorModeDataSection.cpp b/src/Psd/PsdParseColorModeDataSection.cpp index ae413a4..1c36151 100644 --- a/src/Psd/PsdParseColorModeDataSection.cpp +++ b/src/Psd/PsdParseColorModeDataSection.cpp @@ -4,20 +4,42 @@ #include "PsdPch.h" #include "PsdParseColorModeDataSection.h" -#include "PsdColorModeDataSection.h" #include "PsdAllocator.h" +#include "PsdAssert.h" +#include "PsdColorModeDataSection.h" +#include "PsdDocument.h" #include "PsdMemoryUtil.h" +#include "PsdSyncFileReader.h" +#include "PsdSyncFileUtil.h" PSD_NAMESPACE_BEGIN // --------------------------------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------------------------------- -ColorModeDataSection* ParseColorModeDataSection(const Document&, File*, Allocator*) +ColorModeDataSection* ParseColorModeDataSection(const Document* document, File* file, Allocator* allocator) { - // not implemented yet. - // only indexed color and Duotone have color mode data, but both are not supported by this library at the moment. - return nullptr; + PSD_ASSERT_NOT_NULL(document); + PSD_ASSERT_NOT_NULL(file); + PSD_ASSERT_NOT_NULL(allocator); + + const Section& section = document->colorModeDataSection; + if (section.length == 0u) + { + return nullptr; + } + + ColorModeDataSection* colorModeData = memoryUtil::Allocate(allocator); + *colorModeData = ColorModeDataSection(); + + SyncFileReader reader(file); + reader.SetPosition(document->colorModeDataSection.offset); + + colorModeData->colorData = memoryUtil::AllocateArray(allocator, section.length); + colorModeData->sizeOfColorData = section.length; + reader.Read(colorModeData->colorData, section.length); + + return colorModeData; } @@ -28,7 +50,13 @@ void DestroyColorModeDataSection(ColorModeDataSection*& section, Allocator* allo PSD_ASSERT_NOT_NULL(section); PSD_ASSERT_NOT_NULL(allocator); - memoryUtil::Free(allocator, section); + if (section) { + if (section->colorData) + { + memoryUtil::FreeArray(allocator, section->colorData); + } + memoryUtil::Free(allocator, section); + } } PSD_NAMESPACE_END diff --git a/src/Psd/PsdParseColorModeDataSection.h b/src/Psd/PsdParseColorModeDataSection.h index 7c0e3a7..6b386bf 100644 --- a/src/Psd/PsdParseColorModeDataSection.h +++ b/src/Psd/PsdParseColorModeDataSection.h @@ -16,7 +16,7 @@ struct ColorModeDataSection; /// Parses the color mode data section in the document, and returns a newly created instance that needs to be freed /// by a call to \ref DestroyColorModeDataSection. /// \remark This function does not yet parse meaningful data. -ColorModeDataSection* ParseColorModeDataSection(const Document& document, File* file, Allocator* allocator); +ColorModeDataSection* ParseColorModeDataSection(const Document* document, File* file, Allocator* allocator); /// \ingroup Parser /// Destroys and nullifies the given \a section previously created by a call to \ref ParseColorModeDataSection. From 1af5dd1be0df46d056f25027918192c025ff8c7d Mon Sep 17 00:00:00 2001 From: heavenstone Date: Thu, 20 Jul 2023 09:36:27 +0900 Subject: [PATCH 2/2] fix the check for a valid section --- src/Psd/PsdParseColorModeDataSection.cpp | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/Psd/PsdParseColorModeDataSection.cpp b/src/Psd/PsdParseColorModeDataSection.cpp index 1c36151..8d75f9d 100644 --- a/src/Psd/PsdParseColorModeDataSection.cpp +++ b/src/Psd/PsdParseColorModeDataSection.cpp @@ -50,13 +50,11 @@ void DestroyColorModeDataSection(ColorModeDataSection*& section, Allocator* allo PSD_ASSERT_NOT_NULL(section); PSD_ASSERT_NOT_NULL(allocator); - if (section) { - if (section->colorData) - { - memoryUtil::FreeArray(allocator, section->colorData); - } - memoryUtil::Free(allocator, section); + if (section->colorData) + { + memoryUtil::FreeArray(allocator, section->colorData); } + memoryUtil::Free(allocator, section); } PSD_NAMESPACE_END