/// <summary>
 /// Initializes a new instance of the <see cref="FileCollection"/> class with an existing <see cref="Json.Master"/> storage.
 /// </summary>
 /// <param name="Master">A <see cref="Json.Master"/> to use as a storage for the collection.</param>
 public FileCollection(Json.Master Master)
 {
     _master = Master;
     if (_master.Files == null)
     {
         _master.Files = new Json.File[0];
     }
 }
Esempio n. 2
0
        /// <summary>
        /// Loads a <see cref="FieldImage"/> from a <see cref="LightFieldPackage"/>.
        /// </summary>
        /// <param name="package">The package to load the field image from.</param>
        /// <returns>a new instance of the <see cref="FieldImage"/> class.</returns>
        public static FieldImage From(LightFieldPackage package)
        {
            if (package == null)
            {
                throw new ArgumentNullException("package");
            }

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

            if (metadataComponent == null)
            {
                throw new ArgumentException("The package does not contain any metadata.", "package");
            }

            Json.Master master = new Json.Master();
            try { master.LoadFromJson(metadataComponent.GetDataAsString()); }
            catch (FormatException e) { throw new ArgumentException("The package contains invalid metadata.", e); }
            if (master.Picture == null)
            {
                throw new ArgumentException("The package does not contain required metadata.", "package");
            }

            PictureMetadata pictureMetadata = new PictureMetadata(master.Picture);

            LightFieldComponent frameMetadataComponent = package.GetComponent(pictureMetadata.Frame.MetadataReference).FirstOrDefault();

            if (frameMetadataComponent == null)
            {
                throw new ArgumentException("The package does not contain any frame metadata.", "package");
            }

            Json.FrameMetadata frameMetadata = new Json.FrameMetadata();
            try { frameMetadata.LoadFromJson(frameMetadataComponent.GetDataAsString()); }
            catch (FormatException e) { throw new ArgumentException("The package contains invalid metadata.", e); }


            Json.FrameMetadata  privateMetadata          = new Json.FrameMetadata();
            LightFieldComponent privateMetadataComponent = package.GetComponent(pictureMetadata.Frame.PrivateMetadataReference).FirstOrDefault();

            if (privateMetadataComponent != null)
            {
                try { privateMetadata.LoadFromJson(frameMetadataComponent.GetDataAsString()); }
                catch (FormatException) { }
            }

            LightFieldComponent frameComponent = package.GetComponent(pictureMetadata.Frame.ImageReference).FirstOrDefault();

            if (frameComponent == null)
            {
                throw new ArgumentException("The package does not contain the frame data.", "package");
            }

            return(new FieldImage(frameComponent, frameMetadata, privateMetadata));
        }
        /// <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();
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="FilesMetadata"/> class with an existing <see cref="Json.Master"/> storage.
 /// </summary>
 /// <param name="Master">A <see cref="Json.Master"/> to use as storage of the collection.</param>
 public FilesMetadata(Json.Master Master)
 {
     JsonMaster = Master;
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="FilesMetadata"/> class.
 /// </summary>
 public FilesMetadata()
 {
     JsonMaster = new Json.Master();
 }
        /// <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);
        }