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> /// Extracts the rectangular part of the image /// </summary> /// <param name="x">Left border of part</param> /// <param name="y">Top border of part</param> /// <param name="rectWidth">Width of part</param> /// <param name="rectHeight">Height of part</param> /// <returns>Part of image</returns> public KrecImage GetSubregion(int x, int y, int rectWidth, int rectHeight) { var sourceBytesPerLine = bytesPerLine; var bytesPerPixel = format.BytesPerPixel(); var newBytesPerLine = KrecImage.CalculateStride(rectWidth, format); var newImageData = new byte[rectHeight * newBytesPerLine]; var sourceImageData = imageData; var sourceIdxDelta = y * sourceBytesPerLine + x * bytesPerPixel; for (int rowIdx = 0, sourceIdx = sourceIdxDelta, targetIdx = 0; rowIdx < rectHeight; rowIdx++, sourceIdx += sourceBytesPerLine, targetIdx += newBytesPerLine) { Array.Copy(sourceImageData, sourceIdx, newImageData, targetIdx, rectWidth * bytesPerPixel); } return(new KrecImage(rectWidth, rectHeight, newBytesPerLine, horizontalResolution, verticalResolution, format, newImageData)); }