Skip to content

Commit

Permalink
tidying
Browse files Browse the repository at this point in the history
  • Loading branch information
saucecontrol committed Dec 3, 2024
1 parent 45fec58 commit 17937d8
Show file tree
Hide file tree
Showing 58 changed files with 246 additions and 281 deletions.
25 changes: 22 additions & 3 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,26 @@ indent_style = space
indent_size = 2

[*.cs]
file_header_template = Copyright © Clinton Ingram and Contributors. Licensed under the MIT License.
file_header_template = Copyright © Clinton Ingram and Contributors\r\nSPDX-License-Identifier: MIT

csharp_style_var_for_built_in_types = false
csharp_style_var_when_type_is_apparent = true
csharp_style_var_elsewhere = true

csharp_prefer_braces = false
csharp_indent_labels = one_less_than_current
csharp_new_line_before_open_brace = types, methods, properties, events, accessors, control_blocks, local_functions

csharp_style_expression_bodied_local_functions = true
csharp_style_expression_bodied_constructors = true
csharp_style_expression_bodied_operators = true
csharp_style_expression_bodied_methods = true

csharp_style_prefer_tuple_swap = true:silent
csharp_style_prefer_range_operator = true:silent
csharp_style_prefer_utf8_string_literals = true:silent
csharp_style_deconstructed_variable_declaration = true:silent
csharp_style_namespace_declarations = file_scoped:suggestion
csharp_style_namespace_declarations = file_scoped:warning

dotnet_diagnostic.CA1031.severity = silent
dotnet_diagnostic.CA1308.severity = silent
Expand All @@ -26,9 +39,15 @@ dotnet_diagnostic.CA2255.severity = silent
dotnet_diagnostic.CS3016.severity = silent ; https://github.com/dotnet/runtime/issues/40461
dotnet_diagnostic.CS1573.severity = silent ; https://github.com/dotnet/roslyn/issues/40325

dotnet_separate_import_directive_groups = true;
dotnet_style_namespace_match_folder = false

dotnet_naming_rule.private_non_field_members_should_be_camel_case.severity = suggestion
dotnet_style_parentheses_in_relational_binary_operators = never_if_unnecessary:silent
dotnet_style_parentheses_in_arithmetic_binary_operators = never_if_unnecessary:silent
dotnet_style_parentheses_in_other_binary_operators = never_if_unnecessary:silent
dotnet_style_parentheses_in_other_operators = never_if_unnecessary:silent

dotnet_naming_rule.private_non_field_members_should_be_camel_case.severity = warning
dotnet_naming_rule.private_non_field_members_should_be_camel_case.symbols = private_non_field_members
dotnet_naming_rule.private_non_field_members_should_be_camel_case.style = camel_case

