Browse Source

Quantizer works. Encoding is the issue.

Former-commit-id: 6f7bfcd5302cf0ea9991dec53a4521906eaea4ea
Former-commit-id: 6144a66e2c92b9adb9a33f9f04572837fcd8226c
Former-commit-id: 853c19e80f3b000f62d913916dd9c35c0e3a3627
af/merge-core
James Jackson-South 11 years ago
parent
commit
005784c6d8
  1. 1
      src/ImageProcessor/Formats/Bmp/BmpEncoder.cs
  2. 2
      src/ImageProcessor/Formats/Gif/Quantizer/OctreeQuantizer.cs
  3. 3
      src/ImageProcessor/Formats/Gif/Quantizer/QuantizedImage.cs
  4. 13
      src/ImageProcessor/Formats/Gif/Quantizer/Quantizer.cs
  5. 2
      tests/ImageProcessor.Tests/Formats/EncoderDecoderTests.cs

1
src/ImageProcessor/Formats/Bmp/BmpEncoder.cs

@ -108,6 +108,7 @@ namespace ImageProcessor.Formats
/// </param>
private static void WriteImage(BinaryWriter writer, ImageBase image)
{
// TODO: Check this as Bitmaps can have an alpha channel.
int amount = (image.Width * 3) % 4;
if (amount != 0)
{

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

@ -114,7 +114,7 @@ namespace ImageProcessor.Formats
protected override List<Bgra> GetPalette()
{
// First off convert the Octree to maxColors colors
List<Bgra> palette = this.octree.Palletize(this.maxColors - 1);
List<Bgra> palette = this.octree.Palletize(Math.Max(this.maxColors - 1, 1));
// Add empty color for transparency
palette.Add(Bgra.Empty);

3
src/ImageProcessor/Formats/Gif/Quantizer/QuantizedImage.cs

@ -60,11 +60,12 @@ namespace ImageProcessor.Formats
/// <returns></returns>
public Image ToImage()
{
// TODO: Something is going wrong here. We have a palette.
Image image = new Image();
int pixelCount = Pixels.Length;
byte[] bgraPixels = new byte[pixelCount * 4];
for (int i = 0; i < pixelCount; i++)
for (int i = 0; i < pixelCount; i += 4)
{
Bgra color = Palette[Pixels[i]];
bgraPixels[i + 0] = color.B;

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

@ -11,7 +11,6 @@
namespace ImageProcessor.Formats
{
using System.Collections.Generic;
using System.Linq;
/// <summary>
/// Encapsulates methods to calculate the color palette of an image.
@ -55,16 +54,16 @@ namespace ImageProcessor.Formats
// Call the FirstPass function if not a single pass algorithm.
// For something like an Octree quantizer, this will run through
// all image pixels, build a data structure, and create a palette.
if (!singlePass)
if (!this.singlePass)
{
FirstPass(imageBase, width, height);
this.FirstPass(imageBase, width, height);
}
byte[] quantizedPixels = new byte[width * height];
SecondPass(imageBase, quantizedPixels, width, height);
this.SecondPass(imageBase, quantizedPixels, width, height);
return new QuantizedImage(width, height, GetPalette().ToArray(), quantizedPixels);
return new QuantizedImage(width, height, this.GetPalette().ToArray(), quantizedPixels);
}
/// <summary>
@ -100,7 +99,7 @@ namespace ImageProcessor.Formats
// Convert the first pixel, so that I have values going into the loop
Bgra previousPixel = source[0, 0];
byte pixelValue = QuantizePixel(previousPixel);
byte pixelValue = this.QuantizePixel(previousPixel);
output[0] = pixelValue;
@ -115,7 +114,7 @@ namespace ImageProcessor.Formats
if (sourcePixel != previousPixel)
{
// Quantize the pixel
pixelValue = QuantizePixel(sourcePixel);
pixelValue = this.QuantizePixel(sourcePixel);
// And setup the previous pointer
previousPixel = sourcePixel;

2
tests/ImageProcessor.Tests/Formats/EncoderDecoderTests.cs

@ -59,7 +59,9 @@
}
[Theory]
[InlineData("../../TestImages/Formats/Jpg/Backdrop.jpg")]
[InlineData("../../TestImages/Formats/Bmp/Car.bmp")]
[InlineData("../../TestImages/Formats/Png/cmyk.png")]
public void QuantizedImageShouldPreserveMaximumColorPrecision(string filename)
{
if (!Directory.Exists("Quantized"))

Loading…
Cancel
Save