예제 #1
0
        private MediaStream GetImageStream(MediaStream stream, TransformationOptions options)
        {
            Assert.ArgumentNotNull((object)stream, nameof(stream));
            Assert.ArgumentNotNull((object)options, nameof(options));
            var mediaOptions = new MediaOptions()
            {
                AllowStretch      = options.AllowStretch,
                BackgroundColor   = options.BackgroundColor,
                IgnoreAspectRatio = options.IgnoreAspectRatio,
                Scale             = options.Scale,
                Width             = options.Size.Width,
                Height            = options.Size.Height,
                MaxWidth          = options.MaxSize.Width,
                MaxHeight         = options.MaxSize.Height
            };

            mediaOptions.CustomOptions["extension"] = "webp";
            var args = new OptimizerArgs(stream.Stream, mediaOptions);

            CorePipeline.Run("dianogaOptimizeWebP", args);

            if (args.IsOptimized)
            {
                return(new MediaStream(args.Stream, args.Extension, stream.MediaItem));
            }
            return(null);
        }
예제 #2
0
 private TransformationOptions GetOptions(MediaData mediaData, TransformationOptions options)
 {
     options = options.Clone();
     if (options.Size.IsEmpty && (double)options.Scale == 0.0)
     {
         options.Size = MediaManager.Config.GetThumbnailSize(mediaData.Extension);
     }
     options.PreserveResolution = false;
     return(options);
 }
예제 #3
0
        /// <summary>
        /// Gets the scale.
        /// </summary>
        /// <param name="options">The options.</param>
        /// <param name="originalImage">The original image.</param>
        /// <param name="size">The size.</param>
        /// <returns>The scale.</returns>
        protected float GetScale(TransformationOptions options, Image originalImage, Size size)
        {
            float val  = (float)size.Width / (float)originalImage.Width;
            float val2 = (float)size.Height / (float)originalImage.Height;
            float num  = Math.Min(val, val2);

            if (!options.AllowStretch && num > 1f)
            {
                num = 1f;
            }
            return(num);
        }
예제 #4
0
 /// <summary>
 /// Gets the resize options.
 /// </summary>
 /// <param name="options">The options.</param>
 /// <returns>The resize options.</returns>
 protected ResizeOptions GetResizeOptions(TransformationOptions options)
 {
     return(new ResizeOptions
     {
         AllowStretch = options.AllowStretch,
         BackgroundColor = options.BackgroundColor,
         IgnoreAspectRatio = options.IgnoreAspectRatio,
         MaxSize = options.MaxSize,
         Scale = options.Scale,
         Size = options.Size,
         PreserveResolution = options.PreserveResolution,
         CompositingMode = options.CompositingMode,
         InterpolationMode = options.InterpolationMode,
         PixelOffsetMode = options.PixelOffsetMode
     });
 }
예제 #5
0
        public override MediaStream GetStream(
            MediaData mediaData,
            TransformationOptions options)
        {
            Assert.ArgumentNotNull((object)mediaData, nameof(mediaData));
            Assert.ArgumentNotNull((object)options, "transformationOptions");
            MediaStream stream = mediaData.GetStream();

            if (stream == null)
            {
                return((MediaStream)null);
            }
            options = this.GetOptions(mediaData, options);
            using (stream)
            {
                return(this.GetImageStream(stream, options));
            }
        }
