/// <summary> /// Converts pixel format to standard System.Drawing PixelFormat enumeration /// </summary> /// <param name="pixelFormat"></param> /// <returns></returns> /// <exception cref="ArgumentOutOfRangeException"></exception> public static PixelFormat ToPixelFormat(this KrecImagePixelFormat pixelFormat) { switch (pixelFormat) { case KrecImagePixelFormat.Format1BitPerPixel: return(PixelFormat.Format1bppIndexed); case KrecImagePixelFormat.Format8BitPerPixel: return(PixelFormat.Format8bppIndexed); case KrecImagePixelFormat.Format24bppRgb: return(PixelFormat.Format24bppRgb); case KrecImagePixelFormat.Format32bppArgb: return(PixelFormat.Format32bppArgb); case KrecImagePixelFormat.Format32bppRgb: return(PixelFormat.Format32bppRgb); case KrecImagePixelFormat.Format32bppPArgb: return(PixelFormat.Format32bppArgb); default: throw new ArgumentOutOfRangeException(nameof(pixelFormat), pixelFormat, null); } }
public KrecImage(int width, int height, int bytesPerLine, float horizontalResolution, float verticalResolution, KrecImagePixelFormat pixelFormat, [NotNull] byte[] imageData) { this.width = width; this.height = height; this.bytesPerLine = bytesPerLine; this.horizontalResolution = horizontalResolution; this.verticalResolution = verticalResolution; format = pixelFormat; this.imageData = imageData; var bytesPerPixel = pixelFormat.BytesPerPixel(); if (bytesPerLine < bytesPerPixel * width) { throw new ArgumentException( string.Format("Number of bytes per line ({0}) is less than needed for the specified image width ({1}) and pixel format ({2})", bytesPerLine, width, pixelFormat)); } if (imageData.Length < (height * bytesPerLine)) { throw new ArgumentException( string.Format("Pixel data array for image is too small (size = {0}) for given bytes per line ({1}) and height ({2})", imageData.Length, bytesPerLine, height)); } }
/// <summary> /// Returns number of bytes needed to store one pixel in the specified format /// </summary> /// <param name="pixelFormat"></param> /// <returns></returns> /// <exception cref="ArgumentOutOfRangeException"></exception> public static int BytesPerPixel(this KrecImagePixelFormat pixelFormat) { switch (pixelFormat) { case KrecImagePixelFormat.Format1BitPerPixel: case KrecImagePixelFormat.Format8BitPerPixel: // intentionally pass through return(1); case KrecImagePixelFormat.Format24bppRgb: return(3); case KrecImagePixelFormat.Format32bppArgb: case KrecImagePixelFormat.Format32bppRgb: // intentionally pass through case KrecImagePixelFormat.Format32bppPArgb: // intentionally pass through return(4); default: throw new ArgumentOutOfRangeException(nameof(pixelFormat), pixelFormat, null); } }
public KrecImage(KrecImage source, byte[] newImageData, KrecImagePixelFormat newKrecImagePixelFormat) : this(source.Width, source.Height, source.BytesPerLine, source.HorizontalResolution, source.VerticalResolution, newKrecImagePixelFormat, newImageData) { }
public static int CalculateStride(int width, KrecImagePixelFormat format) { return(CalculateStride(width, format.ToPixelFormat())); }