Skip to content

Commit

Permalink
Merge pull request #1443 from SixLabors/bp/bmpEnsureSpanLengthIsEqual
Browse files Browse the repository at this point in the history
Ensure Span length of source and destination are equal during pixel conversion
  • Loading branch information
brianpopow authored Nov 29, 2020
2 parents 976bdd5 + 7c77e7e commit 4aed9f4
Show file tree
Hide file tree
Showing 14 changed files with 62 additions and 217 deletions.
17 changes: 11 additions & 6 deletions src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -262,16 +262,18 @@ private void Write32Bit<TPixel>(Stream stream, Buffer2D<TPixel> pixels)
private void Write24Bit<TPixel>(Stream stream, Buffer2D<TPixel> pixels)
where TPixel : unmanaged, IPixel<TPixel>
{
using (IManagedByteBuffer row = this.AllocateRow(pixels.Width, 3))
int width = pixels.Width;
int rowBytesWithoutPadding = width * 3;
using (IManagedByteBuffer row = this.AllocateRow(width, 3))
{
for (int y = pixels.Height - 1; y >= 0; y--)
{
Span<TPixel> pixelSpan = pixels.GetRowSpan(y);
PixelOperations<TPixel>.Instance.ToBgr24Bytes(
this.configuration,
pixelSpan,
row.GetSpan(),
pixelSpan.Length);
row.Slice(0, rowBytesWithoutPadding),
width);
stream.Write(row.Array, 0, row.Length());
}
}
Expand All @@ -286,7 +288,9 @@ private void Write24Bit<TPixel>(Stream stream, Buffer2D<TPixel> pixels)
private void Write16Bit<TPixel>(Stream stream, Buffer2D<TPixel> pixels)
where TPixel : unmanaged, IPixel<TPixel>
{
using (IManagedByteBuffer row = this.AllocateRow(pixels.Width, 2))
int width = pixels.Width;
int rowBytesWithoutPadding = width * 2;
using (IManagedByteBuffer row = this.AllocateRow(width, 2))
{
for (int y = pixels.Height - 1; y >= 0; y--)
{
Expand All @@ -295,7 +299,7 @@ private void Write16Bit<TPixel>(Stream stream, Buffer2D<TPixel> pixels)
PixelOperations<TPixel>.Instance.ToBgra5551Bytes(
this.configuration,
pixelSpan,
row.GetSpan(),
row.Slice(0, rowBytesWithoutPadding),
pixelSpan.Length);

stream.Write(row.Array, 0, row.Length());
Expand Down Expand Up @@ -341,7 +345,8 @@ private void Write8BitColor<TPixel>(Stream stream, ImageFrame<TPixel> image, Spa
using IndexedImageFrame<TPixel> quantized = frameQuantizer.BuildPaletteAndQuantizeFrame(image, image.Bounds());

ReadOnlySpan<TPixel> quantizedColors = quantized.Palette.Span;
PixelOperations<TPixel>.Instance.ToBgra32(this.configuration, quantizedColors, MemoryMarshal.Cast<byte, Bgra32>(colorPalette));
var quantizedColorBytes = quantizedColors.Length * 4;
PixelOperations<TPixel>.Instance.ToBgra32(this.configuration, quantizedColors, MemoryMarshal.Cast<byte, Bgra32>(colorPalette.Slice(0, quantizedColorBytes)));
Span<uint> colorPaletteAsUInt = MemoryMarshal.Cast<byte, uint>(colorPalette);
for (int i = 0; i < colorPaletteAsUInt.Length; i++)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@ public partial struct Argb32
/// </summary>
internal partial class PixelOperations : PixelOperations<Argb32>
{

/// <inheritdoc />
/// <inheritdoc />
public override void FromArgb32(Configuration configuration, ReadOnlySpan<Argb32> source, Span<Argb32> destinationPixels)
{
Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(source, destinationPixels, nameof(destinationPixels));

source.CopyTo(destinationPixels);
source.CopyTo(destinationPixels.Slice(0, source.Length));
}

/// <inheritdoc />
Expand All @@ -37,9 +36,8 @@ public override void ToArgb32(Configuration configuration, ReadOnlySpan<Argb32>
Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels));

sourcePixels.CopyTo(destinationPixels);
sourcePixels.CopyTo(destinationPixels.Slice(0, sourcePixels.Length));
}

/// <inheritdoc />
public override void FromVector4Destructive(
Configuration configuration,
Expand All @@ -59,7 +57,6 @@ public override void ToVector4(
{
Vector4Converters.RgbaCompatible.ToVector4(configuration, this, sourcePixels, destVectors, modifiers.Remove(PixelConversionModifiers.Scale));
}

/// <inheritdoc />
public override void ToRgba32(
Configuration configuration,
Expand Down Expand Up @@ -87,7 +84,6 @@ public override void FromRgba32(
Span<byte> dest = MemoryMarshal.Cast<Argb32, byte>(destinationPixels);
PixelConverter.FromRgba32.ToArgb32(source, dest);
}

/// <inheritdoc />
public override void ToBgra32(
Configuration configuration,
Expand Down Expand Up @@ -115,7 +111,6 @@ public override void FromBgra32(
Span<byte> dest = MemoryMarshal.Cast<Argb32, byte>(destinationPixels);
PixelConverter.FromBgra32.ToArgb32(source, dest);
}

/// <inheritdoc />
public override void ToRgb24(
Configuration configuration,
Expand Down Expand Up @@ -143,7 +138,6 @@ public override void FromRgb24(
Span<byte> dest = MemoryMarshal.Cast<Argb32, byte>(destinationPixels);
PixelConverter.FromRgb24.ToArgb32(source, dest);
}

/// <inheritdoc />
public override void ToBgr24(
Configuration configuration,
Expand Down Expand Up @@ -171,7 +165,6 @@ public override void FromBgr24(
Span<byte> dest = MemoryMarshal.Cast<Argb32, byte>(destinationPixels);
PixelConverter.FromBgr24.ToArgb32(source, dest);
}

/// <inheritdoc />
public override void ToL8(
Configuration configuration,
Expand All @@ -192,7 +185,6 @@ public override void ToL8(
dp.FromArgb32(sp);
}
}

/// <inheritdoc />
public override void ToL16(
Configuration configuration,
Expand All @@ -213,7 +205,6 @@ public override void ToL16(
dp.FromArgb32(sp);
}
}

/// <inheritdoc />
public override void ToLa16(
Configuration configuration,
Expand All @@ -234,7 +225,6 @@ public override void ToLa16(
dp.FromArgb32(sp);
}
}

/// <inheritdoc />
public override void ToLa32(
Configuration configuration,
Expand All @@ -255,7 +245,6 @@ public override void ToLa32(
dp.FromArgb32(sp);
}
}

/// <inheritdoc />
public override void ToRgb48(
Configuration configuration,
Expand All @@ -276,7 +265,6 @@ public override void ToRgb48(
dp.FromArgb32(sp);
}
}

/// <inheritdoc />
public override void ToRgba64(
Configuration configuration,
Expand All @@ -297,7 +285,6 @@ public override void ToRgba64(
dp.FromArgb32(sp);
}
}

/// <inheritdoc />
public override void ToBgra5551(
Configuration configuration,
Expand All @@ -318,14 +305,13 @@ public override void ToBgra5551(
dp.FromArgb32(sp);
}
}

/// <inheritdoc />
public override void From<TSourcePixel>(
Configuration configuration,
ReadOnlySpan<TSourcePixel> sourcePixels,
Span<Argb32> destinationPixels)
{
PixelOperations<TSourcePixel>.Instance.ToArgb32(configuration, sourcePixels, destinationPixels);
PixelOperations<TSourcePixel>.Instance.ToArgb32(configuration, sourcePixels, destinationPixels.Slice(0, sourcePixels.Length));
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@ public partial struct Bgr24
/// </summary>
internal partial class PixelOperations : PixelOperations<Bgr24>
{

/// <inheritdoc />
/// <inheritdoc />
public override void FromBgr24(Configuration configuration, ReadOnlySpan<Bgr24> source, Span<Bgr24> destinationPixels)
{
Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(source, destinationPixels, nameof(destinationPixels));

source.CopyTo(destinationPixels);
source.CopyTo(destinationPixels.Slice(0, source.Length));
}

/// <inheritdoc />
Expand All @@ -37,9 +36,8 @@ public override void ToBgr24(Configuration configuration, ReadOnlySpan<Bgr24> so
Guard.NotNull(configuration, nameof(configuration));
Guard.DestinationShouldNotBeTooShort(sourcePixels, destinationPixels, nameof(destinationPixels));

sourcePixels.CopyTo(destinationPixels);
sourcePixels.CopyTo(destinationPixels.Slice(0, sourcePixels.Length));
}

/// <inheritdoc />
public override void FromVector4Destructive(
Configuration configuration,
Expand All @@ -59,7 +57,6 @@ public override void ToVector4(
{
Vector4Converters.RgbaCompatible.ToVector4(configuration, this, sourcePixels, destVectors, modifiers.Remove(PixelConversionModifiers.Scale | PixelConversionModifiers.Premultiply));
}

/// <inheritdoc />
public override void ToRgba32(
Configuration configuration,
Expand Down Expand Up @@ -87,7 +84,6 @@ public override void FromRgba32(
Span<byte> dest = MemoryMarshal.Cast<Bgr24, byte>(destinationPixels);
PixelConverter.FromRgba32.ToBgr24(source, dest);
}

/// <inheritdoc />
public override void ToArgb32(
Configuration configuration,
Expand Down Expand Up @@ -115,7 +111,6 @@ public override void FromArgb32(
Span<byte> dest = MemoryMarshal.Cast<Bgr24, byte>(destinationPixels);
PixelConverter.FromArgb32.ToBgr24(source, dest);
}

/// <inheritdoc />
public override void ToBgra32(
Configuration configuration,
Expand Down Expand Up @@ -143,7 +138,6 @@ public override void FromBgra32(
Span<byte> dest = MemoryMarshal.Cast<Bgr24, byte>(destinationPixels);
PixelConverter.FromBgra32.ToBgr24(source, dest);
}

/// <inheritdoc />
public override void ToRgb24(
Configuration configuration,
Expand Down Expand Up @@ -171,7 +165,6 @@ public override void FromRgb24(
Span<byte> dest = MemoryMarshal.Cast<Bgr24, byte>(destinationPixels);
PixelConverter.FromRgb24.ToBgr24(source, dest);
}

/// <inheritdoc />
public override void ToL8(
Configuration configuration,
Expand All @@ -192,7 +185,6 @@ public override void ToL8(
dp.FromBgr24(sp);
}
}

/// <inheritdoc />
public override void ToL16(
Configuration configuration,
Expand All @@ -213,7 +205,6 @@ public override void ToL16(
dp.FromBgr24(sp);
}
}

/// <inheritdoc />
public override void ToLa16(
Configuration configuration,
Expand All @@ -234,7 +225,6 @@ public override void ToLa16(
dp.FromBgr24(sp);
}
}

/// <inheritdoc />
public override void ToLa32(
Configuration configuration,
Expand All @@ -255,7 +245,6 @@ public override void ToLa32(
dp.FromBgr24(sp);
}
}

/// <inheritdoc />
public override void ToRgb48(
Configuration configuration,
Expand All @@ -276,7 +265,6 @@ public override void ToRgb48(
dp.FromBgr24(sp);
}
}

/// <inheritdoc />
public override void ToRgba64(
Configuration configuration,
Expand All @@ -297,7 +285,6 @@ public override void ToRgba64(
dp.FromBgr24(sp);
}
}

/// <inheritdoc />
public override void ToBgra5551(
Configuration configuration,
Expand All @@ -318,14 +305,13 @@ public override void ToBgra5551(
dp.FromBgr24(sp);
}
}

/// <inheritdoc />
public override void From<TSourcePixel>(
Configuration configuration,
ReadOnlySpan<TSourcePixel> sourcePixels,
Span<Bgr24> destinationPixels)
{
PixelOperations<TSourcePixel>.Instance.ToBgr24(configuration, sourcePixels, destinationPixels);
PixelOperations<TSourcePixel>.Instance.ToBgr24(configuration, sourcePixels, destinationPixels.Slice(0, sourcePixels.Length));
}

}
Expand Down
Loading

0 comments on commit 4aed9f4

Please sign in to comment.