Browse Source

A bit more gif encoder work

Not going well 😦


Former-commit-id: 0fc05546fcc3a29e61620da83b7421ee4601f18e
Former-commit-id: 13403576c1acfa541456e97568852d845e9a8a42
Former-commit-id: f81fa27c751ee3171e9e9f330133d2bfa671d081
pull/17/head
James South 11 years ago
parent
commit
67f3a2fb2d
  1. 4
      old/src/ImageProcessor/Imaging/Quantizers/IQuantizer.cs
  2. 4
      src/ImageProcessor/Common/Extensions/ByteExtensions.cs
  3. 2
      src/ImageProcessor/Formats/Gif/GifDecoderCore.cs
  4. 30
      src/ImageProcessor/Formats/Gif/GifEncoder.cs
  5. 27
      src/ImageProcessor/Formats/Gif/Quantizer/IQuantizer.cs
  6. 55
      src/ImageProcessor/Formats/Gif/Quantizer/OctreeQuantizer.cs
  7. 51
      src/ImageProcessor/Formats/Gif/Quantizer/Quantizer.cs
  8. 2
      src/ImageProcessor/Formats/Png/GrayscaleReader.cs
  9. 2
      src/ImageProcessor/Formats/Png/PaletteIndexReader.cs
  10. 2
      src/ImageProcessor/Formats/Png/TrueColorReader.cs
  11. 3
      src/ImageProcessor/ImageProcessor.csproj
  12. 1
      src/ImageProcessor/ImageProcessor.csproj.DotSettings
  13. 2
      src/ImageProcessor/Settings.StyleCop

4
old/src/ImageProcessor/Imaging/Quantizers/IQuantizer.cs

