public static ImageDecoderEnumerator CreateDecoderEnumerator(
            IImagingConfig imagingConfig,
            Stream stream,
            DecoderOptions?decoderOptions       = null,
            CancellationToken cancellationToken = default)
        {
            if (imagingConfig == null)
            {
                throw new ArgumentNullException(nameof(imagingConfig));
            }

            var           prefixStream = imagingConfig.CreateStreamWithHeaderPrefix(stream);
            Memory <byte> prefix       = prefixStream.GetPrefix(cancellationToken);
            ImageFormat?  format       = DetectFormat(imagingConfig, prefix.Span);

            if (format == null)
            {
                if (prefix.Length == 0)
                {
                    throw new UnknownImageFormatException(
                              "No bytes were read from the stream.");
                }
                else
                {
                    throw new UnknownImageFormatException(
                              $"Failed to recognize any format from the first {prefix.Length} bytes.");
                }
            }

            IImageDecoder decoder = imagingConfig.CreateDecoder(prefixStream, format, decoderOptions);

            return(new ImageDecoderEnumerator(decoder, cancellationToken));
        }
Esempio n. 2
0
        public static Image?Load(
            IImagingConfig config,
            Stream stream,
            VectorType?preferredPixelType = null,
            DecoderOptions?decoderOptions = null,
            ImagingProgressCallback <IImageDecoder>?onProgress = null,
            CancellationToken cancellationToken = default)
        {
            using ImageDecoderEnumerator frames = CreateDecoderEnumerator(
                      config, stream, decoderOptions, cancellationToken);

            frames.Decoder.TargetPixelType = preferredPixelType;

            if (onProgress != null && frames.Decoder is IProgressReportingCoder <IImageDecoder> progressReporter)
            {
                progressReporter.Progress += onProgress;
            }

            if (frames.MoveNext())
            {
                return(frames.Current);
            }

            return(null);
        }
Esempio n. 3
0
        public static PrefixedStream CreateStreamWithHeaderPrefix(
            this IImagingConfig config, Stream stream)
        {
            int readAhead = GetMaxHeaderSize(config);

            return(new PrefixedStream(stream, readAhead, leaveOpen: true));
        }
 public ImageFormat?DetectFormat(IImagingConfig config, ReadOnlySpan <byte> header)
 {
     if (TestFormat(config, header))
     {
         return(Format);
     }
     return(null);
 }
Esempio n. 5
0
 public static IImageInfoDetector GetInfoDetector(this IImagingConfig config, ImageFormat format)
 {
     if (!TryGetInfoDetector(config, format, out var decoder))
     {
         throw new MissingDecoderException(format);
     }
     return(decoder);
 }
        public StbImageEncoderBase(
            IImagingConfig config, Stream stream, TOptions?encoderOptions)
        {
            ImagingConfig  = config ?? throw new ArgumentNullException(nameof(config));
            EncoderOptions = encoderOptions ?? new();

            byte[] buffer = RecyclableMemoryManager.Default.GetBlock();
            Writer = new ImageBinWriter(stream, buffer);
        }
Esempio n. 7
0
 public static T GetModule <T>(this IImagingConfig config)
     where T : class
 {
     if (!config.TryGetModule <T>(out T? module) || module == null)
     {
         throw new MissingImagingModuleException(typeof(T));
     }
     return(module);
 }
Esempio n. 8
0
 public static Image <TPixel>?Load <TPixel>(
     IImagingConfig config,
     string filePath,
     DecoderOptions?decoderOptions = null,
     ImagingProgressCallback <IImageDecoder>?onProgress = null,
     CancellationToken cancellationToken = default)
     where TPixel : unmanaged, IPixel <TPixel>
 {
     using var fs = File.OpenRead(filePath);
     return(Load <TPixel>(config, fs, decoderOptions, onProgress, cancellationToken));
 }
Esempio n. 9
0
 public static Image?Load(
     IImagingConfig config,
     string filePath,
     VectorType?preferredPixelType = null,
     DecoderOptions?decoderOptions = null,
     ImagingProgressCallback <IImageDecoder>?onProgress = null,
     CancellationToken cancellationToken = default)
 {
     using var fs = File.OpenRead(filePath);
     return(Load(config, fs, preferredPixelType, decoderOptions, onProgress, cancellationToken));
 }
Esempio n. 10
0
        public static IEnumerable <IImageFormatDetector> GetFormatDetectors(this IImagingConfig config)
        {
            if (config == null)
            {
                throw new ArgumentNullException(nameof(config));
            }

            var provider = config.GetModule <ImagingInstanceProvider <IImageFormatDetector> >();

            return(provider.Values);
        }
