public void Test_StrokeMiterLimit()
    {
      using (MagickImage image = new MagickImage(MagickColors.SkyBlue, 100, 60))
      {
        Assert.AreEqual(10, image.Settings.StrokeMiterLimit);

        image.Settings.StrokeWidth = 5;
        image.Settings.StrokeColor = MagickColors.MediumSpringGreen;
        image.Settings.StrokeMiterLimit = 6;

        image.Draw(new DrawablePath(new PathMoveToAbs(65, 70), new PathLineToAbs(80, 20), new PathLineToAbs(95, 70)));

        ColorAssert.AreEqual(MagickColors.SkyBlue, image, 80, 18);
      }
    }
    private MagickImage CreateImage(int? density)
    {
      MagickImage image = new MagickImage(MagickColors.Purple, 500, 500);
      DrawableFontPointSize pointSize = new DrawableFontPointSize(20);
      DrawableText text = new DrawableText(250, 250, "Magick.NET");

      if (!density.HasValue)
        image.Draw(pointSize, text);
      else
        image.Draw(pointSize, new DrawableDensity(density.Value), text);

      image.Trim();

      return image;
    }
    public void Test_DrawablePaths()
    {
      using (MagickImage image = new MagickImage(MagickColor.Transparent, 100, 100))
      {
        List<IPath> paths = new List<IPath>();
        paths.Add(new PathArcAbs(new PathArc(50, 50, 20, 20, 45, true, false)));
        paths.Add(new PathArcRel(new PathArc(10, 10, 5, 5, 40, false, true)));
        paths.Add(new PathClosePath());
        paths.Add(new PathCurvetoAbs(new PathCurveto(80, 80, 10, 10, 60, 60)));
        paths.Add(new PathCurvetoRel(new PathCurveto(30, 30, 60, 60, 90, 90)));
        paths.Add(new PathLinetoAbs(new Coordinate(70, 70)));
        paths.Add(new PathLinetoHorizontalAbs(20));
        paths.Add(new PathLinetoHorizontalRel(90));
        paths.Add(new PathLinetoRel(new Coordinate(0, 0)));
        paths.Add(new PathLinetoVerticalAbs(70));
        paths.Add(new PathLinetoVerticalRel(30));
        paths.Add(new PathMovetoAbs(new Coordinate(50, 50)));
        paths.Add(new PathMovetoRel(new Coordinate(20, 20)));
        paths.Add(new PathQuadraticCurvetoAbs(new PathQuadraticCurveto(70, 70, 30, 30)));
        paths.Add(new PathQuadraticCurvetoRel(new PathQuadraticCurveto(10, 10, 40, 40)));
        paths.Add(new PathSmoothCurvetoAbs(new Coordinate(0, 0), new Coordinate(30, 30)));
        paths.Add(new PathSmoothCurvetoRel(new Coordinate(60, 60), new Coordinate(10, 10)));
        paths.Add(new PathSmoothQuadraticCurvetoAbs(new Coordinate(50, 50)));
        paths.Add(new PathSmoothQuadraticCurvetoRel(new Coordinate(80, 80)));

        image.Draw(new DrawablePath(paths));
      }
    }
    public void Test_Draw()
    {
      using (MagickImage image = new MagickImage(MagickColors.Fuchsia, 100, 100))
      {
        image.Draw(new Drawables()
          .FillColor(MagickColors.Red)
          .Rectangle(10, 10, 90, 90));

        ColorAssert.AreEqual(MagickColors.Fuchsia, image, 9, 9);
        ColorAssert.AreEqual(MagickColors.Red, image, 10, 10);
        ColorAssert.AreEqual(MagickColors.Red, image, 90, 90);
        ColorAssert.AreEqual(MagickColors.Fuchsia, image, 91, 91);
      }
    }
    public void Test_BorderColor()
    {
      using (MagickImage image = new MagickImage(MagickColors.MediumTurquoise, 10, 10))
      {
        ColorAssert.AreEqual(new MagickColor("#df"), image.Settings.BorderColor);

        image.Settings.FillColor = MagickColors.Beige;
        image.Settings.BorderColor = MagickColors.MediumTurquoise;
        image.Extent(20, 20, Gravity.Center, MagickColors.Aqua);
        image.Draw(new DrawableAlpha(0, 0, PaintMethod.FillToBorder));

        ColorAssert.AreEqual(MagickColors.Beige, image, 0, 0);
        ColorAssert.AreEqual(MagickColors.MediumTurquoise, image, 10, 10);
      }
    }
Exemple #6
0
    public void Test_Draw_Drawables()
    {
      using (MagickImage image = new MagickImage(MagickColors.Green, 100, 10))
      {
        image.Draw(new Drawables()
          .StrokeColor(MagickColors.Red)
          .StrokeWidth(5)
          .Paths()
          .LineToRel(10, 2)
          .LineToRel(80, 4));

        ColorAssert.AreEqual(MagickColors.Green, image, 9, 5);
        ColorAssert.AreEqual(MagickColors.Red, image, 55, 5);
        ColorAssert.AreEqual(MagickColors.Green, image, 90, 2);
        ColorAssert.AreEqual(MagickColors.Green, image, 90, 9);
      }
    }
Exemple #7
0
    public void Test_Draw_Paths()
    {
      using (MagickImage image = new MagickImage(MagickColors.Fuchsia, 100, 3))
      {
        image.Draw(new Paths()
          .LineToAbs(10, 1)
          .LineToAbs(90, 1));

        ColorAssert.AreEqual(MagickColors.Fuchsia, image, 9, 1);

        ColorAssert.AreEqual(MagickColors.Fuchsia, image, 10, 0);
        ColorAssert.AreEqual(MagickColors.Black, image, 10, 1);
        ColorAssert.AreEqual(MagickColors.Fuchsia, image, 10, 2);

        ColorAssert.AreEqual(MagickColors.Fuchsia, image, 90, 0);
        ColorAssert.AreEqual(MagickColors.Black, image, 90, 1);
        ColorAssert.AreEqual(MagickColors.Fuchsia, image, 90, 2);
      }
    }
 public void Test_Drawable()
 {
   using (MagickImage image = new MagickImage(Color.Red, 10, 10))
   {
     MagickColor yellow = Color.Yellow;
     image.Draw(new DrawableFillColor(yellow), new DrawableRectangle(0, 0, 10, 10));
     Test_Pixel_Equal(image, 5, 5, yellow);
   }
 }
