Browse Source

Make quantizers per instance

Former-commit-id: a7f37947dd2e4ca8dd3f8a15cf0eab84c3974376
Former-commit-id: b0a11a6e64ee0f028124483ad83eccd2ca0e849a
af/merge-core
James South 12 years ago
parent
commit
05d8f1a01e
  1. 30
      src/ImageProcessor/Imaging/Formats/GifFormat.cs
  2. 30
      src/ImageProcessor/Imaging/Formats/PngFormat.cs

30
src/ImageProcessor/Imaging/Formats/GifFormat.cs

@ -21,8 +21,13 @@ namespace ImageProcessor.Imaging.Formats
/// <summary> /// <summary>
/// Provides the necessary information to support gif images. /// Provides the necessary information to support gif images.
/// </summary> /// </summary>
public class GifFormat : FormatBase public class GifFormat : FormatBase, IQuantizableImageFormat
{ {
/// <summary>
/// The quantizer for reducing the image palette.
/// </summary>
private IQuantizer quantizer = new OctreeQuantizer(255, 8);
/// <summary> /// <summary>
/// Gets the file headers. /// Gets the file headers.
/// </summary> /// </summary>
@ -67,6 +72,22 @@ namespace ImageProcessor.Imaging.Formats
} }
} }
/// <summary>
/// Gets or sets the quantizer for reducing the image palette.
/// </summary>
public IQuantizer Quantizer
{
get
{
return this.quantizer;
}
set
{
this.quantizer = value;
}
}
/// <summary> /// <summary>
/// Applies the given processor the current image. /// Applies the given processor the current image.
/// </summary> /// </summary>
@ -78,12 +99,11 @@ namespace ImageProcessor.Imaging.Formats
if (decoder.IsAnimated) if (decoder.IsAnimated)
{ {
OctreeQuantizer quantizer = new OctreeQuantizer(255, 8);
GifEncoder encoder = new GifEncoder(null, null, decoder.LoopCount); GifEncoder encoder = new GifEncoder(null, null, decoder.LoopCount);
foreach (GifFrame frame in decoder.GifFrames) foreach (GifFrame frame in decoder.GifFrames)
{ {
factory.Image = frame.Image; factory.Image = frame.Image;
frame.Image = quantizer.Quantize(processor.Invoke(factory)); frame.Image = this.Quantizer.Quantize(processor.Invoke(factory));
encoder.AddFrame(frame); encoder.AddFrame(frame);
} }
@ -109,7 +129,7 @@ namespace ImageProcessor.Imaging.Formats
{ {
if (!FormatUtilities.IsAnimated(image)) if (!FormatUtilities.IsAnimated(image))
{ {
image = new OctreeQuantizer(255, 8).Quantize(image); image = this.Quantizer.Quantize(image);
} }
return base.Save(stream, image); return base.Save(stream, image);
@ -128,7 +148,7 @@ namespace ImageProcessor.Imaging.Formats
{ {
if (!FormatUtilities.IsAnimated(image)) if (!FormatUtilities.IsAnimated(image))
{ {
image = new OctreeQuantizer(255, 8).Quantize(image); image = this.Quantizer.Quantize(image);
} }
return base.Save(path, image); return base.Save(path, image);

30
src/ImageProcessor/Imaging/Formats/PngFormat.cs

@ -14,13 +14,19 @@ namespace ImageProcessor.Imaging.Formats
using System.Drawing.Imaging; using System.Drawing.Imaging;
using System.IO; using System.IO;
using ImageProcessor.Imaging.Quantizers;
using ImageProcessor.Imaging.Quantizers.WuQuantizer; using ImageProcessor.Imaging.Quantizers.WuQuantizer;
/// <summary> /// <summary>
/// Provides the necessary information to support png images. /// Provides the necessary information to support png images.
/// </summary> /// </summary>
public class PngFormat : FormatBase public class PngFormat : FormatBase, IQuantizableImageFormat
{ {
/// <summary>
/// The quantizer for reducing the image palette.
/// </summary>
private IQuantizer quantizer = new WuQuantizer();
/// <summary> /// <summary>
/// Gets the file headers. /// Gets the file headers.
/// </summary> /// </summary>
@ -34,8 +40,6 @@ namespace ImageProcessor.Imaging.Formats
/// <summary> /// <summary>
/// Gets the list of file extensions. /// Gets the list of file extensions.
/// Obviously png8 isn't a valid file extension but it's a neat way to
/// add the value to the format method detection.
/// </summary> /// </summary>
public override string[] FileExtensions public override string[] FileExtensions
{ {
@ -67,6 +71,22 @@ namespace ImageProcessor.Imaging.Formats
} }
} }
/// <summary>
/// Gets or sets the quantizer for reducing the image palette.
/// </summary>
public IQuantizer Quantizer
{
get
{
return this.quantizer;
}
set
{
this.quantizer = value;
}
}
/// <summary> /// <summary>
/// Saves the current image to the specified output stream. /// Saves the current image to the specified output stream.
/// </summary> /// </summary>
@ -79,7 +99,7 @@ namespace ImageProcessor.Imaging.Formats
{ {
if (this.IsIndexed) if (this.IsIndexed)
{ {
image = new WuQuantizer().Quantize(image); image = this.Quantizer.Quantize(image);
} }
return base.Save(stream, image); return base.Save(stream, image);
@ -98,7 +118,7 @@ namespace ImageProcessor.Imaging.Formats
{ {
if (this.IsIndexed) if (this.IsIndexed)
{ {
image = new WuQuantizer().Quantize(image); image = this.Quantizer.Quantize(image);
} }
return base.Save(path, image); return base.Save(path, image);

Loading…
Cancel
Save