Browse Source

Final migrations

pull/1683/head
James Jackson-South 5 years ago
parent
commit
bd4d2c0ab3
  1. 23
      src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs
  2. 22
      src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs
  3. 4
      src/ImageSharp/Formats/Png/PngDecoderCore.cs
  4. 2
      src/ImageSharp/Formats/Tiff/Writers/TiffBiColorWriter{TPixel}.cs
  5. 4
      src/ImageSharp/Formats/Tiff/Writers/TiffPaletteWriter{TPixel}.cs
  6. 4
      src/ImageSharp/Memory/Allocators/MemoryAllocator.cs
  7. 25
      tests/ImageSharp.Tests/Formats/Tiff/PhotometricInterpretation/RgbPlanarTiffColorTests.cs

23
src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs

@ -348,17 +348,16 @@ namespace SixLabors.ImageSharp.Formats.Bmp
where TPixel : unmanaged, IPixel<TPixel> where TPixel : unmanaged, IPixel<TPixel>
{ {
bool isL8 = typeof(TPixel) == typeof(L8); bool isL8 = typeof(TPixel) == typeof(L8);
using (IMemoryOwner<byte> colorPaletteBuffer = this.memoryAllocator.AllocateManagedByteBuffer(ColorPaletteSize8Bit, AllocationOptions.Clean)) using IMemoryOwner<byte> colorPaletteBuffer = this.memoryAllocator.Allocate<byte>(ColorPaletteSize8Bit, AllocationOptions.Clean);
Span<byte> colorPalette = colorPaletteBuffer.GetSpan();
if (isL8)
{ {
Span<byte> colorPalette = colorPaletteBuffer.GetSpan(); this.Write8BitGray(stream, image, colorPalette);
if (isL8) }
{ else
this.Write8BitGray(stream, image, colorPalette); {
} this.Write8BitColor(stream, image, colorPalette);
else
{
this.Write8BitColor(stream, image, colorPalette);
}
} }
} }
@ -442,7 +441,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp
MaxColors = 16 MaxColors = 16
}); });
using IndexedImageFrame<TPixel> quantized = frameQuantizer.BuildPaletteAndQuantizeFrame(image, image.Bounds()); using IndexedImageFrame<TPixel> quantized = frameQuantizer.BuildPaletteAndQuantizeFrame(image, image.Bounds());
using IMemoryOwner<byte> colorPaletteBuffer = this.memoryAllocator.AllocateManagedByteBuffer(ColorPaletteSize4Bit, AllocationOptions.Clean); using IMemoryOwner<byte> colorPaletteBuffer = this.memoryAllocator.Allocate<byte>(ColorPaletteSize4Bit, AllocationOptions.Clean);
Span<byte> colorPalette = colorPaletteBuffer.GetSpan(); Span<byte> colorPalette = colorPaletteBuffer.GetSpan();
ReadOnlySpan<TPixel> quantizedColorPalette = quantized.Palette.Span; ReadOnlySpan<TPixel> quantizedColorPalette = quantized.Palette.Span;
@ -486,7 +485,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp
MaxColors = 2 MaxColors = 2
}); });
using IndexedImageFrame<TPixel> quantized = frameQuantizer.BuildPaletteAndQuantizeFrame(image, image.Bounds()); using IndexedImageFrame<TPixel> quantized = frameQuantizer.BuildPaletteAndQuantizeFrame(image, image.Bounds());
using IMemoryOwner<byte> colorPaletteBuffer = this.memoryAllocator.AllocateManagedByteBuffer(ColorPaletteSize1Bit, AllocationOptions.Clean); using IMemoryOwner<byte> colorPaletteBuffer = this.memoryAllocator.Allocate<byte>(ColorPaletteSize1Bit, AllocationOptions.Clean);
Span<byte> colorPalette = colorPaletteBuffer.GetSpan(); Span<byte> colorPalette = colorPaletteBuffer.GetSpan();
ReadOnlySpan<TPixel> quantizedColorPalette = quantized.Palette.Span; ReadOnlySpan<TPixel> quantizedColorPalette = quantized.Palette.Span;

22
src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs

