Browse Source

Merge branch 'master' into HEAD

pull/34/head
Anton Firszov 9 years ago
parent
commit
44d7359ae5
  1. 2
      Settings.StyleCop
  2. 20
      src/ImageSharp/Common/Extensions/StreamExtensions.cs
  3. 3
      src/ImageSharp/Formats/Jpg/JpegEncoderCore.cs
  4. 43
      src/ImageSharp/Formats/Png/Filters/AverageFilter.cs
  5. 11
      src/ImageSharp/Formats/Png/Filters/NoneFilter.cs
  6. 42
      src/ImageSharp/Formats/Png/Filters/PaethFilter.cs
  7. 32
      src/ImageSharp/Formats/Png/Filters/SubFilter.cs
  8. 36
      src/ImageSharp/Formats/Png/Filters/UpFilter.cs
  9. 5
      src/ImageSharp/Formats/Png/PngDecoderCore.cs
  10. 131
      src/ImageSharp/Formats/Png/PngEncoderCore.cs
  11. 26
      src/ImageSharp/IO/EndianBinaryWriter.cs
  12. 3
      src/ImageSharp/Image.cs
  13. 2
      src/ImageSharp/Image/Image.cs
  14. 15
      src/ImageSharp/Numerics/Ellipse.cs
  15. 8
      src/ImageSharp/Profiles/Exif/ExifReader.cs
  16. 3
      src/ImageSharp/Profiles/Exif/ExifTag.cs
  17. 38
      src/ImageSharp/Samplers/Options/Orientation.cs
  18. 2
      src/ImageSharp/Samplers/Processors/RotateProcessor.cs
  19. 22
      src/ImageSharp/Samplers/Processors/SkewProcessor.cs
  20. 4
      tests/ImageSharp.Benchmarks/Image/EncodeBmp.cs
  21. 4
      tests/ImageSharp.Benchmarks/Image/EncodeGif.cs
  22. 4
      tests/ImageSharp.Benchmarks/Image/EncodePng.cs

2
Settings.StyleCop

@ -32,6 +32,8 @@
<Value>Vol</Value>
<Value>pp</Value>
<Value>cmyk</Value>
<Value>Paeth</Value>
<Value>th</Value>
</CollectionProperty>
</GlobalSettings>
<Analyzers>

20
src/ImageSharp/Common/Extensions/StreamExtensions.cs

@ -5,10 +5,19 @@
namespace ImageSharp
{
using System.Buffers;
using System.IO;
/// <summary>
/// Extension methods for the <see cref="Stream"/> type.
/// </summary>
internal static class StreamExtensions
{
/// <summary>
/// Skips the number of bytes in the given stream.
/// </summary>
/// <param name="stream">The stream.</param>
/// <param name="count">The count.</param>
public static void Skip(this Stream stream, int count)
{
if (count < 1)
@ -22,8 +31,15 @@ namespace ImageSharp
}
else
{
byte[] foo = new byte[count];
stream.Read(foo, 0, count);
byte[] foo = ArrayPool<byte>.Shared.Rent(count);
try
{
stream.Read(foo, 0, count);
}
finally
{
ArrayPool<byte>.Shared.Return(foo);
}
}
}
}

3
src/ImageSharp/Formats/Jpg/JpegEncoderCore.cs

@ -246,7 +246,6 @@ namespace ImageSharp.Formats
/// <summary>
/// The AC luminance huffman table index
/// </summary>
LuminanceAC = 1,
// ReSharper restore UnusedMember.Local
@ -851,6 +850,7 @@ namespace ImageSharp.Formats
Block b = new Block();
Block cb = new Block();
Block cr = new Block();
// ReSharper disable once InconsistentNaming
int prevDCY = 0, prevDCCb = 0, prevDCCr = 0;
@ -880,6 +880,7 @@ namespace ImageSharp.Formats
Block b = new Block();
Block[] cb = new Block[4];
Block[] cr = new Block[4];
// ReSharper disable once InconsistentNaming
int prevDCY = 0, prevDCCb = 0, prevDCCr = 0;

43
src/ImageSharp/Formats/Png/Filters/AverageFilter.cs

