Browse Source

Add additional image features into separate class

pull/1552/head
Brian Popow 7 years ago
parent
commit
8a0f53e971
  1. 46
      src/ImageSharp/Formats/WebP/WebPDecoderCore.cs
  2. 36
      src/ImageSharp/Formats/WebP/WebPFeatures.cs
  3. 4
      src/ImageSharp/Formats/WebP/WebPImageInfo.cs

46
src/ImageSharp/Formats/WebP/WebPDecoderCore.cs

@ -71,7 +71,7 @@ namespace SixLabors.ImageSharp.Formats.WebP
uint fileSize = this.ReadImageHeader();
WebPImageInfo imageInfo = this.ReadVp8Info();
if (imageInfo.IsAnimation)
if (imageInfo.Features != null && imageInfo.Features.Animation)
{
WebPThrowHelper.ThrowNotSupportedException("Animations are not supported");
}
@ -88,7 +88,10 @@ namespace SixLabors.ImageSharp.Formats.WebP
}
// There can be optional chunks after the image data, like EXIF, XMP etc.
this.ParseOptionalChunks();
if (imageInfo.Features != null)
{
this.ParseOptionalChunks(imageInfo.Features);
}
return image;
}
@ -201,7 +204,10 @@ namespace SixLabors.ImageSharp.Formats.WebP
{
Width = width,
Height = height,
IsAnimation = true
Features = new WebPFeatures()
{
Animation = true
}
};
}
@ -212,6 +218,15 @@ namespace SixLabors.ImageSharp.Formats.WebP
this.currentStream.Skip((int)alphaChunkSize);
}
var features = new WebPFeatures()
{
Animation = isAnimationPresent,
Alpha = isAlphaPresent,
ExifProfile = isExifPresent,
IccProfile = isIccPresent,
XmpMetaData = isXmpPresent
};
// A VP8 or VP8L chunk should follow here.
chunkType = this.ReadChunkType();
@ -219,9 +234,9 @@ namespace SixLabors.ImageSharp.Formats.WebP
switch (chunkType)
{
case WebPChunkType.Vp8:
return this.ReadVp8Header();
return this.ReadVp8Header(features);
case WebPChunkType.Vp8L:
return this.ReadVp8LHeader();
return this.ReadVp8LHeader(features);
}
WebPThrowHelper.ThrowImageFormatException("Unexpected chunk followed VP8X header");
@ -229,7 +244,7 @@ namespace SixLabors.ImageSharp.Formats.WebP
return new WebPImageInfo();
}
private WebPImageInfo ReadVp8Header()
private WebPImageInfo ReadVp8Header(WebPFeatures features = null)
{
this.webpMetadata.Format = WebPFormatType.Lossy;
@ -268,11 +283,12 @@ namespace SixLabors.ImageSharp.Formats.WebP
Width = width,
Height = height,
IsLossLess = false,
ImageDataSize = dataSize
ImageDataSize = dataSize,
Features = features
};
}
private WebPImageInfo ReadVp8LHeader()
private WebPImageInfo ReadVp8LHeader(WebPFeatures features = null)
{
this.webpMetadata.Format = WebPFormatType.Lossless;
@ -304,7 +320,8 @@ namespace SixLabors.ImageSharp.Formats.WebP
Width = (int)width,
Height = (int)height,
IsLossLess = true,
ImageDataSize = dataSize
ImageDataSize = dataSize,
Features = features
};
}
@ -312,7 +329,7 @@ namespace SixLabors.ImageSharp.Formats.WebP
where TPixel : struct, IPixel<TPixel>
{
// TODO: implement decoding. For simulating the decoding: skipping the chunk size bytes.
this.currentStream.Skip(imageDataSize - 10);
this.currentStream.Skip(imageDataSize - 10); // TODO: Not sure why we need to skip 10 bytes less here
}
private void ReadSimpleLossless<TPixel>(Buffer2D<TPixel> pixels, int width, int height, int imageDataSize)
@ -322,7 +339,7 @@ namespace SixLabors.ImageSharp.Formats.WebP
losslessDecoder.Decode(pixels, width, height, imageDataSize);
// TODO: implement decoding. For simulating the decoding: skipping the chunk size bytes.
this.currentStream.Skip(imageDataSize); // TODO: this does not seem to work in all cases
this.currentStream.Skip(imageDataSize + 34); // TODO: Not sure why the additional data starts at offset +34 at the moment.
}
private void ReadExtended<TPixel>(Buffer2D<TPixel> pixels, int width, int height)
@ -331,8 +348,13 @@ namespace SixLabors.ImageSharp.Formats.WebP
// TODO: implement decoding
}
private void ParseOptionalChunks()
private void ParseOptionalChunks(WebPFeatures features)
{
if (features.ExifProfile == false && features.XmpMetaData == false)
{
return;
}
while (this.currentStream.Position < this.currentStream.Length)
{
// Read chunk header.

36
src/ImageSharp/Formats/WebP/WebPFeatures.cs

@ -0,0 +1,36 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
namespace SixLabors.ImageSharp.Formats.WebP
{
/// <summary>
/// Image features of a VP8X image.
/// </summary>
public class WebPFeatures
{
/// <summary>
/// Gets or sets whether this image has a ICC Profile.
/// </summary>
public bool IccProfile { get; set; }
/// <summary>
/// Gets or sets whether this image has a alpha channel.
/// </summary>
public bool Alpha { get; set; }
/// <summary>
/// Gets or sets whether this image has a EXIF Profile.
/// </summary>
public bool ExifProfile { get; set; }
/// <summary>
/// Gets or sets whether this image has XMP Metadata.
/// </summary>
public bool XmpMetaData { get; set; }
/// <summary>
/// Gets or sets whether this image is a animation.
/// </summary>
public bool Animation { get; set; }
}
}

4
src/ImageSharp/Formats/WebP/WebPImageInfo.cs

@ -21,9 +21,9 @@ namespace SixLabors.ImageSharp.Formats.WebP
public bool IsLossLess { get; set; }
/// <summary>
/// Gets or sets whether this image is a animation.
/// Gets or sets additional features present in a VP8X image.
/// </summary>
public bool IsAnimation { get; set; }
public WebPFeatures Features { get; set; }
/// <summary>
/// The bytes of the image payload.

Loading…
Cancel
Save