Пример #1
0
        /// <summary>
        /// Loads a <see cref="FieldImage"/> from a raw and metadata components data.
        /// </summary>
        /// <param name="framePath">Path to the raw frame data.</param>
        /// <param name="metadataPath">Path to the metadata.</param>
        /// <returns>a new instance of the <see cref="FieldImage"/> class.</returns>
        /// <remarks>This overload allows to load an image from components exported from a backup package.</remarks>
        public static FieldImage From(string framePath, string metadataPath)
        {
            if (framePath == null)
            {
                throw new ArgumentNullException("framePath");
            }

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

            byte[] frameData = File.ReadAllBytes(framePath);

            Json.Root root = new Json.Root();
            try { root.LoadFromJson(File.ReadAllText(metadataPath)); }
            catch (FormatException e) { throw new ArgumentException("Invalid metadata.", e); }

            if (root.Master == null || root.Master.Picture == null || root.Master.Picture.FrameArray == null ||
                root.Master.Picture.FrameArray.Length < 1 || root.Master.Picture.FrameArray[0].Frame == null)
            {
                throw new ArgumentException("Critical metadata missing.");
            }

            Json.FrameMetadata frameMetadata = root.Master.Picture.FrameArray[0].Frame.Metadata;

            return(From(frameData, frameMetadata));
        }
        /// <summary>
        /// Creates a new <see cref="LightFieldPackage"/> from raw camera files.
        /// </summary>
        /// <param name="rootMetadataPath">The path to the file with picture metadata.</param>
        /// <param name="imageDataPaths">Paths to the files with 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="rootMetadataPath"/>, <paramref name="imageDataPaths"/> itself or any path in the array is null.</exception>
        /// <exception cref="ArgumentException">Metadata do not contain information required to create the package, or the <paramref name="imageDataPaths"/> contains less paths than there is frames in the metadata.</exception>
        public static LightFieldPackage FromCameraFiles(string rootMetadataPath, params string[] imageDataPaths)
        {
            if (rootMetadataPath == null)
            {
                throw new ArgumentNullException("rootMetadataPath");
            }

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

            string rootJson = File.ReadAllText(rootMetadataPath);

            Json.Root rootMetadata = new Json.Root();
            rootMetadata.LoadFromJson(rootJson);

            return(FromCameraFiles(rootMetadata, from rawDataPath in imageDataPaths
                                   select File.ReadAllBytes(rawDataPath)));
        }