@ -12,7 +12,7 @@ namespace ImageSharp.Formats
/// the value of a pixel.
/// <see href="https://www.w3.org/TR/PNG-Filters.html"/>
/// </summary>
internal static class AverageFilter
internal static unsafe class AverageFilter
{
/// <summary>
/// Decodes the scanline
@ -28,12 +28,17 @@ namespace ImageSharp.Formats
// Average(x) + floor((Raw(x-bpp)+Prior(x))/2)
byte[] result = new byte[scanline.Length];
for (int x = 1; x < scanline.Length; x++)
fixed (byte* scan = scanline)
fixed (byte* prev = previousScanline)
fixed (byte* res = result)
{
byte left = (x - bytesPerPixel < 1) ? (byte)0 : result[x - bytesPerPixel];
byte above = previousScanline[x];
for (int x = 1; x < scanline.Length; x++)
{
byte left = (x - bytesPerPixel < 1) ? (byte)0 : res[x - bytesPerPixel];
byte above = prev[x];
result[x] = (byte)((scanline[x] + Average(left, above)) % 256);
res[x] = (byte)((scan[x] + Average(left, above)) % 256);
}
}
return result;
@ -44,25 +49,29 @@ namespace ImageSharp.Formats
/// </summary>
/// <param name="scanline">The scanline to encode</param>
/// <param name="previousScanline">The previous scanline.</param>
/// <param name="result">The encoded scanline.</param>
/// <param name="bytesPerPixel">The bytes per pixel.</param>
/// <param name="bytesPerScanline">The number of bytes per scanline</param>
/// <returns>The <see cref="T:byte[]"/></returns>
public static byte[] Encode(byte[] scanline, byte[] previousScanline, int bytesPerPixel, int bytesPerScanline)
public static byte[] Encode(byte[] scanline, byte[] previousScanline, byte[] result, int bytesPerPixel, int bytesPerScanline)
{
// Average(x) = Raw(x) - floor((Raw(x-bpp)+Prior(x))/2)
byte[] encodedScanline = new byte[bytesPerScanline + 1];
encodedScanline[0] = (byte)FilterType.Average;
for (int x = 0; x < bytesPerScanline; x++)
fixed (byte* scan = scanline)
fixed (byte* prev = previousScanline)
fixed (byte* res = result)
{
byte left = (x - bytesPerPixel < 0) ? (byte)0 : scanline[x - bytesPerPixel];
byte above = previousScanline[x];
res[0] = 3;
encodedScanline[x + 1] = (byte)((scanline[x] - Average(left, above)) % 256);
for (int x = 0; x < bytesPerScanline; x++)
{
byte left = (x - bytesPerPixel < 0) ? (byte)0 : scan[x - bytesPerPixel];
byte above = prev[x];
res[x + 1] = (byte)((scan[x] - Average(left, above)) % 256);
}
}
return encodedScanline;
return result;
}
/// <summary>
@ -73,7 +82,7 @@ namespace ImageSharp.Formats
/// <returns>The <see cref="int"/></returns>
private static int Average(byte left, byte above)
{
return Convert.ToInt32(Math.Floor((left + above) / 2.0D));
return (left + above) >> 1;
}
}
}
}

11
src/ImageSharp/Formats/Png/Filters/NoneFilter.cs

@ -29,16 +29,13 @@ namespace ImageSharp.Formats
/// Encodes the scanline
/// </summary>
/// <param name="scanline">The scanline to encode</param>
/// <param name="result">The encoded scanline.</param>
/// <param name="bytesPerScanline">The number of bytes per scanline</param>
/// <returns>The <see cref="T:byte[]"/></returns>
public static byte[] Encode(byte[] scanline, int bytesPerScanline)
public static void Encode(byte[] scanline, byte[] result, int bytesPerScanline)
{
// Insert a byte before the data.
byte[] encodedScanline = new byte[bytesPerScanline + 1];
encodedScanline[0] = (byte)FilterType.None;
Buffer.BlockCopy(scanline, 0, encodedScanline, 1, bytesPerScanline);
return encodedScanline;
result[0] = 0;
Buffer.BlockCopy(scanline, 0, result, 1, bytesPerScanline);
}
}
}

42
src/ImageSharp/Formats/Png/Filters/PaethFilter.cs

