Skip to content

Commit

Permalink
Test and additional logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
juliusfriedman committed Oct 19, 2024
1 parent 4b3e283 commit cd9831b
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 5 deletions.
48 changes: 48 additions & 0 deletions Codecs/Image/Image.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1185,5 +1185,53 @@ public static void TestFillVector()
if (image.Data.Array.Any(b => b != byte.MaxValue)) throw new InvalidOperationException("Did not set Component data (Vector)");
}
}

public static void TestCalculateSize()
{
var format = ImageFormat.RGB(8);
int width = 100;
int height = 100;
int size = Image.CalculateSize(format, width, height);
Console.WriteLine(width * height * format.Length == size ? "Pass" : "Fail");
}

public static void TestPlaneDimensions()
{
var format = ImageFormat.RGB(8);
var image = new Image(format, 100, 100);
int planeWidth = image.PlaneWidth(0);
int planeHeight = image.PlaneHeight(0);
Console.WriteLine(planeWidth == 100 && planeHeight == 100 ? "Pass" : "Fail");
}

public static void TestSaveBitmap()
{
var format = ImageFormat.RGB(8);
var image = new Image(format, 100, 100);
using (var stream = new MemoryStream())
{
image.SaveBitmap(stream);
Console.WriteLine(stream.Length > 0 ? "Pass" : "Fail");
}
}

public static void TestFill()
{
var format = ImageFormat.RGB(8);
var image = new Image(format, 100, 100);
image.Fill(255);
var data = image.GetComponentData(0, 0, format.Components[0]);
Console.WriteLine(data.All(b => b == 255) ? "Pass" : "Fail");
}

public static void TestSetComponentData()
{
var format = ImageFormat.RGB(8);
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");
}
}
}
126 changes: 124 additions & 2 deletions Codecs/Image/ImageFormat.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Linq;
using Media.Codec;
using Media.Codecs.Image;
using System.Linq;
using System.Text;

namespace Media.Codecs.Image
Expand Down Expand Up @@ -606,5 +608,125 @@ public bool IsPremultipliedWithAlpha
// }
// }
// }
//}
//}
}

namespace Media.UnitTests
{
internal class ImageFormatTests
{
public void TestWithoutAlphaComponent()
{
// Arrange
var originalFormat = ImageFormat.ARGB(8);

// Act
var result = ImageFormat.WithoutAlphaComponent(originalFormat);

// Assert
System.Diagnostics.Debug.Assert(originalFormat.HasAlphaComponent && !result.HasAlphaComponent);
}

public void TestWithProceedingAlphaComponent()
{
// Arrange
var originalFormat = ImageFormat.RGB(8);

// Act
var result = ImageFormat.WithProceedingAlphaComponent(originalFormat, 8);

// Assert
System.Diagnostics.Debug.Assert(result.HasAlphaComponent);
}

public void TestWithPreceedingAlphaComponent()
{
// Arrange
var originalFormat = ImageFormat.RGB(8);

// Act
var result = ImageFormat.WithPreceedingAlphaComponent(originalFormat, 8);

// Assert
System.Diagnostics.Debug.Assert(result.HasAlphaComponent);
}

public void TestBinaryFormat()
{
// Act
var result = ImageFormat.Binary(1);

// Assert
System.Diagnostics.Debug.Assert(1 == result.Widths.Length);
System.Diagnostics.Debug.Assert(1 == result.Heights.Length);
}

public void TestMonochromeFormat()
{
// Act
var result = ImageFormat.Monochrome(1);

// Assert
System.Diagnostics.Debug.Assert(1 == result.Widths.Length);
System.Diagnostics.Debug.Assert(1 == result.Heights.Length);
}

public void TestRGBFormat()
{
// Act
var result = ImageFormat.RGB(8);

// Assert
System.Diagnostics.Debug.Assert(3 == result.Widths.Length);
System.Diagnostics.Debug.Assert(3 == result.Heights.Length);
}

public void TestARGBFormat()
{
// Act
var result = ImageFormat.ARGB(8);

// Assert
System.Diagnostics.Debug.Assert(4 == result.Widths.Length);
System.Diagnostics.Debug.Assert(4 == result.Heights.Length);
System.Diagnostics.Debug.Assert(result.HasAlphaComponent);
}

public void TestWithSubSampling()
{
// Arrange
var originalFormat = ImageFormat.YUV(8);
int[] sampling = { 4, 2, 2 };

// Act
var result = ImageFormat.WithSubSampling(originalFormat, sampling);

// Assert
System.Diagnostics.Debug.Assert(result.IsSubSampled);
}

public void TestPackedFormat()
{
// Arrange
var originalFormat = ImageFormat.YUV(8);

// Act
var result = ImageFormat.Packed(originalFormat);

// Assert
System.Diagnostics.Debug.Assert(DataLayout.Packed == result.DataLayout);
}

public void TestPlanarFormat()
{
// Arrange
var originalFormat = ImageFormat.YUV(8);

// Act
var result = ImageFormat.Planar(originalFormat);

// Assert
System.Diagnostics.Debug.Assert(DataLayout.Planar == result.DataLayout);
}
}
}
2 changes: 1 addition & 1 deletion Codecs/MediaFormat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ public MediaFormat(MediaFormat other)
/// <summary>
/// Indicates if there are an even amount of bits in the format.
/// </summary>
public bool IsAligned { get { return Size % Common.Binary.BitsPerByte is 0; } }
public bool IsAligned => (Size & 7) == 0;

/// <summary>
/// Gets the component at the given index.
Expand Down
16 changes: 15 additions & 1 deletion Common/Classes/SegmentStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,18 @@ The above copyright notice and this permission notice shall be included in all c

#region Using Statements
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Metadata.Ecma335;
#endregion

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, IDisposed
public class SegmentStream : System.IO.Stream, IEnumerable<byte>, IDisposed
{
///// <summary>
///// Combines all given instances into a single instance.
Expand Down Expand Up @@ -1094,6 +1096,18 @@ 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
8 changes: 7 additions & 1 deletion UnitTests/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ public class Program
//Codec
TestCodec,
TestAudioBuffer,
TestImageFormat,
TestImageBuffer
//Todo, serperate the tests that should be called either by attribute and use reflection or by using options in the project file.
//E.g. Sources could be defined in appSettings and reused for the RtspServer or otherwise as desired or specified specifically
Expand Down Expand Up @@ -4560,10 +4561,15 @@ private static void TestAudioBuffer()
CreateInstanceAndInvokeAllMethodsWithReturnType(typeof(Media.UnitTests.AudioUnitTests), TypeOfVoid);
}

private static void TestImageFormat()
{
CreateInstanceAndInvokeAllMethodsWithReturnType(typeof(Media.UnitTests.ImageFormatTests), TypeOfVoid);
}

private static void TestImageBuffer()
{
CreateInstanceAndInvokeAllMethodsWithReturnType(typeof(Media.UnitTests.ImageUnitTests), TypeOfVoid);
}
}

#endregion

Expand Down

0 comments on commit cd9831b

Please sign in to comment.