-
-
Notifications
You must be signed in to change notification settings - Fork 852
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Gif and Quantization Improvements #637
Merged
Merged
Changes from 15 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
ad65b30
Stub color table mode enum.
JimBobSquarePants b208be8
Can now encode gifs with global palette
JimBobSquarePants 9b1270f
Minor improvements to diffusion
JimBobSquarePants 4ca807e
Use DistanceSquared
JimBobSquarePants b71c74d
Use single cache base.
JimBobSquarePants cfdc75e
Improve lookup logic
JimBobSquarePants becc80b
Make correct method virtual
JimBobSquarePants 07064e9
Smarter dithering.
JimBobSquarePants 72acc7d
Merge branch 'master' into js/faster-gif
JimBobSquarePants c3d38ba
Refactor to better use base classes.
JimBobSquarePants 9a12d09
Update tests
JimBobSquarePants 4c04817
Update reference images
JimBobSquarePants ab234b3
Handle CI craziness.
JimBobSquarePants 891f6eb
Merge branch 'master' into js/faster-gif
JimBobSquarePants 93585fd
try to fine-tune tolerance in PngEncoderTests + better Rgba64.ToString()
antonfirsov 52c9ca4
pushed a bad value accidentally in my previous commit
antonfirsov ad8c2e2
Merge branch 'master' into js/faster-gif
JimBobSquarePants File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright (c) Six Labors and contributors. | ||
// Licensed under the Apache License, Version 2.0. | ||
|
||
namespace SixLabors.ImageSharp.Formats.Gif | ||
{ | ||
/// <summary> | ||
/// Provides enumeration for the available Gif color table modes. | ||
/// </summary> | ||
public enum GifColorTableMode | ||
{ | ||
/// <summary> | ||
/// A single color table is calculated from the first frame and reused for subsequent frames. | ||
/// </summary> | ||
Global, | ||
|
||
/// <summary> | ||
/// A unique color table is calculated for each frame. | ||
/// </summary> | ||
Local | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,9 @@ namespace SixLabors.ImageSharp.Formats.Gif | |
/// </summary> | ||
internal sealed class GifEncoderCore | ||
{ | ||
/// <summary> | ||
/// Used for allocating memory during procesing operations. | ||
/// </summary> | ||
private readonly MemoryAllocator memoryAllocator; | ||
|
||
/// <summary> | ||
|
@@ -27,15 +30,20 @@ internal sealed class GifEncoderCore | |
private readonly byte[] buffer = new byte[20]; | ||
|
||
/// <summary> | ||
/// Gets the text encoding used to write comments. | ||
/// The text encoding used to write comments. | ||
/// </summary> | ||
private readonly Encoding textEncoding; | ||
|
||
/// <summary> | ||
/// Gets or sets the quantizer used to generate the color palette. | ||
/// The quantizer used to generate the color palette. | ||
/// </summary> | ||
private readonly IQuantizer quantizer; | ||
|
||
/// <summary> | ||
/// The color table mode: Global or local. | ||
/// </summary> | ||
private readonly GifColorTableMode colorTableMode; | ||
|
||
/// <summary> | ||
/// A flag indicating whether to ingore the metadata when writing the image. | ||
/// </summary> | ||
|
@@ -56,6 +64,7 @@ public GifEncoderCore(MemoryAllocator memoryAllocator, IGifEncoderOptions option | |
this.memoryAllocator = memoryAllocator; | ||
this.textEncoding = options.TextEncoding ?? GifConstants.DefaultEncoding; | ||
this.quantizer = options.Quantizer; | ||
this.colorTableMode = options.ColorTableMode; | ||
this.ignoreMetadata = options.IgnoreMetadata; | ||
} | ||
|
||
|
@@ -72,45 +81,95 @@ public void Encode<TPixel>(Image<TPixel> image, Stream stream) | |
Guard.NotNull(stream, nameof(stream)); | ||
|
||
// Quantize the image returning a palette. | ||
QuantizedFrame<TPixel> quantized = this.quantizer.CreateFrameQuantizer<TPixel>().QuantizeFrame(image.Frames.RootFrame); | ||
QuantizedFrame<TPixel> quantized = | ||
this.quantizer.CreateFrameQuantizer<TPixel>().QuantizeFrame(image.Frames.RootFrame); | ||
|
||
// Get the number of bits. | ||
this.bitDepth = ImageMaths.GetBitsNeededForColorDepth(quantized.Palette.Length).Clamp(1, 8); | ||
|
||
int index = this.GetTransparentIndex(quantized); | ||
|
||
// Write the header. | ||
this.WriteHeader(stream); | ||
|
||
// Write the LSD. We'll use local color tables for now. | ||
this.WriteLogicalScreenDescriptor(image, stream, index); | ||
// Write the LSD. | ||
int index = this.GetTransparentIndex(quantized); | ||
bool useGlobalTable = this.colorTableMode.Equals(GifColorTableMode.Global); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you swear you never coded in Java? 😄 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've read a lot of Java source 😄 |
||
this.WriteLogicalScreenDescriptor(image, index, useGlobalTable, stream); | ||
|
||
if (useGlobalTable) | ||
{ | ||
this.WriteColorTable(quantized, stream); | ||
} | ||
|
||
// Write the first frame. | ||
// Write the comments. | ||
this.WriteComments(image.MetaData, stream); | ||
|
||
// Write additional frames. | ||
// Write application extension to allow additional frames. | ||
if (image.Frames.Count > 1) | ||
{ | ||
this.WriteApplicationExtension(stream, image.MetaData.RepeatCount); | ||
} | ||
|
||
if (useGlobalTable) | ||
{ | ||
this.EncodeGlobal(image, quantized, index, stream); | ||
} | ||
else | ||
{ | ||
this.EncodeLocal(image, quantized, stream); | ||
} | ||
|
||
// Clean up. | ||
quantized?.Dispose(); | ||
quantized = null; | ||
|
||
// TODO: Write extension etc | ||
stream.WriteByte(GifConstants.EndIntroducer); | ||
} | ||
|
||
private void EncodeGlobal<TPixel>(Image<TPixel> image, QuantizedFrame<TPixel> quantized, int transparencyIndex, Stream stream) | ||
where TPixel : struct, IPixel<TPixel> | ||
{ | ||
var palleteQuantizer = new PaletteQuantizer(this.quantizer.Diffuser); | ||
|
||
for (int i = 0; i < image.Frames.Count; i++) | ||
{ | ||
ImageFrame<TPixel> frame = image.Frames[i]; | ||
|
||
this.WriteGraphicalControlExtension(frame.MetaData, transparencyIndex, stream); | ||
this.WriteImageDescriptor(frame, false, stream); | ||
|
||
if (i == 0) | ||
{ | ||
this.WriteImageData(quantized, stream); | ||
} | ||
else | ||
{ | ||
using (QuantizedFrame<TPixel> paletteQuantized = palleteQuantizer.CreateFrameQuantizer(() => quantized.Palette).QuantizeFrame(frame)) | ||
{ | ||
this.WriteImageData(paletteQuantized, stream); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private void EncodeLocal<TPixel>(Image<TPixel> image, QuantizedFrame<TPixel> quantized, Stream stream) | ||
where TPixel : struct, IPixel<TPixel> | ||
{ | ||
foreach (ImageFrame<TPixel> frame in image.Frames) | ||
{ | ||
if (quantized == null) | ||
{ | ||
quantized = this.quantizer.CreateFrameQuantizer<TPixel>().QuantizeFrame(frame); | ||
} | ||
|
||
this.WriteGraphicalControlExtension(frame.MetaData, stream, this.GetTransparentIndex(quantized)); | ||
this.WriteImageDescriptor(frame, stream); | ||
this.WriteGraphicalControlExtension(frame.MetaData, this.GetTransparentIndex(quantized), stream); | ||
this.WriteImageDescriptor(frame, true, stream); | ||
this.WriteColorTable(quantized, stream); | ||
this.WriteImageData(quantized, stream); | ||
|
||
quantized?.Dispose(); | ||
quantized = null; // So next frame can regenerate it | ||
} | ||
|
||
// TODO: Write extension etc | ||
stream.WriteByte(GifConstants.EndIntroducer); | ||
} | ||
|
||
/// <summary> | ||
|
@@ -159,12 +218,13 @@ private void WriteHeader(Stream stream) | |
/// </summary> | ||
/// <typeparam name="TPixel">The pixel format.</typeparam> | ||
/// <param name="image">The image to encode.</param> | ||
/// <param name="stream">The stream to write to.</param> | ||
/// <param name="transparencyIndex">The transparency index to set the default background index to.</param> | ||
private void WriteLogicalScreenDescriptor<TPixel>(Image<TPixel> image, Stream stream, int transparencyIndex) | ||
/// <param name="useGlobalTable">Whether to use a global or local color table.</param> | ||
/// <param name="stream">The stream to write to.</param> | ||
private void WriteLogicalScreenDescriptor<TPixel>(Image<TPixel> image, int transparencyIndex, bool useGlobalTable, Stream stream) | ||
where TPixel : struct, IPixel<TPixel> | ||
{ | ||
byte packedValue = GifLogicalScreenDescriptor.GetPackedValue(false, this.bitDepth - 1, false, this.bitDepth - 1); | ||
byte packedValue = GifLogicalScreenDescriptor.GetPackedValue(useGlobalTable, this.bitDepth - 1, false, this.bitDepth - 1); | ||
|
||
var descriptor = new GifLogicalScreenDescriptor( | ||
width: (ushort)image.Width, | ||
|
@@ -243,18 +303,18 @@ private void WriteComments(ImageMetaData metadata, Stream stream) | |
/// Writes the graphics control extension to the stream. | ||
/// </summary> | ||
/// <param name="metaData">The metadata of the image or frame.</param> | ||
/// <param name="stream">The stream to write to.</param> | ||
/// <param name="transparencyIndex">The index of the color in the color palette to make transparent.</param> | ||
private void WriteGraphicalControlExtension(ImageFrameMetaData metaData, Stream stream, int transparencyIndex) | ||
/// <param name="stream">The stream to write to.</param> | ||
private void WriteGraphicalControlExtension(ImageFrameMetaData metaData, int transparencyIndex, Stream stream) | ||
{ | ||
byte packedValue = GifGraphicControlExtension.GetPackedValue( | ||
disposalMethod: metaData.DisposalMethod, | ||
transparencyFlag: transparencyIndex > -1); | ||
|
||
var extension = new GifGraphicControlExtension( | ||
packed: packedValue, | ||
transparencyIndex: unchecked((byte)transparencyIndex), | ||
delayTime: (ushort)metaData.FrameDelay); | ||
delayTime: (ushort)metaData.FrameDelay, | ||
transparencyIndex: unchecked((byte)transparencyIndex)); | ||
|
||
this.WriteExtension(extension, stream); | ||
} | ||
|
@@ -281,15 +341,16 @@ public void WriteExtension(IGifExtension extension, Stream stream) | |
/// </summary> | ||
/// <typeparam name="TPixel">The pixel format.</typeparam> | ||
/// <param name="image">The <see cref="ImageFrame{TPixel}"/> to be encoded.</param> | ||
/// <param name="hasColorTable">Whether to use the global color table.</param> | ||
/// <param name="stream">The stream to write to.</param> | ||
private void WriteImageDescriptor<TPixel>(ImageFrame<TPixel> image, Stream stream) | ||
private void WriteImageDescriptor<TPixel>(ImageFrame<TPixel> image, bool hasColorTable, Stream stream) | ||
where TPixel : struct, IPixel<TPixel> | ||
{ | ||
byte packedValue = GifImageDescriptor.GetPackedValue( | ||
localColorTableFlag: true, | ||
localColorTableFlag: hasColorTable, | ||
interfaceFlag: false, | ||
sortFlag: false, | ||
localColorTableSize: (byte)this.bitDepth); // Note: we subtract 1 from the colorTableSize writing | ||
localColorTableSize: (byte)this.bitDepth); | ||
|
||
var descriptor = new GifImageDescriptor( | ||
left: 0, | ||
|
@@ -342,9 +403,9 @@ private void WriteColorTable<TPixel>(QuantizedFrame<TPixel> image, Stream stream | |
private void WriteImageData<TPixel>(QuantizedFrame<TPixel> image, Stream stream) | ||
where TPixel : struct, IPixel<TPixel> | ||
{ | ||
using (var encoder = new LzwEncoder(this.memoryAllocator, image.Pixels, (byte)this.bitDepth)) | ||
using (var encoder = new LzwEncoder(this.memoryAllocator, (byte)this.bitDepth)) | ||
{ | ||
encoder.Encode(stream); | ||
encoder.Encode(image.GetPixelSpan(), stream); | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should probably allow the user to select the baseline frame.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did think about this but if someone creates a custom
PaletteQuantizer
implementation then it doesn't matter what frame is used. Plus it would vary from image to image.