Exemple #9
0
        private void DebugRiver(int index, WaterConfiguration river, List<Coordinate> riverCoordinates)
        {
            string debugFilename = string.Format("{0}\\debug\\rivers\\{1}_{2}_{3}.jpg", System.Windows.Forms.Application.StartupPath, zoneConfiguration.ZoneId, index, river.Name);

            if (index == 0)
            {
                DirectoryInfo di = new DirectoryInfo(Path.GetDirectoryName(debugFilename));
                if (di.Exists) di.EnumerateFiles().ToList().ForEach(f => f.Delete());
                else di.Create();
            }

            using (MagickImage debugRiver = new MagickImage(MagickColor.Transparent, zoneConfiguration.TargetMapSize, zoneConfiguration.TargetMapSize))
            {
                debugRiver.BackgroundColor = Color.White;
                debugRiver.FillColor = new MagickColor(0, 0, ushort.MaxValue, 256 * 128);

                double resizeFactor = zoneConfiguration.TargetMapSize / zoneConfiguration.Heightmap.Heightmap.Width;

                DrawablePolygon poly = new DrawablePolygon(riverCoordinates);
                debugRiver.Draw(poly);

                List<Coordinate> orginalCoords = river.GetCoordinates();
                for (int i = 0; i < riverCoordinates.Count(); i++)
                {
                    double x, y;

                    if (riverCoordinates[i].X > zoneConfiguration.TargetMapSize / 2) x = riverCoordinates[i].X - 15;
                    else x = riverCoordinates[i].X + 1;

                    if (riverCoordinates[i].Y < zoneConfiguration.TargetMapSize / 2) y = riverCoordinates[i].Y + 15;
                    else y = riverCoordinates[i].Y - 1;

                    debugRiver.FontPointsize = 14.0;
                    debugRiver.FillColor = Color.Black;
                    DrawableText text = new DrawableText(x, y, string.Format("{0} ({1}/{2})", i, orginalCoords[i].X, orginalCoords[i].Y));
                    debugRiver.Draw(text);
                }

                debugRiver.Quality = 100;
                debugRiver.Write(debugFilename);
            }
        }
    public void Test_StrokeAntiAlias()
    {
      using (MagickImage image = new MagickImage(MagickColors.Purple, 300, 300))
      {
        Assert.AreEqual(true, image.Settings.StrokeAntiAlias);

        image.Settings.StrokeWidth = 20;
        image.Settings.StrokeAntiAlias = false;
        image.Settings.StrokeColor = MagickColors.Orange;
        image.Draw(new DrawableCircle(150, 150, 100, 100));

        ColorAssert.AreEqual(MagickColors.Orange, image, 84, 103);
      }
    }
    public void Test_FillRule()
    {
      using (MagickImage image = new MagickImage(MagickColors.SkyBlue, 100, 60))
      {
        Assert.AreEqual(FillRule.EvenOdd, image.Settings.FillRule);

        image.Settings.FillRule = FillRule.Nonzero;
        image.Settings.FillColor = MagickColors.White;
        image.Settings.StrokeColor = MagickColors.Black;
        image.Draw(new DrawablePath(
          new PathMoveToAbs(40, 10),
          new PathLineToAbs(20, 20),
          new PathLineToAbs(70, 50),
          new PathClose(),
          new PathMoveToAbs(20, 40),
          new PathLineToAbs(70, 40),
          new PathLineToAbs(90, 10),
          new PathClose()
        ));

        ColorAssert.AreEqual(MagickColors.White, image, 50, 30);
      }
    }
