Example #1
0
        /// <inheritdoc/>
        public void Encode <TColor>(Image <TColor> image, Stream stream, IEncoderOptions options)
            where TColor : struct, IPixel <TColor>
        {
            IBmpEncoderOptions bmpOptions = BmpEncoderOptions.Create(options);

            this.Encode(image, stream, bmpOptions);
        }
Example #2
0
        /// <inheritdoc/>
        public void Encode <TColor>(Image <TColor> image, Stream stream, IEncoderOptions options)
            where TColor : struct, IPixel <TColor>
        {
            IGifEncoderOptions gifOptions = GifEncoderOptions.Create(options);

            this.Encode(image, stream, gifOptions);
        }
Example #3
0
        /// <inheritdoc/>
        public void Encode <TPixel>(Image <TPixel> image, Stream stream, IEncoderOptions options)
            where TPixel : struct, IPixel <TPixel>
        {
            IJpegEncoderOptions gifOptions = JpegEncoderOptions.Create(options);

            this.Encode(image, stream, gifOptions);
        }
Example #4
0
        /// <inheritdoc/>
        public void Encode <TColor>(Image <TColor> image, Stream stream, IEncoderOptions options)
            where TColor : struct, IPixel <TColor>
        {
            IPngEncoderOptions pngOptions = PngEncoderOptions.Create(options);

            this.Encode(image, stream, pngOptions);
        }
Example #5
0
        public ImageSaveTests()
        {
            this.encoder = new Mock <IImageEncoder>();
            this.format  = new Mock <IImageFormat>();
            this.format.Setup(x => x.Encoder).Returns(this.encoder.Object);
            this.format.Setup(x => x.Decoder).Returns(new Mock <IImageDecoder>().Object);
            this.format.Setup(x => x.MimeType).Returns("img/test");
            this.format.Setup(x => x.Extension).Returns("png");
            this.format.Setup(x => x.SupportedExtensions).Returns(new string[] { "png", "jpg" });


            this.encoderNotInFormat  = new Mock <IImageEncoder>();
            this.formatNotRegistered = new Mock <IImageFormat>();
            this.formatNotRegistered.Setup(x => x.Encoder).Returns(this.encoderNotInFormat.Object);
            this.formatNotRegistered.Setup(x => x.Decoder).Returns(new Mock <IImageDecoder>().Object);
            this.formatNotRegistered.Setup(x => x.MimeType).Returns("img/test");
            this.formatNotRegistered.Setup(x => x.Extension).Returns("png");
            this.formatNotRegistered.Setup(x => x.SupportedExtensions).Returns(new string[] { "png", "jpg" });

            this.fileSystem     = new Mock <IFileSystem>();
            this.encoderOptions = new Mock <IEncoderOptions>().Object;
            this.Image          = new Image(1, 1, new Configuration(this.format.Object)
            {
                FileSystem = this.fileSystem.Object
            });
        }
