private static void TestMetaDataImpl(
            bool useIdentify,
            IImageDecoder decoder,
            string imagePath,
            int expectedPixelSize,
            bool exifProfilePresent,
            bool iccProfilePresent)
        {
            var testFile = TestFile.Create(imagePath);

            using (var stream = new MemoryStream(testFile.Bytes, false))
            {
                IImageInfo imageInfo = useIdentify
                                           ? ((IImageInfoDetector)decoder).Identify(Configuration.Default, stream)
                                           : decoder.Decode <Rgba32>(Configuration.Default, stream);

                Assert.NotNull(imageInfo);
                Assert.NotNull(imageInfo.PixelType);

                if (useIdentify)
                {
                    Assert.Equal(expectedPixelSize, imageInfo.PixelType.BitsPerPixel);
                }
                else
                {
                    // When full Image<TPixel> decoding is performed, BitsPerPixel will match TPixel
                    int bpp32 = Unsafe.SizeOf <Rgba32>() * 8;
                    Assert.Equal(bpp32, imageInfo.PixelType.BitsPerPixel);
                }

                ExifProfile exifProfile = imageInfo.MetaData.ExifProfile;

                if (exifProfilePresent)
                {
                    Assert.NotNull(exifProfile);
                    Assert.NotEmpty(exifProfile.Values);
                }
                else
                {
                    Assert.Null(exifProfile);
                }

                IccProfile iccProfile = imageInfo.MetaData.IccProfile;

                if (iccProfilePresent)
                {
                    Assert.NotNull(iccProfile);
                    Assert.NotEmpty(iccProfile.Entries);
                }
                else
                {
                    Assert.Null(iccProfile);
                }
            }
        }
Пример #2
0
        private static void TestImageInfo(string imagePath, IImageDecoder decoder, bool useIdentify, Action <IImageInfo> test)
        {
            var testFile = TestFile.Create(imagePath);

            using (var stream = new MemoryStream(testFile.Bytes, false))
            {
                IImageInfo imageInfo = useIdentify
                                           ? ((IImageInfoDetector)decoder).Identify(Configuration.Default, stream)
                                           : decoder.Decode <Rgba32>(Configuration.Default, stream);
                test(imageInfo);
            }
        }
Пример #3
0
#pragma warning disable SA1008 // Opening parenthesis must be spaced correctly
        /// <summary>
        /// Decodes the image stream to the current image.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <param name="config">the configuration.</param>
        /// <typeparam name="TPixel">The pixel format.</typeparam>
        /// <returns>
        /// A new <see cref="Image{TPixel}"/>.
        /// </returns>
        private static Tuple <Image <TPixel>, IImageFormat> Decode <TPixel>(Stream stream, Configuration config)
#pragma warning restore SA1008 // Opening parenthesis must be spaced correctly
            where TPixel : struct, IPixel <TPixel>
        {
            IImageDecoder decoder = DiscoverDecoder(stream, config, out IImageFormat format);

            if (decoder == null)
            {
                return(default(Tuple <Image <TPixel>, IImageFormat>));
            }

            Image <TPixel> img = decoder.Decode <TPixel>(config, stream);

            return(new Tuple <Image <TPixel>, IImageFormat>(img, format));
        }
Пример #4
0
        public void Load(Stream stream, IImageDecoder decoder)
        {
            try
            {
                if (!stream.CanRead)
                {
                    throw new NotSupportedException("Cannot read from the stream.");
                }

                if (!stream.CanSeek)
                {
                    throw new NotSupportedException("The stream does not support seeking.");
                }

                int    maxHeaderSize = decoder.HeaderSize;
                byte[] header        = new byte[maxHeaderSize];

                stream.Read(header, 0, maxHeaderSize);
                stream.Position = 0;

                //var decoder = FindFirstSupport(decoders, header); //decoders.FirstOrDefault(x => x.IsSupportedFileFormat(header));
                if (decoder != null)
                {
                    decoder.Decode(this, stream);
                }



                //if (IsLoading)
                //{
                //    IsLoading = false;

                //    StringBuilder stringBuilder = new StringBuilder();
                //    stringBuilder.AppendLine("Image cannot be loaded. Available decoders:");

                //    foreach (IImageDecoder decoder in decoders)
                //    {
                //        stringBuilder.AppendLine("-" + decoder);
                //    }

                //    throw new UnsupportedImageFormatException(stringBuilder.ToString());
                //}
            }
            finally
            {
                stream.Dispose();
            }
        }
