Browse Source

Optimize matrix read/write

pull/1567/head
James Jackson-South 4 years ago
parent
commit
9f0f9cb354
  1. 28
      src/ImageSharp/Metadata/Profiles/ICC/DataReader/IccDataReader.Matrix.cs
  2. 31
      src/ImageSharp/Metadata/Profiles/ICC/DataWriter/IccDataWriter.Matrix.cs

28
src/ImageSharp/Metadata/Profiles/ICC/DataReader/IccDataReader.Matrix.cs

@ -17,16 +17,23 @@ internal sealed partial class IccDataReader
/// <returns>The read matrix</returns> /// <returns>The read matrix</returns>
public float[,] ReadMatrix(int xCount, int yCount, bool isSingle) public float[,] ReadMatrix(int xCount, int yCount, bool isSingle)
{ {
var matrix = new float[xCount, yCount]; float[,] matrix = new float[xCount, yCount];
for (int y = 0; y < yCount; y++)
if (isSingle)
{ {
for (int x = 0; x < xCount; x++) for (int y = 0; y < yCount; y++)
{ {
if (isSingle) for (int x = 0; x < xCount; x++)
{ {
matrix[x, y] = this.ReadSingle(); matrix[x, y] = this.ReadSingle();
} }
else }
}
else
{
for (int y = 0; y < yCount; y++)
{
for (int x = 0; x < xCount; x++)
{ {
matrix[x, y] = this.ReadFix16(); matrix[x, y] = this.ReadFix16();
} }
@ -44,14 +51,17 @@ internal sealed partial class IccDataReader
/// <returns>The read matrix</returns> /// <returns>The read matrix</returns>
public float[] ReadMatrix(int yCount, bool isSingle) public float[] ReadMatrix(int yCount, bool isSingle)
{ {
var matrix = new float[yCount]; float[] matrix = new float[yCount];
for (int i = 0; i < yCount; i++) if (isSingle)
{ {
if (isSingle) for (int i = 0; i < yCount; i++)
{ {
matrix[i] = this.ReadSingle(); matrix[i] = this.ReadSingle();
} }
else }
else
{
for (int i = 0; i < yCount; i++)
{ {
matrix[i] = this.ReadFix16(); matrix[i] = this.ReadFix16();
} }

31
src/ImageSharp/Metadata/Profiles/ICC/DataWriter/IccDataWriter.Matrix.cs

@ -1,4 +1,4 @@
// Copyright (c) Six Labors. // Copyright (c) Six Labors.
// Licensed under the Six Labors Split License. // Licensed under the Six Labors Split License.
using System.Numerics; using System.Numerics;
@ -61,15 +61,21 @@ internal sealed partial class IccDataWriter
public int WriteMatrix(in DenseMatrix<float> value, bool isSingle) public int WriteMatrix(in DenseMatrix<float> value, bool isSingle)
{ {
int count = 0; int count = 0;
for (int y = 0; y < value.Rows; y++) if (isSingle)
{ {
for (int x = 0; x < value.Columns; x++) for (int y = 0; y < value.Rows; y++)
{ {
if (isSingle) for (int x = 0; x < value.Columns; x++)
{ {
count += this.WriteSingle(value[x, y]); count += this.WriteSingle(value[x, y]);
} }
else }
}
else
{
for (int y = 0; y < value.Rows; y++)
{
for (int x = 0; x < value.Columns; x++)
{ {
count += this.WriteFix16(value[x, y]); count += this.WriteFix16(value[x, y]);
} }
@ -88,15 +94,22 @@ internal sealed partial class IccDataWriter
public int WriteMatrix(float[,] value, bool isSingle) public int WriteMatrix(float[,] value, bool isSingle)
{ {
int count = 0; int count = 0;
for (int y = 0; y < value.GetLength(1); y++)
if (isSingle)
{ {
for (int x = 0; x < value.GetLength(0); x++) for (int y = 0; y < value.GetLength(1); y++)
{ {
if (isSingle) for (int x = 0; x < value.GetLength(0); x++)
{ {
count += this.WriteSingle(value[x, y]); count += this.WriteSingle(value[x, y]);
} }
else }
}
else
{
for (int y = 0; y < value.GetLength(1); y++)
{
for (int x = 0; x < value.GetLength(0); x++)
{ {
count += this.WriteFix16(value[x, y]); count += this.WriteFix16(value[x, y]);
} }

Loading…
Cancel
Save