Exemple #12
0
        public void Draw(MagickImage map)
        {
            MainForm.ProgressStart("Rendering water ...");

            using (PixelCollection heightmapPixels = zoneConfiguration.Heightmap.HeightmapScaled.GetReadOnlyPixels())
            {
                using (MagickImage water = new MagickImage(MagickColor.Transparent, zoneConfiguration.TargetMapSize, zoneConfiguration.TargetMapSize))
                {
                    int progressCounter = 0;

                    foreach (WaterConfiguration river in m_waterAreas)
                    {
                        MainForm.Log(river.Name + "...", MainForm.LogLevel.notice);

                        MagickColor fillColor;
                        if (m_useClientColors) fillColor = river.Color;
                        else fillColor = m_waterColor;
                        //water.FillColor = fillColor;

                        // Get the river coordinates and scale them to the targets size
                        List<Coordinate> riverCoordinates = river.GetCoordinates().Select(c => new Coordinate(c.X * zoneConfiguration.MapScale, c.Y * zoneConfiguration.MapScale)).ToList();

                        // Texture
                        using (MagickImage texture = new MagickImage((river.Type.ToLower() == "lava") ? GetLavaTexture() : GetWateryTexture()))
                        {
                            using (MagickImage pattern = new MagickImage(fillColor, texture.Width, texture.Height))
                            {
                                texture.Composite(pattern, 0, 0, CompositeOperator.DstIn);
                                texture.Composite(pattern, 0, 0, CompositeOperator.ColorDodge);

                                water.FillPattern = texture;
                                DrawablePolygon poly = new DrawablePolygon(riverCoordinates);
                                water.Draw(poly);
                            }
                        }

                        // get the min/max and just process them
                        int minX = Convert.ToInt32(riverCoordinates.Min(m => m.X)) - 10;
                        int maxX = Convert.ToInt32(riverCoordinates.Max(m => m.X)) + 10;
                        int minY = Convert.ToInt32(riverCoordinates.Min(m => m.Y)) - 10;
                        int maxY = Convert.ToInt32(riverCoordinates.Max(m => m.Y)) + 10;

                        using (WritablePixelCollection riverPixelCollection = water.GetWritablePixels())
                        {
                            for (int x = minX; x < maxX; x++)
                            {
                                if (x < 0) continue;
                                if (x >= zoneConfiguration.TargetMapSize) continue;

                                for (int y = minY; y < maxY; y++)
                                {
                                    if (y < 0) continue;
                                    if (y >= zoneConfiguration.TargetMapSize) continue;

                                    ushort pixelHeight = heightmapPixels.GetPixel(x, y).GetChannel(0);
                                    if (pixelHeight > river.Height)
                                    {
                                        Pixel newPixel = new Pixel(x, y, new ushort[] { 0, 0, 0, ushort.MinValue });
                                        riverPixelCollection.Set(newPixel);
                                    }
                                }
                            }
                        }

                        if (debug)
                        {
                            DebugRiver(progressCounter, river, riverCoordinates);
                        }

                        int percent = 100 * progressCounter / m_waterAreas.Count();
                        MainForm.ProgressUpdate(percent);
                        progressCounter++;
                    }

                    MainForm.ProgressStartMarquee("Merging...");

                    if (WaterTransparency != 0)
                    {
                        water.Alpha(AlphaOption.Set);
                        double divideValue = 100.0 / (100.0 - WaterTransparency);
                        water.Evaluate(Channels.Alpha, EvaluateOperator.Divide, divideValue);
                    }

                    water.Blur();
                    map.Composite(water, 0, 0, CompositeOperator.SrcOver);
                }
            }

            MainForm.ProgressReset();
        }
 public void Test_Drawable()
 {
   using (MagickImage image = new MagickImage(MagickColors.Red, 10, 10))
   {
     MagickColor yellow = MagickColors.Yellow;
     image.Draw(new DrawableFillColor(yellow), new DrawableRectangle(0, 0, 10, 10));
     ColorAssert.AreEqual(yellow, image, 5, 5);
   }
 }
    public void Test_Composite_ChangeMask()
    {
      using (MagickImage background = new MagickImage("xc:red", 100, 100))
      {
        background.BackgroundColor = Color.White;
        background.Extent(200, 100);

        IDrawable[] drawables = new IDrawable[]
        {
          new DrawablePointSize(50),
          new DrawableText(135, 70, "X")
        };

        using (MagickImage image = background.Clone())
        {
          image.Draw(drawables);
          image.Composite(background, Gravity.Center, CompositeOperator.ChangeMask);

          using (MagickImage result = new MagickImage(MagickColor.Transparent, 200, 100))
          {
            result.Draw(drawables);
            Assert.AreEqual(0.073, result.Compare(image, ErrorMetric.RootMeanSquared), 0.001);
          }
        }
      }
    }
    public void Test_StrokeDashArray()
    {
      using (MagickImage image = new MagickImage(MagickColors.SkyBlue, 100, 60))
      {
        Assert.AreEqual(null, image.Settings.StrokeDashArray);
        Assert.AreEqual(0, image.Settings.StrokeDashOffset);

        image.Settings.StrokeColor = MagickColors.Moccasin;
        image.Settings.StrokeDashArray = new double[] { 5.0, 8.0, 10.0 };
        image.Settings.StrokeDashOffset = 1;

        image.Draw(new DrawablePath(new PathMoveToAbs(10, 20), new PathLineToAbs(90, 20)));

        ColorAssert.AreEqual(MagickColors.Moccasin, image, 13, 20);
        ColorAssert.AreEqual(MagickColors.Moccasin, image, 37, 20);
        ColorAssert.AreEqual(MagickColors.Moccasin, image, 60, 20);
        ColorAssert.AreEqual(MagickColors.Moccasin, image, 84, 20);
      }
    }
    public void Test_StrokeLineCap()
    {
      using (MagickImage image = new MagickImage(MagickColors.SkyBlue, 100, 60))
      {
        Assert.AreEqual(LineCap.Butt, image.Settings.StrokeLineCap);

        image.Settings.StrokeWidth = 8;
        image.Settings.StrokeColor = MagickColors.Sienna;
        image.Settings.StrokeLineCap = LineCap.Round;

        image.Draw(new DrawablePath(new PathMoveToAbs(40, 20), new PathLineToAbs(40, 70)));

        ColorAssert.AreEqual(MagickColors.Sienna, image, 40, 17);
      }
    }
Exemple #17
0
    public void Test_Drawables_Exceptions()
    {
      ExceptionAssert.Throws<ArgumentException>(delegate ()
      {
        new DrawableBezier();
      });

      ExceptionAssert.Throws<ArgumentNullException>(delegate ()
      {
        new DrawableBezier(null);
      });

      ExceptionAssert.Throws<ArgumentException>(delegate ()
      {
        new DrawableBezier(new PointD[] { });
      });

      ExceptionAssert.Throws<ArgumentNullException>(delegate ()
      {
        new DrawableClipPath(null);
      });

      ExceptionAssert.Throws<ArgumentException>(delegate ()
      {
        new DrawableClipPath("");
      });

      ExceptionAssert.Throws<ArgumentNullException>(delegate ()
      {
        new DrawableComposite(null, new MagickImage(Files.Builtin.Logo));
      });

      ExceptionAssert.Throws<ArgumentNullException>(delegate ()
      {
        new DrawableComposite(new MagickGeometry(), null);
      });

      ExceptionAssert.Throws<ArgumentNullException>(delegate ()
      {
        new DrawableFillColor(null);
      });

      ExceptionAssert.Throws<ArgumentNullException>(delegate ()
      {
        new DrawableFont(null);
      });

      ExceptionAssert.Throws<ArgumentException>(delegate ()
      {
        new DrawableFont("");
      });

      ExceptionAssert.Throws<MagickDrawErrorException>(delegate ()
      {
        using (MagickImage image = new MagickImage(Files.Builtin.Wizard))
        {
          image.Draw(new DrawableFillPatternUrl("#fail"));
        }
      });

      ExceptionAssert.Throws<ArgumentException>(delegate ()
      {
        new DrawablePolygon(new PointD[] { new PointD(0, 0) });
      });

      ExceptionAssert.Throws<ArgumentException>(delegate ()
      {
        new DrawablePolyline(new PointD[] { new PointD(0, 0), new PointD(0, 0) });
      });

      ExceptionAssert.Throws<ArgumentNullException>(delegate ()
      {
        new DrawableStrokeColor(null);
      });

      ExceptionAssert.Throws<ArgumentNullException>(delegate ()
      {
        new DrawableText(0, 0, null);
      });

      ExceptionAssert.Throws<ArgumentException>(delegate ()
      {
        new DrawableText(0, 0, "");
      });

      ExceptionAssert.Throws<ArgumentNullException>(delegate ()
      {
        new DrawableTextEncoding(null);
      });
    }
    public void Test_StrokeWidth()
    {
      using (MagickImage image = new MagickImage(MagickColors.Purple, 300, 300))
      {
        Assert.AreEqual(1, image.Settings.StrokeWidth);
        Assert.AreEqual(new MagickColor(0, 0, 0, 0), image.Settings.StrokeColor);

        image.Settings.StrokeWidth = 40;
        image.Settings.StrokeColor = MagickColors.Orange;
        image.Draw(new DrawableCircle(150, 150, 100, 100));

        ColorAssert.AreEqual(MagickColors.Black, image, 150, 150);
        ColorAssert.AreEqual(MagickColors.Orange, image, 201, 150);
        ColorAssert.AreEqual(MagickColors.Purple, image, 244, 150);
      }
    }
    public void Test_StrokeLineJoin()
    {
      using (MagickImage image = new MagickImage(MagickColors.SkyBlue, 100, 60))
      {
        Assert.AreEqual(LineJoin.Miter, image.Settings.StrokeLineJoin);

        image.Settings.StrokeWidth = 5;
        image.Settings.StrokeColor = MagickColors.LemonChiffon;
        image.Settings.StrokeLineJoin = LineJoin.Round;

        image.Draw(new DrawablePath(new PathMoveToAbs(75, 70), new PathLineToAbs(90, 20), new PathLineToAbs(105, 70)));

        ColorAssert.AreEqual(MagickColors.SkyBlue, image, 90, 12);
      }
    }
