Skip to content

Commit

Permalink
Merge branch 'main' into inline-vector-constants
Browse files Browse the repository at this point in the history
  • Loading branch information
JimBobSquarePants authored Jun 14, 2022
2 parents d2dbd6e + c934e2f commit 83e28b0
Show file tree
Hide file tree
Showing 59 changed files with 623 additions and 397 deletions.
6 changes: 6 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: 2
updates:
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "weekly"
14 changes: 7 additions & 7 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ jobs:
git config --global core.longpaths true
- name: Git Checkout
uses: actions/checkout@v2
uses: actions/checkout@v3
with:
fetch-depth: 0
submodules: recursive
Expand All @@ -68,7 +68,7 @@ jobs:
run: git lfs ls-files -l | cut -d' ' -f1 | sort > .lfs-assets-id

- name: Git Setup LFS Cache
uses: actions/cache@v2
uses: actions/cache@v3
id: lfs-cache
with:
path: .git/lfs
Expand All @@ -81,15 +81,15 @@ jobs:
uses: NuGet/setup-nuget@v1

- name: NuGet Setup Cache
uses: actions/cache@v2
uses: actions/cache@v3
id: nuget-cache
with:
path: ~/.nuget
key: ${{ runner.os }}-nuget-${{ hashFiles('**/*.csproj', '**/*.props', '**/*.targets') }}
restore-keys: ${{ runner.os }}-nuget-

- name: DotNet Setup
uses: actions/setup-dotnet@v1
uses: actions/setup-dotnet@v2
with:
include-prerelease: true
dotnet-version: |
Expand Down Expand Up @@ -127,7 +127,7 @@ jobs:
XUNIT_PATH: .\tests\ImageSharp.Tests # Required for xunit

- name: Export Failed Output
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v3
if: failure()
with:
name: actual_output_${{ runner.os }}_${{ matrix.options.framework }}${{ matrix.options.runtime }}.zip
Expand All @@ -148,7 +148,7 @@ jobs:
git config --global core.longpaths true
- name: Git Checkout
uses: actions/checkout@v2
uses: actions/checkout@v3
with:
fetch-depth: 0
submodules: recursive
Expand All @@ -157,7 +157,7 @@ jobs:
uses: NuGet/setup-nuget@v1

- name: NuGet Setup Cache
uses: actions/cache@v2
uses: actions/cache@v3
id: nuget-cache
with:
path: ~/.nuget
Expand Down
12 changes: 6 additions & 6 deletions .github/workflows/code-coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
git config --global core.longpaths true
- name: Git Checkout
uses: actions/checkout@v2
uses: actions/checkout@v3
with:
fetch-depth: 0
submodules: recursive
Expand All @@ -34,7 +34,7 @@ jobs:
run: git lfs ls-files -l | cut -d' ' -f1 | sort > .lfs-assets-id

- name: Git Setup LFS Cache
uses: actions/cache@v2
uses: actions/cache@v3
id: lfs-cache
with:
path: .git/lfs
Expand All @@ -47,15 +47,15 @@ jobs:
uses: NuGet/setup-nuget@v1

- name: NuGet Setup Cache
uses: actions/cache@v2
uses: actions/cache@v3
id: nuget-cache
with:
path: ~/.nuget
key: ${{ runner.os }}-nuget-${{ hashFiles('**/*.csproj', '**/*.props', '**/*.targets') }}
restore-keys: ${{ runner.os }}-nuget-

- name: DotNet Setup
uses: actions/setup-dotnet@v1
uses: actions/setup-dotnet@v2
with:
dotnet-version: |
6.0.x
Expand All @@ -74,14 +74,14 @@ jobs:
XUNIT_PATH: .\tests\ImageSharp.Tests # Required for xunit

- name: Export Failed Output
uses: actions/upload-artifact@v2
uses: actions/upload-artifact@v3
if: failure()
with:
name: actual_output_${{ runner.os }}_${{ matrix.options.framework }}${{ matrix.options.runtime }}.zip
path: tests/Images/ActualOutput/

- name: Codecov Update
uses: codecov/codecov-action@v1
uses: codecov/codecov-action@v3
if: matrix.options.codecov == true && startsWith(github.repository, 'SixLabors')
with:
flags: unittests
6 changes: 6 additions & 0 deletions src/ImageSharp/Common/Helpers/Numerics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ public static int LeastCommonMultiple(int a, int b)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static int Modulo8(int x) => x & 7;

/// <summary>
/// Calculates <paramref name="x"/> % 8
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static nint Modulo8(nint x) => x & 7;

/// <summary>
/// Fast (x mod m) calculator, with the restriction that
/// <paramref name="m"/> should be power of 2.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,14 @@ private ulong GetBytes()
c = this.ReadStream();
}

