From ee00861062a36e8721ced7fec2e8d9a86a1589a2 Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Mon, 13 Jan 2020 17:29:42 +0100 Subject: [PATCH] Add parsing of ICCP chunk --- src/ImageSharp/Formats/WebP/WebPDecoderCore.cs | 18 +++++++++++++++--- tests/Images/Input/WebP/lossless_iccp.webp | 3 +++ tests/Images/Input/WebP/lossy_iccp.webp | 3 +++ 3 files changed, 21 insertions(+), 3 deletions(-) create mode 100644 tests/Images/Input/WebP/lossless_iccp.webp create mode 100644 tests/Images/Input/WebP/lossy_iccp.webp diff --git a/src/ImageSharp/Formats/WebP/WebPDecoderCore.cs b/src/ImageSharp/Formats/WebP/WebPDecoderCore.cs index 104138c965..927ad84f75 100644 --- a/src/ImageSharp/Formats/WebP/WebPDecoderCore.cs +++ b/src/ImageSharp/Formats/WebP/WebPDecoderCore.cs @@ -8,6 +8,7 @@ using System.IO; using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Metadata; using SixLabors.ImageSharp.Metadata.Profiles.Exif; +using SixLabors.ImageSharp.Metadata.Profiles.Icc; using SixLabors.ImageSharp.PixelFormats; using SixLabors.Memory; @@ -208,13 +209,22 @@ namespace SixLabors.ImageSharp.Formats.WebP this.buffer[3] = 0; int height = BinaryPrimitives.ReadInt32LittleEndian(this.buffer) + 1; - // Optional chunks ALPH, ICCP and ANIM can follow here. Ignoring them for now. + // Optional chunks ICCP, ALPH and ANIM can follow here. WebPChunkType chunkType; if (isIccPresent) { chunkType = this.ReadChunkType(); - uint iccpChunkSize = this.ReadChunkSize(); - this.currentStream.Skip((int)iccpChunkSize); + if (chunkType is WebPChunkType.Iccp) + { + uint iccpChunkSize = this.ReadChunkSize(); + var iccpData = new byte[iccpChunkSize]; + this.currentStream.Read(iccpData, 0, (int)iccpChunkSize); + var profile = new IccProfile(iccpData); + if (profile.CheckIsValid()) + { + this.Metadata.IccProfile = profile; + } + } } if (isAnimationPresent) @@ -236,6 +246,8 @@ namespace SixLabors.ImageSharp.Formats.WebP { chunkType = this.ReadChunkType(); uint alphaChunkSize = this.ReadChunkSize(); + + // ALPH chunks will be skipped for now. this.currentStream.Skip((int)alphaChunkSize); } diff --git a/tests/Images/Input/WebP/lossless_iccp.webp b/tests/Images/Input/WebP/lossless_iccp.webp new file mode 100644 index 0000000000..a99d2686fb --- /dev/null +++ b/tests/Images/Input/WebP/lossless_iccp.webp @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:312aea5ac9557bdfa78ec95bab5c3446a97c980317f46c96a20a4b7837d0ae37 +size 68355 diff --git a/tests/Images/Input/WebP/lossy_iccp.webp b/tests/Images/Input/WebP/lossy_iccp.webp new file mode 100644 index 0000000000..a87580edfd --- /dev/null +++ b/tests/Images/Input/WebP/lossy_iccp.webp @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:46e70fecb9cfc72243dfad58b68baa58c14f12660f8d2c88c30d5e050856b059 +size 25241