Common Vision Blox 15.0
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Friends Modules Pages
ImageManager/Cvb.Net/PixelFormatConversion

This example program is located in your CVB installation under %CVB%Tutorial/ImageManager/Cvb.Net/PixelFormatConversion.

Program.cs:

// Example that demonstrates how an arbitrary PFNC-compliant image can be converted to another pixel
// format.

using System;
using System.IO;
namespace PixelFormatConversion
{
class PixelFormatConversion
{
private static ValueTuple<Image, PfncFormat> GetImage(string fileName)
{
// Create a pair of a PFNC-compliant image and its pixel format.
var pixelFormat = PfncFormat.BayerGB8;
var imageSource = Image.FromFile(fileName);
var imageFormatPair = new ValueTuple<Image, PfncFormat>(
Image.FromPixelFormat(imageSource.Size, pixelFormat), pixelFormat);
// For the right buffer layout, copy the data from an image file to
// the PFNC-compliant image buffer.
imageSource.CopyTo(imageFormatPair.Item1);
return imageFormatPair;
}
private static Image GetAnotherRepresentation(Image srcImage, PixelFormatConverter converter)
{
// Create a PFNC-compliant image that will hold the converted image data.
var dstImage = Image.FromPixelFormat(srcImage.Size, converter.DestinationFormat);
// Convert the source data by following the configuration; it will overwrite
// the destination image object with the converted image data.
converter.Execute(srcImage, dstImage);
return dstImage;
}
private static string ToString(PfncFormat pixelFormat)
{
return PfncFormatValue.ToString(pixelFormat);
}
private static void Save(Image image, string path)
{
image.Save(path);
Console.WriteLine($" Saved as: {path}");
}
static void Main(string[] args)
{
Console.WriteLine("As a source, a Bayer pattern image is given.");
const string baseName = "FruitBowl";
const string extension = ".bmp";
var srcImageFormatPair =
GetImage(Path.Combine(new string[] { SystemInfo.InstallPath, "Tutorial", baseName + "Bayer" + extension }));
Save(srcImageFormatPair.Item1, baseName + ToString(srcImageFormatPair.Item2) + extension);
Console.WriteLine("As the destination, an RGB format is given.");
const PfncFormat dstFormat = PfncFormat.RGB8;
Console.WriteLine("Create a pixel format converter.");
var converter = PixelFormatConverter.Create(srcImageFormatPair.Item2, dstFormat);
Console.WriteLine("Convert the source to another.");
var dstImage = GetAnotherRepresentation(srcImageFormatPair.Item1, converter);
Console.WriteLine("Saving the converted image as a file.");
Save(dstImage, baseName + ToString(dstFormat) + extension);
}
}
}
void Execute(T sourceObject, Image destinationObject)
static PixelFormatConverter Create(PfncFormat sourceFormat, PfncFormat destinationFormat)
__int3264 Image