Beispiel #1
0
        protected override Effect GetEffect(ImageGenerationContext context, FastBitmap source)
        {
            // Fill temporary graphics buffer with mask (currently always a rectangle).
            DrawingVisual dv = new DrawingVisual
            {
                Effect = new BlurEffect
                {
                    Radius     = Radius,
                    KernelType = KernelType.Gaussian
                }
            };

            DrawingContext dc = dv.RenderOpen();

            dc.DrawImage(source.InnerBitmap, new Rect(0, 0, source.Width, source.Height));
            dc.Close();

            RenderTargetBitmap rtb = RenderTargetBitmapUtility.CreateRenderTargetBitmap(source.Width, source.Height);

            rtb.Render(dv);

            Brush blurredImage = new ImageBrush(rtb);

            return(new UnsharpMaskEffect
            {
                BlurMask = blurredImage,
                Amount = Amount / 100.0,
                Threshold = Threshold
            });
        }
Beispiel #2
0
        public Task <GeneratedImage> GenerateImageAsync()
        {
            // Create image in separate thread, and wait for result.
            var context = new ImageGenerationContext(HttpContext.Current);

            return(new TaskFactory(DispatcherTaskScheduler.Default)
                   .StartNew(() =>
            {
                var image = CreateImage(context);

                var properties = new ImageProperties
                {
                    ColorDepth = ColorDepth,
                    Format = ImageFormat,
                    JpegCompressionLevel = JpegCompressionLevel
                };
                if (image != null)
                {
                    image.Freeze();
                    properties.Width = image.PixelWidth;
                    properties.Height = image.PixelHeight;
                }
                properties.IsImagePresent = (image != null);
                return new GeneratedImage(properties, image);
            }));
        }
        protected override sealed void CreateImage(ImageGenerationContext context)
        {
            base.CreateImage(context);

            Rect bounds = new Rect(StrokeWidth / 2, StrokeWidth / 2,
                                   CalculatedWidth - StrokeWidth,
                                   CalculatedHeight - StrokeWidth);
            Brush brush = Fill.GetBrush();

            Pen pen = GetPen();

            DrawingVisual  dv = new DrawingVisual();
            DrawingContext dc = dv.RenderOpen();

            if (Roundness == 0)
            {
                dc.DrawRectangle(brush, pen, bounds);
            }
            else
            {
                dc.DrawRoundedRectangle(brush, pen, bounds, Roundness, Roundness);
            }

            dc.Close();

            RenderTargetBitmap rtb = RenderTargetBitmapUtility.CreateRenderTargetBitmap(CalculatedWidth, CalculatedHeight);

            rtb.Render(dv);
            Bitmap = new FastBitmap(rtb);
        }
        protected sealed override void CreateImage(ImageGenerationContext context)
        {
            base.CreateImage(context);

            Rect bounds = new Rect(StrokeWidth / 2, StrokeWidth / 2,
                                   CalculatedWidth - StrokeWidth,
                                   CalculatedHeight - StrokeWidth);
            Brush brush = Fill.GetBrush();

            PointCollection points   = GetPoints(bounds);
            PathGeometry    geometry = CanonicalSplineHelper.CreateSpline(points, Roundness / 100.0, null, true, true, 0.25);

            Pen pen = GetPen();

            DrawingVisual  dv = new DrawingVisual();
            DrawingContext dc = dv.RenderOpen();

            // Draw polygon.
            dc.DrawGeometry(brush, pen, geometry);

            dc.Close();

            RenderTargetBitmap rtb = RenderTargetBitmapUtility.CreateRenderTargetBitmap(CalculatedWidth, CalculatedHeight);

            rtb.Render(dv);
            Bitmap = new FastBitmap(rtb);
        }
Beispiel #5
0
 protected override Effect GetEffect(ImageGenerationContext context, FastBitmap source)
 {
     return(new ContrastAdjustmentEffect
     {
         Level = (Level + 100) / 40.0
     });
 }
Beispiel #6
0
        private void UseFormattedText(ImageGenerationContext context, RenderCallback renderCallback)
        {
            Brush           textBrush       = new SolidColorBrush(ForeColor.ToWpfColor());
            FontDescription fontDescription = Font.GetFontDescription(context);
            var             formattedText   = new FormattedText(
                Text, CultureInfo.CurrentCulture, FlowDirection.LeftToRight,
                fontDescription.Typeface, fontDescription.Size, textBrush);

            formattedText.SetTextDecorations(fontDescription.TextDecorations);
            if (Width != null)
            {
                formattedText.MaxTextWidth = Width.Value;
            }
            if (Height != null)
            {
                formattedText.MaxTextHeight = Height.Value;
            }
            if (!Multiline)
            {
                formattedText.MaxLineCount = 1;
            }
            formattedText.Trimming = TextTrimming.None;
            //formattedText.TextAlignment = HorizontalTextAlignment;

            renderCallback(formattedText);
        }
