IDS peak ICV v1.3.0
Loading...
Searching...
No Matches
DebayerModule.cs
Go to the documentation of this file.
6
8
29public class DebayerModule : ModuleBase, IDisposable
30{
35
40
44 public const uint DefaultTargetBitDepth = 8;
45
50
51 internal const string ConversionPolicyKey = "ConversionPolicy";
52 internal const string ChannelLayoutKey = "ChannelLayout";
53 private readonly ImageConverterCInterface.Handle _handle;
54
59 {
60 var handle = new ImageConverterCInterface.Handle();
61 DllHelper.ExecuteAndMapReturnCodes(() =>
62 ImageConverterCInterface.CInterface.peak_icv_Preprocessing_ImageConverter_Create(out handle));
63 _handle = handle;
64 }
65
67 public override string TypeId => "Debayer";
68
76
85
108 public uint TargetBitDepth { get; set; } = DefaultTargetBitDepth;
109
114
118 public void Dispose()
119 {
120 _handle.Dispose();
121 GC.SuppressFinalize(this);
122 }
123
144 public override object Process(object input)
145 {
146 return base.Process(input);
147 }
148
169 public override Image Process(Image inputImage)
170 {
171 if (!IsEnabled)
172 {
173 return inputImage;
174 }
175
176 PixelFormat inputFormat = inputImage.PixelFormat;
177 PixelFormat outputFormat = GetOutputFormat(inputFormat);
178
179 if (inputFormat == outputFormat)
180 {
181 return inputImage;
182 }
183
184 var outputImageHandle = new Image.Handle();
185
186 DllHelper.ExecuteAndMapReturnCodes(() =>
187 ImageConverterCInterface.CInterface.peak_icv_Preprocessing_ImageConverter_Convert(_handle,
188 inputImage._handle,
189 outputFormat, out outputImageHandle));
190
191 return new Image(outputImageHandle);
192 }
193
205 public override void Serialize(IArchive archive)
206 {
207 base.Serialize(archive);
208
209 archive.SetString(ChannelLayoutKey, ChannelLayout.ToString());
210 archive.SetString(ConversionPolicyKey, ConversionPolicy.ToString());
211 }
212
237 public override void Deserialize(IArchive archive)
238 {
239 base.Deserialize(archive);
240
241 try
242 {
244 (DebayerChannelLayout) Enum.Parse(typeof(DebayerChannelLayout), archive.GetString(ChannelLayoutKey));
247 archive.GetString(ConversionPolicyKey));
248 }
249 catch (Exception e)
250 {
251 throw new IcvCorruptedException(e.Message);
252 }
253 }
254
271
288 public void ReleaseBuffers()
289 {
290 DllHelper.ExecuteAndMapReturnCodes(() =>
291 ImageConverterCInterface.CInterface.peak_icv_Preprocessing_ImageConverter_ReleaseBuffers(_handle));
292 }
293
294 private long GetOutputBitDepth(long inputBitDepth)
295 {
296 return ProcessingPolicy switch
297 {
298 ProcessingPolicy.Fast => Math.Min(inputBitDepth, TargetBitDepth),
299 ProcessingPolicy.Enhanced => Math.Max(inputBitDepth, TargetBitDepth),
300 _ => inputBitDepth
301 };
302 }
303
304 private PixelFormat GetOutputFormat(PixelFormat inputFormat)
305 {
307 {
308 return inputFormat;
309 }
310
311 var info = new PixelFormatInfo(inputFormat);
312
313 if (!info.IsSingleChannel)
314 {
315 return inputFormat;
316 }
317
318 var isBayer = info.HasChannel(Channel.Bayer);
319 var isMono = info.HasIntensityChannel;
320
321 if (!isBayer && (!isMono || ConversionPolicy != DebayerConversionPolicy.BayerAndMono))
322 {
323 return inputFormat;
324 }
325
326 var inputBitDepth = info.StorageBitsPerChannel;
327 var outputBitDepth = GetOutputBitDepth(inputBitDepth);
328
329 PixelFormat ThrowNotSupported()
330 {
331 throw new IcvNotSupportedException(
332 $"The given input pixel format 0x{(long) inputFormat:X} is unknown!");
333 }
334
335 if (outputBitDepth <= 8)
336 {
337 return ChannelLayout switch
338 {
339 DebayerChannelLayout.RGB => PixelFormat.RGB8,
340 DebayerChannelLayout.BGR => PixelFormat.BGR8,
341 DebayerChannelLayout.RGBA => PixelFormat.RGBa8,
342 DebayerChannelLayout.BGRA => PixelFormat.BGRa8,
343 _ => ThrowNotSupported()
344 };
345 }
346
347 if (outputBitDepth <= 10)
348 {
349 return ChannelLayout switch
350 {
351 DebayerChannelLayout.RGB => PixelFormat.RGB10,
352 DebayerChannelLayout.BGR => PixelFormat.BGR10,
353 DebayerChannelLayout.RGBA => PixelFormat.RGBa10,
354 DebayerChannelLayout.BGRA => PixelFormat.BGRa10,
355 _ => ThrowNotSupported()
356 };
357 }
358
359 if (outputBitDepth <= 12)
360 {
361 return ChannelLayout switch
362 {
363 DebayerChannelLayout.RGB => PixelFormat.RGB12,
364 DebayerChannelLayout.BGR => PixelFormat.BGR12,
365 DebayerChannelLayout.RGBA => PixelFormat.RGBa12,
366 DebayerChannelLayout.BGRA => PixelFormat.BGRa12,
367 _ => ThrowNotSupported()
368 };
369 }
370
371 return ThrowNotSupported();
372 }
373}
Provides information and utility functions for pixel formats. The PixelFormatInfo class offers method...
Occurs when reading from a file or stream that is incomplete, damaged, or in an unexpected format.
Definition Exception.cs:75
Occurs when a requested operation is not implemented or supported (e.g., exporting to an unsupported ...
Definition Exception.cs:62
Debayer is an image pipeline module that converts Bayer RAW images into standard color formats.
override void Deserialize(IArchive archive)
Restores the object's state from the provided archive.
override Image Process(Image inputImage)
Converts a Bayer or mono image to a color image, or returns the input image unchanged.
const ProcessingPolicy DefaultPolicy
The default processing policy.
void ReleaseBuffers()
Frees unused internal buffers.
override void ResetToDefault()
Resets all module settings to its default settings.
const DebayerChannelLayout DefaultChannelLayout
The default channel layout.
DebayerModule()
Creates an instance of class DebayerModule.
override object Process(object input)
Converts a Bayer or mono image to a color image, or returns the input image unchanged.
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 uint DefaultTargetBitDepth
The default target bit depth.
DebayerChannelLayout ChannelLayout
The output channel layout used for debayered images.
uint TargetBitDepth
The target bit depth for processed images.
const DebayerConversionPolicy DefaultConversionPolicy
The default conversion policy.
DebayerConversionPolicy ConversionPolicy
The debayer conversion policy.
override void Serialize(IArchive archive)
Serializes the object's internal state into the provided archive.
Base class for all pipeline modules.
Definition ModuleBase.cs:13
virtual bool IsEnabled
Get whether this module is enabled.
Definition ModuleBase.cs:28
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...
DebayerConversionPolicy
Specifies the conditions under which the debayer module applies color conversion.
DebayerChannelLayout
Specifies the channel layout used by the debayer module in image processing pipelines.
ProcessingPolicy
Specifies the processing policy.
Represents a hierarchical, strongly-typed key-value archive.
Definition IArchive.cs:79
string GetString(string key)
Retrieves a string value by key.
void SetString(string key, string value)
Stores a string value under the specified key.