Skip to content

Commit

Permalink
Merge pull request SimpleITK#766 from pettaa123/getSetBufferExample
Browse files Browse the repository at this point in the history
added setBuffer to getBuffer example
  • Loading branch information
dave3d committed Sep 16, 2021
2 parents d481949 + 5534eb2 commit 6850980
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 90 deletions.
15 changes: 12 additions & 3 deletions Examples/CSharp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,21 @@ endif()
# CSharp Examples
#

sitk_add_csharp_test( Example.ImageGetBuffer
"${CMAKE_CURRENT_SOURCE_DIR}/ImageGetBuffer.cs"
sitk_add_csharp_test( Example.ImageGetSetBuffer
"${CMAKE_CURRENT_SOURCE_DIR}/ImageGetSetBuffer.cs"
DATA{${SimpleITK_DATA_ROOT}/Input/RA-Short.nrrd})
set_tests_properties( CSharp.Example.ImageGetBuffer
set_tests_properties( CSharp.Example.ImageGetSetBuffer
PROPERTIES PASS_REGULAR_EXPRESSION "total: 1009713" )

sitk_add_csharp_test( Example.ImageGetSetBuffer
"${CMAKE_CURRENT_SOURCE_DIR}/ImageGetSetBuffer.cs"
DATA{${SimpleITK_DATA_ROOT}/Input/RA-Short.nrrd}
"${SimpleITK_TEST_OUTPUT_DIR}/CSharp.SetBuffer.png"
IMAGE_MD5_COMPARE
"${SimpleITK_TEST_OUTPUT_DIR}/CSharp.SetBuffer.png"
"80d3be0e7a9cafad1ec4fca5bc5cfe89c8a416bc203efccd49cfaf177fb88a4e7e76a336bb09fc2aedca2522a0911e72622a4cde5888b0ec6e415e49d977845a"
)

sitk_add_csharp_test( Example.ConnectedThresholdSegmentation
"${CMAKE_CURRENT_SOURCE_DIR}/ConnectedThresholdSegmentation.cs"
DATA{${SimpleITK_DATA_ROOT}/Input/BrainProtonDensitySlice.png}
Expand Down
87 changes: 0 additions & 87 deletions Examples/CSharp/ImageGetBuffer.cs

This file was deleted.

117 changes: 117 additions & 0 deletions Examples/CSharp/ImageGetSetBuffer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*=========================================================================
*
* Copyright Insight Software Consortium
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/

// Get and Set Buffer of SimpleITK Image

using System;
using System.Runtime.InteropServices;

using SitkImage = itk.simple.Image;
using PixelId = itk.simple.PixelIDValueEnum;

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];

// Read input image
SitkImage input = SimpleITK.ReadImage(inputFilename);

// Cast so we know the the pixel type
input = SimpleITK.Cast(input, PixelId.sitkFloat32);

// calculate the number of pixels
VectorUInt32 size = input.GetSize();
int len = 1;
for (int dim = 0; dim < input.GetDimension(); dim++)
{
len *= (int)size[dim];
}
IntPtr buffer = input.GetBufferAsFloat();

// There are two ways to access the buffer:

// (1) Access the underlying buffer as a pointer in an "unsafe" block
// (note that in C# "unsafe" simply means that the compiler can not
// perform full type checking), and requires the -unsafe compiler flag
// unsafe {
// float* bufferPtr = (float*)buffer.ToPointer();

// // Now the byte pointer can be accessed as per Brad's email
// // (of course this example is only a 2d single channel image):
// // This is a 1-D array but can be access as a 3-D. Given an
// // image of size [xS,yS,zS], you can access the image at
// // index [x,y,z] as you wish by image[x+y*xS+z*xS*yS],
// // so x is the fastest axis and z is the slowest.
// for (int j = 0; j < size[1]; j++) {
// for (int i = 0; i < size[0]; i++) {
// float pixel = bufferPtr[i + j*size[1]];
// // Do something with pixel here
// }
// }
// }

// (2) Copy the buffer to a "safe" array (i.e. a fully typed array)
// (note that this means memory is duplicated)
float[] bufferAsArray = new float[len]; // Allocates new memory the size of input
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);

// Set buffer of new SimpleITK Image from managed array.
// bufferAsArray could also have come from a bmp,png,etc...

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]));

//
// Write out the resulting file
//
outImage = SimpleITK.RescaleIntensity(outImage, 0, 255);
outImage = SimpleITK.Cast(outImage, PixelId.sitkUInt8);

SimpleITK.WriteImage(outImage, outputFilename);


}
}
}

0 comments on commit 6850980

Please sign in to comment.