Beispiel #7
0
        protected override Effect GetEffect(ImageGenerationContext context, FastBitmap source)
        {
            // Get curves either from Curves collection or ACV file.
            CurveCollection curves = GetCurves(context);

            // Check that there are at least 4 curves.
            if (curves.Count < 4)
            {
                throw new DynamicImageException(
                          "At least 4 curves (corresponding to Composite, Red, Green, Blue) must be specified.");
            }

            // Convert mathematical curve definitions into 4x256 lookup texture (Composite, Red, Green, Blue are the 4 "columns").
            FastBitmap curvesLookup = new FastBitmap(4, 256);

            curvesLookup.Lock();
            for (int x = 0; x < 4; ++x)
            {
                IEnumerable <CurvePoint> points = curves[x].Points;
                float[] xValues     = points.Select(p => (float)p.Input).ToArray();
                float[] yValues     = points.Select(p => (float)p.Output).ToArray();
                float[] derivatives = CubicSplineUtility.CalculateSpline(xValues, yValues);
                for (int y = 0; y < 256; ++y)
                {
                    curvesLookup[x, y] = SWMColor.FromRgb((byte)CubicSplineUtility.InterpolateSpline(xValues, yValues, derivatives, y), 0, 0);
                }
            }
            curvesLookup.Unlock();

            return(new CurvesEffect
            {
                CurvesLookup = new ImageBrush(curvesLookup.InnerBitmap)
            });
        }
Beispiel #8
0
 protected override Effect GetEffect(ImageGenerationContext context, FastBitmap source)
 {
     return(new BrightnessAdjustmentEffect
     {
         Level = this.Level / 100.0
     });
 }
Beispiel #9
0
 protected override Effect GetEffect(ImageGenerationContext context, FastBitmap source)
 {
     return(new EmbossEffect
     {
         Amount = Amount,
         Width = Width / (double)source.Width
     });
 }
Beispiel #10
0
 public override FastBitmap GetBitmap(ImageGenerationContext context)
 {
     using (var webClient = new ImpatientWebClient(Timeout))
     {
         var bytes = webClient.DownloadData(Url);
         return(new FastBitmap(bytes));
     }
 }
Beispiel #11
0
 protected override Effect GetEffect(ImageGenerationContext context, FastBitmap source)
 {
     return(new ColorTintEffect
     {
         Amount = Amount / 100.0,
         RequiredColor = Color.ToWpfColor()
     });
 }
 public override FastBitmap GetBitmap(ImageGenerationContext context)
 {
     byte[] bytes = this.Bytes;
     if (bytes != null && bytes.Length > 0)
     {
         return(new FastBitmap(bytes));
     }
     return(null);
 }
Beispiel #13
0
 protected override Effect GetEffect(ImageGenerationContext context, FastBitmap source)
 {
     return(new BlurEffect
     {
         KernelType = KernelType.Gaussian,
         Radius = Radius,
         RenderingBias = RenderingBias.Quality
     });
 }
Beispiel #14
0
        private CurveCollection GetCurves(ImageGenerationContext context)
        {
            if (!string.IsNullOrEmpty(PhotoshopCurvesFileName))
            {
                return(PhotoshopCurvesReader.ReadPhotoshopCurvesFile(FileSourceHelper.ResolveFileName(context, PhotoshopCurvesFileName)));
            }

            return(Curves);
        }
Beispiel #15
0
        public override Scene GetScene(ImageGenerationContext context)
        {
            string resolvedFileName = FileSourceHelper.ResolveFileName(context, FileName);

            if (File.Exists(resolvedFileName))
            {
                return(MeshellatorLoader.ImportFromFile(resolvedFileName));
            }
            return(null);
        }
Beispiel #16
0
        private Size MeasureString(ImageGenerationContext context)
        {
            Size size = System.Windows.Size.Empty;

            UseFormattedText(context, ft =>
            {
                size = new Size(ft.WidthIncludingTrailingWhitespace, ft.Height);
            });
            return(size);
        }
Beispiel #17
0
        public override FastBitmap GetBitmap(ImageGenerationContext context)
        {
            string resolvedFileName = FileSourceHelper.ResolveFileName(context, FileName);

            if (File.Exists(resolvedFileName))
            {
                return(new FastBitmap(resolvedFileName));
            }
            return(null);
        }
