/// <summary> /// Convert byte image to float image /// </summary> /// <param name="source">image source</param> /// <param name="dest">image destination</param> public void ByteToFloat(IImage2DByte source, IImage2DFloat dest) { if (source == null) throw new ArgumentNullException("source"); if (dest == null) throw new ArgumentNullException("dest"); if ((source.Width > dest.Width) || (source.Height > dest.Height)) throw new ArgumentException("Destination image (" + dest.Width + "x" + dest.Height + ") must have at least the same size as the source image (" + source.Width + "x" + source.Height + ")"); int length = source.HostBuffer.Length; for (int i = 0; i < length; i++) dest.HostBuffer[i] = (float)source.HostBuffer[i]; dest.Normalized = false; }
/// <summary> /// Convert float image to byte image /// </summary> /// <param name="source">image source</param> /// <param name="dest">image destination</param> public void FloatToByte(IImage2DFloat source, IImage2DByte dest) { if (source == null) throw new ArgumentNullException("source"); if (dest == null) throw new ArgumentNullException("dest"); if ((source.Width > dest.Width) || (source.Height > dest.Height)) throw new ArgumentException("Destination image (" + dest.Width + "x" + dest.Height + ") must have at least the same size as the source image (" + source.Width + "x" + source.Height + ")"); if (source.Normalized) { // use normalized kernel int length = source.HostBuffer.Length; for (int i = 0; i < length; i++) { float val = 255f * source.HostBuffer[i]; if (val < 0) val = 0; else if (val > 255) val = 255; dest.HostBuffer[i] = (byte)val; } } else { // use normal kernel int length = source.HostBuffer.Length; for (int i = 0; i < length; i++) { float val = source.HostBuffer[i]; if (val < 0) val = 0; else if (val > 255) val = 255; dest.HostBuffer[i] = (byte)val; } } }
/// <summary> /// Sets a constant value to all cells in an image /// </summary> /// <param name="image">image</param> /// <param name="value">value to set</param> public void SetValue(IImage2DByte image, byte value) { if (image == null) throw new ArgumentNullException("image"); image.HostBuffer.Clear(value); }