Exemple #20
0
        private void DrawTreeCluster(MagickImage overlay, DrawableFixture fixture)
        {
            //MainForm.Log(string.Format("Image: {0} ({1}) ...", fixture.Name, fixture.TreeCluster.Tree), MainForm.LogLevel.notice);
            string fileName = System.IO.Path.GetFileNameWithoutExtension(fixture.TreeCluster.Tree);
            string defaultTree = "elm1";

            // Load model image
            if (!m_modelImages.ContainsKey(fileName))
            {
                string treeImageFile = string.Format("{0}\\data\\prerendered\\trees\\{1}.png", System.Windows.Forms.Application.StartupPath, fileName);
                if (System.IO.File.Exists(treeImageFile))
                {
                    MagickImage modelImage = new MagickImage(treeImageFile);
                    modelImage.Blur();
                    m_modelImages.Add(fileName, modelImage);
                }
                else
                {
                    MainForm.Log(string.Format("Can not find image for tree {0} ({1}), using default tree", fixture.TreeCluster.Tree, fixture.NifName), MainForm.LogLevel.warning);
                    m_modelImages.Add(fileName, m_modelImages[defaultTree]);
                }
            }

            if (m_modelImages.ContainsKey(fileName) && m_modelImages[fileName] != null)
            {
                // Get the width of the orginal tree shape
                NifRow tree = FixturesLoader.NifRows.Where(n => n.Filename.ToLower() == fixture.TreeCluster.Tree.ToLower()).FirstOrDefault();
                if (tree == null) return;

                System.Drawing.SizeF treeSize = tree.GetSize(0, 0);

                int dimensions = ((fixture.CanvasWidth > fixture.CanvasHeight) ? fixture.CanvasWidth : fixture.CanvasHeight) + 10;
                int extendedWidth = dimensions - fixture.CanvasWidth;
                int extendedHeight = dimensions - fixture.CanvasHeight;

                using (MagickImage treeCluster = new MagickImage(MagickColor.Transparent, dimensions, dimensions))
                {
                    double centerX = treeCluster.Width / 2d;
                    double centerY = treeCluster.Height / 2d;

                    foreach (SharpDX.Vector3 treeInstance in fixture.TreeCluster.TreeInstances)
                    {
                        using (MagickImage treeImage = m_modelImages[fileName].Clone())
                        {
                            double scaleWidthToTreeImage = treeSize.Width / treeImage.Width;
                            double scaleHeightToTreeImage = treeSize.Height / treeImage.Height;
                            int width = Convert.ToInt32(treeImage.Width * scaleWidthToTreeImage * fixture.Scale);
                            int height = Convert.ToInt32(treeImage.Height * scaleHeightToTreeImage * fixture.Scale);
                            treeImage.Resize(width, height);

                            int x = Convert.ToInt32(centerX - width / 2d - zoneConfiguration.ZoneCoordinateToMapCoordinate(treeInstance.X) * (fixture.FixtureRow.Scale / 100));
                            int y = Convert.ToInt32(centerY - height / 2d - zoneConfiguration.ZoneCoordinateToMapCoordinate(treeInstance.Y) * (fixture.FixtureRow.Scale / 100));
                            treeCluster.Composite(treeImage, x, y, CompositeOperator.SrcOver);
                        }
                    }

                    treeCluster.Rotate((360d * fixture.FixtureRow.AxisZ3D - fixture.FixtureRow.A) * -1);

                    using (MagickImage modelCanvas = new MagickImage(MagickColor.Transparent, fixture.CanvasWidth, fixture.CanvasHeight))
                    {
                        foreach (DrawableElement drawableElement in fixture.DrawableElements)
                        {
                            modelCanvas.FillColor = new MagickColor(
                                Convert.ToUInt16(128 * 256 * drawableElement.lightning),
                                Convert.ToUInt16(128 * 256 * drawableElement.lightning),
                                Convert.ToUInt16(128 * 256 * drawableElement.lightning)
                            );

                            DrawablePolygon polyDraw = new DrawablePolygon(drawableElement.coordinates);
                            modelCanvas.Draw(polyDraw);
                        }

                        modelCanvas.Composite(treeCluster, Gravity.Center, CompositeOperator.DstIn);
                        treeCluster.Composite(modelCanvas, Gravity.Center, CompositeOperator.Overlay);
                        //treeCluster.Composite(modelCanvas, Gravity.Center, CompositeOperator.SrcOver);
                    }

                    if (fixture.RendererConf.HasShadow)
                    {
                        treeCluster.BorderColor = MagickColor.Transparent;
                        treeCluster.Border(1);
                        treeCluster.Shadow(
                            fixture.RendererConf.ShadowOffsetX,
                            fixture.RendererConf.ShadowOffsetY,
                            fixture.RendererConf.ShadowSize,
                            new Percentage(100 - fixture.RendererConf.ShadowTransparency),
                            fixture.RendererConf.ShadowColor
                        );
                    }

                    if (fixture.RendererConf.Transparency != 0)
                    {
                        treeCluster.Alpha(AlphaOption.Set);

                        double divideValue = 100.0 / (100.0 - fixture.RendererConf.Transparency);
                        treeCluster.Evaluate(Channels.Alpha, EvaluateOperator.Divide, divideValue);
                    }

                    overlay.Composite(treeCluster, Convert.ToInt32(fixture.CanvasX - extendedWidth/2), Convert.ToInt32(fixture.CanvasY - extendedHeight/2), CompositeOperator.SrcOver);
                }
            }
        }
