|
2 | 2 | using System.Collections.Generic;
|
3 | 3 | using System.IO;
|
4 | 4 | using System.Linq;
|
| 5 | +using System.Reflection; |
5 | 6 | using System.Text;
|
6 | 7 | using System.Threading;
|
7 | 8 |
|
@@ -522,105 +523,55 @@ protected static PixFileFormat GetFormatOfExtension(string filename)
|
522 | 523 | throw new ArgumentException("File name has unknown extension: " + ext);
|
523 | 524 | }
|
524 | 525 |
|
525 |
| - public static string GetPreferredExtensionOfFormat(PixFileFormat format) |
| 526 | + // Helper class to create PixImage from given Type |
| 527 | + private static class Dispatch |
526 | 528 | {
|
527 |
| - return s_preferredExtensionOfFormat.Value[format]; |
528 |
| - } |
| 529 | + private static class CreateDispatcher |
| 530 | + { |
| 531 | + public static PixImage<T> Create<T>(Col.Format format, long x, long y, long channels) |
| 532 | + => new PixImage<T>(format, x, y, channels); |
529 | 533 |
|
530 |
| - private static Dictionary<PixFormat, Func<long, long, long, PixImage>> s_pixImageCreatorMap = |
531 |
| - new Dictionary<PixFormat, Func<long, long, long, PixImage>>() |
| 534 | + public static PixImage<T> CreateArray<T>(T[] data, Col.Format format, long x, long y, long channels) |
| 535 | + => new PixImage<T>(format, data.CreateImageVolume(new V3l(x, y, channels))); |
| 536 | + } |
| 537 | + |
| 538 | + private const BindingFlags flags = BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public; |
| 539 | + private static readonly MethodInfo s_createMethod = typeof(CreateDispatcher).GetMethod("Create", flags); |
| 540 | + private static readonly MethodInfo s_createArrayMethod = typeof(CreateDispatcher).GetMethod("CreateArray", flags); |
| 541 | + |
| 542 | + public static PixImage Create(PixFormat format, long x, long y, long channels) |
532 | 543 | {
|
533 |
| - { PixFormat.ByteBW, (x, y, c) => new PixImage<byte>(Col.Format.BW, x, y, c) }, |
534 |
| - { PixFormat.ByteGray, (x, y, c) => new PixImage<byte>(Col.Format.Gray, x, y, c) }, |
535 |
| - { PixFormat.ByteBGR, (x, y, c) => new PixImage<byte>(Col.Format.BGR, x, y, c) }, |
536 |
| - { PixFormat.ByteBGRA, (x, y, c) => new PixImage<byte>(Col.Format.BGRA, x, y, c) }, |
537 |
| - { PixFormat.ByteRGBA, (x, y, c) => new PixImage<byte>(Col.Format.RGBA, x, y, c) }, |
538 |
| - { PixFormat.ByteRGB, (x, y, c) => new PixImage<byte>(Col.Format.RGB, x, y, c) }, |
539 |
| - { PixFormat.ByteBGRP, (x, y, c) => new PixImage<byte>(Col.Format.BGRP, x, y, c) }, |
540 |
| - |
541 |
| - { PixFormat.SByteBW, (x, y, c) => new PixImage<sbyte>(Col.Format.BW, x, y, c) }, |
542 |
| - { PixFormat.SByteGray, (x, y, c) => new PixImage<sbyte>(Col.Format.Gray, x, y, c) }, |
543 |
| - { PixFormat.SByteBGR, (x, y, c) => new PixImage<sbyte>(Col.Format.BGR, x, y, c) }, |
544 |
| - { PixFormat.SByteBGRA, (x, y, c) => new PixImage<sbyte>(Col.Format.BGRA, x, y, c) }, |
545 |
| - { PixFormat.SByteRGBA, (x, y, c) => new PixImage<sbyte>(Col.Format.RGBA, x, y, c) }, |
546 |
| - { PixFormat.SByteRGB, (x, y, c) => new PixImage<sbyte>(Col.Format.RGB, x, y, c) }, |
547 |
| - { PixFormat.SByteBGRP, (x, y, c) => new PixImage<sbyte>(Col.Format.BGRP, x, y, c) }, |
548 |
| - |
549 |
| - { PixFormat.UShortGray, (x, y, c) => new PixImage<ushort>(Col.Format.Gray, x, y, c) }, |
550 |
| - { PixFormat.UShortRGB, (x, y, c) => new PixImage<ushort>(Col.Format.RGB, x, y, c) }, |
551 |
| - { PixFormat.UShortRGBA, (x, y, c) => new PixImage<ushort>(Col.Format.RGBA, x, y, c) }, |
552 |
| - { PixFormat.UShortRGBP, (x, y, c) => new PixImage<ushort>(Col.Format.RGBP, x, y, c) }, |
553 |
| - |
554 |
| - { PixFormat.ShortGray, (x, y, c) => new PixImage<short>(Col.Format.Gray, x, y, c) }, |
555 |
| - { PixFormat.ShortRGB, (x, y, c) => new PixImage<short>(Col.Format.RGB, x, y, c) }, |
556 |
| - { PixFormat.ShortRGBA, (x, y, c) => new PixImage<short>(Col.Format.RGBA, x, y, c) }, |
557 |
| - { PixFormat.ShortRGBP, (x, y, c) => new PixImage<short>(Col.Format.RGBP, x, y, c) }, |
558 |
| - |
559 |
| - { PixFormat.UIntGray, (x, y, c) => new PixImage<uint>(Col.Format.Gray, x, y, c) }, |
560 |
| - { PixFormat.UIntRGB, (x, y, c) => new PixImage<uint>(Col.Format.RGB, x, y, c) }, |
561 |
| - { PixFormat.UIntRGBA, (x, y, c) => new PixImage<uint>(Col.Format.RGBA, x, y, c) }, |
562 |
| - { PixFormat.UIntRGBP, (x, y, c) => new PixImage<uint>(Col.Format.RGBP, x, y, c) }, |
563 |
| - |
564 |
| - { PixFormat.IntGray, (x, y, c) => new PixImage<int>(Col.Format.Gray, x, y, c) }, |
565 |
| - { PixFormat.IntRGB, (x, y, c) => new PixImage<int>(Col.Format.RGB, x, y, c) }, |
566 |
| - { PixFormat.IntRGBA, (x, y, c) => new PixImage<int>(Col.Format.RGBA, x, y, c) }, |
567 |
| - { PixFormat.IntRGBP, (x, y, c) => new PixImage<int>(Col.Format.RGBP, x, y, c) }, |
568 |
| - |
569 |
| - { PixFormat.HalfGray, (x, y, c) => new PixImage<Half>(Col.Format.Gray, x, y, c) }, |
570 |
| - { PixFormat.HalfRGB, (x, y, c) => new PixImage<Half>(Col.Format.RGB, x, y, c) }, |
571 |
| - { PixFormat.HalfRGBA, (x, y, c) => new PixImage<Half>(Col.Format.RGBA, x, y, c) }, |
572 |
| - { PixFormat.HalfRGBP, (x, y, c) => new PixImage<Half>(Col.Format.RGBP, x, y, c) }, |
573 |
| - |
574 |
| - { PixFormat.FloatGray, (x, y, c) => new PixImage<float>(Col.Format.Gray, x, y, c) }, |
575 |
| - { PixFormat.FloatRGB, (x, y, c) => new PixImage<float>(Col.Format.RGB, x, y, c) }, |
576 |
| - { PixFormat.FloatRGBA, (x, y, c) => new PixImage<float>(Col.Format.RGBA, x, y, c) }, |
577 |
| - { PixFormat.FloatRGBP, (x, y, c) => new PixImage<float>(Col.Format.RGBP, x, y, c) }, |
578 |
| - |
579 |
| - { PixFormat.DoubleGray, (x, y, c) => new PixImage<double>(Col.Format.Gray, x, y, c) }, |
580 |
| - { PixFormat.DoubleRGB, (x, y, c) => new PixImage<double>(Col.Format.RGB, x, y, c) }, |
581 |
| - { PixFormat.DoubleRGBA, (x, y, c) => new PixImage<double>(Col.Format.RGBA, x, y, c) }, |
582 |
| - { PixFormat.DoubleRGBP, (x, y, c) => new PixImage<double>(Col.Format.RGBP, x, y, c) }, |
583 |
| - }; |
| 544 | + var mi = s_createMethod.MakeGenericMethod(format.Type); |
| 545 | + return (PixImage)mi.Invoke(null, new object[] { format.Format, x, y, channels }); |
| 546 | + } |
584 | 547 |
|
585 |
| - private static Dictionary<Type, Func<Array, Col.Format, long, long, long, PixImage>> s_pixImageArrayCreatorMap = |
586 |
| - new Dictionary<Type, Func<Array, Col.Format, long, long, long, PixImage>>() |
| 548 | + public static PixImage Create(Array array, Col.Format format, long x, long y, long channels) |
587 | 549 | {
|
588 |
| - { typeof(byte[]), (a, f, x, y, c) => |
589 |
| - new PixImage<byte>(f, ((byte[])a).CreateImageVolume(new V3l(x, y, c))) }, |
590 |
| - { typeof(sbyte[]), (a, f, x, y, c) => |
591 |
| - new PixImage<sbyte>(f, ((sbyte[])a).CreateImageVolume(new V3l(x, y, c))) }, |
592 |
| - { typeof(ushort[]), (a, f, x, y, c) => |
593 |
| - new PixImage<ushort>(f, ((ushort[])a).CreateImageVolume(new V3l(x, y, c))) }, |
594 |
| - { typeof(short[]), (a, f, x, y, c) => |
595 |
| - new PixImage<short>(f, ((short[])a).CreateImageVolume(new V3l(x, y, c))) }, |
596 |
| - { typeof(uint[]), (a, f, x, y, c) => |
597 |
| - new PixImage<uint>(f, ((uint[])a).CreateImageVolume(new V3l(x, y, c))) }, |
598 |
| - { typeof(int[]), (a, f, x, y, c) => |
599 |
| - new PixImage<int>(f, ((int[])a).CreateImageVolume(new V3l(x, y, c))) }, |
600 |
| - { typeof(Half[]), (a, f, x, y, c) => |
601 |
| - new PixImage<Half>(f, ((Half[])a).CreateImageVolume(new V3l(x, y, c))) }, |
602 |
| - { typeof(float[]), (a, f, x, y, c) => |
603 |
| - new PixImage<float>(f, ((float[])a).CreateImageVolume(new V3l(x, y, c))) }, |
604 |
| - { typeof(double[]), (a, f, x, y, c) => |
605 |
| - new PixImage<double>(f, ((double[])a).CreateImageVolume(new V3l(x, y, c))) }, |
606 |
| - }; |
| 550 | + var mi = s_createArrayMethod.MakeGenericMethod(array.GetType().GetElementType()); |
| 551 | + return (PixImage)mi.Invoke(null, new object[] { array, format, x, y, channels }); |
| 552 | + } |
| 553 | + } |
607 | 554 |
|
| 555 | + public static string GetPreferredExtensionOfFormat(PixFileFormat format) |
| 556 | + { |
| 557 | + return s_preferredExtensionOfFormat.Value[format]; |
| 558 | + } |
608 | 559 |
|
609 | 560 | #endregion
|
610 | 561 |
|
611 | 562 | #region Static Creator Functions
|
612 | 563 |
|
613 |
| - public static PixImage Create(PixFormat pixFormat, long sx, long sy, long ch) |
614 |
| - => s_pixImageCreatorMap[pixFormat](sx, sy, ch); |
| 564 | + public static PixImage Create(PixFormat format, long sx, long sy, long ch) |
| 565 | + => Dispatch.Create(format, sx, sy, ch); |
615 | 566 |
|
616 |
| - public static PixImage Create(PixFormat pixFormat, long sx, long sy) |
617 |
| - => s_pixImageCreatorMap[pixFormat](sx, sy, pixFormat.Format.ChannelCount()); |
| 567 | + public static PixImage Create(PixFormat format, long sx, long sy) |
| 568 | + => Dispatch.Create(format, sx, sy, format.ChannelCount); |
618 | 569 |
|
619 | 570 | public static PixImage Create(Array array, Col.Format format, long sx, long sy, long ch)
|
620 |
| - => s_pixImageArrayCreatorMap[array.GetType()](array, format, sx, sy, ch); |
| 571 | + => Dispatch.Create(array, format, sx, sy, ch); |
621 | 572 |
|
622 | 573 | public static PixImage Create(Array array, Col.Format format, long sx, long sy)
|
623 |
| - => s_pixImageArrayCreatorMap[array.GetType()](array, format, sx, sy, format.ChannelCount()); |
| 574 | + => Dispatch.Create(array, format, sx, sy, format.ChannelCount()); |
624 | 575 |
|
625 | 576 | public static Volume<T> CreateVolume<T>(V3i size) => size.ToV3l().CreateImageVolume<T>();
|
626 | 577 |
|
|
0 commit comments