@ -13,7 +13,7 @@ namespace ImageSharp.Formats
/// This technique is due to Alan W. Paeth.
/// <see href="https://www.w3.org/TR/PNG-Filters.html"/>
/// </summary>
internal static class PaethFilter
internal static unsafe class PaethFilter
{
/// <summary>
/// Decodes the scanline
@ -27,13 +27,18 @@ namespace ImageSharp.Formats
// Paeth(x) + PaethPredictor(Raw(x-bpp), Prior(x), Prior(x-bpp))
byte[] result = new byte[scanline.Length];
for (int x = 1; x < scanline.Length; x++)
fixed (byte* scan = scanline)
fixed (byte* prev = previousScanline)
fixed (byte* res = result)
{
byte left = (x - bytesPerPixel < 1) ? (byte)0 : result[x - bytesPerPixel];
byte above = previousScanline[x];
byte upperLeft = (x - bytesPerPixel < 1) ? (byte)0 : previousScanline[x - bytesPerPixel];
for (int x = 1; x < scanline.Length; x++)
{
byte left = (x - bytesPerPixel < 1) ? (byte)0 : res[x - bytesPerPixel];
byte above = prev[x];
byte upperLeft = (x - bytesPerPixel < 1) ? (byte)0 : prev[x - bytesPerPixel];
result[x] = (byte)((scanline[x] + PaethPredicator(left, above, upperLeft)) % 256);
res[x] = (byte)((scan[x] + PaethPredicator(left, above, upperLeft)) % 256);
}
}
return result;
@ -44,25 +49,30 @@ namespace ImageSharp.Formats
/// </summary>
/// <param name="scanline">The scanline to encode</param>
/// <param name="previousScanline">The previous scanline.</param>
/// <param name="result">The encoded scanline.</param>
/// <param name="bytesPerPixel">The bytes per pixel.</param>
/// <param name="bytesPerScanline">The number of bytes per scanline</param>
/// <returns>The <see cref="T:byte[]"/></returns>
public static byte[] Encode(byte[] scanline, byte[] previousScanline, int bytesPerPixel, int bytesPerScanline)
public static byte[] Encode(byte[] scanline, byte[] previousScanline, byte[] result, int bytesPerPixel, int bytesPerScanline)
{
// Paeth(x) = Raw(x) - PaethPredictor(Raw(x-bpp), Prior(x), Prior(x - bpp))
byte[] encodedScanline = new byte[bytesPerScanline + 1];
encodedScanline[0] = (byte)FilterType.Paeth;
for (int x = 0; x < bytesPerScanline; x++)
fixed (byte* scan = scanline)
fixed (byte* prev = previousScanline)
fixed (byte* res = result)
{
byte left = (x - bytesPerPixel < 0) ? (byte)0 : scanline[x - bytesPerPixel];
byte above = previousScanline[x];
byte upperLeft = (x - bytesPerPixel < 0) ? (byte)0 : previousScanline[x - bytesPerPixel];
res[0] = 4;
for (int x = 0; x < bytesPerScanline; x++)
{
byte left = (x - bytesPerPixel < 0) ? (byte)0 : scan[x - bytesPerPixel];
byte above = prev[x];
byte upperLeft = (x - bytesPerPixel < 0) ? (byte)0 : prev[x - bytesPerPixel];
encodedScanline[x + 1] = (byte)((scanline[x] - PaethPredicator(left, above, upperLeft)) % 256);
res[x + 1] = (byte)((scan[x] - PaethPredicator(left, above, upperLeft)) % 256);
}
}
return encodedScanline;
return result;
}
/// <summary>

32
src/ImageSharp/Formats/Png/Filters/SubFilter.cs

@ -10,7 +10,7 @@ namespace ImageSharp.Formats
/// of the prior pixel.
/// <see href="https://www.w3.org/TR/PNG-Filters.html"/>
/// </summary>
internal static class SubFilter
internal static unsafe class SubFilter
{
/// <summary>
/// Decodes the scanline
@ -23,11 +23,15 @@ namespace ImageSharp.Formats
// Sub(x) + Raw(x-bpp)
byte[] result = new byte[scanline.Length];
for (int x = 1; x < scanline.Length; x++)
fixed (byte* scan = scanline)
fixed (byte* res = result)
{
byte priorRawByte = (x - bytesPerPixel < 1) ? (byte)0 : result[x - bytesPerPixel];
for (int x = 1; x < scanline.Length; x++)
{
byte priorRawByte = (x - bytesPerPixel < 1) ? (byte)0 : res[x - bytesPerPixel];
result[x] = (byte)((scanline[x] + priorRawByte) % 256);
res[x] = (byte)((scan[x] + priorRawByte) % 256);
}
}
return result;
@ -37,23 +41,27 @@ namespace ImageSharp.Formats
/// Encodes the scanline
/// </summary>
/// <param name="scanline">The scanline to encode</param>
/// <param name="result">The encoded scanline.</param>
/// <param name="bytesPerPixel">The bytes per pixel.</param>
/// <param name="bytesPerScanline">The number of bytes per scanline</param>
/// <returns>The <see cref="T:byte[]"/></returns>
public static byte[] Encode(byte[] scanline, int bytesPerPixel, int bytesPerScanline)
public static byte[] Encode(byte[] scanline, byte[] result, int bytesPerPixel, int bytesPerScanline)
{
// Sub(x) = Raw(x) - Raw(x-bpp)
byte[] encodedScanline = new byte[bytesPerScanline + 1];
encodedScanline[0] = (byte)FilterType.Sub;
for (int x = 0; x < bytesPerScanline; x++)
fixed (byte* scan = scanline)
fixed (byte* res = result)
{
byte priorRawByte = (x - bytesPerPixel < 0) ? (byte)0 : scanline[x - bytesPerPixel];
res[0] = 1;
for (int x = 0; x < bytesPerScanline; x++)
{
byte priorRawByte = (x - bytesPerPixel < 0) ? (byte)0 : scan[x - bytesPerPixel];
encodedScanline[x + 1] = (byte)((scanline[x] - priorRawByte) % 256);
res[x + 1] = (byte)((scan[x] - priorRawByte) % 256);
}
}
return encodedScanline;
return result;
}
}
}

36
src/ImageSharp/Formats/Png/Filters/UpFilter.cs