Exemple #21
0
        private void DrawTree(MagickImage overlay, DrawableFixture fixture)
        {
            System.Drawing.Color testColor = System.Drawing.ColorTranslator.FromHtml("#5e683a");

            using (MagickImage pattern = new MagickImage(MagickColor.Transparent, fixture.CanvasWidth, fixture.CanvasHeight))
            {
                using (MagickImage patternTexture = new MagickImage(string.Format("{0}\\data\\textures\\{1}.png", System.Windows.Forms.Application.StartupPath, "leaves_mask")))
                {
                    patternTexture.Resize(fixture.CanvasWidth / 2, fixture.CanvasHeight / 2);
                    pattern.Texture(patternTexture);

                    Random rnd = new Random();
                    pattern.Rotate(rnd.Next(0, 360));

                    using (MagickImage modelCanvas = new MagickImage(MagickColor.Transparent, fixture.CanvasWidth, fixture.CanvasHeight))
                    {
                        foreach (DrawableElement drawableElement in fixture.DrawableElements)
                        {
                            DrawablePolygon polyDraw = new DrawablePolygon(drawableElement.coordinates);

                            // A Shaded model without lightning is not shaded... but just we add this just be flexible
                            if (fixture.RendererConf.HasLight)
                            {
                                float r, g, b, light;

                                light = (float)drawableElement.lightning * 2f;
                                r = fixture.Tree.AverageColor.R * light;
                                g = fixture.Tree.AverageColor.G * light;
                                b = fixture.Tree.AverageColor.B * light;

                                modelCanvas.FillColor = new MagickColor(
                                    Convert.ToUInt16(r * 255),
                                    Convert.ToUInt16(g * 255),
                                    Convert.ToUInt16(b * 255)
                                );
                            }
                            else
                            {
                                modelCanvas.FillColor = fixture.RendererConf.Color;
                            }

                            modelCanvas.Draw(polyDraw);
                        }

                        // Add leaves pattern
                        pattern.Composite(modelCanvas, Gravity.Center, CompositeOperator.DstIn);
                        modelCanvas.Composite(pattern, Gravity.Center, CompositeOperator.CopyAlpha);

                        if (fixture.RendererConf.HasShadow)
                        {
                            modelCanvas.BorderColor = MagickColor.Transparent;
                            modelCanvas.Border((int)fixture.RendererConf.ShadowSize);
                            modelCanvas.Shadow(
                                fixture.RendererConf.ShadowOffsetX,
                                fixture.RendererConf.ShadowOffsetY,
                                fixture.RendererConf.ShadowSize,
                                new Percentage(100 - fixture.RendererConf.ShadowTransparency),
                                fixture.RendererConf.ShadowColor
                            );

                            // Update the canvas position to match the new border
                            fixture.CanvasX -= fixture.RendererConf.ShadowSize;
                            fixture.CanvasY -= fixture.RendererConf.ShadowSize;
                        }

                        if (fixture.RendererConf.Transparency != 0)
                        {
                            modelCanvas.Alpha(AlphaOption.Set);

                            double divideValue = 100.0 / (100.0 - fixture.RendererConf.Transparency);
                            modelCanvas.Evaluate(Channels.Alpha, EvaluateOperator.Divide, divideValue);
                        }

                        overlay.Composite(modelCanvas, Convert.ToInt32(fixture.CanvasX), Convert.ToInt32(fixture.CanvasY), CompositeOperator.SrcOver);
                    }
                }
            }
        }
    public void Test_FillPattern()
    {
      using (MagickImage image = new MagickImage(MagickColors.Transparent, 500, 500))
      {
        Assert.AreEqual(null, image.Settings.FillPattern);

        image.Settings.FillPattern = new MagickImage(Files.SnakewarePNG);

        PointD[] coordinates = new PointD[4];
        coordinates[0] = new PointD(50, 50);
        coordinates[1] = new PointD(150, 50);
        coordinates[2] = new PointD(150, 150);
        coordinates[3] = new PointD(50, 150);
        image.Draw(new DrawablePolygon(coordinates));

        ColorAssert.AreEqual(new MagickColor("#02"), image, 84, 80);
      }
    }