예제 #6
0
        /// <summary>
        /// Resizes an image represented by a stream.
        /// </summary>
        /// <param name="inputStream">The input stream.</param>
        /// <param name="options">The options.</param>
        /// <param name="outputFormat">The output format.</param>
        /// <returns>The image stream.</returns>
        public virtual Stream ResizeImageFromStream(Stream inputStream, TransformationOptions options, ImageFormat outputFormat)
        {
            Assert.ArgumentNotNull((object)inputStream, "inputStream");
            Assert.ArgumentNotNull((object)options, "options");
            Assert.ArgumentNotNull((object)outputFormat, "outputFormat");
            ResizeOptions resizeOptions = this.GetResizeOptions(options);

            if (resizeOptions.IsEmpty)
            {
                return(inputStream);
            }
            if (inputStream.Length > Settings.Media.MaxSizeInMemory)
            {
                Tracer.Error((object)"Could not resize image stream as it was larger than the maximum size allowed for memory processing.");
                return((Stream)null);
            }
            if (Settings.Media.UseLegacyResizing)
            {
                return(this.ResizeLegacy(inputStream, options, outputFormat));
            }
            Resizer resizer = new Resizer();

            using (Bitmap originalBitmap = new Bitmap((Stream)inputStream))
            {
                Size frameSize = resizer.GetFrameSize(originalBitmap, resizeOptions);
                if (originalBitmap.Size.Equals((object)frameSize))
                {
                    inputStream.Seek(0L, SeekOrigin.Begin);
                    return((Stream)inputStream);
                }
                using (Bitmap resizedBitmap = resizer.Resize(originalBitmap, resizeOptions, outputFormat))
                {
                    MemoryStream memoryStream = new MemoryStream();
                    memoryStream.Seek(0L, SeekOrigin.Begin);
                    ImageCodecInfo    encoderInfo   = this.FindEncoderInfo(outputFormat);
                    EncoderParameters encoderParams = new EncoderParameters(1);
                    encoderParams.Param[0] = new EncoderParameter(Encoder.Quality, (long)options.Quality);
                    resizedBitmap.Save((Stream)memoryStream, encoderInfo, encoderParams);
                    memoryStream.SetLength(memoryStream.Position);
                    memoryStream.Seek(0L, SeekOrigin.Begin);
                    return((Stream)memoryStream);
                }
            }
        }
        public void Process(GetMediaStreamPipelineArgs args)
        {
            Assert.ArgumentNotNull(args, "args");

            var outputStream = args.OutputStream;

            if (outputStream == null)
            {
                return;
            }

            if (!IMAGE_EXTENSIONS.Any(i => i.Equals(args.MediaData.Extension, StringComparison.InvariantCultureIgnoreCase)))
            {
                return;
            }

            var   cx = args.Options.CustomOptions["cx"];
            var   cy = args.Options.CustomOptions["cy"];
            var   width = args.Options.CustomOptions["cw"];
            var   height = args.Options.CustomOptions["ch"];
            float x, y;
            int   w, h;

            if (!string.IsNullOrEmpty(cx) && !string.IsNullOrEmpty(cy) && float.TryParse(cx, out x) && float.TryParse(cy, out y) &&
                !string.IsNullOrEmpty(width) && Int32.TryParse(width, out w) && !string.IsNullOrEmpty(height) && Int32.TryParse(height, out h))
            {
                var outputStrm = Stream.Synchronized(GetCroppedImage(args.MediaData.Extension, w, h, x, y, outputStream.MediaItem));
                args.OutputStream = new MediaStream(outputStrm, args.MediaData.Extension, outputStream.MediaItem);
            }
            else if (args.Options.Thumbnail)
            {
                TransformationOptions transformationOptions = args.Options.GetTransformationOptions();
                MediaStream           thumbnailStream       = args.MediaData.GetThumbnailStream(transformationOptions);

                if (thumbnailStream != null)
                {
                    args.OutputStream = thumbnailStream;
                }
            }
        }
예제 #8
0
 /// <summary>
 /// Gets the set of options to use in the image transformation.
 /// </summary>
 /// <param name="options">The options.</param>
 /// <param name="image">The image.</param>
 /// <returns>The transformed size.</returns>
 protected Size GetSize(TransformationOptions options, Image image)
 {
     if (options.Scale > 0f)
     {
         return(new Size(this.Scale(image.Width, options.Scale), this.Scale(image.Height, options.Scale)));
     }
     if (options.Size.IsEmpty || options.Size == image.Size)
     {
         return(new Size(image.Size.Width, image.Size.Height));
     }
     if (options.Size.Width == 0)
     {
         float amount = (float)options.Size.Height / (float)image.Height;
         return(new Size(this.Scale(image.Width, amount), options.Size.Height));
     }
     if (options.Size.Height == 0)
     {
         float amount2 = (float)options.Size.Width / (float)image.Width;
         return(new Size(options.Size.Width, this.Scale(image.Height, amount2)));
     }
     return(new Size(options.Size.Width, options.Size.Height));
 }
예제 #9
0
        public void Process(GetMediaStreamPipelineArgs args)
        {
            try
            {
                if (!args.Options.Thumbnail)
                {
                    return;
                }

                TransformationOptions transformationOptions = args.Options.GetTransformationOptions();
                ImageFormat           imageFormat           = MediaManager.Config.GetImageFormat(args.MediaData.MediaItem.Extension);

                var imageResizer = new ImageResizer();
                var stream       = imageResizer.ResizeImageFromStream(args.MediaData.GetStream().Stream, transformationOptions, imageFormat);
                if (stream != null)
                {
                    args.OutputStream = new MediaStream(stream, args.MediaData.MediaItem.Extension, args.MediaData.MediaItem);
                }
            }
            catch (Exception ex)
            {
                Log.Error($"Error while generating thumbnail for media item: {args.MediaData.MediaId}", ex, this);
            }
        }
예제 #10
0
 /// <summary>
 /// Gets the size.
 /// </summary>
 /// <param name="options">The options.</param>
 /// <param name="size">The size.</param>
 /// <returns>The transformed size.</returns>
 protected Size GetSize(TransformationOptions options, Size size)
 {
     if (options.MaxSize.IsEmpty)
     {
         return(size);
     }
     if (options.MaxSize.Width > 0 && size.Width > options.MaxSize.Width)
     {
         if (options.Size.Height == 0)
         {
             size.Height = (int)Math.Round((double)((float)options.MaxSize.Width / (float)size.Width * (float)size.Height));
         }
         size.Width = options.MaxSize.Width;
     }
     if (options.MaxSize.Height > 0 && size.Height > options.MaxSize.Height)
     {
         if (options.Size.Width == 0)
         {
             size.Width = (int)Math.Round((double)((float)options.MaxSize.Height / (float)size.Height * (float)size.Width));
         }
         size.Height = options.MaxSize.Height;
     }
     return(size);
 }
