public void TestIsMetaDataAvailable() { EncodedImage encodedImage1 = new EncodedImage(_byteBufferRef); EncodedImage encodedImage2 = new EncodedImage(_byteBufferRef); encodedImage2.RotationAngle = 1; encodedImage2.Width = 1; encodedImage2.Height = 1; Assert.IsFalse(EncodedImage.IsMetaDataAvailable(encodedImage1)); Assert.IsTrue(EncodedImage.IsMetaDataAvailable(encodedImage2)); }
internal static float DetermineDownsampleRatio( ImageRequest imageRequest, EncodedImage encodedImage) { Preconditions.CheckArgument(EncodedImage.IsMetaDataAvailable(encodedImage)); ResizeOptions resizeOptions = imageRequest.ResizeOptions; if (resizeOptions == null || resizeOptions.Height <= 0 || resizeOptions.Width <= 0 || encodedImage.Width == 0 || encodedImage.Height == 0) { return(1.0f); } int rotationAngle = GetRotationAngle(imageRequest, encodedImage); bool swapDimensions = rotationAngle == 90 || rotationAngle == 270; int widthAfterRotation = swapDimensions ? encodedImage.Height : encodedImage.Width; int heightAfterRotation = swapDimensions ? encodedImage.Width : encodedImage.Height; float widthRatio = ((float)resizeOptions.Width) / widthAfterRotation; float heightRatio = ((float)resizeOptions.Height) / heightAfterRotation; float ratio = Math.Max(widthRatio, heightRatio); Debug.Write(string.Format( "Downsample - Specified size: {0}x{1}, image size: {2}x{3} ratio: {4} x {5}, ratio: {6} for {7}", resizeOptions.Width, resizeOptions.Height, widthAfterRotation, heightAfterRotation, widthRatio, heightRatio, ratio, imageRequest.SourceUri.ToString())); return(ratio); }
/// <summary> /// Get the factor between the dimensions of the encodedImage /// (actual image) and the ones of the imageRequest /// (requested size). /// </summary> /// <param name="imageRequest"> /// The request containing the requested dimensions. /// </param> /// <param name="encodedImage"> /// The encoded image with the actual dimensions. /// </param> public static int DetermineSampleSize(ImageRequest imageRequest, EncodedImage encodedImage) { if (!EncodedImage.IsMetaDataAvailable(encodedImage)) { return(DEFAULT_SAMPLE_SIZE); } float ratio = DetermineDownsampleRatio(imageRequest, encodedImage); int sampleSize; if (encodedImage.Format == ImageFormat.JPEG) { sampleSize = RatioToSampleSizeJPEG(ratio); } else { sampleSize = RatioToSampleSize(ratio); } // Check the case when the dimension of the downsampled image // is still larger than the max possible dimension for an image. int maxDimension = Math.Max(encodedImage.Height, encodedImage.Width); while (maxDimension / sampleSize > MAX_BITMAP_SIZE) { if (encodedImage.Format == ImageFormat.JPEG) { sampleSize *= 2; } else { sampleSize++; } } return(sampleSize); }