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);
        }
Esempio n. 2
0
        /// <summary>
        /// Loads components from a stream.
        /// </summary>
        /// <param name="stream">A stream to load the components from.</param>
        /// <remarks>The components in <paramref name="stream" /> replace components currently in the package.</remarks>
        public void Load(Stream stream)
        {
            _components.Clear();
            _components.Add(this); // this component was already read by base's constructor

            using (stream)
            {
                while (stream.Position < stream.Length)
                {
                    OnProgressChanged(stream.Position, stream.Length);

                    LightFieldComponent component = new LightFieldComponent(stream);

                    _components.Add(component);
                }
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Gets the <see cref="FieldImage"/> for image data.
        /// </summary>
        /// <param name="frameIndex">The frame index.</param>
        /// <returns>the <see cref="FieldImage"/> for image data if available in the package; null otherwise.</returns>
        public FieldImage GetFieldImage(int frameIndex = 0)
        {
            LightFieldComponent imageComponent = GetImageComponent(frameIndex);

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

            Json.FrameReferences frame = _frames[frameIndex];
            if (frame.Metadata != null)
            {
                return(FieldImage.From(imageComponent, frame.Metadata, frame.PrivateMetadata));
            }

            return(null);
        }
        /// <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. 5
0
        private void LoadFrame(Json.FrameItem frameItem)
        {
            if (frameItem == null || frameItem.Frame == null)
            {
                return;
            }

            Json.FrameReferences frameReferences = frameItem.Frame;

            if (frameReferences.Metadata == null && frameReferences.MetadataRef != null)
            {
                LightFieldComponent frameMetadataComponent = Package.GetComponent(frameReferences.MetadataRef).FirstOrDefault();
                if (frameMetadataComponent != null)
                {
                    try
                    {
                        Json.FrameMetadata frameMetadata = new Json.FrameMetadata();
                        frameMetadata.LoadFromJson(frameMetadataComponent.GetDataAsString());
                        frameReferences.Metadata = frameMetadata;
                    }
                    catch (FormatException e) { OnException(e); }
                }
            }

            if (frameReferences.PrivateMetadata == null && frameReferences.PrivateMetadataRef != null)
            {
                LightFieldComponent privateMetadataComponent = Package.GetComponent(frameReferences.PrivateMetadataRef).FirstOrDefault();
                if (privateMetadataComponent != null)
                {
                    try
                    {
                        Json.FrameMetadata privateMetadata = new Json.FrameMetadata();
                        privateMetadata.LoadFromJson(privateMetadataComponent.GetDataAsString());
                        frameReferences.PrivateMetadata = privateMetadata;
                    }
                    catch (FormatException e) { OnException(e); }
                }
            }

            _frames.Add(frameReferences);
        }
Esempio n. 6
0
 /// <summary>
 /// Initializes a new instance of the <see cref="ComponentReader" /> class.
 /// </summary>
 /// <param name="component">The <see cref="LightFieldComponent"/> to read from.</param>
 public ComponentReader(LightFieldComponent component) : base(new MemoryStream(component.Data), component.Encoding)
 {
     _length = component.Length;
 }
        /// <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);
        }