@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. // Licensed under the Apache License, Version 2.0.
using System; using System;
using System.Buffers;
using System.Buffers.Binary; using System.Buffers.Binary;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
@ -928,9 +929,10 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
{ {
int length = remaining; int length = remaining;
using (IManagedByteBuffer huffmanData = this.Configuration.MemoryAllocator.AllocateManagedByteBuffer(256, AllocationOptions.Clean)) using (IMemoryOwner<byte> huffmanData = this.Configuration.MemoryAllocator.Allocate<byte>(256, AllocationOptions.Clean))
{ {
ref byte huffmanDataRef = ref MemoryMarshal.GetReference(huffmanData.GetSpan()); Span<byte> huffmanDataSpan = huffmanData.GetSpan();
ref byte huffmanDataRef = ref MemoryMarshal.GetReference(huffmanDataSpan);
for (int i = 2; i < remaining;) for (int i = 2; i < remaining;)
{ {
byte huffmanTableSpec = (byte)stream.ReadByte(); byte huffmanTableSpec = (byte)stream.ReadByte();
@ -949,11 +951,12 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
JpegThrowHelper.ThrowInvalidImageContentException("Bad Huffman Table index."); JpegThrowHelper.ThrowInvalidImageContentException("Bad Huffman Table index.");
} }
stream.Read(huffmanData.Array, 0, 16); stream.Read(huffmanDataSpan, 0, 16);
using (IManagedByteBuffer codeLengths = this.Configuration.MemoryAllocator.AllocateManagedByteBuffer(17, AllocationOptions.Clean)) using (IMemoryOwner<byte> codeLengths = this.Configuration.MemoryAllocator.Allocate<byte>(17, AllocationOptions.Clean))
{ {
ref byte codeLengthsRef = ref MemoryMarshal.GetReference(codeLengths.GetSpan()); Span<byte> codeLengthsSpan = codeLengths.GetSpan();
ref byte codeLengthsRef = ref MemoryMarshal.GetReference(codeLengthsSpan);
int codeLengthSum = 0; int codeLengthSum = 0;
for (int j = 1; j < 17; j++) for (int j = 1; j < 17; j++)
@ -968,17 +971,18 @@ namespace SixLabors.ImageSharp.Formats.Jpeg
JpegThrowHelper.ThrowInvalidImageContentException("Huffman table has excessive length."); JpegThrowHelper.ThrowInvalidImageContentException("Huffman table has excessive length.");
} }
using (IManagedByteBuffer huffmanValues = this.Configuration.MemoryAllocator.AllocateManagedByteBuffer(256, AllocationOptions.Clean)) using (IMemoryOwner<byte> huffmanValues = this.Configuration.MemoryAllocator.Allocate<byte>(256, AllocationOptions.Clean))
{ {
stream.Read(huffmanValues.Array, 0, codeLengthSum); Span<byte> huffmanValuesSpan = huffmanValues.GetSpan();
stream.Read(huffmanValuesSpan, 0, codeLengthSum);
i += 17 + codeLengthSum; i += 17 + codeLengthSum;
this.BuildHuffmanTable( this.BuildHuffmanTable(
tableType == 0 ? this.dcHuffmanTables : this.acHuffmanTables, tableType == 0 ? this.dcHuffmanTables : this.acHuffmanTables,
tableIndex, tableIndex,
codeLengths.GetSpan(), codeLengthsSpan,
huffmanValues.GetSpan()); huffmanValuesSpan);
} }
} }
} }

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

@ -393,8 +393,8 @@ namespace SixLabors.ImageSharp.Formats.Png
this.bytesPerSample = this.header.BitDepth / 8; this.bytesPerSample = this.header.BitDepth / 8;
} }
this.previousScanline = this.memoryAllocator.AllocateManagedByteBuffer(this.bytesPerScanline, AllocationOptions.Clean); this.previousScanline = this.memoryAllocator.Allocate<byte>(this.bytesPerScanline, AllocationOptions.Clean);
this.scanline = this.Configuration.MemoryAllocator.AllocateManagedByteBuffer(this.bytesPerScanline, AllocationOptions.Clean); this.scanline = this.Configuration.MemoryAllocator.Allocate<byte>(this.bytesPerScanline, AllocationOptions.Clean);
} }
/// <summary> /// <summary>

2
src/ImageSharp/Formats/Tiff/Writers/TiffBiColorWriter{TPixel}.cs

