IDS peak ICV v1.3.0
Loading...
Searching...
No Matches
UnpackModule.cs
Go to the documentation of this file.
6
8
33public class UnpackModule : ModuleBase, IDisposable
34{
38 public const uint DefaultTargetBitDepth = 8;
39
44
45 private readonly ImageConverterCInterface.Handle _handle;
46
50 public UnpackModule()
51 {
52 var handle = new ImageConverterCInterface.Handle();
53 DllHelper.ExecuteAndMapReturnCodes(() =>
54 ImageConverterCInterface.CInterface.peak_icv_Preprocessing_ImageConverter_Create(out handle));
55 _handle = handle;
56 }
57
59 public override string TypeId => "Unpack";
60
88 public uint TargetBitDepth { get; set; } = DefaultTargetBitDepth;
89
94
107 public override bool IsEnabled
108 {
109 get => true;
110 set { }
111 }
112
116 public void Dispose()
117 {
118 _handle.Dispose();
119 GC.SuppressFinalize(this);
120 }
121
141 public override Image Process(Image inputImage)
142 {
143 var inputBitDepth = new PixelFormatInfo(inputImage.PixelFormat).StorageBitsPerChannel;
144 var outputBitDepth = GetOutputBitDepth(inputBitDepth);
145 PixelFormat outputPixelFormat = GetOutputPixelFormat(inputImage.PixelFormat, outputBitDepth);
146
147 if (inputImage.PixelFormat == outputPixelFormat)
148 {
149 return inputImage;
150 }
151
152 var outputImageHandle = new Image.Handle();
153
154 DllHelper.ExecuteAndMapReturnCodes(() =>
155 ImageConverterCInterface.CInterface.peak_icv_Preprocessing_ImageConverter_Convert(_handle,
156 inputImage._handle,
157 outputPixelFormat, out outputImageHandle));
158
159 return new Image(outputImageHandle);
160 }
161
187
204 public void ReleaseBuffers()
205 {
206 DllHelper.ExecuteAndMapReturnCodes(() =>
207 ImageConverterCInterface.CInterface.peak_icv_Preprocessing_ImageConverter_ReleaseBuffers(_handle));
208 }
209
213 public override void Serialize(IArchive archive)
214 {
215 // Unlike other modules, Unpack behaves differently: it has no version,
216 // is always enabled, and therefore cannot use the base class implementation.
217 }
218
222 public override void Deserialize(IArchive archive)
223 {
224 // Unpack behaves differently from other modules. Since it is always enabled,
225 // the archive does not need to include an "enabled" entry.
226 // It also does not have a version.
227 }
228
229 private uint GetOutputBitDepth(uint inputBitDepth)
230 {
231 return ProcessingPolicy switch
232 {
233 ProcessingPolicy.Fast => Math.Min(inputBitDepth, TargetBitDepth),
234 ProcessingPolicy.Enhanced => Math.Max(inputBitDepth, TargetBitDepth),
235 _ => inputBitDepth
236 };
237 }
238
239 private PixelFormat GetOutputPixelFormat(PixelFormat inputPixelFormat, uint outputBitDepth)
240 {
241 var inputPixelFormatInfo = new PixelFormatInfo(inputPixelFormat);
242 var isBayer = inputPixelFormatInfo.HasChannel(Channel.Bayer);
243 var isYuv = inputPixelFormatInfo.HasChannel(Channel.ChromaV);
244 var isMono = !isYuv && inputPixelFormatInfo.HasChannel(Channel.Intensity);
245
246 if (isYuv)
247 {
248 return PixelFormat.RGB8;
249 }
250
251 if (isMono)
252 {
253 return outputBitDepth switch
254 {
255 >= 12 => PixelFormat.Mono12,
256 >= 10 => PixelFormat.Mono10,
257 _ => PixelFormat.Mono8
258 };
259 }
260
261 if (isBayer)
262 {
263 PixelFormat unpackedFormat = new PixelFormatInfo(inputPixelFormat).UnpackedPixelFormat;
264
265 var isBayerRg = unpackedFormat is
266 PixelFormat.BayerRG8 or
267 PixelFormat.BayerRG10 or
268 PixelFormat.BayerRG12;
269 if (isBayerRg)
270 {
271 return outputBitDepth switch
272 {
273 >= 12 => PixelFormat.BayerRG12,
274 >= 10 => PixelFormat.BayerRG10,
275 _ => PixelFormat.BayerRG8
276 };
277 }
278
279 var isBayerGr = unpackedFormat is
280 PixelFormat.BayerGR8 or
281 PixelFormat.BayerGR10 or
282 PixelFormat.BayerGR12;
283 if (isBayerGr)
284 {
285 return outputBitDepth switch
286 {
287 >= 12 => PixelFormat.BayerGR12,
288 >= 10 => PixelFormat.BayerGR10,
289 _ => PixelFormat.BayerGR8
290 };
291 }
292
293 var isBayerBg = unpackedFormat is
294 PixelFormat.BayerBG8 or
295 PixelFormat.BayerBG10 or
296 PixelFormat.BayerBG12;
297 if (isBayerBg)
298 {
299 return outputBitDepth switch
300 {
301 >= 12 => PixelFormat.BayerBG12,
302 >= 10 => PixelFormat.BayerBG10,
303 _ => PixelFormat.BayerBG8
304 };
305 }
306
307 var isBayerGb = unpackedFormat is
308 PixelFormat.BayerGB8 or
309 PixelFormat.BayerGB10 or
310 PixelFormat.BayerGB12;
311 if (isBayerGb)
312 {
313 return outputBitDepth switch
314 {
315 >= 12 => PixelFormat.BayerGB12,
316 >= 10 => PixelFormat.BayerGB10,
317 _ => PixelFormat.BayerGB8
318 };
319 }
320 }
321
322 return inputPixelFormat;
323 }
324}
Provides information and utility functions for pixel formats. The PixelFormatInfo class offers method...
PixelFormat UnpackedPixelFormat
Returns the unpacked pixel format for a packed pixel format. If the pixel format is already unpacked,...
uint StorageBitsPerChannel
The number of bits used per channel for storage.
Base class for all pipeline modules.
Definition ModuleBase.cs:13
Unpack is an image pipeline module that converts packed pixel formats into corresponding unpacked Bay...
override void Deserialize(IArchive archive)
This function has no effect on the Unpack module.
UnpackModule()
Creates an instance of class UnpackModule.
override Image Process(Image inputImage)
Converts a packed pixel format image into an unpacked Bayer or monochrome format.
void ReleaseBuffers()
Frees unused internal buffers.
override void ResetToDefault()
Resets all settings to their defaults.
void Dispose()
Releases all resources used by this class.
override string TypeId
The unique type identifier for this module implementation.This should be a unique string identifying ...
const ProcessingPolicy DefaultProcessingPolicy
The default processing policy.
const uint DefaultTargetBitDepth
The default target bit depth.
uint TargetBitDepth
The target bit depth used during image processing.
override bool IsEnabled
Gets a value indicating whether the unpack module is enabled. This property always returns true.
override void Serialize(IArchive archive)
This function has no effect on the Unpack module.
Constructs an Image from an IImageView Instance.
Definition Image.cs:18
PixelFormat PixelFormat
Gets the pixel format from the image.
Definition Image.cs:217
Channel
Represents the various data channels in image or spatial data formats. /// This enumeration defines s...
Definition Channel.cs:13
PixelFormat
Each enumerator represents a specific pixel layout and bit-depth, used for interpreting raw image dat...
ProcessingPolicy
Specifies the processing policy.
Represents a hierarchical, strongly-typed key-value archive.
Definition IArchive.cs:79