Пример #3
0
        public void download(int count, string[] words, string delete, string[] calib3)
        {
            string pwd = Environment.CurrentDirectory;

            Console.WriteLine("Downloading...");
            PictureList picturelist = Program.lytronetclient.DownloadPictureList();
            int         c           = picturelist.Count;
            string      sn          = info.SerialNumber;

            string calib = calib3[0];

            // if creating new calibration data set, delete old sets if they exist
            if (calib.Equals("Y") || calib.Equals("y"))
            {
                string path = String.Format(@"{0}/LFToolbox0.4/LFToolbox0.3_Samples1/Cameras/{1}/Calibration_dataset/01/", pwd);
                System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(path);

                foreach (System.IO.FileInfo file in di.GetFiles())
                {
                    file.Delete();
                }
                foreach (System.IO.DirectoryInfo dir in di.GetDirectories())
                {
                    dir.Delete(true);
                }
            }

            int a = 0;

            foreach (PictureListEntry entry in picturelist) // download files
            {
                string str  = picturelist[a].ToString();
                string path = picturelist[a].Path.ToString();

                string[] detail = str.Split();
                string   ids    = detail[0];
                string   addres = detail[1];
                addres = addres.Replace("[", "");
                addres = addres.Replace("]", "");
                string[] splits = addres.Split('\\');

                string nameL     = String.Format(@"IMG_{0}.LFR", a);
                string datanameL = String.Format(@"{0}/LFToolbox0.4/LFToolbox0.3_Samples1/Images/Lytro/{1}", pwd, nameL);
                string nameR     = String.Format(@"IMG_{0}.RAW", a);
                string datanameR = String.Format(@"{0}/LFToolbox0.4/LFToolbox0.3_Samples1/Images/Lytro/{1}", pwd, nameR);
                string nameJ     = String.Format(@"IMG_{0}.JPG", a);
                string datanameJ = String.Format(@"{0}/LFToolbox0.4/LFToolbox0.3_Samples1/Images/Lytro/{1}", pwd, nameJ);
                string nameT     = String.Format(@"IMG_{0}.TXT", a);
                string datanameT = String.Format(@"{0}/LFToolbox0.4/LFToolbox0.3_Samples1/Images/Lytro/{1}", pwd, nameT);

                if (!System.IO.File.Exists(datanameL) && !System.IO.File.Exists(datanameR) &&
                    !System.IO.File.Exists(datanameT) && !System.IO.File.Exists(datanameJ))
                {
                    foreach (string word in words)
                    {
                        if (word.Equals("j"))
                        {
                            string[] details = str.Split();
                            string   id      = details[0];
                            string   address = details[1];
                            address = address.Replace("[", "");
                            address = address.Replace("]", "");
                            string[] split = address.Split('\\');
                            string   name  = split[3];
                            byte[]   data  = Program.lytronetclient.DownloadPicture(id, LoadPictureFormat.Jpeg);
                            if (calib.Equals("Y") || calib.Equals("y"))
                            {
                                string dataname = String.Format(@"{0}/LFToolbox0.4/LFToolbox0.3_Samples1/Cameras/{1}/Calibration_dataset/01/{2}.jpg", pwd, sn, name);
                                System.IO.File.WriteAllBytes(dataname, data);
                            }
                            else
                            {
                                string dataname = String.Format(@"{0}/LFToolbox0.4/LFToolbox0.3_Samples1/Images/Lytro/{1}.jpg", pwd, name);
                                System.IO.File.WriteAllBytes(dataname, data);
                            }
                        }
                        else if (word.Equals("r"))
                        {
                            string[] details = str.Split();
                            string   id      = details[0];
                            string   address = details[1];
                            address = address.Replace("[", "");
                            address = address.Replace("]", "");
                            string[] split = address.Split('\\');
                            string   name  = split[3];
                            name = name.Replace(".RAW", "");
                            byte[] data = Program.lytronetclient.DownloadPicture(id, LoadPictureFormat.Raw);
                            if (calib.Equals("Y") || calib.Equals("y"))
                            {
                                string dataname = String.Format(@"{0}/LFToolbox0.4/LFToolbox0.3_Samples1/Cameras/{1}/Calibration_dataset/01/{2}.raw", pwd, sn, name);
                                System.IO.File.WriteAllBytes(dataname, data);
                            }
                            else
                            {
                                string dataname = String.Format(@"{0}/LFToolbox0.4/LFToolbox0.3_Samples1/Images/Lytro/{1}.raw", pwd, name);
                                System.IO.File.WriteAllBytes(dataname, data);
                            }
                        }
                        else if (word.Equals("m"))
                        {
                            string[] details = str.Split();
                            string   id      = details[0];
                            string   address = details[1];
                            address = address.Replace("[", "");
                            address = address.Replace("]", "");
                            string[] split = address.Split('\\');
                            string   name  = split[3];
                            name = name.Replace(".RAW", "");
                            byte[] data = Program.lytronetclient.DownloadPicture(id, LoadPictureFormat.Metadata);
                            if (calib.Equals("Y") || calib.Equals("y"))
                            {
                                string dataname = String.Format(@"{0}/LFToolbox0.4/LFToolbox0.3_Samples1/Cameras/{1}/Calibration_dataset/01/{2}.txt", pwd, sn, name);
                                System.IO.File.WriteAllBytes(dataname, data);
                            }
                            else
                            {
                                string dataname = String.Format(@"{0}/LFToolbox0.4/LFToolbox0.3_Samples1/Images/Lytro/{1}.txt", pwd, name);
                                System.IO.File.WriteAllBytes(dataname, data);
                            }
                        }
                        else if (word.Equals("s"))
                        {
                            string[] details = str.Split();
                            string   id      = details[0];
                            string   address = details[1];
                            address = address.Replace("[", "");
                            address = address.Replace("]", "");
                            string[] split = address.Split('\\');
                            string   name  = split[3];
                            name = name.Replace(".RAW", "");
                            name = String.Format("{0}/LFToolbox0.4/LFToolbox0.3_Samples1/Images/Lytro/{1}.raw", pwd, name);
                            int    offset = 0x1C;
                            byte[] data   = Program.lytronetclient.DownloadPicture(id, LoadPictureFormat.Stack);
                            string ext    = System.IO.Path.GetExtension(path);
                            int    index  = 1;

                            while (offset + 4 < data.Length)
                            {
                                int length = BitConverter.ToInt32(data, offset); offset += 4;
                                if (offset + length > data.Length)
                                {
                                    break;
                                }

                                string pathstack = System.IO.Path.ChangeExtension(name, index + ext);
                                using (System.IO.FileStream file = System.IO.File.Create(pathstack))
                                    file.Write(data, offset, length);

                                offset += length;
                                index++;
                            }
                        }
                        else if (word.Equals("L"))
                        {
                            byte[]    rawData  = Program.lytronetclient.DownloadFile(path);
                            string    npath    = System.IO.Path.ChangeExtension(path, "TXT");
                            byte[]    metaData = Program.lytronetclient.DownloadFile(npath);
                            Json.Root root     = new Json.Root();
                            root.LoadFromJson(Encoding.UTF8.GetString(metaData));
                            string name = String.Format(@"IMG_{0}.LFR", a);
                            if (calib.Equals("Y") || calib.Equals("y"))
                            {
                                string savepath = String.Format(@"{0}/LFToolbox0.4/LFToolbox0.3_Samples1/Cameras/{1}/Calibration_dataset/01/{2}", pwd, sn, name);
                                using (System.IO.FileStream file = System.IO.File.Create(savepath))
                                    LightFieldPackage.FromCameraFiles(root, rawData).WriteTo(file);
                            }
                            else
                            {
                                string savepath = String.Format(@"{0}/LFToolbox0.4/LFToolbox0.3_Samples1/Images/Lytro/{1}", pwd, name);
                                using (System.IO.FileStream file = System.IO.File.Create(savepath))
                                    LightFieldPackage.FromCameraFiles(root, rawData).WriteTo(file);
                            }
                        }
                        else if (word.Equals("J"))
                        {
                            string name    = System.IO.Path.ChangeExtension(path, "rawCompressed.jpg");
                            byte[] rawData = Program.lytronetclient.DownloadPictureRawJpeg(path);
                            if (calib.Equals("Y") || calib.Equals("y"))
                            {
                                string savepath = String.Format(@"{0}/LFToolbox0.4/LFToolbox0.3_Samples1/Cameras/{1}/Calibration_dataset/01/{2}", pwd, sn, name);
                                System.IO.File.WriteAllBytes(savepath, rawData);
                            }
                            else
                            {
                                string savepath = String.Format(@"{0}/LFToolbox0.4/LFToolbox0.3_Samples1/Images/Lytro/{1}", pwd, name);
                                System.IO.File.WriteAllBytes(savepath, rawData);
                            }
                        }
                        else if (word.Equals("R"))
                        {
                            byte[]   image     = Program.lytronetclient.DownloadFile(path);
                            string[] directory = path.Split('\\');
                            string   name      = directory[3];
                            if (calib.Equals("Y") || calib.Equals("y"))
                            {
                                string savepath = String.Format(@"{0}/LFToolbox0.4/LFToolbox0.3_Samples1/Cameras/{1}/Calibration_dataset/01/{2}", pwd, sn, name);
                                System.IO.File.WriteAllBytes(savepath, image);
                            }
                            else
                            {
                                string savepath = String.Format(@"{0}/LFToolbox0.4/LFToolbox0.3_Samples1/Images/Lytro/{1}", pwd, name);
                                System.IO.File.WriteAllBytes(savepath, image);
                            }
                        }
                    }
                    int    b         = a + 1;
                    string timestamp = picturelist[a].DateTaken.ToString();
                    Console.WriteLine("timestamp: " + timestamp);
                    Console.WriteLine("Downloaded Photo " + b + "/" + c);

                    if (delete.Equals("Y") || delete.Equals("y"))
                    {
                        Program.lytronetclient.DeletePicture(picturelist[a]); // delete photo from camera
                    }
                }
                a = a + 1;
            }
            Console.WriteLine("Download Complete.");
        }
        /// <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);
        }
 /// <summary>
 /// Creates a new, single frame <see cref="LightFieldPackage"/> from raw camera data.
 /// </summary>
 /// <param name="rootMetadata">The picture metadata.</param>
 /// <param name="imageData">The frame raw data.</param>
 /// <returns>A <see cref="LightFieldPackage"/> with components containing the supplied data.</returns>
 /// <exception cref="ArgumentNullException"><paramref name="rootMetadata"/> or <paramref name="imageData"/> is null.</exception>
 /// <exception cref="ArgumentException">Metadata do not contain information required to create the package.</exception>
 /// <remarks>This method is intended to build <see cref="LightFieldPackage"/> from files coming directly from the camera storage. For Lytro camera, the <paramref name="rootMetadata"/> and <paramref name="imageData"/> correspond to the contents of TXT and RAW files, respectively.</remarks>
 public static LightFieldPackage FromCameraFiles(Json.Root rootMetadata, byte[] imageData)
 {
     return(FromCameraFiles(rootMetadata, new byte[][] { imageData }));
 }
Пример #6
0
        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);
        }