Esempio n. 11
0
        public static int GetMaxHeaderSize(this IImagingConfig config)
        {
            if (config == null)
            {
                throw new ArgumentNullException(nameof(config));
            }

            var provider = config.GetModule <ImagingInstanceProvider <IImageFormatDetector> >();

            return(provider.GetMaxHeaderSize());
        }
Esempio n. 12
0
 public static IImageDecoder CreateDecoder(
     this IImagingConfig config,
     Stream stream,
     ImageFormat format,
     DecoderOptions?decoderOptions)
 {
     if (!TryCreateDecoder(config, stream, format, decoderOptions, out var decoder))
     {
         throw new MissingDecoderException(format);
     }
     return(decoder);
 }
Esempio n. 13
0
 public static IImageEncoder CreateEncoder(
     this IImagingConfig config,
     Stream stream,
     ImageFormat format,
     EncoderOptions?encoderOptions)
 {
     if (!TryCreateEncoder(config, stream, format, encoderOptions, out var encoder))
     {
         throw new MissingEncoderException(format);
     }
     return(encoder);
 }
        private ImageInfo Identify(IImagingConfig config, ImageBinReader reader)
        {
            var info      = GetInfo(config, reader);
            var readState = info.ReadState;

            int comp        = readState.Components;
            int bitsPerComp = readState.Depth;

            var vectorType = TryGetVectorType(comp, bitsPerComp);
            var compInfo   = vectorType.Type?.ComponentInfo ?? new VectorComponentInfo(new VectorComponent(
                                                                                           VectorComponentType.Undefined, VectorComponentChannel.Raw, comp * bitsPerComp));

            return(new ImageInfo(readState.Width, readState.Height, compInfo, Format));
        }
Esempio n. 15
0
        public static bool TryGetInfoDetector(
            this IImagingConfig config,
            ImageFormat format,
            [MaybeNullWhen(false)] out IImageInfoDetector infoDetector)
        {
            if (config == null)
            {
                throw new ArgumentNullException(nameof(config));
            }

            var provider = config.GetModule <ImagingInstanceProvider <IImageInfoDetector> >();

            return(provider.TryGetValue(format, out infoDetector));
        }
Esempio n. 16
0
        public static Image <TPixel>?Load <TPixel>(
            IImagingConfig config,
            Stream stream,
            DecoderOptions?decoderOptions = null,
            ImagingProgressCallback <IImageDecoder>?onProgress = null,
            CancellationToken cancellationToken = default)
            where TPixel : unmanaged, IPixel <TPixel>
        {
            var preferredType = VectorType.Get <TPixel>();

            var image = Load(
                config, stream, preferredType, decoderOptions, onProgress, cancellationToken);

            return((Image <TPixel>?)image);
        }
Esempio n. 17
0
        public static void Save(
            this IEnumerable <IReadOnlyPixelRows> images,
            IImagingConfig imagingConfig,
            Stream output,
            ImageFormat format,
            EncoderOptions?encoderOptions = null,
            ImagingProgressCallback <IImageEncoder>?onProgress = null,
            CancellationToken cancellationToken = default)
        {
            if (images == null)
            {
                throw new ArgumentNullException(nameof(images));
            }
            if (imagingConfig == null)
            {
                throw new ArgumentNullException(nameof(imagingConfig));
            }
            if (format == null)
            {
                throw new ArgumentNullException(nameof(format));
            }
            if (output == null)
            {
                throw new ArgumentNullException(nameof(output));
            }

            if (!output.CanWrite)
            {
                throw new ArgumentException("The stream is not writable.", nameof(output));
            }

            using IImageEncoder encoder = imagingConfig.CreateEncoder(output, format, encoderOptions);

            if (onProgress != null && encoder is IProgressReportingCoder <IImageEncoder> progressReporter)
            {
                progressReporter.Progress += onProgress;
            }

            foreach (IReadOnlyPixelRows image in images)
            {
                if (!encoder.CanEncodeImage(image))
                {
                    break;
                }

                encoder.Encode(image, cancellationToken);
            }
        }
        public ImageInfo Identify(
            IImagingConfig config, Stream stream, CancellationToken cancellationToken = default)
        {
            var buffer = RecyclableMemoryManager.Default.GetBlock();

            try
            {
                using var reader         = new ImageBinReader(stream, buffer);
                reader.CancellationToken = cancellationToken;
                return(Identify(config, reader));
            }
            finally
            {
                RecyclableMemoryManager.Default.ReturnBlock(buffer);
            }
        }
        public StbImageDecoderBase(
            IImagingConfig config, Stream stream, TOptions?decoderOptions)
        {
            ImagingConfig  = config ?? throw new ArgumentNullException(nameof(config));
            DecoderOptions = decoderOptions ?? new();

            byte[] buffer = RecyclableMemoryManager.Default.GetBlock();
            Reader = new ImageBinReader(stream, buffer);

            ReadState = new ReadState()
            {
                StateReadyCallback      = OnStateReady,
                OutputPixelLineCallback = OnOutputPixelLine,
                OutputPixelCallback     = OnOutputPixel
            };
        }
        public static ImageInfo Identify(
            IImagingConfig config, Stream stream, CancellationToken cancellationToken = default)
        {
            using var prefixedStream = config.CreateStreamWithHeaderPrefix(stream);
            Memory <byte> prefix = prefixedStream.GetPrefix(cancellationToken);

            ImageFormat?format = DetectFormat(config, prefix.Span);

            if (format == null)
            {
                throw new UnknownImageFormatException();
            }

            var infoDetector = config.GetInfoDetector(format);

            return(infoDetector.Identify(config, prefixedStream, cancellationToken));
        }