@ -4,7 +4,7 @@
// Licensed under the Apache License, Version 2.0.
// </copyright>
// <summary>
// The Quantizer interface for allowing quantization of images.
// Encapsulates methods to calculate the color palette of an image.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
@ -13,7 +13,7 @@ namespace ImageProcessor.Imaging.Quantizers
using System.Drawing;
/// <summary>
/// The Quantizer interface for allowing quantization of images.
/// Encapsulates methods to calculate the color palette of an image.
/// </summary>
public interface IQuantizer
{

4
src/ImageProcessor/Common/Extensions/ByteExtensions.cs

@ -25,14 +25,14 @@
result = new byte[bytes.Length * 8 / bits];
int factor = (int)Math.Pow(2, bits) - 1;
int mask = (0xFF >> (8 - bits));
int mask = 0xFF >> (8 - bits);
int resultOffset = 0;
foreach (byte b in bytes)
{
for (int shift = 0; shift < 8; shift += bits)
{
int colorIndex = (((b) >> (8 - bits - shift)) & mask) * (255 / factor);
int colorIndex = ((b >> (8 - bits - shift)) & mask) * (255 / factor);
result[resultOffset] = (byte)colorIndex;

2
src/ImageProcessor/Formats/Gif/GifDecoderCore.cs

@ -32,6 +32,8 @@
this.image = image;
this.currentStream = stream;
// Skip the identifier
this.currentStream.Seek(6, SeekOrigin.Current);
this.ReadLogicalScreenDescriptor();

30
src/ImageProcessor/Formats/Gif/GifEncoder.cs

@ -2,7 +2,6 @@
namespace ImageProcessor.Formats
{
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
public class GifEncoder : IImageEncoder
@ -32,10 +31,7 @@ namespace ImageProcessor.Formats
/// <summary>
/// Gets the default file extension for this encoder.
/// </summary>
public string Extension
{
get { return "GIF"; }
}
public string Extension => "GIF";
/// <summary>
/// Returns a value indicating whether the <see cref="IImageDecoder"/> supports the specified
@ -53,6 +49,11 @@ namespace ImageProcessor.Formats
return extension.Equals("GIF", StringComparison.OrdinalIgnoreCase);
}
/// <summary>
/// Encodes the image to the specified stream from the <see cref="ImageBase"/>.
/// </summary>
/// <param name="image">The <see cref="ImageBase"/> to encode from.</param>
/// <param name="stream">The <see cref="Stream"/> to encode the image data to.</param>
public void Encode(ImageBase image, Stream stream)
{
Guard.NotNull(image, "image");
@ -73,6 +74,8 @@ namespace ImageProcessor.Formats
this.WriteGlobalLogicalScreenDescriptor(stream, descriptor);
throw new System.NotImplementedException();
}
@ -80,15 +83,24 @@ namespace ImageProcessor.Formats
{
this.WriteShort(stream, descriptor.Width);
this.WriteShort(stream, descriptor.Width);
int bitdepth = this.GetBitsNeededForColorDepth(descriptor.GlobalColorTableSize) - 1;
int packed = 0x80 | // 1 : global color table flag = 1 (gct used)
int size = descriptor.GlobalColorTableSize;
int bitdepth = this.GetBitsNeededForColorDepth(size) - 1;
int packed = 0x80 | // 1 : global color table flag = 1 (GCT used)
0x70 | // 2-4 : color resolution
0x00 | // 5 : gct sort flag = 0
bitdepth; // 6-8 : gct size`
0x00 | // 5 : GCT sort flag = 0
bitdepth; // 6-8 : GCT size assume 1:1
this.WriteByte(stream, packed);
this.WriteByte(stream, descriptor.BackgroundColorIndex); // Background Color Index
this.WriteByte(stream, descriptor.PixelAspectRatio); // Pixel aspect ratio
// Write the global color table.
this.WriteColorTable(stream, size);
}
private void WriteColorTable(Stream stream, int size)
{
}
/// <summary>

27
src/ImageProcessor/Formats/Gif/Quantizer/IQuantizer.cs

@ -0,0 +1,27 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="IQuantizer.cs" company="James South">
// Copyright © James South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
// <summary>
// Provides methods for allowing quantization of images pixels.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace ImageProcessor.Formats
{
/// <summary>
/// Provides methods for allowing quantization of images pixels.
/// </summary>
public interface IQuantizer
{
/// <summary>
/// Quantize an image and return the resulting output pixels.
/// </summary>
/// <param name="image">The image to quantize.</param>
/// <returns>
/// A <see cref="T:byte[]"/> representing a quantized version of the image pixels.
/// </returns>
byte[] Quantize(ImageBase image);
}
}

55
src/ImageProcessor/Formats/Gif/Quantizer/OctreeQuantizer.cs

@ -0,0 +1,55 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="OctreeQuantizer.cs" company="James South">
// Copyright © James South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
// <summary>
// Encapsulates methods to calculate the color palette of an image using an Octree pattern.
// <see href="http://msdn.microsoft.com/en-us/library/aa479306.aspx" />
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace ImageProcessor.Formats
{
/// <summary>
/// Encapsulates methods to calculate the color palette of an image using an Octree pattern.
/// <see href="http://msdn.microsoft.com/en-us/library/aa479306.aspx"/>
/// </summary>
public class OctreeQuantizer : Quantizer
{
/// <summary>
/// Maximum allowed color depth
/// </summary>
private readonly int maxColors;
/// <summary>
/// Initializes a new instance of the <see cref="OctreeQuantizer"/> class.
/// </summary>
/// <remarks>
/// The Octree quantizer is a two pass algorithm. The initial pass sets up the Octree,
/// the second pass quantizes a color based on the nodes in the tree.
/// <para>
/// Defaults to return a maximum of 255 colors plus transparency with 8 significant bits.
/// </para>
/// </remarks>
public OctreeQuantizer()
: this(255, 8)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="OctreeQuantizer"/> class.
/// </summary>
/// <remarks>
/// The Octree quantizer is a two pass algorithm. The initial pass sets up the Octree,
/// the second pass quantizes a color based on the nodes in the tree
/// </remarks>
/// <param name="maxColors">The maximum number of colors to return</param>
/// <param name="maxColorBits">The number of significant bits</param>
public OctreeQuantizer(int maxColors, int maxColorBits)
: base(false)
{
Guard.LessEquals(maxColors, 255, "maxColors");
}
}
}

51
src/ImageProcessor/Formats/Gif/Quantizer/Quantizer.cs

@ -0,0 +1,51 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Quantizer.cs" company="James South">
// Copyright © James South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
// <summary>
// Encapsulates methods to calculate the color palette of an image.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace ImageProcessor.Formats
{
/// <summary>
/// Encapsulates methods to calculate the color palette of an image.
/// </summary>
public abstract class Quantizer : IQuantizer
{
/// <summary>
/// Flag used to indicate whether a single pass or two passes are needed for quantization.
/// </summary>
private readonly bool singlePass;
/// <summary>
/// Initializes a new instance of the <see cref="Quantizer"/> class.
/// </summary>
/// <param name="singlePass">
/// If true, the quantization only needs to loop through the source pixels once
/// </param>
/// <remarks>
/// If you construct this class with a true value for singlePass, then the code will, when quantizing your image,
/// only call the 'QuantizeImage' function. If two passes are required, the code will call 'InitialQuantizeImage'
/// and then 'QuantizeImage'.
/// </remarks>
protected Quantizer(bool singlePass)
{
this.singlePass = singlePass;
}
/// <summary>
/// Quantize an image and return the resulting output pixels.
/// </summary>
/// <param name="image">The image to quantize.</param>
/// <returns>
/// A <see cref="T:byte[]"/> representing a quantized version of the image pixels.
/// </returns>
public byte[] Quantize(ImageBase image)
{
throw new System.NotImplementedException();
}
}
}

2
src/ImageProcessor/Formats/Png/GrayscaleReader.cs

@ -41,7 +41,7 @@ namespace ImageProcessor.Formats
/// Reads the specified scanline.
/// </summary>
/// <param name="scanline">The scanline.</param>
/// <param name="pixels">The pixels, where the colors should be stored in RGBA format.</param>
/// <param name="pixels">The pixels, where the colors should be stored in BGRA format.</param>
/// <param name="header">
/// The header, which contains information about the png file, like
/// the width of the image and the height.

2
src/ImageProcessor/Formats/Png/PaletteIndexReader.cs

@ -47,7 +47,7 @@ namespace ImageProcessor.Formats
/// Reads the specified scanline.
/// </summary>
/// <param name="scanline">The scanline.</param>
/// <param name="pixels">The pixels, where the colors should be stored in RGBA format.</param>
/// <param name="pixels">The pixels, where the colors should be stored in BGRA format.</param>
/// <param name="header">The header, which contains information about the png file, like
/// the width of the image and the height.</param>
public void ReadScanline(byte[] scanline, byte[] pixels, PngHeader header)

2
src/ImageProcessor/Formats/Png/TrueColorReader.cs

@ -41,7 +41,7 @@ namespace ImageProcessor.Formats
/// Reads the specified scanline.
/// </summary>
/// <param name="scanline">The scanline.</param>
/// <param name="pixels">The pixels, where the colors should be stored in RGBA format.</param>
/// <param name="pixels">The pixels, where the colors should be stored in BGRA format.</param>
/// <param name="header">The header, which contains information about the png file, like
/// the width of the image and the height.</param>
public void ReadScanline(byte[] scanline, byte[] pixels, PngHeader header)

3
src/ImageProcessor/ImageProcessor.csproj

@ -52,6 +52,9 @@
<Compile Include="Formats\Bmp\BmpEncoder.cs" />
<Compile Include="Formats\Gif\GifDecoderCore.cs" />
<Compile Include="Formats\Gif\GifEncoder.cs" />
<Compile Include="Formats\Gif\Quantizer\IQuantizer.cs" />
<Compile Include="Formats\Gif\Quantizer\OctreeQuantizer.cs" />
<Compile Include="Formats\Gif\Quantizer\Quantizer.cs" />
<Compile Include="Formats\Png\PngDecoder.cs" />
<Compile Include="Formats\Png\PngDecoderCore.cs" />
<Compile Include="Formats\Png\PngEncoder.cs" />

1
src/ImageProcessor/ImageProcessor.csproj.DotSettings

@ -7,6 +7,7 @@
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=encoders_005Cgif/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=formats_005Cbmp/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=formats_005Cgif/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=formats_005Cgif_005Cquantizer/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=formats_005Cjpg/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=formats_005Cpng/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/CodeInspection/NamespaceProvider/NamespaceFoldersToSkip/=numerics/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

2
src/ImageProcessor/Settings.StyleCop

@ -2,6 +2,8 @@
<GlobalSettings>
<CollectionProperty Name="RecognizedWords">
<Value>cb</Value>
<Value>octree</Value>
<Value>quantizer</Value>
<Value>cr</Value>
<Value>EX</Value>
<Value>png</Value>

Loading…
Cancel
Save