@ -10,7 +10,7 @@ namespace ImageSharp.Formats
/// rather than just to its left, is used as the predictor.
/// <see href="https://www.w3.org/TR/PNG-Filters.html"/>
/// </summary>
internal static class UpFilter
internal static unsafe class UpFilter
{
/// <summary>
/// Decodes the scanline
@ -23,11 +23,16 @@ namespace ImageSharp.Formats
// Up(x) + Prior(x)
byte[] result = new byte[scanline.Length];
for (int x = 1; x < scanline.Length; x++)
fixed (byte* scan = scanline)
fixed (byte* prev = previousScanline)
fixed (byte* res = result)
{
byte above = previousScanline[x];
for (int x = 1; x < scanline.Length; x++)
{
byte above = prev[x];
result[x] = (byte)((scanline[x] + above) % 256);
res[x] = (byte)((scan[x] + above) % 256);
}
}
return result;
@ -37,23 +42,28 @@ namespace ImageSharp.Formats
/// Encodes the scanline
/// </summary>
/// <param name="scanline">The scanline to encode</param>
/// <param name="bytesPerScanline">The number of bytes per scanline</param>
/// <param name="previousScanline">The previous scanline.</param>
/// <param name="result">The encoded scanline.</param>
/// <param name="bytesPerScanline">The number of bytes per scanline</param>
/// <returns>The <see cref="T:byte[]"/></returns>
public static byte[] Encode(byte[] scanline, int bytesPerScanline, byte[] previousScanline)
public static byte[] Encode(byte[] scanline, byte[] previousScanline, byte[] result, int bytesPerScanline)
{
// Up(x) = Raw(x) - Prior(x)
byte[] encodedScanline = new byte[bytesPerScanline + 1];
encodedScanline[0] = (byte)FilterType.Up;
for (int x = 0; x < bytesPerScanline; x++)
fixed (byte* scan = scanline)
fixed (byte* prev = previousScanline)
fixed (byte* res = result)
{
byte above = previousScanline[x];
res[0] = 2;
encodedScanline[x + 1] = (byte)((scanline[x] - above) % 256);
for (int x = 0; x < bytesPerScanline; x++)
{
byte above = prev[x];
res[x + 1] = (byte)((scan[x] - above) % 256);
}
}
return encodedScanline;
return result;
}
}
}

5
src/ImageSharp/Formats/Png/PngDecoderCore.cs