Beispiel #18
0
 protected override Effect GetEffect(ImageGenerationContext context, FastBitmap source)
 {
     return(new DropShadowEffect
     {
         Direction = 0,
         Color = Color.ToWpfColor(),
         BlurRadius = Size,
         ShadowDepth = 0,
         Opacity = Opacity
     });
 }
Beispiel #19
0
        protected override void CreateImage(ImageGenerationContext context)
        {
            if (this.Width != null)
            {
                this.CalculatedWidth = this.Width.Value;
            }

            if (this.Height != null)
            {
                this.CalculatedHeight = this.Height.Value;
            }
        }
Beispiel #20
0
        protected override void CreateImage(ImageGenerationContext context)
        {
            // If width and height are not set, we need to measure the string.
            int  calculatedWidth, calculatedHeight;
            Size measuredSize = MeasureString(context);

            if (Width == null || Height == null)
            {
                double width  = Width ?? measuredSize.Width;
                double height = Height ?? measuredSize.Height;
                calculatedWidth  = (int)width;
                calculatedHeight = (int)height;
            }
            else             // otherwise just create the image at the desired size
            {
                calculatedWidth  = Width.Value;
                calculatedHeight = Height.Value;
            }

            #region Draw text

            DrawingVisual  dv = new DrawingVisual();
            DrawingContext dc = dv.RenderOpen();

            //RenderOptions.SetClearTypeHint(dv, ClearTypeHint.Auto);
            TextOptions.SetTextRenderingMode(dv, TextRenderingMode.Auto);
            //TextOptions.SetTextFormattingMode(dv, TextFormattingMode.Ideal)

            UseFormattedText(context, ft =>
            {
                Pen pen = null;
                if (StrokeWidth > 0 && StrokeColor != null)
                {
                    pen = new Pen(new SolidColorBrush(StrokeColor.Value.ToWpfColor()), StrokeWidth);
                }

                // Calculate position to draw text at, based on vertical text alignment.
                int x = CalculateHorizontalPosition((int)measuredSize.Width);
                int y = CalculateVerticalPosition((int)measuredSize.Height);

                dc.DrawGeometry(new SolidColorBrush(ForeColor.ToWpfColor()), pen,
                                ft.BuildGeometry(new Point(x, y)));
            });

            dc.Close();

            RenderTargetBitmap rtb = RenderTargetBitmapUtility.CreateRenderTargetBitmap(calculatedWidth, calculatedHeight);
            rtb.Render(dv);

            #endregion

            Bitmap = new FastBitmap(rtb);
        }
        protected override Effect GetEffect(ImageGenerationContext context, FastBitmap source)
        {
            FastBitmap maskBitmap = MaskImage.GetBitmap(context);

            _maskImageWidth  = maskBitmap.Width;
            _maskImageHeight = maskBitmap.Height;
            return(new ClippingMaskEffect
            {
                Mask = new ImageBrush(maskBitmap.InnerBitmap),
                InputCoordsOffset = new Vector(MaskPositionX / (double)source.Width, MaskPositionY / (double)source.Height),
                InputCoordsScale = new Vector(_maskImageWidth / (double)source.Width, _maskImageHeight / (double)source.Height)
            });
        }
        public sealed override void ApplyFilter(ImageGenerationContext context, FastBitmap bitmap)
        {
            OnBeginApplyFilter(bitmap);

            // get destination dimensions
            int  destWidth, destHeight;
            bool shouldContinue = GetDestinationDimensions(bitmap, out destWidth, out destHeight);

            if (!shouldContinue)
            {
                return;
            }

            FastBitmap destination = new FastBitmap(destWidth, destHeight);

            // copy metadata
            // TODO

            /*foreach (PropertyItem propertyItem in bitmap.InnerBitmap.PropertyItems)
             *      destination.InnerBitmap.SetPropertyItem(propertyItem);*/

            int width  = bitmap.Width;
            int height = bitmap.Height;

            OriginalSpace = new Int32Rect(0, 0, width, height);
            Int32Rect transformedSpace = GetTransformedSpace(OriginalSpace);

            try
            {
                bitmap.Lock();
                destination.Lock();

                if (InterpolationMode == InterpolationMode.NearestNeighbor)
                {
                    FilterPixelsNearestNeighbor(bitmap, destination, width, height, transformedSpace);
                }
                else
                {
                    FilterPixelsBilinear(bitmap, destination, width, height, transformedSpace);
                }
            }
            finally
            {
                destination.Unlock();
                bitmap.Unlock();
            }

            bitmap.InnerBitmap = destination.InnerBitmap;
        }