Пример #5
0
        /// <summary>
        /// This method pre-seeds the decoder and encoder for a given pixel format in the AoT compiler for iOS.
        /// </summary>
        /// <param name="decoder">The image decoder to seed.</param>
        /// <param name="encoder">The image encoder to seed.</param>
        /// <typeparam name="TPixel">The pixel format.</typeparam>
        private static void AotCodec <TPixel>(IImageDecoder decoder, IImageEncoder encoder)
            where TPixel : struct, IPixel <TPixel>
        {
            try
            {
                decoder.Decode <TPixel>(Configuration.Default, null);
            }
            catch
            {
            }

            try
            {
                encoder.Encode <TPixel>(null, null);
            }
            catch
            {
            }
        }
Пример #6
0
 /// <summary>
 /// Create a new instance of the <see cref="Image{TPixel}"/> class from the given stream.
 /// </summary>
 /// <param name="configuration">The Configuration.</param>
 /// <param name="stream">The stream containing image information.</param>
 /// <param name="decoder">The decoder.</param>
 /// <exception cref="ArgumentNullException">The configuration is null.</exception>
 /// <exception cref="ArgumentNullException">The stream is null.</exception>
 /// <exception cref="NotSupportedException">The stream is not readable.</exception>
 /// <exception cref="UnknownImageFormatException">Image format not recognised.</exception>
 /// <exception cref="InvalidImageContentException">Image contains invalid content.</exception>
 /// <typeparam name="TPixel">The pixel format.</typeparam>
 /// <returns>A new <see cref="Image{TPixel}"/>.</returns>>
 public static Image <TPixel> Load <TPixel>(Configuration configuration, Stream stream, IImageDecoder decoder)
     where TPixel : unmanaged, IPixel <TPixel>
 => WithSeekableStream(configuration, stream, s => decoder.Decode <TPixel>(configuration, s));
Пример #7
0
 /// <summary>
 /// Decode a new instance of the <see cref="Image"/> class from the given stream.
 /// The pixel format is selected by the decoder.
 /// </summary>
 /// <param name="configuration">The configuration for the decoder.</param>
 /// <param name="stream">The stream containing image information.</param>
 /// <param name="decoder">The decoder.</param>
 /// <exception cref="ArgumentNullException">The configuration is null.</exception>
 /// <exception cref="ArgumentNullException">The stream is null.</exception>
 /// <exception cref="ArgumentNullException">The decoder is null.</exception>
 /// <exception cref="NotSupportedException">The stream is not readable.</exception>
 /// <exception cref="UnknownImageFormatException">Image format not recognised.</exception>
 /// <exception cref="InvalidImageContentException">Image contains invalid content.</exception>
 /// <returns>A new <see cref="Image"/>.</returns>>
 public static Image Load(Configuration configuration, Stream stream, IImageDecoder decoder)
 {
     Guard.NotNull(decoder, nameof(decoder));
     return(WithSeekableStream(configuration, stream, s => decoder.Decode(configuration, s)));
 }
Пример #8
0
 /// <summary>
 /// Create a new instance of the <see cref="Image{TPixel}"/> class from the given stream.
 /// </summary>
 /// <param name="config">The Configuration.</param>
 /// <param name="stream">The stream containing image information.</param>
 /// <param name="decoder">The decoder.</param>
 /// <exception cref="NotSupportedException">
 /// Thrown if the stream is not readable nor seekable.
 /// </exception>
 /// <typeparam name="TPixel">The pixel format.</typeparam>
 /// <returns>A new <see cref="Image{TPixel}"/>.</returns>>
 public static Image <TPixel> Load <TPixel>(Configuration config, Stream stream, IImageDecoder decoder)
     where TPixel : struct, IPixel <TPixel>
 {
     return(WithSeekableStream(stream, s => decoder.Decode <TPixel>(config, s)));
 }
