public override FastBitmap GetBitmap(ImageGenerationContext context) { string resolvedFileName = FileSourceHelper.ResolveFileName(context, FileName); if (File.Exists(resolvedFileName)) return new FastBitmap(resolvedFileName); return null; }
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 }; }
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(); 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); }
public override Scene GetScene(ImageGenerationContext context) { string resolvedFileName = FileSourceHelper.ResolveFileName(context, FileName); if (File.Exists(resolvedFileName)) return MeshellatorLoader.ImportFromFile(resolvedFileName); return null; }
/// <summary> /// Applies the filter to the specified <paramref name="bitmap"/>. This method /// first calls <see cref="ImageReplacementFilter.GetDestinationDimensions(FastBitmap, out Int32, out Int32)" /> /// to calculate the size of the destination image. Then it calls /// <see cref="ImageReplacementFilter.ApplyFilter(FastBitmap, DrawingContext, int, int)" /> /// which is where the overridden class implements its filter algorithm. /// </summary> /// <param name="context"></param> /// <param name="bitmap"> /// Image to apply the <see cref="ImageReplacementFilter" /> to. /// </param> public override sealed void ApplyFilter(ImageGenerationContext context, FastBitmap bitmap) { OnBeginApplyFilter(bitmap); // get destination dimensions int width, height; bool shouldContinue = GetDestinationDimensions(bitmap, out width, out height); if (!shouldContinue) return; DrawingVisual dv = new DrawingVisual(); ConfigureDrawingVisual(context, bitmap, dv); DrawingContext dc = dv.RenderOpen(); ApplyFilter(bitmap, dc, width, height); dc.Close(); RenderTargetBitmap rtb = RenderTargetBitmapUtility.CreateRenderTargetBitmap(width, height); rtb.Render(dv); FastBitmap destination = new FastBitmap(rtb); // copy metadata // TODO /*foreach (PropertyItem propertyItem in bitmap.InnerBitmap.PropertyItems) destination.InnerBitmap.SetPropertyItem(propertyItem);*/ // set new image bitmap.InnerBitmap = destination.InnerBitmap; OnEndApplyFilter(); }
public override Scene GetScene(ImageGenerationContext context) { Scene scene = new Scene(); foreach (Mesh mesh in Meshes) { Meshellator.Mesh meshellatorMesh = new Meshellator.Mesh(); scene.Meshes.Add(meshellatorMesh); meshellatorMesh.Positions.AddRange(mesh.Positions.Select(p => new Nexus.Point3D(p.X, p.Y, p.Z))); meshellatorMesh.TextureCoordinates.AddRange(mesh.TextureCoordinates.Select(p => new Nexus.Point3D(p.X, p.Y, 0))); MeshUtility.CalculateNormals(meshellatorMesh, false); meshellatorMesh.Indices.AddRange(mesh.Indices.Select(i => i.Value)); Meshellator.Material meshellatorMaterial = new Meshellator.Material(); meshellatorMaterial.DiffuseColor = ConversionUtility.ToNexusColorRgbF(mesh.Material.DiffuseColor); if (!string.IsNullOrEmpty(mesh.Material.TextureFileName)) { string textureFileName = FileSourceHelper.ResolveFileName(context, mesh.Material.TextureFileName); if (!File.Exists(textureFileName)) throw new DynamicImageException("Could not find texture '" + mesh.Material.TextureFileName + "'."); meshellatorMaterial.DiffuseTextureName = textureFileName; } meshellatorMaterial.Shininess = mesh.Material.Shininess; meshellatorMaterial.SpecularColor = ConversionUtility.ToNexusColorRgbF(mesh.Material.SpecularColor); meshellatorMaterial.Transparency = mesh.Material.Transparency; meshellatorMesh.Material = meshellatorMaterial; scene.Materials.Add(meshellatorMaterial); } return scene; }
public override FastBitmap GetBitmap(ImageGenerationContext context) { byte[] bytes = this.Bytes; if (bytes != null && bytes.Length > 0) return new FastBitmap(bytes); return null; }
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); }
public override void ApplyFilter(ImageGenerationContext context, FastBitmap bitmap) { if (Width == Unit.Empty && Height == Unit.Empty) throw new DynamicImageException("At least one of Width or Height must be set."); string sourceFileName = Path.GetTempFileName(); try { bitmap.Save(sourceFileName); string outputFileName = Path.GetTempFileName(); try { int width = (Width == Unit.Empty) ? bitmap.Width : Unit.GetCalculatedValue(Width, bitmap.Width); int height = (Height == Unit.Empty) ? bitmap.Height : Unit.GetCalculatedValue(Height, bitmap.Height); new CairWrapper(context).ProcessImage(sourceFileName, outputFileName, int.MaxValue, width, height, ConvolutionType); FastBitmap output = new FastBitmap(File.ReadAllBytes(outputFileName)); if (output.InnerBitmap != null) bitmap.InnerBitmap = output.InnerBitmap; } finally { File.Delete(outputFileName); } } finally { File.Delete(sourceFileName); } }
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) }; }
private CurveCollection GetCurves(ImageGenerationContext context) { if (!string.IsNullOrEmpty(PhotoshopCurvesFileName)) return PhotoshopCurvesReader.ReadPhotoshopCurvesFile(FileSourceHelper.ResolveFileName(context, PhotoshopCurvesFileName)); return Curves; }
public override FastBitmap GetBitmap(ImageGenerationContext context) { using (var webClient = new ImpatientWebClient(Timeout)) { var bytes = webClient.DownloadData(Url); return new FastBitmap(bytes); } }
protected override Effect GetEffect(ImageGenerationContext context, FastBitmap source) { return new ColorTintEffect { Amount = Amount/100.0, RequiredColor = Color.ToWpfColor() }; }
protected override void CreateImage(ImageGenerationContext context) { if (this.Width != null) this.CalculatedWidth = this.Width.Value; if (this.Height != null) this.CalculatedHeight = this.Height.Value; }
protected override Effect GetEffect(ImageGenerationContext context, FastBitmap source) { return new BlurEffect { KernelType = KernelType.Gaussian, Radius = Radius, RenderingBias = RenderingBias.Quality }; }
private static string GetCairFolder(ImageGenerationContext context) { string folder = (context.HttpContext == null) ? Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "CAIR") : context.HttpContext.Server.MapPath("~/App_Data/CAIR"); if (!Directory.Exists(folder)) Directory.CreateDirectory(folder); return folder; }
protected override Effect GetEffect(ImageGenerationContext context, FastBitmap source) { return new DropShadowEffect { Direction = 0, Color = Color.ToWpfColor(), BlurRadius = Size, ShadowDepth = 0, Opacity = Opacity }; }
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 CutyCaptWrapper(ImageGenerationContext context) { _cutyCaptPath = GetCutyCaptPath(context); if (!File.Exists(_cutyCaptPath)) { using (var stream = typeof(CutyCaptWrapper).Assembly.GetManifestResourceStream("SoundInTheory.DynamicImage.Resources.CutyCapt.exe")) using (var fileStream = File.OpenWrite(_cutyCaptPath)) stream.CopyTo(fileStream); } CutyCaptDefaultArguments = " --max-wait=0 --out-format=png --javascript=off --java=off --plugins=off --js-can-open-windows=off --js-can-access-clipboard=off --private-browsing=on"; }
public override FastBitmap GetBitmap(ImageGenerationContext context) { SqlConnection conn = null; SqlDataAdapter adapter = null; DataTable dt = null; try { string connectionString = ConfigurationManager.ConnectionStrings[this.ConnectionStringName].ConnectionString; conn = new SqlConnection(connectionString); conn.Open(); string whereFilter = string.Empty; for (int i = 0, length = PrimaryKeyNames.Length; i < length; ++i) { string primaryKeyName = PrimaryKeyNames[i]; object primaryKeyValue = PrimaryKeyValues[i]; whereFilter += string.Format("{0} = {1}", primaryKeyName, primaryKeyValue); if (i < length - 1) whereFilter += " AND "; } string selectSql = string.Format("SELECT {0} FROM {1} WHERE {2}", ColumnName, TableName, whereFilter); adapter = new SqlDataAdapter(selectSql, conn); dt = new DataTable(); adapter.Fill(dt); if (dt.Rows.Count != 1) throw new InvalidOperationException("Could not retrieve value from database"); object value = dt.Rows[0][0]; if (value == null || value is DBNull) return null; // this is fine, just means there's no value in the database if (!(value is byte[])) throw new InvalidOperationException(string.Format("Expected object of type '{0}' but found object of type '{1}'", typeof(byte[]).FullName, value.GetType().FullName)); return new FastBitmap((byte[]) value); } finally { if (dt != null) dt.Dispose(); if (adapter != null) adapter.Dispose(); if (conn != null) { conn.Close(); conn.Dispose(); } } }
private static string GetCairFolder(ImageGenerationContext context) { string folder = (context.HttpContext == null) ? Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "CAIR") : context.HttpContext.Server.MapPath("~/App_Data/CAIR"); if (!Directory.Exists(folder)) { Directory.CreateDirectory(folder); } return(folder); }
protected override void CreateImage(ImageGenerationContext context) { string outputFileName = Path.GetTempFileName(); try { if (!new CutyCaptWrapper(context).SaveScreenShot(WebsiteUrl, outputFileName, Timeout, BrowserWidth)) return; Bitmap = new FastBitmap(File.ReadAllBytes(outputFileName)); } finally { File.Delete(outputFileName); } }
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); }
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(); }
public CairWrapper(ImageGenerationContext context) { // Extract CAIR.exe and pthreadVSE2.dll (stored as embedded resources in this DLL) _cairPath = GetCairPath(context); if (!File.Exists(_cairPath)) { using (var stream = typeof(CairWrapper).Assembly.GetManifestResourceStream("SoundInTheory.DynamicImage.Resources.CAIR.exe")) using (var fileStream = File.OpenWrite(_cairPath)) stream.CopyTo(fileStream); using (var stream = typeof(CairWrapper).Assembly.GetManifestResourceStream("SoundInTheory.DynamicImage.Resources.pthreadVSE2.dll")) using (var fileStream = File.OpenWrite(Path.Combine(GetCairFolder(context), "pthreadVSE2.dll"))) stream.CopyTo(fileStream); } }
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) }; }
public static void EnsureDll(ImageGenerationContext context) { string folder = (context.HttpContext == null) ? AppDomain.CurrentDomain.BaseDirectory : context.HttpContext.Server.MapPath("~/bin"); string gsDllPath = Path.Combine(folder, "gsdll.dll"); if (!File.Exists(gsDllPath)) { string gsDllFile = Environment.Is64BitProcess ? "gsdll64.dll" : "gsdll32.dll"; using (var stream = typeof(GhostscriptUtil).Assembly.GetManifestResourceStream("SoundInTheory.DynamicImage.Resources." + gsDllFile)) using (var fileStream = File.OpenWrite(gsDllPath)) stream.CopyTo(fileStream); } }
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, 96, 96); Bitmap = new FastBitmap(File.ReadAllBytes(outputFileName)); } finally { File.Delete(outputFileName); } }
public static string ResolveFileName(ImageGenerationContext context, string filename) { string fileName = null; if (!Path.IsPathRooted(filename)) { if (context.HttpContext != null) fileName = context.HttpContext.Server.MapPath(filename); if (fileName == null) throw new InvalidOperationException("Could not resolve source filename."); } else { fileName = filename; } return fileName; }
protected override Effect GetEffect(ImageGenerationContext context, FastBitmap source) { SWMColor transparentColor; if (UseFirstPixel) { source.Lock(); transparentColor = source[0, 0]; source.Unlock(); } else { transparentColor = Color.ToWpfColor(); } return new ColorKeyEffect { ColorTolerance = ColorTolerance / 255.0, TransparentColor = transparentColor }; }
public override sealed 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; }
public static string ResolveFileName(ImageGenerationContext context, string filename) { string fileName = null; if (!Path.IsPathRooted(filename)) { if (context.HttpContext != null) { fileName = context.HttpContext.Server.MapPath(filename); } if (fileName == null) { throw new InvalidOperationException("Could not resolve source filename."); } } else { fileName = filename; } return(fileName); }
private static string GetCutyCaptPath(ImageGenerationContext context) { return(Path.Combine(GetCutyCaptFolder(context), "CutyCapt.exe")); }
private static string GetCairPath(ImageGenerationContext context) { return(Path.Combine(GetCairFolder(context), "CAIR.exe")); }