using System;
using System.Runtime.InteropServices;
namespace itk.simple.examples
{
public class Program
{
static void Main(string[] args)
{
if (args.Length < 1)
{
Console.WriteLine("Usage: inputImage outputImage");
return;
}
string inputFilename = args[0];
string outputFilename = args[1];
SitkImage input = SimpleITK.ReadImage(inputFilename);
input = SimpleITK.Cast(input, PixelId.sitkFloat32);
VectorUInt32 size = input.GetSize();
int len = 1;
for (int dim = 0; dim < input.GetDimension(); dim++)
{
len *= (int)size[dim];
}
IntPtr buffer = input.GetBufferAsFloat();
float[] bufferAsArray = new float[len];
Marshal.Copy(buffer, bufferAsArray, 0, len);
double total = 0.0;
for (int j = 0; j < size[1]; j++)
{
for (int i = 0; i < size[0]; i++)
{
float pixel = bufferAsArray[i + j * size[1]];
total += pixel;
}
}
Console.WriteLine("Pixel value total: {0}", total);
uint width = input.GetWidth();
uint height = input.GetHeight();
SitkImage outImage = new SitkImage(width, height, PixelId.sitkFloat32);
IntPtr outImageBuffer = outImage.GetBufferAsFloat();
Marshal.Copy(bufferAsArray, 0, outImageBuffer, (int)(size[0] * size[1]));
outImage = SimpleITK.RescaleIntensity(outImage, 0, 255);
outImage = SimpleITK.Cast(outImage, PixelId.sitkUInt8);
SimpleITK.WriteImage(outImage, outputFilename);
}
}
}