Exemple #23
0
        private void DrawImage(MagickImage overlay, DrawableFixture fixture)
        {
            //MainForm.Log(string.Format("Image: {0} ({1}) ...", fixture.Name, fixture.NifName), MainForm.LogLevel.notice);
            string fileName = System.IO.Path.GetFileNameWithoutExtension(fixture.NifName);
            string defaultTree = "elm1";

            // Load default tree
            if (!m_modelImages.ContainsKey(defaultTree))
            {
                string defaultTreeImage = string.Format("{0}\\data\\prerendered\\trees\\{1}.png", System.Windows.Forms.Application.StartupPath, defaultTree);
                if (System.IO.File.Exists(defaultTreeImage))
                {
                    MagickImage treeImage = new MagickImage(defaultTreeImage);
                    treeImage.Blur();
                    m_modelImages.Add(defaultTree, treeImage);
                }
                else
                {
                    m_modelImages.Add(fileName, null);
                }
            }

            // TreeClusters are sets of trees in a specified arrangement
            // They need to be drawe separately
            if (fixture.IsTreeCluster)
            {
                DrawTreeCluster(overlay, fixture);
                return;
            }

            // Load model image
            if (!m_modelImages.ContainsKey(fileName))
            {
                string objectImageFile = string.Format("{0}\\data\\prerendered\\objects\\{1}.png", System.Windows.Forms.Application.StartupPath, fileName);
                if (fixture.IsTree) objectImageFile = string.Format("{0}\\data\\prerendered\\trees\\{1}.png", System.Windows.Forms.Application.StartupPath, fileName);

                if (System.IO.File.Exists(objectImageFile))
                {
                    MagickImage objectImage = new MagickImage(objectImageFile);
                    if(fixture.IsTree) objectImage.Blur();
                    m_modelImages.Add(fileName, objectImage);
                }
                else
                {
                    if (fixture.IsTree)
                    {
                        MainForm.Log(string.Format("Can not find image for tree {0} ({1}), using default tree", fixture.Name, fixture.NifName), MainForm.LogLevel.warning);
                        m_modelImages.Add(fileName, m_modelImages[defaultTree]);
                    }
                    else m_modelImages.Add(fileName, null);
                }
            }

            // Draw the image
            if (m_modelImages.ContainsKey(fileName) && m_modelImages[fileName] != null)
            {
                NifRow orginalNif = FixturesLoader.NifRows.Where(n => n.NifId == fixture.FixtureRow.NifId).FirstOrDefault();
                if (orginalNif == null)
                {
                    MainForm.Log(string.Format("Error with imaged nif ({0})!", fixture.FixtureRow.TextualName), MainForm.LogLevel.warning);
                }

                System.Drawing.SizeF objectSize = orginalNif.GetSize(0, 0);

                // The final image
                using (MagickImage modelImage = new MagickImage(MagickColor.Transparent, fixture.CanvasWidth, fixture.CanvasHeight))
                {
                    // Place the replacing image
                    using (MagickImage newModelImage = m_modelImages[fileName].Clone())
                    {
                        newModelImage.BackgroundColor = MagickColor.Transparent;

                        double scaleWidthToTreeImage = objectSize.Width / newModelImage.Width;
                        double scaleHeightToTreeImage = objectSize.Height / newModelImage.Height;
                        int width = Convert.ToInt32(newModelImage.Width * scaleWidthToTreeImage * fixture.Scale);
                        int height = Convert.ToInt32(newModelImage.Height * scaleHeightToTreeImage * fixture.Scale);

                        // Resize to new size
                        newModelImage.FilterType = FilterType.Gaussian;
                        newModelImage.VirtualPixelMethod = VirtualPixelMethod.Transparent;
                        newModelImage.Resize(width, height);

                        // Rotate the image
                        //newModelImage.Rotate(fixture.FixtureRow.A * -1 * fixture.FixtureRow.AxisZ3D);
                        newModelImage.Rotate((360d * fixture.FixtureRow.AxisZ3D - fixture.FixtureRow.A) * -1);

                        // Place in center of modelImage
                        modelImage.Composite(newModelImage, Gravity.Center, CompositeOperator.SrcOver);
                    }

                    // Draw the shaped model if wanted
                    if (fixture.RendererConf.HasLight)
                    {
                        using (MagickImage modelShaped = new MagickImage(MagickColor.Transparent, fixture.CanvasWidth, fixture.CanvasHeight))
                        {
                            foreach (DrawableElement drawableElement in fixture.DrawableElements)
                            {
                                var light = 1 - drawableElement.lightning;
                                modelShaped.FillColor = new MagickColor(
                                    Convert.ToUInt16(ushort.MaxValue * light),
                                    Convert.ToUInt16(ushort.MaxValue * light),
                                    Convert.ToUInt16(ushort.MaxValue * light)
                                );

                                DrawablePolygon polyDraw = new DrawablePolygon(drawableElement.coordinates);
                                modelShaped.Draw(polyDraw);
                            }

                            using(MagickImage modelMask = new MagickImage(MagickColor.Transparent, fixture.CanvasWidth, fixture.CanvasHeight))
                            {
                                modelShaped.Blur();
                                modelMask.Composite(modelShaped, 0, 0, CompositeOperator.DstAtop);
                                modelMask.Composite(modelImage, 0, 0, CompositeOperator.DstIn);
                                modelMask.Level(new Percentage(20), new Percentage(100), Channels.All);
                                modelImage.Composite(modelMask, 0, 0, CompositeOperator.ColorDodge);
                            }
                        }
                    }

                    // Add the shadow if not a tree (tree shadow are substituted by a treeoverlay)
                    if (fixture.RendererConf.HasShadow && !fixture.IsTree)
                    {
                        modelImage.BorderColor = MagickColor.Transparent;
                        modelImage.Border((int)fixture.RendererConf.ShadowSize);
                        modelImage.Shadow(
                            fixture.RendererConf.ShadowOffsetX,
                            fixture.RendererConf.ShadowOffsetY,
                            fixture.RendererConf.ShadowSize,
                            new Percentage(100 - fixture.RendererConf.ShadowTransparency),
                            fixture.RendererConf.ShadowColor
                        );

                        // Update the canvas position to match the new border
                        fixture.CanvasX -= fixture.RendererConf.ShadowSize;
                        fixture.CanvasY -= fixture.RendererConf.ShadowSize;
                    }

                    // Set transprency if not a tree (see shadow)
                    if (fixture.RendererConf.Transparency != 0 && !fixture.IsTree)
                    {
                        double divideValue = 100.0 / (100.0 - fixture.RendererConf.Transparency);
                        modelImage.Evaluate(Channels.Alpha, EvaluateOperator.Divide, divideValue);
                    }

                    // Place the image on the right position
                    overlay.Composite(modelImage, Convert.ToInt32(fixture.CanvasX), Convert.ToInt32(fixture.CanvasY), CompositeOperator.SrcOver);
                }
            }
        }