@ -60,7 +60,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Writers
{ {
// Write uncompressed image. // Write uncompressed image.
int bytesPerStrip = this.BytesPerRow * height; int bytesPerStrip = this.BytesPerRow * height;
this.bitStrip ??= this.MemoryAllocator.AllocateManagedByteBuffer(bytesPerStrip); this.bitStrip ??= this.MemoryAllocator.Allocate<byte>(bytesPerStrip);
this.pixelsAsGray ??= this.MemoryAllocator.Allocate<byte>(width); this.pixelsAsGray ??= this.MemoryAllocator.Allocate<byte>(width);
Span<byte> pixelAsGraySpan = this.pixelsAsGray.GetSpan(); Span<byte> pixelAsGraySpan = this.pixelsAsGray.GetSpan();

4
src/ImageSharp/Formats/Tiff/Writers/TiffPaletteWriter{TPixel}.cs

@ -89,7 +89,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Writers
else else
{ {
int stripPixels = width * height; int stripPixels = width * height;
this.indexedPixelsBuffer ??= this.MemoryAllocator.AllocateManagedByteBuffer(stripPixels); this.indexedPixelsBuffer ??= this.MemoryAllocator.Allocate<byte>(stripPixels);
Span<byte> indexedPixels = this.indexedPixelsBuffer.GetSpan(); Span<byte> indexedPixels = this.indexedPixelsBuffer.GetSpan();
int lastRow = y + height; int lastRow = y + height;
int indexedPixelsRowIdx = 0; int indexedPixelsRowIdx = 0;
@ -113,7 +113,7 @@ namespace SixLabors.ImageSharp.Formats.Tiff.Writers
private void AddColorMapTag() private void AddColorMapTag()
{ {
using IMemoryOwner<byte> colorPaletteBuffer = this.MemoryAllocator.AllocateManagedByteBuffer(this.colorPaletteBytes); using IMemoryOwner<byte> colorPaletteBuffer = this.MemoryAllocator.Allocate<byte>(this.colorPaletteBytes);
Span<byte> colorPalette = colorPaletteBuffer.GetSpan(); Span<byte> colorPalette = colorPaletteBuffer.GetSpan();
ReadOnlySpan<TPixel> quantizedColors = this.quantizedImage.Palette.Span; ReadOnlySpan<TPixel> quantizedColors = this.quantizedImage.Palette.Span;

4
src/ImageSharp/Memory/Allocators/MemoryAllocator.cs

@ -1,4 +1,4 @@
// Copyright (c) Six Labors. // Copyright (c) Six Labors.
// Licensed under the Apache License, Version 2.0. // Licensed under the Apache License, Version 2.0.
using System; using System;
@ -18,7 +18,7 @@ namespace SixLabors.ImageSharp.Memory
protected internal abstract int GetBufferCapacityInBytes(); protected internal abstract int GetBufferCapacityInBytes();
/// <summary> /// <summary>
/// Allocates an <see cref="IMemoryOwner{T}" />, holding a <see cref="System.Memory{T}"/> of length <paramref name="length"/>. /// Allocates an <see cref="IMemoryOwner{T}" />, holding a <see cref="Memory{T}"/> of length <paramref name="length"/>.
/// </summary> /// </summary>
/// <typeparam name="T">Type of the data stored in the buffer.</typeparam> /// <typeparam name="T">Type of the data stored in the buffer.</typeparam>
/// <param name="length">Size of the buffer to allocate.</param> /// <param name="length">Size of the buffer to allocate.</param>

25
tests/ImageSharp.Tests/Formats/Tiff/PhotometricInterpretation/RgbPlanarTiffColorTests.cs

@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0. // Licensed under the Apache License, Version 2.0.
using System; using System;
using System.Buffers;
using System.Collections.Generic; using System.Collections.Generic;
using SixLabors.ImageSharp.Formats.Tiff; using SixLabors.ImageSharp.Formats.Tiff;
using SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation; using SixLabors.ImageSharp.Formats.Tiff.PhotometricInterpretation;
@ -242,19 +243,31 @@ namespace SixLabors.ImageSharp.Tests.Formats.Tiff.PhotometricInterpretation
[MemberData(nameof(Rgb4Data))] [MemberData(nameof(Rgb4Data))]
[MemberData(nameof(Rgb8Data))] [MemberData(nameof(Rgb8Data))]
[MemberData(nameof(Rgb484_Data))] [MemberData(nameof(Rgb484_Data))]
public void Decode_WritesPixelData(byte[][] inputData, TiffBitsPerSample bitsPerSample, int left, int top, int width, int height, Rgba32[][] expectedResult) public void Decode_WritesPixelData(
{ byte[][] inputData,
AssertDecode(expectedResult, pixels => TiffBitsPerSample bitsPerSample,
int left,
int top,
int width,
int height,
Rgba32[][] expectedResult)
=> AssertDecode(
expectedResult,
pixels =>
{ {
var buffers = new IManagedByteBuffer[inputData.Length]; var buffers = new IMemoryOwner<byte>[inputData.Length];
for (int i = 0; i < buffers.Length; i++) for (int i = 0; i < buffers.Length; i++)
{ {
buffers[i] = Configuration.Default.MemoryAllocator.AllocateManagedByteBuffer(inputData[i].Length); buffers[i] = Configuration.Default.MemoryAllocator.Allocate<byte>(inputData[i].Length);
((Span<byte>)inputData[i]).CopyTo(buffers[i].GetSpan()); ((Span<byte>)inputData[i]).CopyTo(buffers[i].GetSpan());
} }
new RgbPlanarTiffColor<Rgba32>(bitsPerSample).Decode(buffers, pixels, left, top, width, height); new RgbPlanarTiffColor<Rgba32>(bitsPerSample).Decode(buffers, pixels, left, top, width, height);
foreach (IMemoryOwner<byte> buffer in buffers)
{
buffer.Dispose();
}
}); });
}
} }
} }

Loading…
Cancel
Save