From cd9831b58af354272b2eb9b2030eff3e883051f7 Mon Sep 17 00:00:00 2001 From: Julius Friedman Date: Fri, 18 Oct 2024 21:53:17 -0400 Subject: [PATCH] Test and additional logic. --- Codecs/Image/Image.cs | 48 ++++++++++++ Codecs/Image/ImageFormat.cs | 126 +++++++++++++++++++++++++++++++- Codecs/MediaFormat.cs | 2 +- Common/Classes/SegmentStream.cs | 16 +++- UnitTests/Program.cs | 8 +- 5 files changed, 195 insertions(+), 5 deletions(-) diff --git a/Codecs/Image/Image.cs b/Codecs/Image/Image.cs index 420829a5..8698c997 100644 --- a/Codecs/Image/Image.cs +++ b/Codecs/Image/Image.cs @@ -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"); + } } } \ No newline at end of file diff --git a/Codecs/Image/ImageFormat.cs b/Codecs/Image/ImageFormat.cs index d0cb4871..efb006d8 100644 --- a/Codecs/Image/ImageFormat.cs +++ b/Codecs/Image/ImageFormat.cs @@ -1,4 +1,6 @@ -using System.Linq; +using Media.Codec; +using Media.Codecs.Image; +using System.Linq; using System.Text; namespace Media.Codecs.Image @@ -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); + } + } } diff --git a/Codecs/MediaFormat.cs b/Codecs/MediaFormat.cs index d012d8e0..3d2bd8b3 100644 --- a/Codecs/MediaFormat.cs +++ b/Codecs/MediaFormat.cs @@ -246,7 +246,7 @@ public MediaFormat(MediaFormat other) /// /// Indicates if there are an even amount of bits in the format. /// - public bool IsAligned { get { return Size % Common.Binary.BitsPerByte is 0; } } + public bool IsAligned => (Size & 7) == 0; /// /// Gets the component at the given index. diff --git a/Common/Classes/SegmentStream.cs b/Common/Classes/SegmentStream.cs index 6824126e..4bd45e82 100644 --- a/Common/Classes/SegmentStream.cs +++ b/Common/Classes/SegmentStream.cs @@ -38,8 +38,10 @@ 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 @@ -47,7 +49,7 @@ namespace Media.Common /// /// Used to crete a continious stream to locations of memory which may not be next to each other and could even overlap. /// - public class SegmentStream : System.IO.Stream, IDisposed + public class SegmentStream : System.IO.Stream, IEnumerable, IDisposed { ///// ///// Combines all given instances into a single instance. @@ -1094,6 +1096,18 @@ bool IDisposed.ShouldDispose void IDisposable.Dispose() { Close(); } + public IEnumerator GetEnumerator() + { + int read = 0; + while ((read = ReadByte()) != -1) + yield return (byte)read; + } + + IEnumerator IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } + //~SegmentStream() { Close(); } #endregion diff --git a/UnitTests/Program.cs b/UnitTests/Program.cs index 04391d14..1fdb1829 100644 --- a/UnitTests/Program.cs +++ b/UnitTests/Program.cs @@ -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 @@ -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