public static I <byte> ToU8(this I <float> image) { PixelFormat targetFormat; if (image.Format.ChannelCount == 1) { targetFormat = new PixelFormat(PixelType.U8C1, image.Format.PixelChannels, typeof(byte), new Range <double>[] { new Range <double>(0, Byte.MaxValue) }, image.Format.ColorSpace); } else if (image.Format.ChannelCount == 3) { targetFormat = new PixelFormat(PixelType.U8C3, image.Format.PixelChannels, typeof(byte), new Range <double>[] { new Range <double>(0, Byte.MaxValue), new Range <double>(0, Byte.MaxValue), new Range <double>(0, Byte.MaxValue) }, image.Format.ColorSpace); } else if (image.Format.ChannelCount == 4) { targetFormat = new PixelFormat(PixelType.U8C4, image.Format.PixelChannels, typeof(byte), new Range <double>[] { new Range <double>(0, Byte.MaxValue), new Range <double>(0, Byte.MaxValue), new Range <double>(0, Byte.MaxValue), new Range <double>(0, Byte.MaxValue) }, image.Format.ColorSpace); } else { throw new Exception("Source pixel fomat not supported"); } var r = new I <byte>(targetFormat, image.Height, image.Width, image.Channels); float[] src = image.Data.Buffer; byte[] dst = r.Data.Buffer; for (int i = 0; i < dst.Length; ++i) { dst[i] = (byte)(Range.Saturate(src[i]) * 255); } return(r); }
public static I <float> ToRgbF32(this I <Rgb24> image) { var pixelFormat = new PixelFormat(PixelFormat.RgbF32.PixelType, PixelFormat.RgbF32.PixelChannels, PixelFormat.RgbF32.ElementType, PixelFormat.RgbF32.ChannelRanges, image.Format.ColorSpace); var r = new I <float>(pixelFormat, image.Height, image.Width, 3); var src = image.Data.Buffer; var dst = r.Data.Buffer; for (int i = 0, j = 0; i < src.Length; i += 1, j += 3) { var p = src[i]; dst[j + 0] = Range.Saturate(p.R / 255.0f); dst[j + 1] = Range.Saturate(p.G / 255.0f); dst[j + 2] = Range.Saturate(p.B / 255.0f); } return(r); }
public static I <float> ToF32(this I <byte> image) { var ranges = new Range <double> [image.Channels]; for (int i = 0; i < image.Channels; ++i) { ranges[i] = Range.Unit; } var pixelFormat = new PixelFormat((PixelType)((int)ChannelType.F32 | image.Channels), image.Format.PixelChannels, typeof(float), ranges, image.Format.ColorSpace); var r = new I <float>(pixelFormat, image.Height, image.Width, image.Channels); var src = image.Data.Buffer; var dst = r.Data.Buffer; for (int i = 0; i < src.Length; i += 1) { dst[i] = Range.Saturate(src[i] / 255.0f); } return(r); }