Exemple #24
0
        private void DrawShaded(MagickImage overlay, DrawableFixture fixture)
        {
            //MainForm.Log(string.Format("Shaded: {0} ({1}) ...", fixture.Name, fixture.NifName), MainForm.LogLevel.notice);

            using (MagickImage modelCanvas = new MagickImage(MagickColor.Transparent, fixture.CanvasWidth, fixture.CanvasHeight))
            {
                foreach (DrawableElement drawableElement in fixture.DrawableElements)
                {
                    DrawablePolygon polyDraw = new DrawablePolygon(drawableElement.coordinates);

                    // A Shaded model without lightning is not shaded... but just we add this just be flexible
                    if (fixture.RendererConf.HasLight)
                    {
                        modelCanvas.FillColor = new MagickColor(
                            Convert.ToUInt16(drawableElement.lightning * fixture.RendererConf.Color.R),
                            Convert.ToUInt16(drawableElement.lightning * fixture.RendererConf.Color.G),
                            Convert.ToUInt16(drawableElement.lightning * fixture.RendererConf.Color.B)
                        );
                    }
                    else
                    {
                        modelCanvas.FillColor = fixture.RendererConf.Color;
                    }

                    modelCanvas.Draw(polyDraw);

                }

                if (fixture.RendererConf.HasShadow)
                {
                    modelCanvas.BorderColor = MagickColor.Transparent;
                    modelCanvas.Border((int)fixture.RendererConf.ShadowSize);
                    modelCanvas.Shadow(
                        fixture.RendererConf.ShadowOffsetX,
                        fixture.RendererConf.ShadowOffsetY,
                        fixture.RendererConf.ShadowSize,
                        new Percentage(100 - fixture.RendererConf.ShadowTransparency),
                        fixture.RendererConf.ShadowColor
                    );

                    // Update the canvas position to match the new border
                    fixture.CanvasX -= fixture.RendererConf.ShadowSize;
                    fixture.CanvasY -= fixture.RendererConf.ShadowSize;
                }

                if (fixture.RendererConf.Transparency != 0)
                {
                    modelCanvas.Alpha(AlphaOption.Set);

                    double divideValue = 100.0 / (100.0 - fixture.RendererConf.Transparency);
                    modelCanvas.Evaluate(Channels.Alpha, EvaluateOperator.Divide, divideValue);
                }

                overlay.Composite(modelCanvas, Convert.ToInt32(fixture.CanvasX), Convert.ToInt32(fixture.CanvasY), CompositeOperator.SrcOver);
            }
        }
Exemple #25
0
        private void DebugMaps()
        {
            MainForm.Log("Drawing debug bound images ...", MainForm.LogLevel.warning);
            MainForm.ProgressStartMarquee("Debug bound images ...");

            DirectoryInfo debugDir = new DirectoryInfo(string.Format("{0}\\debug\\bound\\{1}", System.Windows.Forms.Application.StartupPath, zoneConfiguration.ZoneId));
            if (!debugDir.Exists) debugDir.Create();
            debugDir.GetFiles().ToList().ForEach(f => f.Delete());

            int boundIndex = 0;
            foreach (List<PointF> allCoords in m_bounds)
            {
                using (MagickImage bound = new MagickImage(MagickColor.Transparent, zoneConfiguration.TargetMapSize, zoneConfiguration.TargetMapSize))
                {
                    List<Coordinate> coords = allCoords.Select(c => new Coordinate(zoneConfiguration.ZoneCoordinateToMapCoordinate(c.X), zoneConfiguration.ZoneCoordinateToMapCoordinate(c.Y))).ToList();

                    DrawablePolygon poly = new DrawablePolygon(coords);
                    bound.FillColor = new MagickColor(0, 0, 0, 256 * 128);
                    bound.Draw(poly);

                    // Print Text
                    for (int i = 0; i < coords.Count; i++)
                    {
                        double x, y;

                        if (coords[i].X > zoneConfiguration.TargetMapSize / 2) x = coords[i].X - 15;
                        else x = coords[i].X + 1;

                        if (coords[i].Y < zoneConfiguration.TargetMapSize / 2) y = coords[i].Y + 15;
                        else y = coords[i].Y - 1;

                        bound.FontPointsize = 10.0;
                        bound.FillColor = Color.Black;
                        DrawableText text = new DrawableText(x, y, string.Format("{0} ({1}/{2})", i, zoneConfiguration.MapCoordinateToZoneCoordinate(coords[i].X), zoneConfiguration.MapCoordinateToZoneCoordinate(coords[i].Y)));
                        bound.Draw(text);

                        using (WritablePixelCollection pixels = bound.GetWritablePixels())
                        {
                            int x2, y2;
                            if (coords[i].X == zoneConfiguration.TargetMapSize) x2 = zoneConfiguration.TargetMapSize - 1;
                            else x2 = (int)coords[i].X;
                            if (coords[i].Y == zoneConfiguration.TargetMapSize) y2 = zoneConfiguration.TargetMapSize - 1;
                            else y2 = (int)coords[i].Y;

                            pixels.Set(x2, y2, new ushort[] { 0, 0, 65535, 0 });
                        }
                    }

                    //bound.Quality = 100;
                    bound.Write(string.Format("{0}\\bound_{1}.png", debugDir.FullName, boundIndex));

                    boundIndex++;
                }
            }

            MainForm.ProgressReset();
        }
Exemple #26
0
        /// <summary>
        /// Draw the bounds onto map
        /// </summary>
        /// <param name="map"></param>
        public void Draw(MagickImage map)
        {
            if (m_bounds.Count == 0) return;
            MainForm.ProgressStart("Drawing zone bounds ...");

            // Sort the polygons
            List<List<Coordinate>> polygons = new List<List<Coordinate>>();
            List<List<Coordinate>> negatedPolygons = new List<List<Coordinate>>();

            foreach (List<PointF> polygon in m_bounds)
            {
                bool isClockwise = Tools.PolygonHasClockwiseOrder(polygon);
                var polygonConverted = polygon.Select(c => new Coordinate(zoneConfiguration.ZoneCoordinateToMapCoordinate(c.X), zoneConfiguration.ZoneCoordinateToMapCoordinate(c.Y))).ToList();

                // polygons in clockwise order needs to be negated
                if (isClockwise) negatedPolygons.Add(polygonConverted);
                else polygons.Add(polygonConverted);
            }

            MagickColor backgroundColor = MagickColor.Transparent;
            if (polygons.Count == 0) {
                // There are no normal polygons, we need to fill the hole zone and substract negatedPolygons
                backgroundColor = m_boundsColor;
            }

            using (MagickImage boundMap = new MagickImage(backgroundColor, zoneConfiguration.TargetMapSize, zoneConfiguration.TargetMapSize))
            {
                int progressCounter = 0;

                boundMap.Alpha(AlphaOption.Set);
                boundMap.FillColor = m_boundsColor;
                foreach (List<Coordinate> coords in polygons)
                {
                    DrawablePolygon poly = new DrawablePolygon(coords);
                    boundMap.Draw(poly);

                    progressCounter++;
                    int percent = 100 * progressCounter / m_bounds.Count();
                    MainForm.ProgressUpdate(percent);
                }

                if (negatedPolygons.Count > 0)
                {
                    using (MagickImage negatedBoundMap = new MagickImage(Color.Transparent, zoneConfiguration.TargetMapSize, zoneConfiguration.TargetMapSize))
                    {
                        negatedBoundMap.FillColor = m_boundsColor;

                        foreach (List<Coordinate> coords in negatedPolygons)
                        {
                            DrawablePolygon poly = new DrawablePolygon(coords);
                            negatedBoundMap.Draw(poly);

                            progressCounter++;
                            int percent = 100 * progressCounter / m_bounds.Count();
                            MainForm.ProgressUpdate(percent);
                        }
                        boundMap.Composite(negatedBoundMap, 0, 0, CompositeOperator.DstOut);
                    }
                }

                MainForm.ProgressStartMarquee("Merging ...");
                if (ExcludeFromMap)
                {
                    map.Composite(boundMap, 0, 0, CompositeOperator.DstOut);
                }
                else
                {
                    if (m_transparency != 0)
                    {
                        boundMap.Alpha(AlphaOption.Set);
                        double divideValue = 100.0 / (100.0 - m_transparency);
                        boundMap.Evaluate(Channels.Alpha, EvaluateOperator.Divide, divideValue);
                    }

                    map.Composite(boundMap, 0, 0, CompositeOperator.SrcOver);
                }
            }

            if (debug)
            {
                DebugMaps();
            }

            MainForm.ProgressReset();
        }
