Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace ImageSharp.Formats.Jpg
using System.Runtime.InteropServices;

/// <summary>
/// Encapsulates the implementation of processing "raw" <see cref="DecodedBlockArray"/>-s into Jpeg image channels.
/// Encapsulates the implementation of processing "raw" <see cref="Buffer{T}"/>-s into Jpeg image channels.
/// </summary>
[StructLayout(LayoutKind.Sequential)]
internal unsafe struct JpegBlockProcessor
Expand Down Expand Up @@ -47,10 +47,10 @@ public static void Init(JpegBlockProcessor* processor, int componentIndex)
/// <param name="decoder">The <see cref="JpegDecoderCore"/> instance</param>
public void ProcessAllBlocks(JpegDecoderCore decoder)
{
DecodedBlockArray blockArray = decoder.DecodedBlocks[this.componentIndex];
for (int i = 0; i < blockArray.Count; i++)
Buffer<DecodedBlock> blockArray = decoder.DecodedBlocks[this.componentIndex];
for (int i = 0; i < blockArray.Length; i++)
{
this.ProcessBlockColors(decoder, ref blockArray.Buffer[i]);
this.ProcessBlockColors(decoder, ref blockArray[i]);
}
}

Expand Down
62 changes: 20 additions & 42 deletions src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegPixelArea.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
// </copyright>
namespace ImageSharp.Formats.Jpg
{
using System.Buffers;
using System.Runtime.CompilerServices;

/// <summary>
Expand All @@ -15,42 +14,50 @@ internal struct JpegPixelArea
/// <summary>
/// Initializes a new instance of the <see cref="JpegPixelArea" /> struct from existing data.
/// </summary>
/// <param name="pixels">The pixel array</param>
/// <param name="striede">The stride</param>
/// <param name="pixels">The pixel buffer</param>
/// <param name="stride">The stride</param>
/// <param name="offset">The offset</param>
public JpegPixelArea(byte[] pixels, int striede, int offset)
public JpegPixelArea(Buffer2D<byte> pixels, int stride, int offset)
{
this.Stride = striede;
this.Stride = stride;
this.Pixels = pixels;
this.Offset = offset;
}

/// <summary>
/// Gets the pixels.
/// Initializes a new instance of the <see cref="JpegPixelArea" /> struct from existing buffer.
/// <see cref="Stride"/> will be set to <see cref="Buffer2D{T}.Width"/> of <paramref name="pixels"/> and <see cref="Offset"/> will be set to 0.
/// </summary>
public byte[] Pixels { get; private set; }
/// <param name="pixels">The pixel buffer</param>
public JpegPixelArea(Buffer2D<byte> pixels)
: this(pixels, pixels.Width, 0)
{
}

/// <summary>
/// Gets the pixels buffer.
/// </summary>
public Buffer2D<byte> Pixels { get; private set; }

/// <summary>
/// Gets a value indicating whether the instance has been initalized. (Is not default(JpegPixelArea))
/// </summary>
public bool IsInitialized => this.Pixels != null;

/// <summary>
/// Gets or the stride.
/// Gets the stride.
/// </summary>
public int Stride { get; }

/// <summary>
/// Gets or the offset.
/// Gets the offset.
/// </summary>
public int Offset { get; }

/// <summary>
/// Gets a <see cref="MutableSpan{T}" /> of bytes to the pixel area
/// </summary>
public MutableSpan<byte> Span => new MutableSpan<byte>(this.Pixels, this.Offset);

private static ArrayPool<byte> BytePool => ArrayPool<byte>.Shared;
public MutableSpan<byte> Span => new MutableSpan<byte>(this.Pixels.Array, this.Offset);

/// <summary>
/// Returns the pixel at (x, y)
Expand All @@ -67,35 +74,6 @@ public JpegPixelArea(byte[] pixels, int striede, int offset)
}
}