@ -283,7 +283,9 @@ namespace ImageSharp.Formats
FilterType filterType = (FilterType)scanline[0];
byte[] defilteredScanline;
// TODO: It would be good if we can reduce the memory usage here. Each filter is creating a new row.
// TODO: It would be good if we can reduce the memory usage here - Each filter is creating a new row.
// Every time I try to use the same approach as I have in the encoder though I keep messing up.
// Fingers crossed someone with a big brain and a kind heart will come along and finish optimizing this for me.
switch (filterType)
{
case FilterType.None:
@ -590,6 +592,7 @@ namespace ImageSharp.Formats
/// <param name="chunk">The chunk.</param>
private void ReadChunkData(PngChunk chunk)
{
// TODO: It might be possible to rent this but that could also lead to issues assigning the data to various properties
chunk.Data = new byte[chunk.Length];
this.currentStream.Read(chunk.Data, 0, chunk.Length);
}

131
src/ImageSharp/Formats/Png/PngEncoderCore.cs

@ -34,7 +34,6 @@ namespace ImageSharp.Formats
/// </summary>
private readonly byte[] chunkDataBuffer = new byte[16];
/// <summary>
/// Contains the raw pixel data from an indexed image.
/// </summary>
@ -182,7 +181,11 @@ namespace ImageSharp.Formats
this.WritePhysicalChunk(stream, image);
this.WriteGammaChunk(stream);
this.WriteDataChunks(image, stream);
using (PixelAccessor<TColor, TPacked> pixels = image.Lock())
{
this.WriteDataChunks(pixels, stream);
}
this.WriteEndChunk(stream);
stream.Flush();
}
@ -249,35 +252,32 @@ namespace ImageSharp.Formats
/// </summary>
/// <typeparam name="TColor">The pixel format.</typeparam>
/// <typeparam name="TPacked">The packed format. <example>uint, long, float.</example></typeparam>
/// <param name="image">The image to encode.</param>
/// <param name="pixels">The image pixels accessor.</param>
/// <param name="row">The row index.</param>
/// <param name="rawScanline">The raw scanline.</param>
private void CollectGrayscaleBytes<TColor, TPacked>(ImageBase<TColor, TPacked> image, int row, byte[] rawScanline)
private void CollectGrayscaleBytes<TColor, TPacked>(PixelAccessor<TColor, TPacked> pixels, int row, byte[] rawScanline)
where TColor : struct, IPackedPixel<TPacked>
where TPacked : struct
{
// Copy the pixels across from the image.
// Reuse the chunk type buffer.
using (PixelAccessor<TColor, TPacked> pixels = image.Lock())
for (int x = 0; x < this.width; x++)
{
for (int x = 0; x < this.width; x++)
{
// Convert the color to YCbCr and store the luminance
// Optionally store the original color alpha.
int offset = x * this.bytesPerPixel;
pixels[x, row].ToBytes(this.chunkTypeBuffer, 0, ComponentOrder.XYZW);
byte luminance = (byte)((0.299F * this.chunkTypeBuffer[0]) + (0.587F * this.chunkTypeBuffer[1]) + (0.114F * this.chunkTypeBuffer[2]));
// Convert the color to YCbCr and store the luminance
// Optionally store the original color alpha.
int offset = x * this.bytesPerPixel;
pixels[x, row].ToBytes(this.chunkTypeBuffer, 0, ComponentOrder.XYZW);
byte luminance = (byte)((0.299F * this.chunkTypeBuffer[0]) + (0.587F * this.chunkTypeBuffer[1]) + (0.114F * this.chunkTypeBuffer[2]));
for (int i = 0; i < this.bytesPerPixel; i++)
for (int i = 0; i < this.bytesPerPixel; i++)
{
if (i == 0)
{
rawScanline[offset] = luminance;
}
else
{
if (i == 0)
{
rawScanline[offset] = luminance;
}
else
{
rawScanline[offset + i] = this.chunkTypeBuffer[3];
}
rawScanline[offset + i] = this.chunkTypeBuffer[3];
}
}
}
@ -288,20 +288,17 @@ namespace ImageSharp.Formats
/// </summary>
/// <typeparam name="TColor">The pixel format.</typeparam>
/// <typeparam name="TPacked">The packed format. <example>uint, long, float.</example></typeparam>
/// <param name="image">The image to encode.</param>
/// <param name="pixels">The image pixel accessor.</param>
/// <param name="row">The row index.</param>
/// <param name="rawScanline">The raw scanline.</param>
private void CollectColorBytes<TColor, TPacked>(ImageBase<TColor, TPacked> image, int row, byte[] rawScanline)
private void CollectColorBytes<TColor, TPacked>(PixelAccessor<TColor, TPacked> pixels, int row, byte[] rawScanline)
where TColor : struct, IPackedPixel<TPacked>
where TPacked : struct
{
using (PixelAccessor<TColor, TPacked> pixels = image.Lock())
int bpp = this.bytesPerPixel;
for (int x = 0; x < this.width; x++)
{
int bpp = this.bytesPerPixel;
for (int x = 0; x < this.width; x++)
{
pixels[x, row].ToBytes(rawScanline, x * this.bytesPerPixel, bpp == 4 ? ComponentOrder.XYZW : ComponentOrder.XYZ);
}
pixels[x, row].ToBytes(rawScanline, x * this.bytesPerPixel, bpp == 4 ? ComponentOrder.XYZW : ComponentOrder.XYZ);
}
}
@ -311,13 +308,13 @@ namespace ImageSharp.Formats
/// </summary>
/// <typeparam name="TColor">The pixel format.</typeparam>
/// <typeparam name="TPacked">The packed format. <example>uint, long, float.</example></typeparam>
/// <param name="image">The image to encode.</param>
/// <param name="pixels">The image pixel accessor.</param>
/// <param name="row">The row.</param>
/// <param name="previousScanline">The previous scanline.</param>
/// <param name="rawScanline">The raw scanline.</param>
/// <param name="result">The resultant filtered scanline.</param>
/// <param name="bytesPerScanline">The number of bytes per scanline.</param>
/// <returns>The <see cref="T:byte[]"/></returns>
private byte[] EncodePixelRow<TColor, TPacked>(ImageBase<TColor, TPacked> image, int row, byte[] previousScanline, byte[] rawScanline, int bytesPerScanline)
private void EncodePixelRow<TColor, TPacked>(PixelAccessor<TColor, TPacked> pixels, int row, byte[] previousScanline, byte[] rawScanline, byte[] result, int bytesPerScanline)
where TColor : struct, IPackedPixel<TPacked>
where TPacked : struct
{
@ -328,16 +325,14 @@ namespace ImageSharp.Formats
break;
case PngColorType.Grayscale:
case PngColorType.GrayscaleWithAlpha:
this.CollectGrayscaleBytes(image, row, rawScanline);
this.CollectGrayscaleBytes(pixels, row, rawScanline);
break;
default:
this.CollectColorBytes(image, row, rawScanline);
this.CollectColorBytes(pixels, row, rawScanline);
break;
}
byte[] filteredScanline = this.GetOptimalFilteredScanline(rawScanline, previousScanline, bytesPerScanline, this.bytesPerPixel);
return filteredScanline;
this.GetOptimalFilteredScanline(rawScanline, previousScanline, result, bytesPerScanline);
}
/// <summary>
@ -346,37 +341,30 @@ namespace ImageSharp.Formats
/// </summary>
/// <param name="rawScanline">The raw scanline</param>
/// <param name="previousScanline">The previous scanline</param>
/// <param name="result">The filtered scanline result</param>
/// <param name="bytesPerScanline">The number of bytes per scanline</param>
/// <param name="bytesPerPixel">The number of bytes per pixel</param>
/// <returns>The <see cref="T:byte[]"/></returns>
private byte[] GetOptimalFilteredScanline(byte[] rawScanline, byte[] previousScanline, int bytesPerScanline, int bytesPerPixel)
private void GetOptimalFilteredScanline(byte[] rawScanline, byte[] previousScanline, byte[] result, int bytesPerScanline)
{
Tuple<byte[], int>[] candidates;
// Palette images don't compress well with adaptive filtering.
if (this.PngColorType == PngColorType.Palette)
{
candidates = new Tuple<byte[], int>[1];
byte[] none = NoneFilter.Encode(rawScanline, bytesPerScanline);
candidates[0] = new Tuple<byte[], int>(none, this.CalculateTotalVariation(none));
NoneFilter.Encode(rawScanline, result, bytesPerScanline);
return;
}
else
{
candidates = new Tuple<byte[], int>[4];
byte[] sub = SubFilter.Encode(rawScanline, bytesPerPixel, bytesPerScanline);
candidates[0] = new Tuple<byte[], int>(sub, this.CalculateTotalVariation(sub));
Tuple<byte[], int>[] candidates = new Tuple<byte[], int>[4];
byte[] up = UpFilter.Encode(rawScanline, bytesPerScanline, previousScanline);
candidates[1] = new Tuple<byte[], int>(up, this.CalculateTotalVariation(up));
byte[] sub = SubFilter.Encode(rawScanline, result, this.bytesPerPixel, bytesPerScanline);
candidates[0] = new Tuple<byte[], int>(sub, this.CalculateTotalVariation(sub));
byte[] average = AverageFilter.Encode(rawScanline, previousScanline, bytesPerPixel, bytesPerScanline);
candidates[2] = new Tuple<byte[], int>(average, this.CalculateTotalVariation(average));
byte[] up = UpFilter.Encode(rawScanline, previousScanline, result, bytesPerScanline);
candidates[1] = new Tuple<byte[], int>(up, this.CalculateTotalVariation(up));
byte[] paeth = PaethFilter.Encode(rawScanline, previousScanline, bytesPerPixel, bytesPerScanline);
candidates[3] = new Tuple<byte[], int>(paeth, this.CalculateTotalVariation(paeth));
}
byte[] average = AverageFilter.Encode(rawScanline, previousScanline, result, this.bytesPerPixel, bytesPerScanline);
candidates[2] = new Tuple<byte[], int>(average, this.CalculateTotalVariation(average));
byte[] paeth = PaethFilter.Encode(rawScanline, previousScanline, result, this.bytesPerPixel, bytesPerScanline);
candidates[3] = new Tuple<byte[], int>(paeth, this.CalculateTotalVariation(paeth));
int lowestTotalVariation = int.MaxValue;
int lowestTotalVariationIndex = 0;
@ -390,7 +378,8 @@ namespace ImageSharp.Formats
}
}
return candidates[lowestTotalVariationIndex].Item1;
// ReSharper disable once RedundantAssignment
result = candidates[lowestTotalVariationIndex].Item1;
}
/// <summary>
@ -592,15 +581,22 @@ namespace ImageSharp.Formats
/// </summary>
/// <typeparam name="TColor">The pixel format.</typeparam>
/// <typeparam name="TPacked">The packed format. <example>uint, long, float.</example></typeparam>
/// <param name="image">The image to encode.</param>
/// <param name="pixels">The pixel accessor.</param>
/// <param name="stream">The stream.</param>
private void WriteDataChunks<TColor, TPacked>(ImageBase<TColor, TPacked> image, Stream stream)
private void WriteDataChunks<TColor, TPacked>(PixelAccessor<TColor, TPacked> pixels, Stream stream)
where TColor : struct, IPackedPixel<TPacked>
where TPacked : struct
{
int bytesPerScanline = this.width * this.bytesPerPixel;
byte[] previousScanline = ArrayPool<byte>.Shared.Rent(bytesPerScanline);
byte[] rawScanline = ArrayPool<byte>.Shared.Rent(bytesPerScanline);
int resultLength = bytesPerScanline + 1;
byte[] result = ArrayPool<byte>.Shared.Rent(resultLength);
// TODO: Clearing this array makes the visual tests work again when encoding multiple images in a row.
// The png analyser tool I use still cannot decompress the image though my own decoder, chome and edge browsers, and paint can. Twitter also cannot read the file.
// It's 2am now so I'm going to check in what I have and cry. :'(
Array.Clear(result, 0, resultLength);
byte[] buffer;
int bufferLength;
@ -612,9 +608,8 @@ namespace ImageSharp.Formats
{
for (int y = 0; y < this.height; y++)
{
byte[] data = this.EncodePixelRow(image, y, previousScanline, rawScanline, bytesPerScanline);
deflateStream.Write(data, 0, data.Length);
deflateStream.Flush();
this.EncodePixelRow(pixels, y, previousScanline, rawScanline, result, bytesPerScanline);
deflateStream.Write(result, 0, resultLength);
// Do a bit of shuffling;
byte[] tmp = rawScanline;
@ -622,18 +617,20 @@ namespace ImageSharp.Formats
previousScanline = tmp;
}
bufferLength = (int)memoryStream.Length;
deflateStream.Flush();
buffer = memoryStream.ToArray();
bufferLength = buffer.Length;
}
}
finally
{
memoryStream?.Dispose();
ArrayPool<byte>.Shared.Return(previousScanline);
ArrayPool<byte>.Shared.Return(rawScanline);
memoryStream?.Dispose();
ArrayPool<byte>.Shared.Return(result);
}
// Store the chunks in repeated 64k blocks.
// Store the chunks in repeated 64k blocks.
// This reduces the memory load for decoding the image for many decoders.
int numChunks = bufferLength / MaxBlockSize;

