Skip to content

Commit

Permalink
Optomizations.
Browse files Browse the repository at this point in the history
  • Loading branch information
juliusfriedman committed Oct 19, 2024
1 parent 76ea9e9 commit 7f17dff
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 66 deletions.
4 changes: 2 additions & 2 deletions Codecs/Image/Image.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1230,8 +1230,8 @@ public static void TestSetComponentData()
var image = new Image(format, 100, 100);
var data = new MemorySegment(image.ImageFormat.Length);
image.SetComponentData(0, 0, 0, data);
var retrievedData = image.GetSampleData(0, 0);
Console.WriteLine(retrievedData.SequenceEqual(data) ? "Pass" : "Fail");
var retrievedData = image[0, 0];
Console.WriteLine(retrievedData.SelectMany(arg => arg).SequenceEqual(data) ? "Pass" : "Fail");
}
}
}
20 changes: 10 additions & 10 deletions Codecs/Image/Transformations/RgbToYuvImageTransformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@ public RgbToYuvImageTransformation(Image source, Image dest, Codec.Transformatio
private static bool IsRgbImage(ImageFormat format)
{
return format.Components.Length >= 3 &&
format.Components.Any(c => c.Id == ImageFormat.RedChannelId) &&
format.Components.Any(c => c.Id == ImageFormat.GreenChannelId) &&
format.Components.Any(c => c.Id == ImageFormat.BlueChannelId);
format.GetComponentById(ImageFormat.RedChannelId) != null &&
format.GetComponentById(ImageFormat.GreenChannelId) != null &&
format.GetComponentById(ImageFormat.BlueChannelId) != null;
}

// Check if the image format is a YUV format
private static bool IsYuvImage(ImageFormat format)
{
return format.Components.Length >= 3 &&
format.Components.Any(c => c.Id == ImageFormat.LumaChannelId) &&
format.Components.Any(c => c.Id == ImageFormat.ChromaMajorChannelId) &&
format.Components.Any(c => c.Id == ImageFormat.ChromaMinorChannelId);
format.GetComponentById(ImageFormat.LumaChannelId) != null &&
format.GetComponentById(ImageFormat.ChromaMajorChannelId) != null&&
format.GetComponentById(ImageFormat.ChromaMinorChannelId) != null;
}

public override void Transform()
Expand Down Expand Up @@ -111,16 +111,16 @@ private static bool IsRgbImage(ImageFormat format)
return format.Components.Length >= 3 &&
format.Components.Any(c => c.Id == ImageFormat.RedChannelId) &&
format.Components.Any(c => c.Id == ImageFormat.GreenChannelId) &&
format.Components.Any(c => c.Id == ImageFormat.BlueChannelId);
format.GetComponentById(ImageFormat.BlueChannelId) != null;
}

// Check if the image format is a YUV format
private static bool IsYuvImage(ImageFormat format)
{
return format.Components.Length >= 3 &&
format.Components.Any(c => c.Id == ImageFormat.LumaChannelId) &&
format.Components.Any(c => c.Id == ImageFormat.ChromaMajorChannelId) &&
format.Components.Any(c => c.Id == ImageFormat.ChromaMinorChannelId);
format.GetComponentById(ImageFormat.LumaChannelId) != null &&
format.GetComponentById(ImageFormat.ChromaMajorChannelId) != null&&
format.GetComponentById(ImageFormat.ChromaMinorChannelId) != null;
}

public override void Transform()
Expand Down
18 changes: 9 additions & 9 deletions Codecs/Image/Transformations/YuvToRgbTransformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,21 @@ public YuvToRgbTransformation(Image source, Image dest, Codec.TransformationQual
}

