/// <summary>
        /// Creates a memory-backed encoded image from the stream.
        /// The stream is closed.
        /// </summary>
        protected EncodedImage GetByteBufferBackedEncodedImage(Stream inputStream, int length)
        {
            var reference = default(CloseableReference <IPooledByteBuffer>);

            try
            {
                if (length <= 0)
                {
                    reference = CloseableReference <IPooledByteBuffer> .of(
                        _pooledByteBufferFactory.NewByteBuffer(inputStream));
                }
                else
                {
                    reference = CloseableReference <IPooledByteBuffer> .of(
                        _pooledByteBufferFactory.NewByteBuffer(inputStream, length));
                }

                return(new EncodedImage(reference));
            }
            finally
            {
                Closeables.CloseQuietly(inputStream);
                CloseableReference <IPooledByteBuffer> .CloseSafely(reference);
            }
        }
        /// <summary>
        /// Reads image header from a file indicated by provided filename and
        /// determines its format. This method does not throw IOException if
        /// one occurs. In this case, ImageFormat.UNKNOWN will be returned.
        /// </summary>
        /// <returns>ImageFormat for image stored in filename.</returns>
        public static ImageFormat GetImageFormat(string filename)
        {
            FileStream fileInputStream = null;

            try
            {
                fileInputStream = new FileStream(filename, FileMode.Open, FileAccess.Read);
                return(GetImageFormat(fileInputStream));
            }
            catch (IOException)
            {
                return(ImageFormat.UNKNOWN);
            }
            finally
            {
                Closeables.CloseQuietly(fileInputStream);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// If this is the first time calling this method, the buffer will
        /// be checked to make sure it starts with SOI marker (0xffd8).
        /// If the image has been identified as a non-JPEG, data will be
        /// ignored and false will be returned immediately on all
        /// subsequent calls.
        ///
        /// This object maintains state of the position of the last read
        /// byte. On repeated calls to this method, it will continue from
        /// where it left off.
        /// </summary>
        /// <param name="encodedImage">
        /// Next set of bytes received by the caller.
        /// </param>
        /// <returns>true if a new full scan has been found.</returns>
        public bool ParseMoreData(EncodedImage encodedImage)
        {
            if (_parserState == NOT_A_JPEG)
            {
                return(false);
            }

            int dataBufferSize = encodedImage.Size;

            // Is there any new data to parse?
            // _bytesParsed might be greater than size of dataBuffer - that
            // happens when we skip more data than is available to read
            // inside DoParseMoreData method.
            if (dataBufferSize <= _bytesParsed)
            {
                return(false);
            }

            Stream bufferedDataStream = new PooledByteArrayBufferedInputStream(
                encodedImage.GetInputStream(),
                _byteArrayPool.Get(BUFFER_SIZE),
                _byteArrayPool);

            try
            {
                StreamUtil.Skip(bufferedDataStream, _bytesParsed);
                return(DoParseMoreData(encodedImage, bufferedDataStream));
            }
            catch (IOException)
            {
                // Does not happen - streams returned by IPooledByteBuffers
                // do not throw IOExceptions.
                throw;
            }
            finally
            {
                Closeables.CloseQuietly(bufferedDataStream);
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Decodes gif into CloseableImage.
        /// </summary>
        /// <param name="encodedImage">
        /// Input image (encoded bytes plus meta data).
        /// </param>
        /// <param name="options">Decode options.</param>
        /// <returns>A CloseableImage.</returns>
        public Task <CloseableImage> DecodeGifAsync(
            EncodedImage encodedImage,
            ImageDecodeOptions options)
        {
            Stream inputStream = encodedImage.GetInputStream();

            if (inputStream == null)
            {
                return(Task.FromResult(default(CloseableImage)));
            }

            try
            {
                // Phong Cao: always forceStaticImage
                return(DecodeStaticImageAsync(encodedImage)
                       .ContinueWith(
                           task => ((CloseableImage)task.Result),
                           TaskContinuationOptions.ExecuteSynchronously));
            }
            finally
            {
                Closeables.CloseQuietly(inputStream);
            }
        }
Ejemplo n.º 5
0
            private async Task DoTransform(EncodedImage encodedImage, bool isLast)
            {
                _producerContext.Listener.OnProducerStart(_producerContext.Id, PRODUCER_NAME);
                ImageRequest imageRequest = _producerContext.ImageRequest;
                PooledByteBufferOutputStream outputStream =
                    _parent._pooledByteBufferFactory.NewOutputStream();

                IDictionary <string, string> extraMap = default(IDictionary <string, string>);
                EncodedImage ret         = default(EncodedImage);
                Stream       inputStream = default(Stream);

                try
                {
                    int numerator = GetScaleNumerator(imageRequest, encodedImage);
                    extraMap    = GetExtraMap(encodedImage, imageRequest, numerator);
                    inputStream = encodedImage.GetInputStream();
#if HAS_LIBJPEGTURBO
                    JpegTranscoder.TranscodeJpeg(
                        inputStream.AsIStream(),
                        outputStream.AsIStream(),
                        GetRotationAngle(imageRequest, encodedImage),
                        numerator,
                        DEFAULT_JPEG_QUALITY);
#else // HAS_LIBJPEGTURBO
                    inputStream.CopyTo(outputStream);
#endif // HAS_LIBJPEGTURBO

                    CloseableReference <IPooledByteBuffer> reference =
                        CloseableReference <IPooledByteBuffer> .of(outputStream.ToByteBuffer());

                    try
                    {
                        ret        = new EncodedImage(reference);
                        ret.Format = ImageFormat.JPEG;

                        try
                        {
                            await ret.ParseMetaDataAsync().ConfigureAwait(false);

                            _producerContext.Listener.OnProducerFinishWithSuccess(
                                _producerContext.Id, PRODUCER_NAME, extraMap);

                            Consumer.OnNewResult(ret, isLast);
                        }
                        finally
                        {
                            EncodedImage.CloseSafely(ret);
                        }
                    }
                    finally
                    {
                        CloseableReference <IPooledByteBuffer> .CloseSafely(reference);
                    }
                }
                catch (Exception e)
                {
                    _producerContext.Listener.OnProducerFinishWithFailure(
                        _producerContext.Id, PRODUCER_NAME, e, extraMap);

                    Consumer.OnFailure(e);
                    return;
                }
                finally
                {
                    Closeables.CloseQuietly(inputStream);
                    outputStream.Dispose();
                }
            }