Skip to content
This repository has been archived by the owner on Feb 22, 2024. It is now read-only.

Commit

Permalink
#186 - TakePicture not setting ImageContext resolution
Browse files Browse the repository at this point in the history
Allow components which do not feature a resize block to return a valid resolution. StillPort and VideoPort to store local resolution in field. Tidy up Port heirarchy to reduce repetitive code. Camera component max width/height to be taken from MMALCameraConfig.Resolution.
  • Loading branch information
techyian committed Oct 12, 2020
1 parent 8b07ab3 commit 3eef26c
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 73 deletions.
8 changes: 4 additions & 4 deletions src/MMALSharp/Components/MMALCameraComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ public void Initialise(IOutputCaptureHandler stillCaptureHandler = null, IOutput

var camConfig = new MMAL_PARAMETER_CAMERA_CONFIG_T(
new MMAL_PARAMETER_HEADER_T(MMALParametersCamera.MMAL_PARAMETER_CAMERA_CONFIG, Marshal.SizeOf<MMAL_PARAMETER_CAMERA_CONFIG_T>()),
this.CameraInfo.MaxWidth,
this.CameraInfo.MaxHeight,
MMALCameraConfig.Resolution.Width,
MMALCameraConfig.Resolution.Height,
0,
1,
MMALCameraConfig.Resolution.Width,
Expand Down Expand Up @@ -338,8 +338,8 @@ private void InitialiseStill(IOutputCaptureHandler handler)
portConfig = new MMALPortConfig(
encoding,
encoding,
width: currentWidth,
height: currentHeight,
width: resolution.Width,
height: resolution.Height,
framerate: MMALCameraConfig.Framerate,
bufferNum: Math.Max(this.StillPort.BufferNumRecommended, 3),
bufferSize: Math.Max(this.StillPort.BufferSizeRecommended, this.StillPort.BufferSizeMin),
Expand Down
3 changes: 2 additions & 1 deletion src/MMALSharp/Components/MMALComponentBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Runtime.InteropServices;
using System.Text;
using Microsoft.Extensions.Logging;
using MMALSharp.Callbacks;
using MMALSharp.Common;
using MMALSharp.Common.Utility;
using MMALSharp.Components;
Expand Down Expand Up @@ -93,7 +94,7 @@ protected MMALComponentBase(string name)

for (int i = 0; i < this.Ptr->PortNum; i++)
{
this.Ports.Add(new GenericPort((IntPtr)(&(*this.Ptr->Port[i])), this, PortType.Generic, Guid.NewGuid()));
this.Ports.Add(new GenericPort<ICallbackHandler>((IntPtr)(&(*this.Ptr->Port[i])), this, PortType.Generic, Guid.NewGuid()));
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/MMALSharp/Ports/Clocks/ClockPort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
// </copyright>

using System;
using MMALSharp.Callbacks;
using MMALSharp.Components;

namespace MMALSharp.Ports.Clocks
{
/// <summary>
/// Represents a clock port.
/// </summary>
public class ClockPort : GenericPort
public class ClockPort : GenericPort<ICallbackHandler>
{
/// <summary>
/// Creates a new instance of <see cref="ClockPort"/>.
Expand Down
12 changes: 7 additions & 5 deletions src/MMALSharp/Ports/GenericPort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,23 @@ namespace MMALSharp.Ports
/// <summary>
/// Represents a generic MMAL port of any type.
/// </summary>
public class GenericPort : PortBase<ICallbackHandler>
/// <typeparam name="TCallback">The callback handler type.</typeparam>
public class GenericPort<TCallback> : PortBase<TCallback>
where TCallback : ICallbackHandler
{
/// <inheritdoc />
public override Resolution Resolution
{
get => new Resolution(this.Width, this.Height);
get => new Resolution(this.NativeWidth, this.NativeHeight);
internal set
{
this.Width = value.Pad().Width;
this.Height = value.Pad().Height;
this.NativeWidth = value.Pad().Width;
this.NativeHeight = value.Pad().Height;
}
}

/// <summary>
/// Creates a new instance of <see cref="GenericPort"/>.
/// Creates a new instance of <see cref="GenericPort{TCallback}"/>.
/// </summary>
/// <param name="ptr">The native pointer.</param>
/// <param name="comp">The component this port is associated with.</param>
Expand Down
12 changes: 11 additions & 1 deletion src/MMALSharp/Ports/IPort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public interface IPort : IMMALObject
MMAL_ES_FORMAT_T Format { get; }

/// <summary>
/// The Width/Height that this port will process data in.
/// The resolution that this port will process data in. Not aligned value.
/// </summary>
Resolution Resolution { get; }

Expand Down Expand Up @@ -173,6 +173,16 @@ public interface IPort : IMMALObject
/// </summary>
MMAL_RATIONAL_T Par { get; }

/// <summary>
/// The width value stored against this port instance. Typically aligned to 32 pixels.
/// </summary>
int NativeWidth { get; }

/// <summary>
/// The height value stored against this port instance. Typically aligned to 16 pixels.
/// </summary>
int NativeHeight { get; }

/// <summary>
/// Indicates whether ZeroCopy mode should be enabled on this port. When enabled, data is not copied to the ARM processor and is handled directly by the GPU. Useful when
/// transferring large amounts of data or raw capture.
Expand Down
13 changes: 1 addition & 12 deletions src/MMALSharp/Ports/Inputs/InputPort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,8 @@ namespace MMALSharp.Ports.Inputs
/// <summary>
/// Represents an input port.
/// </summary>
public class InputPort : PortBase<IInputCallbackHandler>, IInputPort
public class InputPort : GenericPort<IInputCallbackHandler>, IInputPort
{
/// <inheritdoc />
public override Resolution Resolution
{
get => new Resolution(this.Width, this.Height);
internal set
{
this.Width = value.Pad().Width;
this.Height = value.Pad().Height;
}
}

/// <summary>
/// Creates a new instance of <see cref="InputPort"/>.
/// </summary>
Expand Down
23 changes: 2 additions & 21 deletions src/MMALSharp/Ports/Outputs/FastStillPort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,8 @@ namespace MMALSharp.Ports.Outputs
/// <summary>
/// Represents a still port used specifically when capturing rapid single image frames from the camera's video port.
/// </summary>
public unsafe class FastStillPort : OutputPort, IVideoPort
{
/// <inheritdoc />
public override Resolution Resolution
{
get => new Resolution(this.Width, this.Height);
internal set
{
if (value.Width == 0 || value.Height == 0)
{
this.Width = MMALCameraConfig.Resolution.Pad().Width;
this.Height = MMALCameraConfig.Resolution.Pad().Height;
}
else
{
this.Width = value.Pad().Width;
this.Height = value.Pad().Height;
}
}
}

public unsafe class FastStillPort : VideoPort, IVideoPort
{
/// <summary>
/// Creates a new instance of <see cref="FastStillPort"/>.
/// </summary>
Expand Down
15 changes: 2 additions & 13 deletions src/MMALSharp/Ports/Outputs/OutputPort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,8 @@ namespace MMALSharp.Ports.Outputs
/// <summary>
/// Represents an output port.
/// </summary>
public unsafe class OutputPort : PortBase<IOutputCallbackHandler>, IOutputPort
{
/// <inheritdoc />
public override Resolution Resolution
{
get => new Resolution(this.Width, this.Height);
internal set
{
this.Width = value.Pad().Width;
this.Height = value.Pad().Height;
}
}

public unsafe class OutputPort : GenericPort<IOutputCallbackHandler>, IOutputPort
{
/// <summary>
/// Creates a new instance of <see cref="OutputPort"/>.
/// </summary>
Expand Down
23 changes: 18 additions & 5 deletions src/MMALSharp/Ports/Outputs/StillPort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,34 @@ namespace MMALSharp.Ports.Outputs
/// </summary>
public unsafe class StillPort : OutputPort, IStillPort
{
private Resolution _resolution;

/// <inheritdoc />
public override Resolution Resolution
{
get => new Resolution(this.Width, this.Height);
get
{
if (_resolution.Width == 0 || _resolution.Height == 0)
{
_resolution = new Resolution(MMALCameraConfig.Resolution.Width, MMALCameraConfig.Resolution.Height);
}

return _resolution;
}

internal set
{
if (value.Width == 0 || value.Height == 0)
{
this.Width = MMALCameraConfig.Resolution.Pad().Width;
this.Height = MMALCameraConfig.Resolution.Pad().Height;
this.NativeWidth = MMALCameraConfig.Resolution.Pad().Width;
this.NativeHeight = MMALCameraConfig.Resolution.Pad().Height;
_resolution = new Resolution(MMALCameraConfig.Resolution.Width, MMALCameraConfig.Resolution.Height);
}
else
{
this.Width = value.Pad().Width;
this.Height = value.Pad().Height;
this.NativeWidth = value.Pad().Width;
this.NativeHeight = value.Pad().Height;
_resolution = new Resolution(value.Width, value.Height);
}
}
}
Expand Down
23 changes: 18 additions & 5 deletions src/MMALSharp/Ports/Outputs/VideoPort.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,34 @@ namespace MMALSharp.Ports.Outputs
/// </summary>
public unsafe class VideoPort : OutputPort, IVideoPort
{
private Resolution _resolution;

/// <inheritdoc />
public override Resolution Resolution
{
get => new Resolution(this.Width, this.Height);
get
{
if (_resolution.Width == 0 || _resolution.Height == 0)
{
_resolution = new Resolution(MMALCameraConfig.Resolution.Width, MMALCameraConfig.Resolution.Height);
}

return _resolution;
}

internal set
{
if (value.Width == 0 || value.Height == 0)
{
this.Width = MMALCameraConfig.Resolution.Pad().Width;
this.Height = MMALCameraConfig.Resolution.Pad().Height;
this.NativeWidth = MMALCameraConfig.Resolution.Pad().Width;
this.NativeHeight = MMALCameraConfig.Resolution.Pad().Height;
_resolution = new Resolution(MMALCameraConfig.Resolution.Width, MMALCameraConfig.Resolution.Height);
}
else
{
this.Width = value.Pad().Width;
this.Height = value.Pad().Height;
this.NativeWidth = value.Pad().Width;
this.NativeHeight = value.Pad().Height;
_resolution = new Resolution(value.Width, value.Height);
}
}
}
Expand Down
16 changes: 11 additions & 5 deletions src/MMALSharp/Ports/PortBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public int BufferSize
public MMAL_ES_FORMAT_T Format => *this.Ptr->Format;

/// <summary>
/// The Width/Height that this port will process data in.
/// The resolution that this port will process data in. Not aligned value.
/// </summary>
public abstract Resolution Resolution { get; internal set; }

Expand Down Expand Up @@ -241,16 +241,22 @@ public MMAL_RATIONAL_T Par
internal set => this.Ptr->Format->Es->Video.Par = value;
}

internal int Width
/// <summary>
/// The width value stored against this port instance. Typically aligned to 32 pixels.
/// </summary>
public int NativeWidth
{
get => this.Ptr->Format->Es->Video.Width;
set => this.Ptr->Format->Es->Video.Width = value;
internal set => this.Ptr->Format->Es->Video.Width = value;
}

internal int Height
/// <summary>
/// The height value stored against this port instance. Typically aligned to 16 pixels.
/// </summary>
public int NativeHeight
{
get => this.Ptr->Format->Es->Video.Height;
set => this.Ptr->Format->Es->Video.Height = value;
internal set => this.Ptr->Format->Es->Video.Height = value;
}

#endregion
Expand Down

0 comments on commit 3eef26c

Please sign in to comment.