Пример #9
0
 /// <summary>
 /// Loads the image from the given stream.
 /// </summary>
 /// <typeparam name="TColor">The pixel format.</typeparam>
 /// <param name="stream">The stream containing image information.</param>
 /// <param name="decoder">The decoder.</param>
 /// <param name="options">The options for the decoder.</param>
 /// <exception cref="NotSupportedException">
 /// Thrown if the stream is not readable nor seekable.
 /// </exception>
 /// <returns>The image</returns>
 public static Image <TColor> Load <TColor>(Stream stream, IImageDecoder decoder, IDecoderOptions options)
     where TColor : struct, IPixel <TColor>
 {
     return(WithSeekableStream(stream, s => decoder.Decode <TColor>(Configuration.Default, s, options)));
 }
Пример #10
0
 /// <summary>
 /// Decode a new instance of the <see cref="Image"/> class from the given stream.
 /// The pixel format is selected by the decoder.
 /// </summary>
 /// <param name="configuration">The configuration for the decoder.</param>
 /// <param name="stream">The stream containing image information.</param>
 /// <param name="decoder">The decoder.</param>
 /// <exception cref="NotSupportedException">Thrown if the stream is not readable.</exception>
 /// <exception cref="UnknownImageFormatException">Image cannot be loaded.</exception>
 /// <returns>A new <see cref="Image"/>.</returns>>
 public static Image Load(Configuration configuration, Stream stream, IImageDecoder decoder) =>
 WithSeekableStream(configuration, stream, s => decoder.Decode(configuration, s));
Пример #11
0
 /// <summary>
 /// Create a new instance of the <see cref="Image{TPixel}"/> class from the given stream.
 /// </summary>
 /// <param name="stream">The stream containing image information.</param>
 /// <param name="decoder">The decoder.</param>
 /// <exception cref="NotSupportedException">Thrown if the stream is not readable.</exception>
 /// <typeparam name="TPixel">The pixel format.</typeparam>
 /// <returns>A new <see cref="Image{TPixel}"/>.</returns>>
 public static Image <TPixel> Load <TPixel>(Stream stream, IImageDecoder decoder)
     where TPixel : struct, IPixel <TPixel>
 => WithSeekableStream(Configuration.Default, stream, s => decoder.Decode <TPixel>(Configuration.Default, s));
Пример #12
0
        public void Load(Stream stream, IImageDecoder decoder)
        {
            try
            {
                if (!stream.CanRead)
                {
                    throw new NotSupportedException("Cannot read from the stream.");
                }

                if (!stream.CanSeek)
                {
                    throw new NotSupportedException("The stream does not support seeking.");
                }

                int maxHeaderSize = decoder.HeaderSize;
                byte[] header = new byte[maxHeaderSize];

                stream.Read(header, 0, maxHeaderSize);
                stream.Position = 0;

                //var decoder = FindFirstSupport(decoders, header); //decoders.FirstOrDefault(x => x.IsSupportedFileFormat(header));
                if (decoder != null)
                {
                    decoder.Decode(this, stream);

                }



                //if (IsLoading)
                //{
                //    IsLoading = false;

                //    StringBuilder stringBuilder = new StringBuilder();
                //    stringBuilder.AppendLine("Image cannot be loaded. Available decoders:");

                //    foreach (IImageDecoder decoder in decoders)
                //    {
                //        stringBuilder.AppendLine("-" + decoder);
                //    }

                //    throw new UnsupportedImageFormatException(stringBuilder.ToString());
                //}
            }
            finally
            {
                stream.Dispose();
            }
        }