26
src/ImageSharp/IO/EndianBinaryWriter.cs

@ -346,6 +346,19 @@ namespace ImageSharp.IO
this.BaseStream.Write(this.buffer, 0, index);
}
/// <summary>
/// Disposes of the underlying stream.
/// </summary>
public void Dispose()
{
if (!this.disposed)
{
this.Flush();
this.disposed = true;
((IDisposable)this.BaseStream).Dispose();
}
}
/// <summary>
/// Checks whether or not the writer has been disposed, throwing an exception if so.
/// </summary>
@ -368,18 +381,5 @@ namespace ImageSharp.IO
this.CheckDisposed();
this.BaseStream.Write(bytes, 0, length);
}
/// <summary>
/// Disposes of the underlying stream.
/// </summary>
public void Dispose()
{
if (!this.disposed)
{
this.Flush();
this.disposed = true;
((IDisposable)this.BaseStream).Dispose();
}
}
}
}

3
src/ImageSharp/Image.cs

@ -16,8 +16,7 @@ namespace ImageSharp
public class Image : Image<Color, uint>
{
/// <summary>
/// Initializes a new instance of the <see cref="Image"/> class
/// with the height and the width of the image.
/// Initializes a new instance of the <see cref="Image"/> class.
/// </summary>
public Image()
{

2
src/ImageSharp/Image/Image.cs

@ -191,6 +191,7 @@ namespace ImageSharp
/// </summary>
/// <param name="stream">The stream to save the image to.</param>
/// <exception cref="System.ArgumentNullException">Thrown if the stream is null.</exception>
/// <returns>The <see cref="Image{TColor,TPacked}"/></returns>
public Image<TColor, TPacked> Save(Stream stream)
{
Guard.NotNull(stream, nameof(stream));
@ -204,6 +205,7 @@ namespace ImageSharp
/// <param name="stream">The stream to save the image to.</param>
/// <param name="format">The format to save the image as.</param>
/// <exception cref="System.ArgumentNullException">Thrown if the stream is null.</exception>
/// <returns>The <see cref="Image{TColor,TPacked}"/></returns>
public Image<TColor, TPacked> Save(Stream stream, IImageFormat format)
{
Guard.NotNull(stream, nameof(stream));

15
src/ImageSharp/Numerics/Ellipse.cs

@ -9,18 +9,27 @@ namespace ImageSharp
using System.ComponentModel;
using System.Numerics;
/// <summary>
/// Represents an ellipse.
/// </summary>
public struct Ellipse : IEquatable<Ellipse>
{
/// <summary>
/// Represents a <see cref="Ellipse"/> that has X and Y values set to zero.
/// </summary>
public static readonly Ellipse Empty = default(Ellipse);
/// <summary>
/// The center point.
/// </summary>
private Point center;
/// <summary>
/// Represents a <see cref="Ellipse"/> that has X and Y values set to zero.
/// Initializes a new instance of the <see cref="Ellipse"/> struct.
/// </summary>
public static readonly Ellipse Empty = default(Ellipse);
/// <param name="center">The center point.</param>
/// <param name="radiusX">The x-radius.</param>
/// <param name="radiusY">The y-radius.</param>
public Ellipse(Point center, float radiusX, float radiusY)
{
this.center = center;

8
src/ImageSharp/Profiles/Exif/ExifReader.cs

@ -112,8 +112,16 @@ namespace ImageSharp
return result;
}
/// <summary>
/// Gets the invalid tags.
/// </summary>
public IEnumerable<ExifTag> InvalidTags => this.invalidTags;
/// <summary>
/// Adds the collection of EXIF values to the reader.
/// </summary>
/// <param name="values">The values.</param>
/// <param name="index">The index.</param>
private void AddValues(Collection<ExifValue> values, uint index)
{
this.currentIndex = this.startIndex + index;

3
src/ImageSharp/Profiles/Exif/ExifTag.cs

@ -3,12 +3,11 @@
// Licensed under the Apache License, Version 2.0.
// </copyright>
// Descriptions from: http://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/EXIF.html
namespace ImageSharp
{
/// <summary>
/// All exif tags from the Exif standard 2.2
/// Descriptions from: <see href="http://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/EXIF.html"/>
/// </summary>
public enum ExifTag
{

38
src/ImageSharp/Samplers/Options/Orientation.cs

@ -5,16 +5,54 @@
namespace ImageSharp
{
/// <summary>
/// Enumerates the available orientation values supplied by EXIF metadata.
/// </summary>
internal enum Orientation : ushort
{
/// <summary>
/// Unknown rotation.
/// </summary>
Unknown = 0,
/// <summary>
/// The 0th row at the top, the 0th column on the left.
/// </summary>
TopLeft = 1,
/// <summary>
/// The 0th row at the top, the 0th column on the right.
/// </summary>
TopRight = 2,
/// <summary>
/// The 0th row at the bottom, the 0th column on the right.
/// </summary>
BottomRight = 3,
/// <summary>
/// The 0th row at the bottom, the 0th column on the left.
/// </summary>
BottomLeft = 4,
/// <summary>
/// The 0th row on the left, the 0th column at the top.
/// </summary>
LeftTop = 5,
/// <summary>
/// The 0th row at the right, the 0th column at the top.
/// </summary>
RightTop = 6,
/// <summary>
/// The 0th row on the right, the 0th column at the bottom.
/// </summary>
RightBottom = 7,
/// <summary>
/// The 0th row on the left, the 0th column at the bottom.
/// </summary>
LeftBottom = 8
}
}

2
src/ImageSharp/Samplers/Processors/RotateProcessor.cs

@ -88,7 +88,7 @@ namespace ImageSharp.Processors
/// </summary>
/// <param name="target">The target image.</param>
/// <param name="source">The source image.</param>
/// <returns></returns>
/// <returns>The <see cref="bool"/></returns>
private bool OptimizedApply(ImageBase<TColor, TPacked> target, ImageBase<TColor, TPacked> source)
{
const float Epsilon = .0001F;

22
src/ImageSharp/Samplers/Processors/SkewProcessor.cs

@ -19,7 +19,7 @@ namespace ImageSharp.Processors
where TPacked : struct
{
/// <summary>
/// The tranform matrix to apply.
/// The transform matrix to apply.
/// </summary>
private Matrix3x2 processMatrix;
@ -38,16 +38,6 @@ namespace ImageSharp.Processors
/// </summary>
public bool Expand { get; set; } = true;
/// <inheritdoc/>
protected override void OnApply(ImageBase<TColor, TPacked> target, ImageBase<TColor, TPacked> source, Rectangle targetRectangle, Rectangle sourceRectangle)
{
this.processMatrix = Point.CreateSkew(new Point(0, 0), -this.AngleX, -this.AngleY);
if (this.Expand)
{
CreateNewTarget(target, sourceRectangle, this.processMatrix);
}
}
/// <inheritdoc/>
public override void Apply(ImageBase<TColor, TPacked> target, ImageBase<TColor, TPacked> source, Rectangle targetRectangle, Rectangle sourceRectangle, int startY, int endY)
{
@ -75,5 +65,15 @@ namespace ImageSharp.Processors
});
}
}
/// <inheritdoc/>
protected override void OnApply(ImageBase<TColor, TPacked> target, ImageBase<TColor, TPacked> source, Rectangle targetRectangle, Rectangle sourceRectangle)
{
this.processMatrix = Point.CreateSkew(new Point(0, 0), -this.AngleX, -this.AngleY);
if (this.Expand)
{
CreateNewTarget(target, sourceRectangle, this.processMatrix);
}
}
}
}

4
tests/ImageSharp.Benchmarks/Image/EncodeBmp.cs

@ -32,7 +32,7 @@ namespace ImageSharp.Benchmarks.Image
}
[Benchmark(Baseline = true, Description = "System.Drawing Bmp")]
public void JpegSystemDrawing()
public void BmpSystemDrawing()
{
using (MemoryStream memoryStream = new MemoryStream())
{
@ -41,7 +41,7 @@ namespace ImageSharp.Benchmarks.Image
}
[Benchmark(Description = "ImageSharp Bmp")]
public void JpegCore()
public void BmpCore()
{
using (MemoryStream memoryStream = new MemoryStream())
{

4
tests/ImageSharp.Benchmarks/Image/EncodeGif.cs

@ -32,7 +32,7 @@ namespace ImageSharp.Benchmarks.Image
}
[Benchmark(Baseline = true, Description = "System.Drawing Gif")]
public void JpegSystemDrawing()
public void GifSystemDrawing()
{
using (MemoryStream memoryStream = new MemoryStream())
{
@ -41,7 +41,7 @@ namespace ImageSharp.Benchmarks.Image
}
[Benchmark(Description = "ImageSharp Gif")]
public void JpegCore()
public void GifCore()
{
using (MemoryStream memoryStream = new MemoryStream())
{

4
tests/ImageSharp.Benchmarks/Image/EncodePng.cs

@ -32,7 +32,7 @@ namespace ImageSharp.Benchmarks.Image
}
[Benchmark(Baseline = true, Description = "System.Drawing Png")]
public void JpegSystemDrawing()
public void PngSystemDrawing()
{
using (MemoryStream memoryStream = new MemoryStream())
{
@ -41,7 +41,7 @@ namespace ImageSharp.Benchmarks.Image
}
[Benchmark(Description = "ImageSharp Png")]
public void JpegCore()
public void PngCore()
{
using (MemoryStream memoryStream = new MemoryStream())
{

Loading…
Cancel
Save