Expand Down
2 changes: 2 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# switch to file-scoped namespaces
9bd127e20ffe2923033ea5cd09495977bb893a1a
# add SPDX headers
45fec581eae583daeb0264e504e8dbc025851f3f
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[![NuGet](https://buildstats.info/nuget/PhotoSauce.MagicScaler)](https://www.nuget.org/packages/PhotoSauce.MagicScaler/) [![Build Status](https://dev.azure.com/saucecontrol/PhotoSauce/_apis/build/status/saucecontrol.PhotoSauce?branchName=master)](https://dev.azure.com/saucecontrol/PhotoSauce/_build/latest?definitionId=1&branchName=master) [![CI NuGet](https://img.shields.io/badge/nuget-CI%20builds-4da2db?logo=azure-devops)](https://dev.azure.com/saucecontrol/PhotoSauce/_packaging?_a=feed&feed=photosauce_ci)
[![NuGet](https://img.shields.io/nuget/dt/PhotoSauce.MagicScaler?logo=nuget&label=NuGet)](https://www.nuget.org/packages/PhotoSauce.MagicScaler/) [![Build Status](https://dev.azure.com/saucecontrol/PhotoSauce/_apis/build/status/saucecontrol.PhotoSauce?branchName=master)](https://dev.azure.com/saucecontrol/PhotoSauce/_build/latest?definitionId=1&branchName=master) [![CI NuGet](https://img.shields.io/badge/NuGet-CI%20builds-4da2db?logo=azure-devops)](https://dev.azure.com/saucecontrol/PhotoSauce/_packaging?_a=feed&feed=photosauce_ci)

PhotoSauce.MagicScaler
======================
Expand Down
2 changes: 1 addition & 1 deletion src/MagicScaler/Color/ColorParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static bool TryParse(string value, out Color color)
if (value[0] == '#')
value = value[1..];

if (value.Length != 6 && value.Length != 8)
if (value.Length is not (6 or 8))
return false;

if (!uint.TryParse(value, NumberStyles.HexNumber, NumberFormatInfo.InvariantInfo, out uint ival))
Expand Down
37 changes: 15 additions & 22 deletions src/MagicScaler/Color/ColorProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ private static ProfileCurve curveFromPower(double gamma)
// https://www.adobe.com/digitalimag/pdfs/AdobeRGB1998.pdf
const double limit = 32;

var igt = new float[LookupTables.InverseGammaLength];
float[] igt = new float[LookupTables.InverseGammaLength];
for (int i = 0; i < igt.Length; i++)
{
double val = (double)i / LookupTables.InverseGammaScale;
Expand All @@ -147,7 +147,7 @@ private static ProfileCurve curveFromPower(double gamma)

gamma = 1d / gamma;

var gt = new float[LookupTables.GammaLengthFloat];
float[] gt = new float[LookupTables.GammaLengthFloat];
for (int i = 0; i < gt.Length; i++)
{
double val = (double)i / LookupTables.GammaScaleFloat;
Expand All @@ -169,7 +169,7 @@ private static ProfileCurve curveFromPoints(ReadOnlySpan<ushort> points, bool in
curve[i] = (double)points[i] / ushort.MaxValue;

double cscal = curve.Length - 1;
var igt = new float[LookupTables.InverseGammaLength];
float[] igt = new float[LookupTables.InverseGammaLength];
for (int i = 0; i <= LookupTables.InverseGammaScale; i++)
{
double val = (double)i / LookupTables.InverseGammaScale;
Expand All @@ -185,7 +185,7 @@ private static ProfileCurve curveFromPoints(ReadOnlySpan<ushort> points, bool in
if (inverse)
curve.Reverse();

var gt = new float[LookupTables.GammaLengthFloat];
float[] gt = new float[LookupTables.GammaLengthFloat];
for (int i = 0; i <= LookupTables.GammaScaleFloat; i++)
{
double val = (double)i / LookupTables.GammaScaleFloat;
Expand Down Expand Up @@ -234,7 +234,7 @@ private static ProfileCurve curveFromParameters(double a, double b, double c, do
c.IsRoughlyEqualTo(1.000/12.92)
) return sRGB.Curve;

var igt = new float[LookupTables.InverseGammaLength];
float[] igt = new float[LookupTables.InverseGammaLength];
for (int i = 0; i < igt.Length; i++)
{
double val = (double)i / LookupTables.InverseGammaScale;
Expand All @@ -248,7 +248,7 @@ private static ProfileCurve curveFromParameters(double a, double b, double c, do

g = 1d / g;

var gt = new float[LookupTables.GammaLengthFloat];
float[] gt = new float[LookupTables.GammaLengthFloat];
for (int i = 0; i < gt.Length; i++)
{
double val = (double)i / LookupTables.GammaScaleFloat;
Expand Down Expand Up @@ -507,7 +507,7 @@ public static ColorProfile Parse(ReadOnlySpan<byte> prof)
_ => ProfileColorSpace.Other
};

if (pcsColorSpace is not ProfileColorSpace.Xyz || (dataColorSpace is not (ProfileColorSpace.Rgb or ProfileColorSpace.Grey)))
if (pcsColorSpace is not ProfileColorSpace.Xyz || dataColorSpace is not (ProfileColorSpace.Rgb or ProfileColorSpace.Grey))
return new ColorProfile(prof.ToArray(), dataColorSpace, pcsColorSpace, ColorProfileType.Unknown);

uint tagCount = ReadUInt32BigEndian(prof[headerLength..]);
Expand Down Expand Up @@ -730,20 +730,13 @@ protected ColorProfile(byte[] bytes, ProfileColorSpace dataSpace, ProfileColorSp
public record struct ProfileCurve(float[] Gamma, float[] InverseGamma);
}

internal class CurveProfile : ColorProfile
internal class CurveProfile(byte[] bytes, byte[]? compact, ColorProfile.ProfileCurve? curve, ColorProfile.ProfileColorSpace dataSpace, ColorProfile.ProfileColorSpace pcsSpace) : ColorProfile(bytes, dataSpace, pcsSpace, ColorProfileType.Curve)
{
private readonly ConcurrentDictionary<(Type tfrom, Type tto, Type tenc, Type trng, CurveProfile profile), IConverter> converterCache = new();

public bool IsLinear { get; }
public ProfileCurve Curve { get; }
public byte[]? CompactProfile { get; }

public CurveProfile(byte[] bytes, byte[]? compact, ProfileCurve? curve, ProfileColorSpace dataSpace, ProfileColorSpace pcsSpace) : base(bytes, dataSpace, pcsSpace, ColorProfileType.Curve)
{
IsLinear = curve is null;
Curve = curve ?? new ProfileCurve(null!, LookupTables.Alpha);
CompactProfile = compact;
}
public bool IsLinear { get; } = curve is null;
public ProfileCurve Curve { get; } = curve ?? new ProfileCurve(null!, LookupTables.Alpha);
public byte[]? CompactProfile { get; } = compact;

private IConverter addConverter(in (Type, Type, Type, Type, CurveProfile) cacheKey) => converterCache.GetOrAdd(cacheKey, static key => {
if (key.profile.IsLinear)
Expand All @@ -760,12 +753,12 @@ private IConverter addConverter(in (Type, Type, Type, Type, CurveProfile) cacheK

if (key.tenc == typeof(EncodingType.Linear))
{
var gt = key.profile.Curve.Gamma;
float[] gt = key.profile.Curve.Gamma;
if (key.tfrom == typeof(float) && key.tto == typeof(float))
return new ConverterFromLinear<float, float>(gt);

gt = key.trng == typeof(EncodingRange.Video) ? LookupTables.MakeVideoGamma(gt) : gt;
var bgt = gt == LookupTables.SrgbGamma ? LookupTables.SrgbGammaUQ15 : LookupTables.MakeUQ15Gamma(gt);
byte[] bgt = gt == LookupTables.SrgbGamma ? LookupTables.SrgbGammaUQ15 : LookupTables.MakeUQ15Gamma(gt);
if (key.tfrom == typeof(ushort) && key.tto == typeof(byte))
return new ConverterFromLinear<ushort, byte>(bgt);
if (key.tfrom == typeof(float) && key.tto == typeof(byte))
Expand All @@ -774,7 +767,7 @@ private IConverter addConverter(in (Type, Type, Type, Type, CurveProfile) cacheK

if (key.tenc == typeof(EncodingType.Companded))
{
var igt = key.profile.Curve.InverseGamma;
float[] igt = key.profile.Curve.InverseGamma;
if (key.tfrom == typeof(float) && key.tto == typeof(float))
return new ConverterToLinear<float, float>(igt);

Expand Down Expand Up @@ -823,7 +816,7 @@ private static byte[] getResourceBinary(string name)
string resName = $"{nameof(PhotoSauce)}.{nameof(MagicScaler)}.Resources.{name}";
using var stm = typeof(IccProfiles).Assembly.GetManifestResourceStream(resName)!;

var buff = new byte[(int)stm.Length];
byte[] buff = new byte[(int)stm.Length];
stm.FillBuffer(buff);

return buff;
Expand Down
27 changes: 9 additions & 18 deletions src/MagicScaler/Core/BitmapPixelSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,26 @@
namespace PhotoSauce.MagicScaler;

/// <summary>A base <see cref="IPixelSource" /> implementation for wrapping a fully-decoded image bitmap in memory.</summary>
public abstract class BitmapPixelSource : IPixelSource, IDisposable
/// <param name="format">The format of the bitmap pixels.</param>
/// <param name="width">The bitmap width, in pixels.</param>
/// <param name="height">The bitmap height, in pixels.</param>
/// <param name="stride">The number of bytes between pixels in adjacent bitmap rows.</param>
public abstract class BitmapPixelSource(Guid format, int width, int height, int stride) : IPixelSource, IDisposable
{
/// <inheritdoc />
public virtual Guid Format { get; }
public virtual Guid Format { get; } = format;
/// <inheritdoc />
public virtual int Width { get; }
public virtual int Width { get; } = width;
/// <inheritdoc />
public virtual int Height { get; }
public virtual int Height { get; } = height;

/// <summary>The number of bytes between pixels in adjacent bitmap rows.</summary>
protected virtual int Stride { get; }
protected virtual int Stride { get; } = stride;

/// <summary>Exposes the pixel data in the backing bitmap.</summary>
/// <value>A <see cref="ReadOnlySpan{T}" /> instance that exposes the pixel data in memory.</value>
protected abstract ReadOnlySpan<byte> Span { get; }

/// <summary>Sets base properties of the <see cref="BitmapPixelSource" /> implementation.</summary>
/// <param name="format">The format of the bitmap pixels.</param>
/// <param name="width">The bitmap width, in pixels.</param>
/// <param name="height">The bitmap height, in pixels.</param>
/// <param name="stride">The number of bytes between pixels in adjacent bitmap rows.</param>
protected BitmapPixelSource(Guid format, int width, int height, int stride)
{
Format = format;
Width = width;
Height = height;
Stride = stride;
}

/// <inheritdoc />
public virtual void CopyPixels(Rectangle sourceArea, int cbStride, Span<byte> buffer)
{
Expand Down
10 changes: 5 additions & 5 deletions src/MagicScaler/Core/Enums.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,18 +133,18 @@ public enum OrientationMode : byte
[Flags]
public enum ChromaPosition
{
/// <summary>Chroma components are offset between luma rows and columns, as in JPEG images.</summary>
Jpeg = Center,
/// <summary>Chroma components are aligned with luma columns and offset between luma rows, as in most modern video formats.</summary>
Video = Left,
/// <summary>Chroma components are offset between luma rows/columns.</summary>
Center = 0,
/// <summary>Chroma components are aligned with even luma columns.</summary>
Left = 1,
/// <summary>Chroma components are aligned with even luma rows.</summary>
Top = 2,
/// <summary>Chroma components are aligned with odd luma rows.</summary>
Bottom = 4
Bottom = 4,
/// <summary>Chroma components are offset between luma rows and columns, as in JPEG images.</summary>
Jpeg = Center,
/// <summary>Chroma components are aligned with luma columns and offset between luma rows, as in most modern video formats.</summary>
Video = Left
}

/// <summary>Defines the modes that control <a href="https://en.wikipedia.org/wiki/Chroma_subsampling">chroma subsampling</a> for output image formats that support it.</summary>
Expand Down
2 changes: 1 addition & 1 deletion src/MagicScaler/Core/Interpolators.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public sealed class QuadraticInterpolator : IInterpolator, IUniquelyIdentifiable
/// <param name="r">A value between 0.5 and 1.5, where lower values produce a smoother filter and higher values produce a sharper filter.</param>
public QuadraticInterpolator(double r = 1.0)
{
if (r < 0.5 || r > 1.5) throw new ArgumentOutOfRangeException(nameof(r), "Value must be between 0.5 and 1.5");
if (r is < 0.5 or > 1.5) throw new ArgumentOutOfRangeException(nameof(r), "Value must be between 0.5 and 1.5");

this.r = r;
r0 = -2.0 * r;
Expand Down
20 changes: 10 additions & 10 deletions src/MagicScaler/Core/LookupTables.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ internal static class LookupTables
public const int InverseGammaLength = InverseGammaScale + 2;

private static readonly Lazy<float[]> alphaTable = new(() => {
var tbl = new float[InverseGammaLength];
float[] tbl = new float[InverseGammaLength];

for (int i = 0; i < tbl.Length; i++)
tbl[i] = (float)((double)i / byte.MaxValue);
Expand All @@ -32,12 +32,12 @@ internal static class LookupTables

//http://www.w3.org/Graphics/Color/srgb
private static readonly Lazy<Tuple<float[], byte[]>> gammaTable = new(() => {
var tbl = new float[GammaLengthFloat];
float[] tbl = new float[GammaLengthFloat];

for (int i = 0; i < tbl.Length; i++)
{
double d = (double)i / GammaScaleFloat;
if (d <= (0.04045 / 12.92))
if (d <= 0.04045 / 12.92)
d *= 12.92;
else
d = 1.055 * Pow(d, 1.0 / 2.4) - 0.055;
Expand All @@ -51,8 +51,8 @@ internal static class LookupTables
});

private static readonly Lazy<Tuple<float[], ushort[]>> inverseGammaTable = new(() => {
var igtf = new float[InverseGammaLength];
var igtq = new ushort[InverseGammaLength];
float[] igtf = new float[InverseGammaLength];
ushort[] igtq = new ushort[InverseGammaLength];

for (int i = 0; i < igtf.Length; i++)
{
Expand All @@ -73,7 +73,7 @@ internal static class LookupTables
});

private static readonly Lazy<uint[]> octreeIndexTable = new(() => {
var tbl = new uint[256 * 3];
uint[] tbl = new uint[256 * 3];

for (uint i = 0; i < 256; i++)
{
Expand Down Expand Up @@ -122,7 +122,7 @@ public static void Fixup<T>(T[] t, int maxValid)

public static byte[] MakeUQ15Gamma(float[] gt)
{
var gtq = new byte[GammaLengthUQ15];
byte[] gtq = new byte[GammaLengthUQ15];
for (int i = 0; i < gtq.Length; i++)
{
double val = (double)i / GammaScaleUQ15;
Expand All @@ -144,7 +144,7 @@ public static ushort[] MakeUQ15InverseGamma(float[] igt)
if (igt == SrgbInverseGamma)
return SrgbInverseGammaUQ15;

var igtq = new ushort[InverseGammaLength];
ushort[] igtq = new ushort[InverseGammaLength];
for (int i = 0; i < igtq.Length; i++)
igtq[i] = FixToUQ15One(igt[i]);

Expand All @@ -158,7 +158,7 @@ public static float[] MakeVideoGamma(float[] gt)
const int minVal = VideoLumaMin << 2;
const int maxVal = VideoLumaMax << 2;

var gtv = new float[gt.Length];
float[] gtv = new float[gt.Length];
for (int i = 0; i < gtv.Length; i++)
gtv[i] = (float)(((double)gt[i] * (maxVal - minVal) + minVal) / GammaScaleFloat);

Expand All @@ -172,7 +172,7 @@ public static float[] MakeVideoInverseGamma(float[] igt)
const int minVal = VideoLumaMin;
const int maxVal = VideoLumaMax;

var igtv = new float[igt.Length];
float[] igtv = new float[igt.Length];
for (int i = 0; i < igtv.Length; i++)
{
double val = (double)(i.Clamp(minVal, maxVal) - minVal) / (maxVal - minVal);
Expand Down
2 changes: 1 addition & 1 deletion src/MagicScaler/Core/PipelineContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ internal sealed class PipelineContext(ProcessImageSettings settings, IImageConta
public ColorProfile? SourceColorProfile { get; set; }
public ColorProfile? DestColorProfile { get; set; }

public IEnumerable<PixelSourceStats> Stats => profilers?.OfType<ProcessingProfiler>().Select(static p => p.Stats) ?? Enumerable.Empty<PixelSourceStats>();
public IEnumerable<PixelSourceStats> Stats => profilers?.OfType<ProcessingProfiler>().Select(static p => p.Stats) ?? [ ];

public WicPipelineContext WicContext => wicContext ??= new();

Expand Down
4 changes: 2 additions & 2 deletions src/MagicScaler/Core/PixelFormats.cs
Original file line number Diff line number Diff line change
Expand Up @@ -501,8 +501,8 @@ private static unsafe Dictionary<Guid, PixelFormat> getFormatCache()
name.ContainsOrdinal("CMYK") ? PixelColorRepresentation.Cmyk :
name.ContainsOrdinal("Gray") || name.EndsWithOrdinal(" Y") ? PixelColorRepresentation.Grey :
PixelColorRepresentation.Unspecified;
var valEncoding = colorRep == PixelColorRepresentation.Grey || colorRep == PixelColorRepresentation.Bgr || colorRep == PixelColorRepresentation.Rgb ?
numericRep == PixelNumericRepresentation.Fixed || numericRep == PixelNumericRepresentation.Float ? PixelValueEncoding.scRgb :
var valEncoding = colorRep is PixelColorRepresentation.Grey or PixelColorRepresentation.Bgr or PixelColorRepresentation.Rgb ?
numericRep is PixelNumericRepresentation.Fixed or PixelNumericRepresentation.Float ? PixelValueEncoding.scRgb :
PixelValueEncoding.Companded :
PixelValueEncoding.Unspecified;

Expand Down
Loading

0 comments on commit 17937d8

Please sign in to comment.