/// <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> /// Loads a <see cref="FieldImage"/> from a byte array and metadata. /// </summary> /// <param name="frameData">The raw frame data.</param> /// <param name="frameMetadata">The raw frame metadata.</param> /// <param name="privateMetadata">The private metadata.</param> /// <returns>a new instance of the <see cref="FieldImage"/> class.</returns> public static FieldImage From(byte[] frameData, Json.FrameMetadata frameMetadata, Json.FrameMetadata privateMetadata = null) { if (frameData == null) { throw new ArgumentNullException("frameData"); } return(new FieldImage(frameData, frameMetadata, privateMetadata)); }
private FieldImage(LightFieldComponent frame, Json.FrameMetadata frameMetadata, Json.FrameMetadata privateMetadata) { _metadata = frameMetadata; _privateMetadata = privateMetadata; _frameData = frame.Data; _width = (int)_metadata.Image.Width; _height = (int)_metadata.Image.Height; }
/// <summary> /// Loads a <see cref="FieldImage"/> from a <see cref="LightFieldComponent"/> and metadata. /// </summary> /// <param name="frame">The raw frame component.</param> /// <param name="frameMetadata">The raw frame metadata.</param> /// <param name="privateMetadata">The private metadata.</param> /// <returns>a new instance of the <see cref="FieldImage"/> class.</returns> public static FieldImage From(LightFieldComponent frame, Json.FrameMetadata frameMetadata, Json.FrameMetadata privateMetadata = null) { if (frame == null) { throw new ArgumentNullException("frame"); } return(new FieldImage(frame, frameMetadata, privateMetadata)); }
/// <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> /// Finds the nearest matching flat field image in the set. /// </summary> /// <param name="metadata">The metadata of the reference image.</param> /// <param name="packageReference">The package reference passed in to the <see cref="LoadFrom(LightFieldPackage,string)"/> reference that contains the nearest matching flat field image.</param> /// <param name="componentReference">The component reference of the nearest flat field image in the package.</param> /// <param name="frameImage">Cached image metadata of the flat field image.</param> /// <returns>true if a nearest matching flat field could be found; false otherwise.</returns> /// <remarks>Currently, this method returns false if and only if there are no images in the set.</remarks> public bool FindNearestFlatFieldImage(Json.FrameMetadata metadata, out string packageReference, out string componentReference, out Json.FrameImage frameImage) { if (metadata == null) { throw new ArgumentNullException("metadata"); } if (metadata.Devices == null || metadata.Devices.Lens == null) { throw new ArgumentException("Critical metadata missing.", "metadata"); } return(FindNearestFlatFieldImage((int)metadata.Devices.Lens.ZoomStep, (int)metadata.Devices.Lens.FocusStep, out packageReference, out componentReference, out frameImage)); }
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); }
/// <summary> /// Initializes a new instance of the <see cref="MicroLensCollection"/> from metadata. /// </summary> /// <param name="metadata">The metadata with microlens array parameters.</param> /// <exception cref="ArgumentNullException"><paramref name="metadata"/> is null.</exception> /// <exception cref="ArgumentException"><paramref name="metadata"/> contains invalid or no information about the microlens array.</exception> /// <exception cref="NotSupportedException">The microlens tiling specified by <paramref name="metadata"/> is not supported.</exception> public MicroLensCollection(Json.FrameMetadata metadata) { if (metadata == null) { throw new ArgumentNullException("metadata"); } if (metadata.Image == null || metadata.Devices == null) { throw new ArgumentException("Critical metadata missing."); } _metadata = metadata; Json.Mla mla = metadata.Devices.Mla; Json.Sensor sensor = metadata.Devices.Sensor; if (mla == null || sensor == null) { throw new ArgumentException("Critical metadata missing."); } switch (mla.Tiling) { case "squareUniform": Initialize(metadata, 0.0, 1.0, 1.0); _orthogonal = true; break; case "hexUniformRowMajor": Initialize(metadata, HexagonalSkew, 1.0, HexagonalMinorFactor / 2.0); break; default: throw new NotSupportedException("Unsupported MLA tiling."); } }
private void Initialize(Json.FrameMetadata metadata, double skewX, double deltaXfactor, double deltaYfactor) { global::System.Diagnostics.Debug.Assert(deltaXfactor != 0, "X factor cannot be zero."); global::System.Diagnostics.Debug.Assert(deltaYfactor != 0, "Y factor cannot be zero."); Json.Mla mla = metadata.Devices.Mla; Json.Sensor sensor = metadata.Devices.Sensor; Json.Lens lens = metadata.Devices.Lens; if (sensor.PixelPitch == 0) { throw new ArgumentException("Pixel pitch cannot be zero.", "metadata"); } else if (mla.LensPitch == 0) { throw new ArgumentException("Lens pitch cannot be zero.", "metadata"); } else if (mla.ScaleFactor.X == 0 || mla.ScaleFactor.Y == 0) { throw new ArgumentException("Lens scale factor cannot be zero.", "metadata"); } _width = (int)metadata.Image.Width; _height = (int)metadata.Image.Height; decimal projectedPitch = mla.LensPitch; if (lens != null && lens.ExitPupilOffset.Z != 0) { projectedPitch *= (lens.ExitPupilOffset.Z + mla.SensorOffset.Z) / lens.ExitPupilOffset.Z; } _rotation = (double)mla.Rotation; _startX = _width / 2 + (double)(mla.SensorOffset.X / sensor.PixelPitch); _startY = _height / 2 + (double)(mla.SensorOffset.Y / sensor.PixelPitch); _radius = (double)(projectedPitch / sensor.PixelPitch); _deltaX = (double)(projectedPitch / sensor.PixelPitch * mla.ScaleFactor.X) * deltaXfactor; _deltaY = (double)(projectedPitch / sensor.PixelPitch * mla.ScaleFactor.Y) * deltaYfactor; _skewX = skewX; double pX = _startY * Math.Cos(_rotation) - _startX * Math.Sin(_rotation); double pY = _startY * Math.Sin(_rotation) + _startX * Math.Cos(_rotation); double xMin, xMax, yMin, yMax; if (_rotation >= 0) { xMin = -pY; xMax = _height * Math.Sin(_rotation) + _width * Math.Cos(_rotation) - pY; yMin = -_width *Math.Sin(_rotation) - pX; yMax = _height * Math.Cos(_rotation) - pX; } else { yMin = -pX; yMax = -_width *Math.Sin(_rotation) + _height * Math.Cos(_rotation) - pX; xMin = _width * Math.Sin(_rotation) - pY; xMax = _height * Math.Cos(_rotation) - pY; } if (_skewX > 0) { xMin -= (_height - _startY) * _skewX; xMax += _startY * _skewX; } else if (_skewX < 0) { xMin += _startY * _skewX; xMax -= (_height - _startY) * _skewX; } _xMinStep = (int)Math.Floor(xMin / _deltaX); _xMaxStep = (int)Math.Ceiling(xMax / _deltaX); _yMinStep = (int)Math.Floor(yMin / _deltaY); _yMaxStep = (int)Math.Ceiling(yMax / _deltaY); }
private FieldImage(byte[] frameData, Json.FrameMetadata frameMetadata, Json.FrameMetadata privateMetadata) { _metadata = frameMetadata; _privateMetadata = privateMetadata; _frameData = frameData; }
/// <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 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); }