public void AsCroppedInCorrectHeight(int imageIndex) { var i = imageIndex; var cropSetup = new CropSetup { LeftPx = 27 * 16, RightPx = 29 * 16, TopPx = 1 * 7, BottomPx = 6 * 16 }; var sourceFile = Common.GetSourceImagePath(i); var image = ImageFrame.FromFile(sourceFile); Assert.Throws <ArgumentException>(() => image.AsCroppedImage(cropSetup)); }
/// <summary> /// Crops given image /// </summary> /// <param name="image">Image to crop</param> /// <param name="cropSetup">Crop setup</param> /// <returns>Cropped image</returns> public static ImageFrame AsCroppedImage(this ImageFrame image, CropSetup cropSetup) { if (!cropSetup.IsAnyCropSet()) { return(image); } var newWidthPx = image.ImageWidthPx - cropSetup.LeftPx - cropSetup.RightPx; var newHeightPx = image.ImageHeightPx - cropSetup.TopPx - cropSetup.BottomPx; var widthCheck = newWidthPx % 16; if (widthCheck != 0) { throw new ArgumentException("New width is not divisible by 16!"); } var heightCheck = newHeightPx % 16; if (heightCheck != 0) { throw new ArgumentException("New height is not divisible by 16!"); } var newImage = new byte[(newWidthPx * 3 * newHeightPx) + image.HeaderBytesLength]; //copy header from source and ... image.Image.CopyBytesTo(0, newImage, 0, image.HeaderBytesLength); //copy image bytes from source to new image var leftCropBytes = (cropSetup.LeftPx * 3); var imageWidthBytes = image.ImageWidthPx * 3; var newImageWidthBytes = (newWidthPx * 3); var destRow = 0; //read rows for (var i = cropSetup.BottomPx; i < newHeightPx + cropSetup.BottomPx; i++) { var srcOffSet = (leftCropBytes + (i * imageWidthBytes) + image.HeaderBytesLength); var destOffSet = (destRow++ *newImageWidthBytes + image.HeaderBytesLength); image.Image.CopyBytesTo(srcOffSet, newImage, destOffSet, newImageWidthBytes); } var ret = new ImageFrame(newImage); //.. adjust header with new width and size ret.SetSizeInfo((uint)newImage.Length, newWidthPx, newHeightPx); return(ret); }
public void AsHuffmanEncodedAndDecodedWithSave(int imageIndex) { var i = imageIndex; var cropSetup = new CropSetup { LeftPx = 27 * 16, RightPx = 29 * 16, TopPx = 1 * 16, BottomPx = 6 * 16 }; var sourceFile = Common.GetSourceImagePath(i); var image = ImageFrame.FromFile(sourceFile).AsCroppedImage(cropSetup).AsGrayScale(); var encodedFilename = Common.GetSaveImagePath(i, "gray-huffman-encoded"); image.AsHuffmanEncoded().Save(encodedFilename); var encoded = new HuffmanImageFrame().Open(encodedFilename); var decodedFilename = Common.GetSaveImagePath(i, "gray-huffman-decoded.bmp"); encoded.AsImageGrayScaleFrame().Save(decodedFilename); var decoded = ImageFrame.FromFile(decodedFilename); Assert.True(image.Image.Compare(decoded.Image)); }