private static void ParseVerbatim(IImage image, BinaryParser parser) { int size = image.Width * image.Height; if (image is Image <byte> ) { var image8 = image as Image <byte>; image8.Data = new byte[size * image.Channels]; for (int channel = 0; channel < image.Channels; ++channel) { for (int i = 0; i < size; ++i) { image8.Data[channel * (i * image.Channels)] = parser.Byte(); } } } if (image is Image <short> ) { var image16 = image as Image <short>; image16.Data = new short[size * image.Channels]; for (int channel = 0; channel < image.Channels; ++channel) { for (int i = 0; i < size; ++i) { image16.Data[channel + (i * image.Channels)] = parser.Int16BE(); } } } }
public static IImage ImageFromBytes(byte[] bytes) { var parser = new BinaryParser(bytes); if (parser.Int16BE() != 474) { throw new InvalidDataException("invalid header"); } var storage = parser.Byte(); IImage image = ImageFromBytesPerPixel(parser.Byte()); if (image == null) { throw new InvalidDataException("invalid bpc"); } var dimension = parser.UInt16BE(); image.Width = parser.UInt16BE(); image.Height = parser.UInt16BE(); image.Channels = parser.UInt16BE(); if (dimension != 3) { image.Channels = 1; } parser.Position += 92; var colormap = parser.Int32BE(); if (colormap != 0) { throw new NotSupportedException("unsupported colormap"); } parser.Position += 404; switch (storage) { case 0: ParseVerbatim(image, parser); break; case 1: ParseRLE(image, parser); break; default: throw new InvalidDataException("invalid storage value"); } return(image); }