Beispiel #23
0
        public void Process(ImageGenerationContext context)
        {
            CreateImage(context);

            if (Bitmap != null)
            {
                foreach (Filter filter in Filters)
                {
                    if (filter.Enabled)
                    {
                        filter.ApplyFilter(context, Bitmap);
                    }
                }
            }
        }
Beispiel #24
0
        protected override Effect GetEffect(ImageGenerationContext context, FastBitmap source)
        {
            FastBitmap transferLookup = new FastBitmap(1, 256);

            transferLookup.Lock();
            for (int y = 0; y < 256; ++y)
            {
                byte colorComponent = (byte)(255 * GetTransferFunctionValue(y / 255.0f));
                transferLookup[0, y] = SWMColor.FromRgb(colorComponent, colorComponent, colorComponent);
            }
            transferLookup.Unlock();

            return(new TransferEffect
            {
                TransferLookup = new ImageBrush(transferLookup.InnerBitmap)
            });
        }
Beispiel #25
0
        public FontDescription GetFontDescription(ImageGenerationContext context)
        {
            FontFamily fontFamily;

            if (!string.IsNullOrEmpty(CustomFontFile))
            {
                string fontFileName = FileSourceHelper.ResolveFileName(context, CustomFontFile);
                fontFamily = new FontFamily(fontFileName + "#" + Name);
            }
            else
            {
                fontFamily = new FontFamily(Name);
            }
            Typeface typeface = new Typeface(fontFamily, GetStyle(), GetWeight(), FontStretches.Normal);

            return(new FontDescription(typeface, GetTextDecorations(), Size));
        }
Beispiel #26
0
        protected override void CreateImage(ImageGenerationContext context)
        {
            FastBitmap sourceValue = this.Source.GetBitmap(context);

            if (sourceValue != null && sourceValue.InnerBitmap != null)
            {
                this.Bitmap = sourceValue;
            }
            else if (AlternateSource != null)
            {
                sourceValue = this.AlternateSource.GetBitmap(context);
                if (sourceValue != null && sourceValue.InnerBitmap != null)
                {
                    this.Bitmap = sourceValue;
                }
            }
        }
Beispiel #27
0
        protected override void CreateImage(ImageGenerationContext context)
        {
            Bitmap = new FastBitmap(Width, Height);
            Bitmap.Lock();

            for (int y = 0; y < Height; y++)
            {
                for (int x = 0; x < Width; x++)
                {
                    ColorHsv colorHsv = CalculateFractalColor(x, y);
                    var      color    = (Color)colorHsv;
                    Bitmap[x, y] = color.ToWpfColor();
                }
            }

            Bitmap.Unlock();
        }
        protected override void CreateImage(ImageGenerationContext context)
        {
            GhostscriptUtil.EnsureDll(context);

            string outputFileName = Path.GetTempFileName();

            try
            {
                string filename = FileSourceHelper.ResolveFileName(context, SourceFileName);
                GhostscriptWrapper.GeneratePageThumb(filename, outputFileName, PageNumber, Width, Height);
                Bitmap = new FastBitmap(File.ReadAllBytes(outputFileName));
            }
            finally
            {
                File.Delete(outputFileName);
            }
        }
Beispiel #29
0
        protected override void CreateImage(ImageGenerationContext context)
        {
            string outputFileName = Path.GetTempFileName();

            try
            {
                if (!new CutyCaptWrapper().SaveScreenShot(WebsiteUrl, outputFileName, Timeout, BrowserWidth))
                {
                    return;
                }
                Bitmap = new FastBitmap(File.ReadAllBytes(outputFileName));
            }
            finally
            {
                File.Delete(outputFileName);
            }
        }
Beispiel #30
0
        protected override void CreateImage(ImageGenerationContext context)
        {
            Scene scene = Source.GetScene(context);

            using (var renderer = new WarpSceneRenderer(scene, Width, Height))
            {
                renderer.Initialize();

                renderer.Options.TriangleWindingOrderReversed = ReverseWindingOrder;
                renderer.Options.BackgroundColor = new Nexus.Color(BackgroundColour.A, BackgroundColour.R, BackgroundColour.G, BackgroundColour.B);
                renderer.Options.LightingEnabled = LightingEnabled;

                Viewport viewport = new Viewport(0, 0, Width, Height);
                Nexus.Graphics.Cameras.Camera camera = (Camera != null)
                                        ? Camera.GetNexusCamera(scene, viewport)
                                        : new AutoCamera().GetNexusCamera(scene, viewport);
                BitmapSource renderedBitmap = renderer.Render(camera);
                Bitmap = new FastBitmap(renderedBitmap);
            }
        }