Browse Source

Merge pull request #747 from carbon/empty

Improve CQ8
af/merge-core
James Jackson-South 7 years ago
committed by GitHub
parent
commit
492fa5d334
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 13
      src/ImageSharp/MetaData/Profiles/Exif/ExifProfile.cs
  2. 20
      src/ImageSharp/MetaData/Profiles/Exif/ExifReader.cs
  3. 4
      src/ImageSharp/MetaData/Profiles/Exif/ExifWriter.cs
  4. 18
      src/ImageSharp/MetaData/Profiles/ICC/IccProfile.cs
  5. 3
      src/ImageSharp/MetaData/Profiles/ICC/IccReader.cs
  6. 4
      src/ImageSharp/MetaData/Profiles/ICC/IccWriter.cs
  7. 6
      src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccCurveTagDataEntry.cs
  8. 4
      tests/ImageSharp.Tests/MetaData/Profiles/ICC/DataWriter/IccDataWriter.PrimitivesTests.cs
  9. 4
      tests/ImageSharp.Tests/MetaData/Profiles/ICC/IccReaderTests.cs

13
src/ImageSharp/MetaData/Profiles/Exif/ExifProfile.cs

@ -50,7 +50,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Exif
{
this.Parts = ExifParts.All;
this.data = data;
this.InvalidTags = new List<ExifTag>();
this.InvalidTags = Array.Empty<ExifTag>();
}
/// <summary>
@ -63,7 +63,11 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Exif
this.Parts = other.Parts;
this.thumbnailLength = other.thumbnailLength;
this.thumbnailOffset = other.thumbnailOffset;
this.InvalidTags = new List<ExifTag>(other.InvalidTags);
this.InvalidTags = other.InvalidTags.Count > 0
? new List<ExifTag>(other.InvalidTags)
: (IReadOnlyList<ExifTag>)Array.Empty<ExifTag>();
if (other.values != null)
{
this.values = new List<ExifValue>(other.Values.Count);
@ -289,7 +293,10 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Exif
this.values = reader.ReadValues();
this.InvalidTags = new List<ExifTag>(reader.InvalidTags);
this.InvalidTags = reader.InvalidTags.Count > 0
? new List<ExifTag>(reader.InvalidTags)
: (IReadOnlyList<ExifTag>)Array.Empty<ExifTag>();
this.thumbnailOffset = (int)reader.ThumbnailOffset;
this.thumbnailLength = (int)reader.ThumbnailLength;
}

20
src/ImageSharp/MetaData/Profiles/Exif/ExifReader.cs