예제 #11
0
        /// <summary>
        /// Resizes an image represented by a stream.
        /// </summary>
        /// <param name="inputStream">The input stream.</param>
        /// <param name="options">The options.</param>
        /// <param name="outputFormat">The output format.</param>
        /// <returns>The image stream.</returns>
        protected Stream ResizeLegacy(Stream inputStream, TransformationOptions options, ImageFormat outputFormat)
        {
            Assert.ArgumentNotNull(inputStream, "inputStream");
            Assert.ArgumentNotNull(options, "options");
            Image        image        = Image.FromStream(inputStream);
            MemoryStream memoryStream = new MemoryStream();
            Size         size         = this.GetSize(options, image);

            size = this.GetSize(options, size);
            if (size != image.Size)
            {
                float     scale  = this.GetScale(options, image, size);
                int       num    = this.Scale(image.Width, scale);
                int       num2   = this.Scale(image.Height, scale);
                Rectangle rect   = new Rectangle((size.Width - num) / 2, (size.Height - num2) / 2, num, num2);
                Bitmap    bitmap = new Bitmap(image, size);
                bitmap.SetResolution(image.HorizontalResolution, image.VerticalResolution);
                using (Graphics graphics = Graphics.FromImage(bitmap))
                {
                    graphics.CompositingMode   = CompositingMode.SourceCopy;
                    graphics.InterpolationMode = options.InterpolationMode;
                    graphics.Clear(options.BackgroundColor);
                    graphics.DrawImage(image, rect);
                }
                bitmap.Save(memoryStream, outputFormat);
            }
            else
            {
                image.Save(memoryStream, outputFormat);
            }
            if (memoryStream.CanSeek)
            {
                memoryStream.Seek(0L, SeekOrigin.Begin);
            }
            return(memoryStream);
        }
예제 #12
0
 public IgnoreOuterBlockInConjugations(T sharedInternalState, TransformationOptions options = null)
     : base(sharedInternalState, options ?? TransformationOptions.Default)
 {
 }
예제 #13
0
 public IgnoreOuterBlockInConjugations(SyntaxTreeTransformation <T> parent, TransformationOptions options = null)
     : base(parent, options ?? TransformationOptions.Default)
 {
 }
 public QirStatementKindTransformation(GenerationContext sharedState, TransformationOptions options)
     : base(sharedState, options)
 {
 }
 public QirStatementKindTransformation(SyntaxTreeTransformation <GenerationContext> parentTransformation, TransformationOptions options)
     : base(parentTransformation, options)
 {
 }
예제 #16
0
 public abstract void Configure(TransformationOptions options);
 public QirExpressionTransformation(GenerationContext sharedState, TransformationOptions options)
     : base(sharedState, options)
 {
 }
 public QirExpressionTransformation(SyntaxTreeTransformation <GenerationContext> parentTransformation, TransformationOptions options)
     : base(parentTransformation, options)
 {
 }
 public QirNamespaceTransformation(SyntaxTreeTransformation <GenerationContext> parentTransformation, TransformationOptions options)
     : base(parentTransformation, options)
 {
 }
 public QirNamespaceTransformation(GenerationContext sharedState, TransformationOptions options)
     : base(sharedState, options)
 {
 }
        /// <summary>
        /// Gets the transformation options.
        /// </summary>
        /// <returns></returns>
        private TransformationOptions GetTransformationOptions(MediaItem mediaItem)
        {
            var options = new TransformationOptions();
            if (mediaItem == null)
            {
                return options;
            }

            var mediaWidth = Int32.Parse(mediaItem.InnerItem["Width"]);
            var mediaHeight = Int32.Parse(mediaItem.InnerItem["Height"]);

            var size = new Size();

            var width = 0;
            if (!string.IsNullOrEmpty(this.RenderingItem["Width"]) && Int32.TryParse(this.RenderingItem["Width"], out width))
            {
                size.Width = width;
                size.Height = width * mediaHeight / mediaWidth;
            }
            
            var height = 0;
            if (string.IsNullOrEmpty(this.RenderingItem["Width"]) && !string.IsNullOrEmpty(this.RenderingItem["Height"]) && Int32.TryParse(this.RenderingItem["Height"], out height))
            {
                size.Height = height;
                size.Width = height * mediaWidth / mediaHeight;
            }            

            if (size.Width > 0 && size.Height > 0)
            {
                options.Size = size;
            }
            
            options.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            options.IgnoreAspectRatio = false;
            options.MaxSize = new Size(500, 500);
            options.Quality = 100;
            return options;
        }