/// <summary> /// Divide by a scaled 16-bit stack pixel-wise /// </summary> /// <param name="ims">The stack to divide by</param> /// <param name="outMax">65535 will be assigned to this value</param> public void Divide(ImageStack16 ims, float outMax = ushort.MaxValue) { DisposeGuard(); if (ims.IsDisposed) { throw new ArgumentException("Can't divide by disposed image"); } if (!IsCompatible(ims)) { throw new ArgumentException("Given image has wrong dimensions or z versus t ordering"); } for (int z = 0; z < ZPlanes; z++) { for (int t = 0; t < TimePoints; t++) { for (int y = 0; y < ImageHeight; y++) { for (int x = 0; x < ImageWidth; x++) { *this[x, y, z, t] /= (float)*ims[x, y, z, t] / ushort.MaxValue * outMax; } } } } }
/// <summary> /// Constructs a new floating point image stack with a 16 bit stack as /// a source, optionally rescaling the maximum /// </summary> /// <param name="ims">The 8bit source stack</param> /// <param name="outMax">65535 will be assigned to this value in the float stack</param> public ImageStack32F(ImageStack16 ims, float outMax = ushort.MaxValue) { if (ims == null) { throw new ArgumentNullException(nameof(ims)); } if (ims.IsDisposed) { throw new ArgumentException("Can't copy disposed stack"); } //initialize buffer and dimension properties according to source stack InitializeImageBuffer(ims.ImageWidth, ims.ImageHeight, ims.ZPlanes, ims.TimePoints, 4); //loop over pixels, assigning values for (int z = 0; z < ZPlanes; z++) { for (int t = 0; t < TimePoints; t++) { for (int y = 0; y < ImageHeight; y++) { for (int x = 0; x < ImageWidth; x++) { float temp = *ims[x, y, z, t]; temp = temp / ushort.MaxValue * outMax; if (temp > outMax) { temp = outMax; } *this[x, y, z, t] = temp; } } } } }
/// <summary> /// Copy constructor /// </summary> /// <param name="ims">The image to copy</param> public ImageStack16(ImageStack16 ims) { if (ims == null) throw new ArgumentNullException(nameof(ims)); if (ims.IsDisposed) throw new ArgumentException("Can't copy disposed stack"); InitializeAsCopy(ims); }
/// <summary> /// Performs pixel-by-pixel subtraction of the given image /// stack from the current stack clipping at 0 /// </summary> /// <param name="ims">The stack to subtract</param> public void Subtract(ImageStack16 ims) { DisposeGuard(); if (ims.IsDisposed) { throw new ArgumentException("Can't add disposed image"); } if (!IsCompatible(ims)) { throw new ArgumentException("Given image has wrong dimensions or z versus t ordering"); } if (this.Stride == ims.Stride) { long intIter = ImageNB / 4; uint *iData = (uint *)ImageData; uint *iSub = (uint *)ims.ImageData; for (long i = 0; i < intIter; i++) { iData[i] = SubShortAsUint(iData[i], iSub[i]); } //For all images we create, we expect the following to be 0 because of the 4-byte aligned stride int restIter = (int)(ImageNB % 4) / 2; System.Diagnostics.Debug.Assert(restIter < 2);//there can be only either 0 or 1 ushort left! for (long i = ImageNB / 2 - restIter; i < ImageNB / 2; i++) { ushort prev = ImageData[i]; ImageData[i] -= ims.ImageData[i]; if (ImageData[i] > prev) { ImageData[i] = 0; } } } else { for (int t = 0; t < TimePoints; t++) { for (int z = 0; z < ZPlanes; z++) { for (int y = 0; y < ImageHeight; y++) { for (int x = 0; x < ImageWidth; x++) { ushort *pixel = this[x, y, z, t]; ushort prev = *pixel; * pixel -= *ims[x, y, z, t]; if (*pixel > prev)//indicates that wrap-around occured { *pixel = ushort.MinValue; } } } } } } }
/// <summary> /// Compares every pixel in an image to a given value /// </summary> /// <param name="value">The value each pixel should have</param> /// <param name="image">The image to compare</param> private void CompareValImage(ushort value, ImageStack16 image) { ushort* imStart = image.ImageData; //only compare outside of stride padding for (long i = 0; i < image.ImageNB / 2; i++) { if (i % image.Stride < image.ImageWidth) Assert.AreEqual(value, imStart[i], "Found non-matching pixel at position {0}", i); } }
/// <summary> /// Copy constructor /// </summary> /// <param name="ims">The image to copy</param> public ImageStack16(ImageStack16 ims) { if (ims == null) { throw new ArgumentNullException(nameof(ims)); } if (ims.IsDisposed) { throw new ArgumentException("Can't copy disposed stack"); } InitializeAsCopy(ims); }
public void ShallowConstruction_OddStride() { ushort* buffer = (ushort*)Marshal.AllocHGlobal(51); try { var ims = new ImageStack16(buffer, 51, 51, 1, 1, 1, ImageStack.SliceOrders.TBeforeZ); } finally { Marshal.FreeHGlobal((IntPtr)buffer); } }
public void Construction_WithValidArguments_DimCorrect() { int w = 20; int h = 30; int z = 40; int t = 50; var ims = new ImageStack16(w, h, z, t, ImageStack.SliceOrders.TBeforeZ); Assert.AreEqual(ims.ImageWidth, w, "Image width not correct."); Assert.AreEqual(ims.ImageHeight, h, "Image height not correct."); Assert.AreEqual(ims.ZPlanes, z, "Image z plane number not correct."); Assert.AreEqual(ims.TimePoints, t, "Number of timepoints not correct."); ims.Dispose(); }
/// <summary> /// Performs pixel-by-pixel division of the given image /// stack to the current stack clipping at 255 /// </summary> /// <param name="ims">The stack to divide by element-wise</param> public void Divide(ImageStack16 ims) { DisposeGuard(); if (ims.IsDisposed) { throw new ArgumentException("Can't add disposed image"); } if (!IsCompatible(ims)) { throw new ArgumentException("Given image has wrong dimensions or z versus t ordering"); } //if the strides of the two images aren't equal (pixels not aligned in memory) we have //to laboriously loop over individual pixels otherwise we can move through the buffer in 32bit blocks //for all images created using this code stride should always be the same if the width is the same //but we could be dealing with a foreign memory block via shallow copy if (this.Stride == ims.Stride) { long intIter = ImageNB / 4; uint *iData = (uint *)ImageData; uint *iDiv = (uint *)ims.ImageData; for (long i = 0; i < intIter; i++) { iData[i] = DivShortAsUint(iData[i], iDiv[i]); } //For all images we create, we expect the following to be 0 because of the 4-byte aligned stride int restIter = (int)(ImageNB % 4) / 2; System.Diagnostics.Debug.Assert(restIter < 2);//there can be only either 0 or 1 ushort left! for (long i = ImageNB / 2 - restIter; i < ImageNB / 2; i++) { ImageData[i] /= ims.ImageData[i]; } } else { for (int z = 0; z < ZPlanes; z++) { for (int t = 0; t < TimePoints; t++) { for (int y = 0; y < ImageHeight; y++) { for (int x = 0; x < ImageWidth; x++) { *this[x, y, z, t] /= *ims[x, y, z, t];//no chance of roll-over on this division } } } } } }
/// <summary> /// Constructs an 8bit stack via rescaling of a 16bit stack /// </summary> /// <param name="ims">The source stack to be copied</param> /// <param name="min">This value and below will be set to 0</param> /// <param name="max">This value and above will be set to 255</param> public ImageStack8(ImageStack16 ims, ushort min = ushort.MinValue, ushort max = ushort.MaxValue) { if (ims == null) { throw new ArgumentNullException(nameof(ims)); } if (ims.IsDisposed) { throw new ArgumentException("Can't copy disposed stack"); } if (max <= min) { throw new ArgumentException("max has to be strictly greater than min"); } SliceOrder = ims.SliceOrder; //initialize buffer and dimension properties according to source stack InitializeImageBuffer(ims.ImageWidth, ims.ImageHeight, ims.ZPlanes, ims.TimePoints, 1); //loop over pixels, assigning values for (int z = 0; z < ZPlanes; z++) { for (int t = 0; t < TimePoints; t++) { for (int y = 0; y < ImageHeight; y++) { for (int x = 0; x < ImageWidth; x++) { float temp = *ims[x, y, z, t]; temp = (temp - min) / (max - min) * byte.MaxValue; if (temp < 0) { temp = 0; } else if (temp > byte.MaxValue) { temp = byte.MaxValue; } *this[x, y, z, t] = (byte)temp; } } } } }
public void Downscale_Construction_Correct() { var rnd = new Random(); ImageStack16 source = new ImageStack16(50, 50, 50, 50, ImageStack.SliceOrders.TBeforeZ); //quickly fill image with random values int* buffer = (int*)source.ImageData; long iter = source.ImageNB / 4; for (long i = 0; i < iter; i++) { buffer[i] = rnd.Next(); } ushort rangeMin = 10; ushort rangeMax = 1000; ImageStack8 ims8 = new ImageStack8(source, rangeMin, rangeMax); Assert.AreEqual(source.SliceOrder, ims8.SliceOrder); for (int z = 0; z < source.ZPlanes; z++) for (int t = 0; t < source.TimePoints; t++) for (int y = 0; y < source.ImageHeight; y++) for (int x = 0; x < source.ImageWidth; x++) { float pixel = *source[x, y, z, t]; pixel = pixel - rangeMin; pixel = pixel / (rangeMax - rangeMin) * byte.MaxValue; pixel = pixel < 0 ? 0 : pixel; pixel = pixel > byte.MaxValue ? byte.MaxValue : pixel; Assert.AreEqual((byte)pixel, *ims8[x, y, z, t]); } source.Dispose(); ims8.Dispose(); }
/// <summary> /// Performs pixel-by-pixel subtraction of the given image /// stack from the current stack clipping at 0 /// </summary> /// <param name="ims">The stack to subtract</param> public void Subtract(ImageStack16 ims) { DisposeGuard(); if (ims.IsDisposed) throw new ArgumentException("Can't add disposed image"); if (!IsCompatible(ims)) throw new ArgumentException("Given image has wrong dimensions or z versus t ordering"); if (this.Stride == ims.Stride) { long intIter = ImageNB / 4; uint* iData = (uint*)ImageData; uint* iSub = (uint*)ims.ImageData; for (long i = 0; i < intIter; i++) { iData[i] = SubShortAsUint(iData[i], iSub[i]); } //For all images we create, we expect the following to be 0 because of the 4-byte aligned stride int restIter = (int)(ImageNB % 4) / 2; System.Diagnostics.Debug.Assert(restIter < 2);//there can be only either 0 or 1 ushort left! for (long i = ImageNB / 2 - restIter; i < ImageNB / 2; i++) { ushort prev = ImageData[i]; ImageData[i] -= ims.ImageData[i]; if (ImageData[i] > prev) ImageData[i] = 0; } } else { for (int t = 0; t < TimePoints; t++) for (int z = 0; z < ZPlanes; z++) for (int y = 0; y < ImageHeight; y++) for (int x = 0; x < ImageWidth; x++) { ushort* pixel = this[x, y, z, t]; ushort prev = *pixel; *pixel -= *ims[x, y, z, t]; if (*pixel > prev)//indicates that wrap-around occured *pixel = ushort.MinValue; } } }
/// <summary> /// Performs pixel-by-pixel division of the given image /// stack to the current stack clipping at 255 /// </summary> /// <param name="ims">The stack to divide by element-wise</param> public void Divide(ImageStack16 ims) { DisposeGuard(); if (ims.IsDisposed) throw new ArgumentException("Can't add disposed image"); if (!IsCompatible(ims)) throw new ArgumentException("Given image has wrong dimensions or z versus t ordering"); //if the strides of the two images aren't equal (pixels not aligned in memory) we have //to laboriously loop over individual pixels otherwise we can move through the buffer in 32bit blocks //for all images created using this code stride should always be the same if the width is the same //but we could be dealing with a foreign memory block via shallow copy if (this.Stride == ims.Stride) { long intIter = ImageNB / 4; uint* iData = (uint*)ImageData; uint* iDiv = (uint*)ims.ImageData; for (long i = 0; i < intIter; i++) { iData[i] = DivShortAsUint(iData[i], iDiv[i]); } //For all images we create, we expect the following to be 0 because of the 4-byte aligned stride int restIter = (int)(ImageNB % 4) / 2; System.Diagnostics.Debug.Assert(restIter < 2);//there can be only either 0 or 1 ushort left! for (long i = ImageNB / 2 - restIter; i < ImageNB / 2; i++) { ImageData[i] /= ims.ImageData[i]; } } else { for (int z = 0; z < ZPlanes; z++) for (int t = 0; t < TimePoints; t++) for (int y = 0; y < ImageHeight; y++) for (int x = 0; x < ImageWidth; x++) { *this[x, y, z, t] /= *ims[x, y, z, t];//no chance of roll-over on this division } } }
public void From16bit_Constructor_Correct() { Random rnd = new Random(); var ims16 = new ImageStack16(43, 43, 41, 41, ImageStack.SliceOrders.ZBeforeT); //quickly fill image with random values int* buffer = (int*)ims16.ImageData; long iter = ims16.ImageNB / 4; for (long i = 0; i < iter; i++) { buffer[i] = rnd.Next(); } var ims32 = new ImageStack32F(ims16); Assert.AreEqual(ims16.SliceOrder, ims32.SliceOrder); for (int z = 0; z < ims16.ZPlanes; z++) for (int t = 0; t < ims16.TimePoints; t++) for (int y = 0; y < ims16.ImageHeight; y++) for (int x = 0; x < ims16.ImageWidth; x++) Assert.AreEqual(*ims16[x, y, z, t], *ims32[x, y, z, t], *ims16[x, y, z, t] / 1000); ims16.Dispose(); ims32.Dispose(); }
/// <summary> /// Performs pixel-by-pixel addition of the given image /// stack to the current stack clipping at 65535 /// </summary> /// <param name="ims">The stack to add</param> public void Add(ImageStack16 ims) { DisposeGuard(); if (ims.IsDisposed) throw new ArgumentException("Can't add disposed image"); //NOTE: Not clear whether we should require same z/t ordering for compatibility if (!IsCompatible(ims)) throw new ArgumentException("Given image has wrong dimensions or z versus t ordering"); //if the strides of the two images aren't equal (pixels not aligned in memory) we have //to laboriously loop over individual pixels otherwise we can move through the buffer in 32bit blocks //for all images created using this code stride should always be the same if the width is the same //but we could be dealing with a foreign memory block via shallow copy if (this.Stride == ims.Stride) { long intIter = ImageNB / 4; uint* iData = (uint*)ImageData; uint* iAdd = (uint*)ims.ImageData; for (long i = 0; i < intIter; i++) { iData[i] = AddShortAsUint(iData[i], iAdd[i]); } //For all images we create, we expect the following to be 0 because of the 4-byte aligned stride int restIter = (int)(ImageNB % 4) / 2; System.Diagnostics.Debug.Assert(restIter < 2);//there can be only either 0 or 1 ushort left! for (long i = ImageNB / 2 - restIter; i < ImageNB / 2; i++) { ushort prev = ImageData[i]; ImageData[i] += ims.ImageData[i]; if (ImageData[i] < prev) ImageData[i] = ushort.MaxValue; } } else { for (int z = 0; z < ZPlanes; z++) for (int t = 0; t < TimePoints; t++) for (int y = 0; y < ImageHeight; y++) for (int x = 0; x < ImageWidth; x++) { ushort* pixel = this[x, y, z, t]; ushort prev = *pixel; *pixel += *ims[x, y, z, t]; if (*pixel < prev)//indicates that wrap-around occured *pixel = ushort.MaxValue; } } }
public void CopyConstructor_Correct() { var ims = CreateDefaultStack(); ims.SetAll(3337); var copy = new ImageStack16(ims); Assert.IsFalse(ims.ImageData == copy.ImageData, "Source and its copy point to the same buffer"); ushort* sourceStart = ims.ImageData; ushort* copyStart = copy.ImageData; for (long i = 0; i < ims.ImageNB / 2; i++) Assert.AreEqual(sourceStart[i], copyStart[i], "Found non-matching pixel"); ims.Dispose(); copy.Dispose(); }
public void RangeCheck_OnPixelAccess() { var ims = new ImageStack16(5, 5, 5, 5, ImageStack.SliceOrders.ZBeforeT); var p = ims[5, 0, 0, 0]; ims.Dispose(); }
public void UpscaleConstructor_Correct() { Random rnd = new Random(); var ims8 = new ImageStack8(43, 43, 41, 41, ImageStack.SliceOrders.ZBeforeT); //quickly fill image with random values int* buffer = (int*)ims8.ImageData; long iter = ims8.ImageNB / 4; for (long i = 0; i < iter; i++) { buffer[i] = rnd.Next(); } var ims16 = new ImageStack16(ims8, true); Assert.AreEqual(ims8.SliceOrder, ims16.SliceOrder); for (int z = 0; z < ims8.ZPlanes; z++) for (int t = 0; t < ims8.TimePoints; t++) for (int y = 0; y < ims8.ImageHeight; y++) for (int x = 0; x < ims8.ImageWidth; x++) { float value = *ims8[x, y, z, t]; value = (value / 255) * ushort.MaxValue; Assert.AreEqual((ushort)Math.Floor(value), *ims16[x, y, z, t]); } ims8.Dispose(); ims16.Dispose(); }
/// <summary> /// Performs pixel-by-pixel addition of the given image /// stack to the current stack clipping at 65535 /// </summary> /// <param name="ims">The stack to add</param> public void Add(ImageStack16 ims) { DisposeGuard(); if (ims.IsDisposed) { throw new ArgumentException("Can't add disposed image"); } //NOTE: Not clear whether we should require same z/t ordering for compatibility if (!IsCompatible(ims)) { throw new ArgumentException("Given image has wrong dimensions or z versus t ordering"); } //if the strides of the two images aren't equal (pixels not aligned in memory) we have //to laboriously loop over individual pixels otherwise we can move through the buffer in 32bit blocks //for all images created using this code stride should always be the same if the width is the same //but we could be dealing with a foreign memory block via shallow copy if (this.Stride == ims.Stride) { long intIter = ImageNB / 4; uint *iData = (uint *)ImageData; uint *iAdd = (uint *)ims.ImageData; for (long i = 0; i < intIter; i++) { iData[i] = AddShortAsUint(iData[i], iAdd[i]); } //For all images we create, we expect the following to be 0 because of the 4-byte aligned stride int restIter = (int)(ImageNB % 4) / 2; System.Diagnostics.Debug.Assert(restIter < 2);//there can be only either 0 or 1 ushort left! for (long i = ImageNB / 2 - restIter; i < ImageNB / 2; i++) { ushort prev = ImageData[i]; ImageData[i] += ims.ImageData[i]; if (ImageData[i] < prev) { ImageData[i] = ushort.MaxValue; } } } else { for (int z = 0; z < ZPlanes; z++) { for (int t = 0; t < TimePoints; t++) { for (int y = 0; y < ImageHeight; y++) { for (int x = 0; x < ImageWidth; x++) { ushort *pixel = this[x, y, z, t]; ushort prev = *pixel; * pixel += *ims[x, y, z, t]; if (*pixel < prev)//indicates that wrap-around occured { *pixel = ushort.MaxValue; } } } } } } }
/// <summary> /// Constructs an 8bit stack via rescaling of a 16bit stack /// </summary> /// <param name="ims">The source stack to be copied</param> /// <param name="min">This value and below will be set to 0</param> /// <param name="max">This value and above will be set to 255</param> public ImageStack8(ImageStack16 ims, ushort min=ushort.MinValue, ushort max=ushort.MaxValue) { if (ims == null) throw new ArgumentNullException(nameof(ims)); if (ims.IsDisposed) throw new ArgumentException("Can't copy disposed stack"); if (max <= min) throw new ArgumentException("max has to be strictly greater than min"); SliceOrder = ims.SliceOrder; //initialize buffer and dimension properties according to source stack InitializeImageBuffer(ims.ImageWidth, ims.ImageHeight, ims.ZPlanes, ims.TimePoints, 1); //loop over pixels, assigning values for (int z = 0; z < ZPlanes; z++) for (int t = 0; t < TimePoints; t++) for (int y = 0; y < ImageHeight; y++) for (int x = 0; x < ImageWidth; x++) { float temp = *ims[x, y, z, t]; temp = (temp - min) / (max- min) * byte.MaxValue; if (temp < 0) temp = 0; else if (temp > byte.MaxValue) temp = byte.MaxValue; *this[x, y, z, t] = (byte)temp; } }
/// <summary> /// Constructs a new floating point image stack with a 16 bit stack as /// a source, optionally rescaling the maximum /// </summary> /// <param name="ims">The 8bit source stack</param> /// <param name="outMax">65535 will be assigned to this value in the float stack</param> public ImageStack32F(ImageStack16 ims, float outMax = ushort.MaxValue) { if (ims == null) throw new ArgumentNullException(nameof(ims)); if (ims.IsDisposed) throw new ArgumentException("Can't copy disposed stack"); //initialize buffer and dimension properties according to source stack InitializeImageBuffer(ims.ImageWidth, ims.ImageHeight, ims.ZPlanes, ims.TimePoints, 4); //loop over pixels, assigning values for (int z = 0; z < ZPlanes; z++) for (int t = 0; t < TimePoints; t++) for (int y = 0; y < ImageHeight; y++) for (int x = 0; x < ImageWidth; x++) { float temp = *ims[x, y, z, t]; temp = temp / ushort.MaxValue * outMax; if (temp > outMax) temp = outMax; *this[x, y, z, t] = temp; } }
public void Construction_WithInvalidHeight() { var ims = new ImageStack16(20, 0, 40, 50, ImageStack.SliceOrders.TBeforeZ); ims.Dispose(); }
/// <summary> /// Divide by a scaled 16-bit stack pixel-wise /// </summary> /// <param name="ims">The stack to divide by</param> /// <param name="outMax">65535 will be assigned to this value</param> public void Divide(ImageStack16 ims, float outMax = ushort.MaxValue) { DisposeGuard(); if (ims.IsDisposed) throw new ArgumentException("Can't divide by disposed image"); if (!IsCompatible(ims)) throw new ArgumentException("Given image has wrong dimensions or z versus t ordering"); for (int z = 0; z < ZPlanes; z++) for (int t = 0; t < TimePoints; t++) for (int y = 0; y < ImageHeight; y++) for (int x = 0; x < ImageWidth; x++) *this[x, y, z, t] /= (float)*ims[x, y, z, t] / ushort.MaxValue * outMax; }
public void PixelPointerNull_AfterDispose() { var ims = new ImageStack16(5, 5, 5, 5, ImageStack.SliceOrders.ZBeforeT); ims.Dispose(); Assert.IsTrue(ims[4, 0, 0, 0] == null); }