private static void AddComponents(LightFieldPackage package, Json.FrameReferences references, byte[] imageData, Json.FrameMetadata frameMetadata, Json.FrameMetadata privateMetadata)
        {
            global::System.Diagnostics.Debug.Assert(package != null, "Package cannot be null.");
            global::System.Diagnostics.Debug.Assert(references != null, "References cannot be null.");
            global::System.Diagnostics.Debug.Assert(imageData != null, "Image data cannot be null.");
            global::System.Diagnostics.Debug.Assert(frameMetadata != null, "Frame metadata cannot be null.");
            global::System.Diagnostics.Debug.Assert(privateMetadata != null, "Private metadata cannot be null.");

            LightFieldComponent privateComponent = new LightFieldComponent();
            string privateJson = privateMetadata.ToStringJson();

            privateComponent.Data         = Encoding.UTF8.GetBytes(privateJson);
            privateComponent.Reference    = GenerateRef(privateComponent.Data);
            references.PrivateMetadataRef = privateComponent.Reference;

            LightFieldComponent frameComponent = new LightFieldComponent();
            string frameJson = frameMetadata.ToStringJson();

            frameComponent.Data      = Encoding.UTF8.GetBytes(frameJson);
            frameComponent.Reference = GenerateRef(frameComponent.Data);
            references.MetadataRef   = frameComponent.Reference;

            LightFieldComponent imageComponent = new LightFieldComponent();

            imageComponent.Data      = imageData;
            imageComponent.Reference = GenerateRef(imageComponent.Data);
            references.ImageRef      = imageComponent.Reference;

            package.Components.Add(imageComponent);
            package.Components.Add(frameComponent);
            package.Components.Add(privateComponent);
        }
        /// <summary>
        /// Loads all available flat field images from a calibration package.
        /// </summary>
        /// <param name="package">The package to load the flat field images from.</param>
        /// <param name="packageReference">Reference to the package (usually its file path).</param>
        /// <returns>a number of flat field images that were added to the current set.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="packageReference"/> is null</exception>
        public int LoadFrom(LightFieldPackage package, string packageReference)
        {
            Dictionary <string, FlatFieldItem> items = new Dictionary <string, FlatFieldItem>(StringComparer.OrdinalIgnoreCase);

            LoadFromPhase1(package, packageReference, items);
            return(LoadFromPhase2(items));
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="PackageAccessor"/> class.
        /// </summary>
        /// <param name="package">The package to be accessed.</param>
        public PackageAccessor(LightFieldPackage package)
        {
            if (package == null)
            {
                throw new ArgumentNullException("package");
            }

            _package = package;

            LightFieldComponent metadata = package.GetMetadata().FirstOrDefault();

            if (metadata != null)
            {
                _master = new Json.Master();
                _master.LoadFromJson(metadata.GetDataAsString());
            }

            _exceptions = new List <Exception>();
            _hasContent = Initialize();
        }
Esempio n. 4
0
 /// <summary>
 /// Initializes new instance of the <see cref="RawPackageAccessor"/> class.
 /// </summary>
 /// <param name="package">The package with raw images.</param>
 public RawPackageAccessor(LightFieldPackage package) : base(package)
 {
 }
 /// <summary>
 /// Initializes new instance of the <see cref="DepthPackageAccessor"/> class.
 /// </summary>
 /// <param name="package">The package with raw images.</param>
 public DepthPackageAccessor(LightFieldPackage package) : base(package)
 {
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="FilesPackageAccessor"/> class.
 /// </summary>
 /// <param name="package">The package containing files to access.</param>
 public FilesPackageAccessor(LightFieldPackage package) : base(package)
 {
 }
        /// <summary>
        /// Creates a new <see cref="LightFieldPackage"/> from raw camera files.
        /// </summary>
        /// <param name="rootMetadata">The picture metadata.</param>
        /// <param name="imageData">Frames raw data in order specified by the metadata.</param>
        /// <returns>A <see cref="LightFieldPackage"/> with components containing the specified files.</returns>
        /// <exception cref="ArgumentNullException"><paramref name="rootMetadata"/>, <paramref name="imageData"/> itself or any data in the array is null.</exception>
        /// <exception cref="ArgumentException">Metadata do not contain information required to create the package, or the <paramref name="imageData"/> contains less items than there is frames in the metadata.</exception>
        public static LightFieldPackage FromCameraFiles(Json.Root rootMetadata, IEnumerable <byte[]> imageData)
        {
            if (imageData == null)
            {
                throw new ArgumentNullException("imageData");
            }

            if (rootMetadata == null)
            {
                throw new ArgumentNullException("rootMetadata");
            }

            if (rootMetadata.Master == null || rootMetadata.Master.Picture == null || rootMetadata.Master.Picture.FrameArray == null)
            {
                throw new ArgumentException("Critical metadata missing.", "rootMetadata");
            }

            LightFieldPackage package = new LightFieldPackage();

            Json.Master metadata = rootMetadata.Master;

            IEnumerator <byte[]> rawDataEnumerator = imageData.GetEnumerator();

            for (int i = 0; i < metadata.Picture.FrameArray.Length; i++)
            {
                Json.FrameItem frameItem = metadata.Picture.FrameArray[i];
                if (frameItem == null || frameItem.Frame == null || frameItem.Frame.Metadata == null)
                {
                    throw new ArgumentException("Missing metadata for frame " + i + ".", "rootMetadata");
                }

                if (!rawDataEnumerator.MoveNext())
                {
                    throw new ArgumentException("Missing image data for frame " + i + ".", "imageData");
                }

                byte[] data = rawDataEnumerator.Current;
                if (data == null)
                {
                    throw new ArgumentNullException("Image data cannot be null.", "imageData");
                }

                Json.FrameMetadata frameMetadata = frameItem.Frame.Metadata;
                frameItem.Frame.Metadata = null;

                Json.FrameMetadata privateMetadata = frameItem.Frame.PrivateMetadata;
                frameItem.Frame.PrivateMetadata = null;

                AddComponents(package, frameItem.Frame, data, frameMetadata, privateMetadata);
            }

            LightFieldComponent metadataComponent = new LightFieldComponent();

            metadataComponent.ComponentType = 'M';
            metadataComponent.Data          = Encoding.UTF8.GetBytes(metadata.ToStringJson());
            metadataComponent.Reference     = GenerateRef(metadataComponent.Data);

            package.Components.Insert(1, metadataComponent);

            return(package);
        }
        private string LoadFromPhase1(LightFieldPackage package, string packageReference, Dictionary <string, FlatFieldItem> items)
        {
            LightFieldComponent metadataComponent = package.GetMetadata().FirstOrDefault();

            if (metadataComponent == null)
            {
                return(null);
            }

            Json.Master master = new Json.Master();
            master.LoadFromJson(metadataComponent.GetDataAsString());

            if (master.Files != null)
            {
                foreach (Json.File file in master.Files)
                {
                    if (file.Name != null && file.Name.StartsWith(@"C:\T1CALIB\MOD_", StringComparison.OrdinalIgnoreCase))
                    {
                        if (file.Name.EndsWith(".TXT", StringComparison.OrdinalIgnoreCase))
                        {
                            LightFieldComponent imageMetadataComponent = package.GetComponent(file.DataRef).FirstOrDefault();
                            if (imageMetadataComponent != null)
                            {
                                Json.Root imageRoot = new Json.Root();
                                imageRoot.LoadFromJson(imageMetadataComponent.GetDataAsString());

                                if (imageRoot.Master != null && imageRoot.Master.Picture != null && imageRoot.Master.Picture.FrameArray != null && imageRoot.Master.Picture.FrameArray.Length > 0)
                                {
                                    Json.FrameItem frameItem = imageRoot.Master.Picture.FrameArray[0];
                                    if (frameItem != null && frameItem.Frame != null && frameItem.Frame.Metadata != null && frameItem.Frame.Metadata.Devices != null && frameItem.Frame.Metadata.Devices.Lens != null)
                                    {
                                        string imageFileName = file.Name.Substring(0, file.Name.Length - 3) + "RAW";

                                        FlatFieldItem item;
                                        if (!items.TryGetValue(imageFileName, out item))
                                        {
                                            items[imageFileName] = item = new FlatFieldItem();
                                        }

                                        item.FrameImage = frameItem.Frame.Metadata.Image;
                                        item.ZoomStep   = (int)frameItem.Frame.Metadata.Devices.Lens.ZoomStep;
                                        item.FocusStep  = (int)frameItem.Frame.Metadata.Devices.Lens.FocusStep;

                                        if (_serialNumber == null && frameItem.Frame.PrivateMetadata != null && frameItem.Frame.PrivateMetadata.Camera != null)
                                        {
                                            _serialNumber = frameItem.Frame.PrivateMetadata.Camera.SerialNumber;
                                        }
                                    }
                                }
                            }
                        }
                        else if (file.Name.EndsWith(".RAW", StringComparison.OrdinalIgnoreCase))
                        {
                            FlatFieldItem item;
                            if (!items.TryGetValue(file.Name, out item))
                            {
                                items[file.Name] = item = new FlatFieldItem();
                            }

                            item.DataReference    = file.DataRef;
                            item.PackageReference = packageReference;
                        }
                    }
                }
            }

            RawPackageAccessor raw = package.AccessRaw();

            if (raw.HasContent)
            {
                foreach (Json.FrameReferences frame in raw.GetFrames())
                {
                    if (frame.Metadata != null && frame.ImageRef != null)
                    {
                        FlatFieldItem item = new FlatFieldItem();
                        item.PackageReference = packageReference;
                        item.DataReference    = frame.ImageRef;
                        item.FrameImage       = frame.Metadata.Image;

                        if (frame.Metadata.Devices != null && frame.Metadata.Devices.Lens != null)
                        {
                            item.ZoomStep  = (int)frame.Metadata.Devices.Lens.ZoomStep;
                            item.FocusStep = (int)frame.Metadata.Devices.Lens.FocusStep;

                            items[frame.ImageRef] = item;
                        }
                    }
                }
            }

            return(master.NextFile);
        }