public Block ConvertToBlock(IImage <T> input, IEnumerable <IFitsValue> extraKeys, bool isXtension = false) { if (input is null) { throw new ArgumentNullException(nameof(input), SR.NullArgument); } AllowedTypes.ValidateDataType <T>(); var extraKeysInst = (extraKeys ?? Enumerable.Empty <IFitsValue>()).ToList(); // Throws if mandatory keys are present Extensions.CheckExtraFitsKeys(extraKeysInst); var bitPix = FitsCs.Extensions.ConvertTypeToBitPix <T>() ?? throw new InvalidOperationException(string.Format(SR.TypeNotSupported, typeof(T))); var desc = new Descriptor( bitPix, new[] { input.Width, input.Height }, isXtension ? ExtensionType.Image : ExtensionType.Primary); var keys = new List <IFitsValue>(extraKeysInst.Count + 10); keys.AddRange(desc.GenerateFitsHeader()); keys.AddRange(extraKeysInst); keys.Add(FitsKey.CreateEnd()); var block = Block <T> .CreateWithData(desc, keys, x => { input.GetView().CopyTo(x); }); return(block); }
public void Test_CommentKeys_Text_Roundtrips() { var keys = FitsKey.ToComments(LoremIpsum()); Assert.That(keys.Length, Is.EqualTo(7)); var roundTrip = FitsKey.ParseCommentString(keys); Assert.That(roundTrip.AsSpan().SequenceEqual(roundTrip.AsSpan())); }
public void Test_Continue_TextComment_Roundtrips() { var keys = FitsKey.ToContinueKeys(LoremIpsum(), LoremIpsum(), @"LOREM"); Assert.That(keys.Length, Is.EqualTo(14)); Assert.That(keys[0] is IFitsValue <string> key && key.Name == @"LOREM"); var roundTrip = FitsKey.ParseContinuedString(keys, true); Assert.That(roundTrip.Text.AsSpan().SequenceEqual(LoremIpsum())); Assert.That(roundTrip.Comment.AsSpan().SequenceEqual(LoremIpsum())); }
public void Test_Continue_Comment_Roundtrips() { var keys = FitsKey.ToContinueKeys(ReadOnlySpan <char> .Empty, LoremIpsum(), @"LOREM"); Assert.That(keys.Length, Is.EqualTo(7)); Assert.That(keys[0] is IFitsValue <string> key && key.Name == @"LOREM"); var roundTrip = FitsKey.ParseContinuedString(keys, true); Assert.That(string.IsNullOrEmpty(roundTrip.Text)); Assert.That(roundTrip.Comment.AsSpan().SequenceEqual(LoremIpsum())); }
public void Test_CommentSpaces() { var bytes = new byte[80]; var bSpan = bytes.AsSpan(); bSpan.Fill((byte)' '); Encoding.ASCII.GetBytes("TEST = 1234/Comment").CopyTo(bSpan); var key = FitsKey.ParseRawData(bSpan); Assert.That(key is IFitsValue <int>); Assert.That((int)key.Value, Is.EqualTo(1234)); Assert.That(key.Comment, Is.EqualTo(@" Comment")); }
public void Test_ComplexKey() { var value = new Complex(-100500e-10, 100500e10); var key = FitsKey.Create("TEST", value, type: KeyType.Free); var str = key.ToString(); var buff = Encoding.ASCII.GetBytes(str); var compKey1 = FitsKey.ParseRawData(buff); key = FitsKey.Create("TEST", value); str = key.ToString(); buff = Encoding.ASCII.GetBytes(str); var compKey2 = FitsKey.ParseRawData(buff); Assert.That(compKey1.Equals(compKey2)); }
public void TestThroughBlocks <T>(object?__ = null) where T : unmanaged, IComparable <T>, IEquatable <T> { const int n = 248; const int m = 496; var data = ArrayPool <T> .Shared.Rent(n *m); var converter = ConverterProvider.GetImageConverter <T>(); try { for (var i = 0; i < n; i++) { for (var j = 0; j < m; j++) { data[m * i + j] = Internal.UnsafeNumerics.MathOps.DangerousCast <double, T>(Math.Sqrt( (i - 50) * (i - 50) * (1 + Math.Pow(Math.Sin(Math.PI * (i + 23) * 10 / n), 2)) + (j / 2.0 - 100) * (j / 2.0 - 100) * (1.1 + Math.Sin(Math.PI * j * 10 / m)))); } } data[247 * 496 + 495] = default; var src = Image.Create <T>(data.AsSpan(..(n * m)), n, m); var key = FitsKey.Create("DEBUG", "Debug key", "With a comment"); var block = converter.ConvertToBlock(src, key); var rec = converter.ConvertFromBlock(block, out var keys); Assert.AreEqual(src, rec); Assert.AreEqual(data[247 * 496 + 495], src[247, 495]); Assert.AreEqual(data[247 * 496 + 495], rec[247, 495]); Assert.That(keys, Has.One.Items); Assert.That(keys, Contains.Item(key)); } finally { ArrayPool <T> .Shared.Return(data); } }