Example #6
0
 /// <summary>
 /// Saves the image to the given stream using the given image format.
 /// </summary>
 /// <param name="stream">The stream to save the image to.</param>
 /// <param name="format">The format to save the image as.</param>
 /// <param name="options">The options for the encoder.</param>
 /// <returns>The <see cref="Image{TColor}"/></returns>
 public Image <TColor> Save(Stream stream, IImageFormat format, IEncoderOptions options)
 {
     Guard.NotNull(stream, nameof(stream));
     Guard.NotNull(format, nameof(format));
     format.Encoder.Encode(this, stream, options);
     return(this);
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="EncoderOptions"/> class.
 /// </summary>
 /// <param name="options">The encoder options</param>
 protected EncoderOptions(IEncoderOptions options)
 {
     if (options != null)
     {
         this.IgnoreMetadata = options.IgnoreMetadata;
     }
 }
Example #8
0
 /// <summary>
 /// Saves the image to the given stream using the currently loaded image format.
 /// </summary>
 /// <param name="filePath">The file path to save the image to.</param>
 /// <param name="encoder">The encoder to save the image with.</param>
 /// <param name="options">The options for the encoder.</param>
 /// <exception cref="System.ArgumentNullException">Thrown if the encoder is null.</exception>
 /// <returns>The <see cref="Image{TPixel}"/></returns>
 public Image <TPixel> Save(string filePath, IImageEncoder encoder, IEncoderOptions options)
 {
     Guard.NotNull(encoder, nameof(encoder));
     using (Stream fs = this.Configuration.FileSystem.Create(filePath))
     {
         return(this.Save(fs, encoder, options));
     }
 }
Example #9
0
 /// <summary>
 /// Saves the image to the given stream using the currently loaded image format.
 /// </summary>
 /// <param name="filePath">The file path to save the image to.</param>
 /// <param name="format">The format to save the image as.</param>
 /// <param name="options">The options for the encoder.</param>
 /// <exception cref="System.ArgumentNullException">Thrown if the format is null.</exception>
 /// <returns>The <see cref="Image{TColor}"/></returns>
 public Image <TColor> Save(string filePath, IImageFormat format, IEncoderOptions options)
 {
     Guard.NotNull(format, nameof(format));
     using (FileStream fs = File.Create(filePath))
     {
         return(this.Save(fs, format));
     }
 }
Example #10
0
 /// <summary>
 /// Saves the image to the given stream using the currently loaded image format.
 /// </summary>
 /// <param name="filePath">The file path to save the image to.</param>
 /// <param name="encoder">The encoder to save the image with.</param>
 /// <param name="options">The options for the encoder.</param>
 /// <exception cref="System.ArgumentNullException">Thrown if the encoder is null.</exception>
 /// <returns>The <see cref="Image{TColor}"/></returns>
 public Image <TColor> Save(string filePath, IImageEncoder encoder, IEncoderOptions options)
 {
     Guard.NotNull(encoder, nameof(encoder));
     using (FileStream fs = File.Create(filePath))
     {
         return(this.Save(fs, encoder));
     }
 }
Example #11
0
        /// <summary>
        /// Saves the image to the given stream using the given image encoder.
        /// </summary>
        /// <param name="stream">The stream to save the image to.</param>
        /// <param name="encoder">The encoder to save the image with.</param>
        /// <param name="options">The options for the encoder.</param>
        /// <exception cref="System.ArgumentNullException">Thrown if the stream or encoder is null.</exception>
        /// <returns>
        /// The <see cref="Image{TPixel}"/>.
        /// </returns>
        public Image <TPixel> Save(Stream stream, IImageEncoder encoder, IEncoderOptions options)
        {
            Guard.NotNull(stream, nameof(stream));
            Guard.NotNull(encoder, nameof(encoder));

            encoder.Encode(this, stream, options);

            return(this);
        }
Example #12
0
        /// <summary>
        /// Saves the image to the given stream using the currently loaded image format.
        /// </summary>
        /// <param name="filePath">The file path to save the image to.</param>
        /// <param name="options">The options for the encoder.</param>
        /// <exception cref="System.ArgumentNullException">Thrown if the stream is null.</exception>
        /// <returns>The <see cref="Image{TColor}"/></returns>
        public Image <TColor> Save(string filePath, IEncoderOptions options)
        {
            string       ext    = Path.GetExtension(filePath).Trim('.');
            IImageFormat format = this.Configuration.ImageFormats.SingleOrDefault(f => f.SupportedExtensions.Contains(ext, StringComparer.OrdinalIgnoreCase));

            if (format == null)
            {
                throw new InvalidOperationException($"No image formats have been registered for the file extension '{ext}'.");
            }

            return(this.Save(filePath, format));
        }
Example #13
0
        /// <summary>
        /// Saves the image to the given stream using the given image encoder.
        /// </summary>
        /// <param name="stream">The stream to save the image to.</param>
        /// <param name="encoder">The encoder to save the image with.</param>
        /// <param name="options">The options for the encoder.</param>
        /// <exception cref="System.ArgumentNullException">Thrown if the stream or encoder is null.</exception>
        /// <returns>
        /// The <see cref="Image{TColor}"/>.
        /// </returns>
        public Image <TColor> Save(Stream stream, IImageEncoder encoder, IEncoderOptions options)
        {
            Guard.NotNull(stream, nameof(stream));
            Guard.NotNull(encoder, nameof(encoder));
            encoder.Encode(this, stream, options);

            // Reset to the start of the stream.
            if (stream.CanSeek)
            {
                stream.Position = 0;
            }

            return(this);
        }
Example #14
0
 /// <summary>
 /// Saves the image to the given stream using the currently loaded image format.
 /// </summary>
 /// <param name="stream">The stream to save the image to.</param>
 /// <param name="options">The options for the encoder.</param>
 /// <exception cref="System.ArgumentNullException">Thrown if the stream is null.</exception>
 /// <returns>The <see cref="Image{TPixel}"/></returns>
 public Image <TPixel> Save(Stream stream, IEncoderOptions options)
 {
     return(this.Save(stream, this.CurrentImageFormat?.Encoder, options));
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="JpegEncoderOptions"/> class.
 /// </summary>
 /// <param name="options">The options for the encoder.</param>
 private JpegEncoderOptions(IEncoderOptions options)
     : base(options)
 {
 }
 /// <summary>
 /// Converts the options to a <see cref="IJpegEncoderOptions"/> instance with a
 /// cast or by creating a new instance with the specfied options.
 /// </summary>
 /// <param name="options">The options for the encoder.</param>
 /// <returns>The options for the <see cref="JpegEncoder"/>.</returns>
 internal static IJpegEncoderOptions Create(IEncoderOptions options)
 {
     return(options as IJpegEncoderOptions ?? new JpegEncoderOptions(options));
 }
Example #17
0
 /// <summary>
 /// Saves the image to the given stream using the currently loaded image format.
 /// </summary>
 /// <param name="stream">The stream to save the image to.</param>
 /// <param name="options">The options for the encoder.</param>
 /// <exception cref="System.ArgumentNullException">Thrown if the stream is null.</exception>
 /// <returns>The <see cref="Image{TColor}"/></returns>
 public Image <TColor> Save(Stream stream, IEncoderOptions options)
 {
     Guard.NotNull(stream, nameof(stream));
     this.CurrentImageFormat.Encoder.Encode(this, stream, options);
     return(this);
 }
Example #18
0
        /// <summary>
        /// Encodes image by the format matching the required extension, than saves it to the recommended output file.
        /// </summary>
        /// <typeparam name="TColor">The pixel format of the image</typeparam>
        /// <param name="image">The image instance</param>
        /// <param name="extension">The requested extension</param>
        /// <param name="encoder">Optional encoder</param>
        /// <param name="options">Optional encoder options</param>
        public void SaveTestOutputFile <TColor>(Image <TColor> image, string extension = null, IImageEncoder encoder = null, IEncoderOptions options = null)
            where TColor : struct, IPixel <TColor>
        {
            string path = this.GetTestOutputFileName(extension);

            IImageFormat format = GetImageFormatByExtension(extension);

            encoder = encoder ?? format.Encoder;

            using (FileStream stream = File.OpenWrite(path))
            {
                image.Save(stream, encoder, options);
            }
        }
Example #19
0
 /// <summary>
 /// Initializes a new instance of the <see cref="BmpEncoderOptions"/> class.
 /// </summary>
 /// <param name="options">The options for the encoder.</param>
 private BmpEncoderOptions(IEncoderOptions options)
     : base(options)
 {
 }
Example #20
0
 /// <summary>
 /// Converts the options to a <see cref="IBmpEncoderOptions"/> instance with a cast
 /// or by creating a new instance with the specfied options.
 /// </summary>
 /// <param name="options">The options for the encoder.</param>
 /// <returns>The options for the <see cref="BmpEncoder"/>.</returns>
 internal static IBmpEncoderOptions Create(IEncoderOptions options)
 {
     return(options as IBmpEncoderOptions ?? new BmpEncoderOptions(options));
 }
Example #21
0
 /// <summary>
 /// Saves the image to the given stream using the currently loaded image format.
 /// </summary>
 /// <param name="filePath">The file path to save the image to.</param>
 /// <param name="format">The format to save the image as.</param>
 /// <param name="options">The options for the encoder.</param>
 /// <exception cref="System.ArgumentNullException">Thrown if the format is null.</exception>
 /// <returns>The <see cref="Image{TPixel}"/></returns>
 public Image <TPixel> Save(string filePath, IImageFormat format, IEncoderOptions options)
 {
     Guard.NotNull(format, nameof(format));
     return(this.Save(filePath, format.Encoder, options));
 }
Example #22
0
 public void Encode <TColor>(Image <TColor> image, Stream stream, IEncoderOptions options) where TColor : struct, IPixel <TColor>
 {
     // TODO record this happend so we an verify it.
 }
 /// <summary>
 /// Converts the options to a <see cref="IGifEncoderOptions"/> instance with a
 /// cast or by creating a new instance with the specfied options.
 /// </summary>
 /// <param name="options">The options for the encoder.</param>
 /// <returns>The options for the <see cref="GifEncoder"/>.</returns>
 internal static IGifEncoderOptions Create(IEncoderOptions options)
 {
     return(options as IGifEncoderOptions ?? new GifEncoderOptions(options));
 }
            private async Task UploadWholeFileAsync(Command message, CommandResult result)
            {
                const int    THUMB_WIDTH            = 80;
                const int    THUMB_HEIGHT           = 80;
                const int    NORMAL_IMAGE_MAX_WIDTH = 540;
                const string THUMBS_FOLDER_NAME     = "thumbs";

                // Ensure the storage root exists.
                Directory.CreateDirectory(_filesHelper.StorageRootPath);

                foreach (var file in message.Files)
                {
                    var extension = Path.GetExtension(file.FileName);
                    if (!_allowedExtensions.Contains(extension))
                    {
                        // This is not a supported image type.
                        throw new InvalidOperationException($"Unsupported image type: {extension}. The supported types are: {string.Join(", ", _allowedExtensions)}");
                    }

                    if (file.Length > 0L)
                    {
                        var fullPath = Path.Combine(_filesHelper.StorageRootPath, Path.GetFileName(file.FileName));
                        using (var fs = new FileStream(fullPath, FileMode.Create))
                        {
                            await file.CopyToAsync(fs);
                        }


                        //
                        // Create an 80x80 thumbnail.
                        //

                        var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(file.FileName);
                        var thumbName = $"{fileNameWithoutExtension}{THUMB_WIDTH}x{THUMB_HEIGHT}{extension}";
                        var thumbPath = Path.Combine(_filesHelper.StorageRootPath, THUMBS_FOLDER_NAME, thumbName);

                        using (var originalImage = Image.Load(fullPath))
                            using (var thumbStream = File.OpenWrite(thumbPath))
                            {
                                originalImage
                                .Resize(THUMB_WIDTH, THUMB_HEIGHT)
                                .Save(thumbStream);

                                var width = originalImage.Width;
                            }

                        // If the image is wider than 540px, resize it so that it is 540px wide. Otherwise, upload a copy of the original.
                        using (var originalImage = Image.Load(fullPath))
                        {
                            if (originalImage.Width > NORMAL_IMAGE_MAX_WIDTH)
                            {
                                // Resize it so that the max width is 540px. Maintain the aspect ratio.
                                var newHeight = originalImage.Height * NORMAL_IMAGE_MAX_WIDTH / originalImage.Width;

                                var normalImageName = $"{fileNameWithoutExtension}{NORMAL_IMAGE_MAX_WIDTH}x{newHeight}{extension}";
                                var normalImagePath = Path.Combine(_filesHelper.StorageRootPath, normalImageName);

                                IEncoderOptions encoderOptions = null;
                                if (extension == ".jpg" || extension == ".jpeg")
                                {
                                    encoderOptions = new JpegEncoderOptions {
                                        Quality = 90
                                    };
                                }

                                using (var normalImageStream = File.OpenWrite(normalImagePath))
                                {
                                    originalImage
                                    .Resize(NORMAL_IMAGE_MAX_WIDTH, newHeight)
                                    .Save(normalImageStream, encoderOptions);
                                }
                            }
                        }
                    }

                    result.FileResults.Add(UploadResult(file.FileName, file.Length));
                }
            }
Example #25
0
        /// <summary>
        /// Saves the image to the given stream using the given image format.
        /// </summary>
        /// <param name="stream">The stream to save the image to.</param>
        /// <param name="format">The format to save the image as.</param>
        /// <param name="options">The options for the encoder.</param>
        /// <returns>The <see cref="Image{TPixel}"/></returns>
        public Image <TPixel> Save(Stream stream, IImageFormat format, IEncoderOptions options)
        {
            Guard.NotNull(format, nameof(format));

            return(this.Save(stream, format.Encoder, options));
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="GifEncoderOptions"/> class.
 /// </summary>
 /// <param name="options">The options for the encoder.</param>
 private GifEncoderOptions(IEncoderOptions options)
     : base(options)
 {
 }