Exemple #27
0
    private void ExecuteDraw(XmlElement element, MagickImage image)
    {
      Collection<IDrawable> drawables = new Collection<IDrawable>();

      foreach (XmlElement elem in element.SelectNodes("*"))
      {
        ExecuteIDrawable(elem, drawables);
      }

      image.Draw(drawables);
    }
Exemple #28
-1
    public void Test_Drawables()
    {
      Coordinate[] coordinates = new Coordinate[3];
      coordinates[0] = new Coordinate(0, 0);
      coordinates[1] = new Coordinate(50, 50);
      coordinates[2] = new Coordinate(99, 99);

      using (MagickImage image = new MagickImage(MagickColor.Transparent, 100, 100))
      {
        image.Draw(new DrawableAffine(0, 0, 1, 1, 2, 2));
        image.Draw(new DrawableArc(0, 0, 10, 10, 45, 90));
        image.Draw(new DrawableBezier(coordinates));
        image.Draw(new DrawableCircle(0, 0, 50, 50));
        image.Draw(new DrawableClipPath("foo"));
        image.Draw(new DrawableColor(0, 0, PaintMethod.Floodfill));

        using (MagickImage compositeImage = new MagickImage(new MagickColor("red"), 50, 50))
        {
          image.Draw(new DrawableCompositeImage(0, 0, compositeImage));
          image.Draw(new DrawableCompositeImage(0, 0, CompositeOperator.Over, compositeImage));
          image.Draw(new DrawableCompositeImage(new MagickGeometry(50, 50, 10, 10), compositeImage));
          image.Draw(new DrawableCompositeImage(new MagickGeometry(50, 50, 10, 10), CompositeOperator.Over, compositeImage));
        }

        image.Draw(new DrawableDashArray(new double[2] { 10, 20 }));
        image.Draw(new DrawableDashOffset(2));
        image.Draw(new DrawableDensity(97));
        image.Draw(new DrawableEllipse(10, 10, 4, 4, 0, 360));
        image.Draw(new DrawableFillOpacity(new Percentage(50)));
        image.Draw(new DrawableFillColor(Color.Red));
        image.Draw(new DrawableFont("Arial"));
        image.Draw(new DrawableGravity(Gravity.Center));
        image.Draw(new DrawableLine(20, 20, 40, 40));
        image.Draw(new DrawableMiterLimit(5));
        image.Draw(new DrawableOpacity(0, 0, PaintMethod.Floodfill));
        image.Draw(new DrawablePoint(60, 60));
        image.Draw(new DrawablePointSize(5));
        image.Draw(new DrawablePolygon(coordinates));
        image.Draw(new DrawablePolyline(coordinates));
        image.Draw(new DrawableRectangle(30, 30, 70, 70));
        image.Draw(new DrawableRotation(180));
        image.Draw(new DrawableRoundRectangle(50, 50, 30, 30, 70, 70));
        image.Draw(new DrawableScaling(15, 15));
        image.Draw(new DrawableSkewX(90));
        image.Draw(new DrawableSkewY(90));
        image.Draw(new DrawableStrokeAntialias(true));
        image.Draw(new DrawableStrokeColor(Color.Purple));
        image.Draw(new DrawableStrokeLineCap(LineCap.Square));
        image.Draw(new DrawableStrokeLineJoin(LineJoin.Bevel));
        image.Draw(new DrawableStrokeOpacity(new Percentage(80)));
        image.Draw(new DrawableStrokeWidth(4));
        image.Draw(new DrawableText(0, 60, "test"));
        image.Draw(new DrawableTextAntialias(true));
        image.Draw(new DrawableTextDecoration(TextDecoration.LineThrough));
        image.Draw(new DrawableTextDirection(TextDirection.RightToLeft));
        image.Draw(new DrawableTextInterlineSpacing(4));
        image.Draw(new DrawableTextInterwordSpacing(6));
        image.Draw(new DrawableTextKerning(2));
        image.Draw(new DrawableTextUnderColor(Color.Yellow));
        image.Draw(new DrawableTranslation(65, 65));
        image.Draw(new DrawableViewbox(0, 0, 100, 100));

        image.Draw(new DrawablePushClipPath("#1"), new DrawablePopClipPath());
        image.Draw(new DrawablePushPattern("test", 30, 30, 10, 10), new DrawablePopPattern());
        image.Draw(new DrawablePushGraphicContext(), new DrawablePopGraphicContext());
      }
    }
    public void Test_StrokePattern()
    {
      using (MagickImage image = new MagickImage(MagickColors.Red, 250, 100))
      {
        Assert.AreEqual(null, image.Settings.StrokePattern);

        image.Settings.StrokeWidth = 40;
        image.Settings.StrokePattern = new MagickImage(Files.Builtin.Logo);

        image.Draw(new DrawableLine(50, 50, 200, 50));

        ColorAssert.AreEqual(MagickColors.Red, image, 49, 50);
        ColorAssert.AreEqual(MagickColors.White, image, 50, 50);
        ColorAssert.AreEqual(MagickColors.White, image, 50, 70);
        ColorAssert.AreEqual(MagickColors.White, image, 200, 50);
        ColorAssert.AreEqual(MagickColors.White, image, 200, 70);
        ColorAssert.AreEqual(MagickColors.Red, image, 201, 50);
      }
    }