// Found a marker
// We accept multiple FF bytes followed by a 0 as meaning a single FF data byte.
// This data pattern is not valid according to the standard.
// even though it's considered 'invalid' according to the specs.
if (c != 0)
{
this.Marker = (byte)c;
// It's a trick so we won't read past actual marker
this.badData = true;
this.Marker = (byte)c;
this.MarkerPosition = this.stream.Position - 2;
}
}
Expand Down Expand Up @@ -199,7 +201,6 @@ public bool FindNextMarker()
if (b != 0)
{
this.Marker = (byte)b;
this.badData = true;
this.MarkerPosition = this.stream.Position - 2;
return true;
}
Expand Down
53 changes: 25 additions & 28 deletions src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,38 +164,38 @@ public JpegDecoderCore(Configuration configuration, IJpegDecoderOptions options)
/// <summary>
/// Finds the next file marker within the byte stream.
/// </summary>
/// <param name="marker">The buffer to read file markers to.</param>
/// <param name="stream">The input stream.</param>
/// <returns>The <see cref="JpegFileMarker"/></returns>
public static JpegFileMarker FindNextFileMarker(byte[] marker, BufferedReadStream stream)
/// <returns>The <see cref="JpegFileMarker"/>.</returns>
public static JpegFileMarker FindNextFileMarker(BufferedReadStream stream)
{
int value = stream.Read(marker, 0, 2);

if (value == 0)
while (true)
{
return new JpegFileMarker(JpegConstants.Markers.EOI, stream.Length - 2);
}
int b = stream.ReadByte();
if (b == -1)
{
return new JpegFileMarker(JpegConstants.Markers.EOI, stream.Length - 2);
}

if (marker[0] == JpegConstants.Markers.XFF)
{
// According to Section B.1.1.2:
// "Any marker may optionally be preceded by any number of fill bytes, which are bytes assigned code 0xFF."
int m = marker[1];
while (m == JpegConstants.Markers.XFF)
// Found a marker.
if (b == JpegConstants.Markers.XFF)
{
int suffix = stream.ReadByte();
if (suffix == -1)
while (b == JpegConstants.Markers.XFF)
{
return new JpegFileMarker(JpegConstants.Markers.EOI, stream.Length - 2);
// Loop here to discard any padding FF bytes on terminating marker.
b = stream.ReadByte();
if (b == -1)
{
return new JpegFileMarker(JpegConstants.Markers.EOI, stream.Length - 2);
}
}

m = suffix;
// Found a valid marker. Exit loop
if (b is not 0 and (< JpegConstants.Markers.RST0 or > JpegConstants.Markers.RST7))
{
return new JpegFileMarker((byte)(uint)b, stream.Position - 2);
}
}

return new JpegFileMarker((byte)m, stream.Position - 2);
}

return new JpegFileMarker(marker[1], stream.Position - 2, true);
}

/// <inheritdoc/>
Expand Down Expand Up @@ -331,15 +331,12 @@ internal void ParseStream(BufferedReadStream stream, SpectralConverter spectralC
JpegThrowHelper.ThrowInvalidImageContentException("Missing SOI marker.");
}

stream.Read(this.markerBuffer, 0, 2);
byte marker = this.markerBuffer[1];
fileMarker = new JpegFileMarker(marker, (int)stream.Position - 2);
fileMarker = FindNextFileMarker(stream);
this.QuantizationTables ??= new Block8x8F[4];

// Break only when we discover a valid EOI marker.
// https://github.com/SixLabors/ImageSharp/issues/695
while (fileMarker.Marker != JpegConstants.Markers.EOI
|| (fileMarker.Marker == JpegConstants.Markers.EOI && fileMarker.Invalid))
while (fileMarker.Marker != JpegConstants.Markers.EOI)
{
cancellationToken.ThrowIfCancellationRequested();

Expand Down Expand Up @@ -491,7 +488,7 @@ internal void ParseStream(BufferedReadStream stream, SpectralConverter spectralC
}

// Read on.
fileMarker = FindNextFileMarker(this.markerBuffer, stream);
fileMarker = FindNextFileMarker(stream);
}
}

Expand Down
30 changes: 21 additions & 9 deletions src/ImageSharp/Formats/Tiff/Compression/BitWriterUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,23 @@
// Licensed under the Apache License, Version 2.0.

using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

namespace SixLabors.ImageSharp.Formats.Tiff.Compression
{
internal static class BitWriterUtils
{
public static void WriteBits(Span<byte> buffer, int pos, uint count, byte value)
public static void WriteBits(Span<byte> buffer, nint pos, nint count, byte value)
{
int bitPos = pos % 8;
int bufferPos = pos / 8;
int startIdx = bufferPos + bitPos;
int endIdx = (int)(startIdx + count);
nint bitPos = Numerics.Modulo8(pos);
nint bufferPos = pos / 8;
nint startIdx = bufferPos + bitPos;
nint endIdx = startIdx + count;

if (value == 1)
{
for (int i = startIdx; i < endIdx; i++)
for (nint i = startIdx; i < endIdx; i++)
{
WriteBit(buffer, bufferPos, bitPos);

Expand All @@ -30,7 +32,7 @@ public static void WriteBits(Span<byte> buffer, int pos, uint count, byte value)
}
else
{
for (int i = startIdx; i < endIdx; i++)
for (nint i = startIdx; i < endIdx; i++)
{
WriteZeroBit(buffer, bufferPos, bitPos);

Expand All @@ -44,8 +46,18 @@ public static void WriteBits(Span<byte> buffer, int pos, uint count, byte value)
}
}

public static void WriteBit(Span<byte> buffer, int bufferPos, int bitPos) => buffer[bufferPos] |= (byte)(1 << (7 - bitPos));
[MethodImpl(InliningOptions.ShortMethod)]
public static void WriteBit(Span<byte> buffer, nint bufferPos, nint bitPos)
{
ref byte b = ref Unsafe.Add(ref MemoryMarshal.GetReference(buffer), bufferPos);
b |= (byte)(1 << (int)(7 - bitPos));
}

public static void WriteZeroBit(Span<byte> buffer, int bufferPos, int bitPos) => buffer[bufferPos] = (byte)(buffer[bufferPos] & ~(1 << (7 - bitPos)));
[MethodImpl(InliningOptions.ShortMethod)]
public static void WriteZeroBit(Span<byte> buffer, nint bufferPos, nint bitPos)
{
ref byte b = ref Unsafe.Add(ref MemoryMarshal.GetReference(buffer), bufferPos);
b = (byte)(b & ~(1 << (int)(7 - bitPos)));
}
}
}
Loading

0 comments on commit 83e28b0

Please sign in to comment.