/// <summary>
/// Creates a new instance of the <see cref="JpegPixelArea" /> struct.
/// Pixel array will be taken from a pool, this instance will be the owner of it's pixel data, therefore
/// <see cref="ReturnPooled" /> should be called when the instance is no longer needed.
/// </summary>
/// <param name="width">The width.</param>
/// <param name="height">The height.</param>
/// <returns>A <see cref="JpegPixelArea" /> with pooled data</returns>
public static JpegPixelArea CreatePooled(int width, int height)
{
int size = width * height;
byte[] pixels = BytePool.Rent(size);
return new JpegPixelArea(pixels, width, 0);
}

/// <summary>
/// Returns <see cref="Pixels" /> to the pool
/// </summary>
public void ReturnPooled()
{
if (this.Pixels == null)
{
return;
}

BytePool.Return(this.Pixels);
this.Pixels = null;
}

/// <summary>
/// Gets the subarea that belongs to the Block8x8 defined by block indices
/// </summary>
Expand Down Expand Up @@ -129,7 +107,7 @@ public int GetRowOffset(int y)
public unsafe void LoadColorsFrom(Block8x8F* block, Block8x8F* temp)
{
// Level shift by +128, clip to [0, 255], and write to dst.
block->CopyColorsTo(new MutableSpan<byte>(this.Pixels, this.Offset), this.Stride, temp);
block->CopyColorsTo(new MutableSpan<byte>(this.Pixels.Array, this.Offset), this.Stride, temp);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -171,16 +171,16 @@ public void DecodeBlocks(JpegDecoderCore decoder)

// Take an existing block (required when progressive):
int blockIndex = this.GetBlockIndex(decoder);
this.data.Block = decoder.DecodedBlocks[this.ComponentIndex].Buffer[blockIndex].Block;
this.data.Block = decoder.DecodedBlocks[this.ComponentIndex][blockIndex].Block;

if (!decoder.InputProcessor.UnexpectedEndOfStreamReached)
{
this.DecodeBlock(decoder, scanIndex);
}

// Store the decoded block
DecodedBlockArray blocks = decoder.DecodedBlocks[this.ComponentIndex];
blocks.Buffer[blockIndex].SaveBlock(this.bx, this.by, ref this.data.Block);
Buffer<DecodedBlock> blocks = decoder.DecodedBlocks[this.ComponentIndex];
blocks[blockIndex].SaveBlock(this.bx, this.by, ref this.data.Block);
}

// for j
Expand Down
18 changes: 9 additions & 9 deletions src/ImageSharp/Formats/Jpeg/Components/Decoder/YCbCrImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ internal class YCbCrImage : IDisposable
/// <summary>
/// Gets the luminance components channel as <see cref="JpegPixelArea" />.
/// </summary>
public JpegPixelArea YChannel;
public Buffer2D<byte> YChannel;

/// <summary>
/// Gets the blue chroma components channel as <see cref="JpegPixelArea" />.
/// </summary>
public JpegPixelArea CbChannel;
public Buffer2D<byte> CbChannel;

/// <summary>
/// Gets an offseted <see cref="JpegPixelArea" /> to the Cr channel
/// </summary>
public JpegPixelArea CrChannel;
public Buffer2D<byte> CrChannel;
#pragma warning restore SA1401

/// <summary>
Expand All @@ -44,9 +44,9 @@ public YCbCrImage(int width, int height, YCbCrSubsampleRatio ratio)
this.YStride = width;
this.CStride = cSize.Width;

this.YChannel = JpegPixelArea.CreatePooled(width, height);
this.CbChannel = JpegPixelArea.CreatePooled(cSize.Width, cSize.Height);
this.CrChannel = JpegPixelArea.CreatePooled(cSize.Width, cSize.Height);
this.YChannel = Buffer2D<byte>.CreateClean(width, height);
this.CbChannel = Buffer2D<byte>.CreateClean(cSize.Width, cSize.Height);
this.CrChannel = Buffer2D<byte>.CreateClean(cSize.Width, cSize.Height);
}

/// <summary>
Expand Down Expand Up @@ -106,9 +106,9 @@ public enum YCbCrSubsampleRatio
/// </summary>
public void Dispose()
{
this.YChannel.ReturnPooled();
this.CbChannel.ReturnPooled();
this.CrChannel.ReturnPooled();
this.YChannel.Dispose();
this.CbChannel.Dispose();
this.CrChannel.Dispose();
}

/// <summary>
Expand Down
Loading