Esempio n. 21
0
        public static void Save(
            this IReadOnlyPixelRows image,
            IImagingConfig imagingConfig,
            string filePath,
            ImageFormat?format            = null,
            EncoderOptions?encoderOptions = null,
            ImagingProgressCallback <IImageEncoder>?onProgress = null,
            CancellationToken cancellationToken = default)
        {
            if (format == null)
            {
                format = ImageFormat.GetByPath(filePath)[0];
            }

            Save(
                new[] { image }, imagingConfig, filePath, format,
                encoderOptions, onProgress, cancellationToken);
        }
Esempio n. 22
0
        public static void Save(
            this IReadOnlyPixelRows image,
            IImagingConfig imagingConfig,
            Stream output,
            ImageFormat format,
            EncoderOptions?encoderOptions = null,
            ImagingProgressCallback <IImageEncoder>?onProgress = null,
            CancellationToken cancellationToken = default)
        {
            if (image == null)
            {
                throw new ArgumentNullException(nameof(image));
            }

            Save(
                new[] { image }, imagingConfig, output, format,
                encoderOptions, onProgress, cancellationToken);
        }
Esempio n. 23
0
        public static void Save(
            this IEnumerable <IReadOnlyPixelRows> images,
            IImagingConfig imagingConfig,
            string filePath,
            ImageFormat?format            = null,
            EncoderOptions?encoderOptions = null,
            ImagingProgressCallback <IImageEncoder>?onProgress = null,
            CancellationToken cancellationToken = default)
        {
            if (format == null)
            {
                format = ImageFormat.GetByPath(filePath)[0];
            }

            using var output = OpenWriteStream(filePath);
            Save(
                images, imagingConfig, output, format,
                encoderOptions, onProgress, cancellationToken);
        }
        public static ImageFormat?DetectFormat(IImagingConfig config, ReadOnlySpan <byte> header)
        {
            if (config == null)
            {
                throw new ArgumentNullException(nameof(config));
            }

            foreach (IImageFormatDetector formatDetector in config.GetFormatDetectors())
            {
                if (header.Length < formatDetector.HeaderSize)
                {
                    continue;
                }

                ImageFormat?format = formatDetector.DetectFormat(config, header);
                if (format != null)
                {
                    return(format);
                }
            }
            return(null);
        }
Esempio n. 25
0
        public static bool TryCreateDecoder(
            this IImagingConfig config,
            Stream stream,
            ImageFormat format,
            DecoderOptions?decoderOptions,
            [MaybeNullWhen(false)] out IImageDecoder decoder)
        {
            if (config == null)
            {
                throw new ArgumentNullException(nameof(config));
            }
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            var provider = config.GetModule <ImageCoderProvider <IImageDecoder> >();
            var factory  = provider.GetFactory(format);

            decoder = factory.Invoke(stream, decoderOptions) ?? throw new ImagingException("Coder factory returned null.");
            return(true);
        }
 public PngImageDecoder(IImagingConfig config, Stream stream, DecoderOptions?decoderOptions) :
     base(config, stream, decoderOptions)
 {
 }
Esempio n. 27
0
 public GifImageEncoder(IImagingConfig config, Stream stream, EncoderOptions?encoderOptions) :
     base(config, stream, encoderOptions)
 {
 }
Esempio n. 28
0
 protected override bool TestFormat(IImagingConfig config, ReadOnlySpan <byte> header)
 {
     return(StbSharp.ImageRead.Jpeg.Test(header));
 }
 public JpegImageEncoder(IImagingConfig config, Stream stream, JpegEncoderOptions?encoderOptions) :
     base(config, stream, encoderOptions)
 {
 }
 public PixelRowsContext(IImagingConfig imagingConfig, IPixelRows pixels) :
     base(imagingConfig, pixels)
 {
 }