// Check if the image format is a YUV format
private static bool IsYuvImage(ImageFormat format)
private static bool IsRgbImage(ImageFormat format)
{
return format.Components.Length >= 3 &&
format.Components.Any(c => c.Id == ImageFormat.LumaChannelId) &&
format.Components.Any(c => c.Id == ImageFormat.ChromaMajorChannelId) &&
format.Components.Any(c => c.Id == ImageFormat.ChromaMinorChannelId);
format.GetComponentById(ImageFormat.RedChannelId) != null &&
format.GetComponentById(ImageFormat.GreenChannelId) != null &&
format.GetComponentById(ImageFormat.BlueChannelId) != null;
}

// Check if the image format is an RGB format
private static bool IsRgbImage(ImageFormat format)
// Check if the image format is a YUV format
private static bool IsYuvImage(ImageFormat format)
{
return format.Components.Length >= 3 &&
format.Components.Any(c => c.Id == ImageFormat.RedChannelId) &&
format.Components.Any(c => c.Id == ImageFormat.GreenChannelId) &&
format.Components.Any(c => c.Id == ImageFormat.BlueChannelId);
format.GetComponentById(ImageFormat.LumaChannelId) != null &&
format.GetComponentById(ImageFormat.ChromaMajorChannelId) != null &&
format.GetComponentById(ImageFormat.ChromaMinorChannelId) != null;
}

public override void Transform()
Expand Down
55 changes: 10 additions & 45 deletions Common/Classes/SegmentStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ namespace Media.Common
/// <summary>
/// Used to crete a continious stream to locations of memory which may not be next to each other and could even overlap.
/// </summary>
public class SegmentStream : System.IO.Stream, IEnumerable<byte>, IDisposed
public class SegmentStream : System.IO.Stream, IDisposed
{
///// <summary>
///// Combines all given instances into a single instance.
Expand Down Expand Up @@ -1098,18 +1098,6 @@ bool IDisposed.ShouldDispose

void IDisposable.Dispose() { Close(); }

public IEnumerator<byte> GetEnumerator()
{
int read = 0;
while ((read = ReadByte()) != -1)
yield return (byte)read;
}

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}

//~SegmentStream() { Close(); }

#endregion
Expand Down Expand Up @@ -1705,46 +1693,23 @@ public static void TestSeek()
}

public static void TestAppend()
{
// Arrange
var segmentStream = new SegmentStream();
var memorySegment1 = new Common.MemorySegment(new byte[] { 1, 2, 3, 4 });
var memorySegment2 = new Common.MemorySegment(new byte[] { 5, 6, 7, 8 });

segmentStream.AddMemory(memorySegment1);

// Act
segmentStream.AddMemory(memorySegment2);

// Assert
// Add assertions to verify the memory segments were appended correctly
var result = segmentStream.ToArray();
if (result.Length != 8 || result[0] != 1 || result[4] != 5)
{
throw new Exception("TestAppend failed");
}
}

public static void TestEnumerator()
{
// Arrange
var segmentStream = new SegmentStream();
var memorySegment = new Common.MemorySegment(new byte[] { 1, 2, 3, 4 });
segmentStream.AddMemory(memorySegment);
var memorySegment1 = new Common.MemorySegment(new byte[] { 1, 2, 3, 4 });
var memorySegment2 = new Common.MemorySegment(new byte[] { 5, 6, 7, 8 });

segmentStream.AddMemory(memorySegment1);

// Act
var enumerator = segmentStream.GetEnumerator();
var result = new List<byte>();
while (enumerator.MoveNext())
{
result.Add(enumerator.Current);
}
segmentStream.AddMemory(memorySegment2);

// Assert
// Add assertions to verify the enumerator works correctly
if (result.Count != 4 || result[0] != 1 || result[3] != 4)
// Add assertions to verify the memory segments were appended correctly
var result = segmentStream.ToArray();
if (result.Length != 8 || result[0] != 1 || result[4] != 5)
{
throw new Exception("TestEnumerator failed");
throw new Exception("TestAppend failed");
}
}
}
Expand Down

0 comments on commit 7f17dff

Please sign in to comment.