public void TestIsJpegCompleteAt_Complete() { byte[] encodedBytes = new byte[ENCODED_BYTES_LENGTH]; encodedBytes[ENCODED_BYTES_LENGTH - 2] = JfifUtil.MARKER_FIRST_BYTE; encodedBytes[ENCODED_BYTES_LENGTH - 1] = JfifUtil.MARKER_EOI; IPooledByteBuffer buf = new TrivialPooledByteBuffer(encodedBytes); EncodedImage encodedImage = new EncodedImage( CloseableReference <IPooledByteBuffer> .of(buf, _releaser)); encodedImage.Format = ImageFormat.JPEG; Assert.IsTrue(encodedImage.IsCompleteAt(ENCODED_BYTES_LENGTH)); }
/// <summary> /// Creates a bitmap from encoded JPEG bytes. /// Supports a partial JPEG image. /// </summary> /// <param name="encodedImage"> /// The reference to the encoded image with the reference to the /// encoded bytes. /// </param> /// <param name="bitmapConfig"> /// The <see cref="BitmapPixelFormat"/> used to create the decoded /// SoftwareBitmap. /// </param> /// <param name="length"> /// The number of encoded bytes in the buffer. /// </param> /// <returns>The bitmap.</returns> /// <exception cref="OutOfMemoryException"> /// If the Bitmap cannot be allocated. /// </exception> public Task <CloseableReference <SoftwareBitmap> > DecodeJPEGFromEncodedImageAsync( EncodedImage encodedImage, BitmapPixelFormat bitmapConfig, int length) { return(_executor.Execute(async() => { bool isJpegComplete = encodedImage.IsCompleteAt(length); Stream jpegDataStream = encodedImage.GetInputStream(); // At this point the Stream from the encoded image should not // be null since in the pipeline,this comes from a call stack where // this was checked before. Also this method needs the Stream to // decode the image so this can't be null. Preconditions.CheckNotNull(jpegDataStream); if (encodedImage.Size > length) { jpegDataStream = new LimitedInputStream(jpegDataStream, length); } if (!isJpegComplete) { jpegDataStream = new TailAppendingInputStream(jpegDataStream, EOI_TAIL); } BitmapDecoder decoder = await BitmapDecoder.CreateAsync( jpegDataStream.AsRandomAccessStream()) .AsTask() .ConfigureAwait(false); SoftwareBitmap bitmap = await decoder .GetSoftwareBitmapAsync(bitmapConfig, BitmapAlphaMode.Premultiplied) .AsTask() .ConfigureAwait(false); return CloseableReference <SoftwareBitmap> .of(bitmap); }) .Unwrap()); }