@ -7,7 +7,6 @@ using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using SixLabors.ImageSharp.IO;
using SixLabors.ImageSharp.Primitives;
@ -19,7 +18,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Exif
/// </summary>
internal sealed class ExifReader
{
private readonly List<ExifTag> invalidTags = new List<ExifTag>();
private List<ExifTag> invalidTags;
private readonly byte[] exifData;
private int position;
private Endianness endianness = Endianness.BigEndian;
@ -38,7 +37,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Exif
/// <summary>
/// Gets the invalid tags.
/// </summary>
public IReadOnlyList<ExifTag> InvalidTags => this.invalidTags;
public IReadOnlyList<ExifTag> InvalidTags => this.invalidTags ?? (IReadOnlyList<ExifTag>)Array.Empty<ExifTag>();
/// <summary>
/// Gets the thumbnail length in the byte stream
@ -338,7 +337,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Exif
// Ensure that the new index does not overrun the data
if (newIndex > int.MaxValue)
{
this.invalidTags.Add(tag);
this.AddInvalidTag(tag);
exifValue = default;
@ -349,7 +348,8 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Exif
if (this.RemainingLength < size)
{
this.invalidTags.Add(tag);
this.AddInvalidTag(tag);
this.position = oldIndex;
exifValue = default;
@ -372,6 +372,16 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Exif
return true;
}
private void AddInvalidTag(ExifTag tag)
{
if (this.invalidTags == null)
{
this.invalidTags = new List<ExifTag>();
}
this.invalidTags.Add(tag);
}
private TEnum ToEnum<TEnum>(int value, TEnum defaultValue)
where TEnum : struct
{

4
src/ImageSharp/MetaData/Profiles/Exif/ExifWriter.cs

@ -17,8 +17,8 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Exif
/// <summary>
/// Which parts will be written.
/// </summary>
private ExifParts allowedParts;
private IList<ExifValue> values;
private readonly ExifParts allowedParts;
private readonly IList<ExifValue> values;
private List<int> dataOffsets;
private readonly List<int> ifdIndexes;
private readonly List<int> exifIndexes;

18
src/ImageSharp/MetaData/Profiles/ICC/IccProfile.cs

@ -2,7 +2,6 @@
// Licensed under the Apache License, Version 2.0.
using System;
using System.Collections.Generic;
using System.Security.Cryptography;
namespace SixLabors.ImageSharp.MetaData.Profiles.Icc
@ -20,7 +19,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc
/// <summary>
/// The backing file for the <see cref="Entries"/> property
/// </summary>
private List<IccTagDataEntry> entries;
private IccTagDataEntry[] entries;
/// <summary>
/// ICC profile header
@ -46,13 +45,10 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc
/// </summary>
/// <param name="header">The profile header</param>
/// <param name="entries">The actual profile data</param>
internal IccProfile(IccProfileHeader header, IEnumerable<IccTagDataEntry> entries)
internal IccProfile(IccProfileHeader header, IccTagDataEntry[] entries)
{
Guard.NotNull(header, nameof(header));
Guard.NotNull(entries, nameof(entries));
this.header = header;
this.entries = new List<IccTagDataEntry>(entries);
this.header = header ?? throw new ArgumentNullException(nameof(header));
this.entries = entries ?? throw new ArgumentNullException(nameof(entries));
}
/// <summary>
@ -85,7 +81,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc
/// <summary>
/// Gets the actual profile data
/// </summary>
public List<IccTagDataEntry> Entries
public IccTagDataEntry[] Entries
{
get
{
@ -212,12 +208,12 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc
if (this.data is null)
{
this.entries = new List<IccTagDataEntry>();
this.entries = Array.Empty<IccTagDataEntry>();
return;
}
var reader = new IccReader();
this.entries = new List<IccTagDataEntry>(reader.ReadTagData(this.data));
this.entries = reader.ReadTagData(this.data);
}
}
}

3
src/ImageSharp/MetaData/Profiles/ICC/IccReader.cs

@ -1,6 +1,7 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System;
using System.Collections.Generic;
namespace SixLabors.ImageSharp.MetaData.Profiles.Icc
@ -126,7 +127,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc
// A normal profile usually has 5-15 entries
if (tagCount > 100)
{
return new IccTagTableEntry[0];
return Array.Empty<IccTagTableEntry>();
}
var table = new List<IccTagTableEntry>((int)tagCount);

4
src/ImageSharp/MetaData/Profiles/ICC/IccWriter.cs

@ -68,12 +68,12 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc
}
}
private IccTagTableEntry[] WriteTagData(IccDataWriter writer, List<IccTagDataEntry> entries)
private IccTagTableEntry[] WriteTagData(IccDataWriter writer, IccTagDataEntry[] entries)
{
IEnumerable<IGrouping<IccTagDataEntry, IccTagDataEntry>> grouped = entries.GroupBy(t => t);
// (Header size) + (entry count) + (nr of entries) * (size of table entry)
writer.SetIndex(128 + 4 + (entries.Count * 12));
writer.SetIndex(128 + 4 + (entries.Length * 12));
var table = new List<IccTagTableEntry>();
foreach (IGrouping<IccTagDataEntry, IccTagDataEntry> group in grouped)

6
src/ImageSharp/MetaData/Profiles/ICC/TagDataEntries/IccCurveTagDataEntry.cs

@ -14,7 +14,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc
/// Initializes a new instance of the <see cref="IccCurveTagDataEntry"/> class.
/// </summary>
public IccCurveTagDataEntry()
: this(new float[0], IccProfileTag.Unknown)
: this(Array.Empty<float>(), IccProfileTag.Unknown)
{
}
@ -41,7 +41,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc
/// </summary>
/// <param name="tagSignature">Tag Signature</param>
public IccCurveTagDataEntry(IccProfileTag tagSignature)
: this(new float[0], tagSignature)
: this(Array.Empty<float>(), tagSignature)
{
}
@ -63,7 +63,7 @@ namespace SixLabors.ImageSharp.MetaData.Profiles.Icc
public IccCurveTagDataEntry(float[] curveData, IccProfileTag tagSignature)
: base(IccTypeSignature.Curve, tagSignature)
{
this.CurveData = curveData ?? new float[0];
this.CurveData = curveData ?? Array.Empty<float>();
}
/// <summary>

4
tests/ImageSharp.Tests/MetaData/Profiles/ICC/DataWriter/IccDataWriter.PrimitivesTests.cs

@ -42,7 +42,7 @@ namespace SixLabors.ImageSharp.Tests.Icc
byte[] output = writer.GetData();
Assert.Equal(0, count);
Assert.Equal(new byte[0], output);
Assert.Equal(Array.Empty<byte>(), output);
}
[Fact]
@ -62,7 +62,7 @@ namespace SixLabors.ImageSharp.Tests.Icc
byte[] output = writer.GetData();
Assert.Equal(0, count);
Assert.Equal(new byte[0], output);
Assert.Equal(Array.Empty<byte>(), output);
}
[Theory]

4
tests/ImageSharp.Tests/MetaData/Profiles/ICC/IccReaderTests.cs

@ -15,7 +15,7 @@ namespace SixLabors.ImageSharp.Tests.Icc
IccProfile output = reader.Read(IccTestDataProfiles.Header_Random_Array);
Assert.Equal(0, output.Entries.Count);
Assert.Equal(0, output.Entries.Length);
Assert.NotNull(output.Header);
IccProfileHeader header = output.Header;
@ -46,7 +46,7 @@ namespace SixLabors.ImageSharp.Tests.Icc
IccProfile output = reader.Read(IccTestDataProfiles.Profile_Random_Array);
Assert.Equal(2, output.Entries.Count);
Assert.Equal(2, output.Entries.Length);
Assert.True(ReferenceEquals(output.Entries[0], output.Entries[1]));
}

Loading…
Cancel
Save