Ejemplo n.º 1
0
 private static void Test_Pixel_NotEqual(MagickImage image, int x, int y, MagickColor color)
 {
   using (PixelCollection collection = image.GetReadOnlyPixels())
   {
     ColorAssert.AreNotEqual(color, collection.GetPixel(x, y));
   }
 }
Ejemplo n.º 2
0
 public void Test_IEnumerable()
 {
   using (MagickImage image = new MagickImage(Color.Red, 5, 10))
   {
     PixelCollection pixels = image.GetReadOnlyPixels();
     Assert.AreEqual(50, pixels.Count());
   }
 }
Ejemplo n.º 3
0
 public void Test_Dimensions()
 {
   using (MagickImage image = new MagickImage(Color.Red, 5, 10))
   {
     using (PixelCollection pixels = image.GetReadOnlyPixels())
     {
       Assert.AreEqual(5, pixels.Width);
       Assert.AreEqual(10, pixels.Height);
       Assert.AreEqual(5 * 10 * pixels.Channels, pixels.GetValues().Length);
     }
   }
 }
Ejemplo n.º 4
0
    public void Test_GetValues()
    {
      using (MagickImage image = new MagickImage(Color.PowderBlue, 1, 1))
      {
        using (PixelCollection pixels = image.GetReadOnlyPixels())
        {
          var values = pixels.GetValues();
          Assert.AreEqual(3, values.Length);

          MagickColor color = new MagickColor(values[0], values[1], values[2]);
          ColorAssert.AreEqual(Color.PowderBlue, color);
        }
      }
    }
Ejemplo n.º 5
0
    public void Test_Colors()
    {
      using (MagickImage image = new MagickImage(Files.Coders.PlayerPSD))
      {
        using (PixelCollection pixels = image.GetReadOnlyPixels())
        {
          Pixel pixel = pixels.GetPixel(0, 0);
          ColorAssert.AreEqual(MagickColor.Transparent, pixel.ToColor());

          pixel = pixels.GetPixel(8, 6);
          ColorAssert.AreEqual(Color.FromArgb(15, 43, 255), pixel.ToColor());
        }
      }
    }
    private static void TestPixels(MagickImage image, MagickColor color)
    {
      using (MemoryStream memStream = new MemoryStream())
      {
        image.Format = MagickFormat.Png;
        image.Write(memStream);
        memStream.Position = 0;

        using (MagickImage output = new MagickImage(memStream))
        {
          using (PixelCollection pixels = image.GetReadOnlyPixels())
          {
            for (int i = 0; i < 10; i++)
              ColorAssert.AreEqual(color, pixels.GetPixel(i, 0).ToColor());
          }
        }
      }
    }
Ejemplo n.º 7
0
    public void Test_Read_Bytes()
    {
#if Q8
      var bytes = new byte[] { 1, 2, 3, 4 };
#elif Q16 || Q16HDRI
      var bytes = new byte[] { 1, 0, 2, 0, 3, 0, 4, 0 };
#else
#error Not implemented!
#endif

      MagickReadSettings settings = new MagickReadSettings()
      {
        Width = 1,
        Height = 1
      };

      settings.Format = MagickFormat.Rgba;
      using (MagickImage image = new MagickImage(bytes, settings))
      {
        using (PixelCollection pixels = image.GetReadOnlyPixels())
        {
          Pixel pixel = pixels.GetPixel(0, 0);
          Assert.AreEqual(4, pixel.Channels);
          Assert.AreEqual(1, pixel.GetChannel(0));
          Assert.AreEqual(2, pixel.GetChannel(1));
          Assert.AreEqual(3, pixel.GetChannel(2));
          Assert.AreEqual(4, pixel.GetChannel(3));
        }
      }

      settings.Format = MagickFormat.Rgbo;
      using (MagickImage image = new MagickImage(bytes, settings))
      {
        using (PixelCollection pixels = image.GetReadOnlyPixels())
        {
          Pixel pixel = pixels.GetPixel(0, 0);
          Assert.AreEqual(4, pixel.Channels);
          Assert.AreEqual(1, pixel.GetChannel(0));
          Assert.AreEqual(2, pixel.GetChannel(1));
          Assert.AreEqual(3, pixel.GetChannel(2));
          Assert.AreEqual(Quantum.Max - 4, pixel.GetChannel(3));
        }
      }
    }
Ejemplo n.º 8
0
    public void Test_IndexOutOfRange()
    {
      using (MagickImage image = new MagickImage(Color.Red, 5, 10))
      {
        using (PixelCollection pixels = image.GetReadOnlyPixels())
        {
          ExceptionAssert.Throws<ArgumentException>(delegate ()
          {
            pixels.GetValue(5, 0);
          });

          ExceptionAssert.Throws<ArgumentException>(delegate ()
          {
            pixels.GetValue(-1, 0);
          });

          ExceptionAssert.Throws<ArgumentException>(delegate ()
          {
            pixels.GetValue(0, -1);
          });

          ExceptionAssert.Throws<ArgumentException>(delegate ()
          {
            pixels.GetValue(0, 10);
          });
        }
      }
    }
Ejemplo n.º 9
0
    public void Test_Scale()
    {
      using (MagickImage image = new MagickImage(Files.CirclePNG))
      {
        MagickColor color = Color.FromArgb(159, 255, 255, 255);
        using (PixelCollection pixels = image.GetReadOnlyPixels())
        {
          ColorAssert.AreEqual(color, pixels.GetPixel(image.Width / 2, image.Height / 2).ToColor());
        }

        image.Scale((Percentage)400);

        using (PixelCollection pixels = image.GetReadOnlyPixels())
        {
          ColorAssert.AreEqual(color, pixels.GetPixel(image.Width / 2, image.Height / 2).ToColor());
        }
      }
    }
Ejemplo n.º 10
0
    public void Test_Execute_Variables()
    {
      MagickScript script = new MagickScript(Files.Scripts.Variables);
      string[] names = script.Variables.Names.ToArray();
      Assert.AreEqual(3, names.Length);
      Assert.AreEqual("width", names[0]);
      Assert.AreEqual("height", names[1]);
      Assert.AreEqual("color", names[2]);

      using (MagickImage image = new MagickImage(Files.MagickNETIconPNG))
      {
        ExceptionAssert.Throws<ArgumentNullException>(delegate ()
        {
          script.Execute(image);
        });

        script.Variables["width"] = "test";

        ExceptionAssert.Throws<FormatException>(delegate ()
        {
          script.Execute(image);
        });

        script.Variables.Set("width", 100);

        ExceptionAssert.Throws<ArgumentNullException>(delegate ()
        {
          script.Execute(image);
        });

        script.Variables["height"] = "100";
        Assert.AreEqual("100", script.Variables.Get("height"));
        script.Variables["color"] = Color.Yellow;

        script.Execute(image);

        Assert.AreEqual(100, image.Width);
        Assert.AreEqual(100, image.Height);
        using (PixelCollection pixels = image.GetReadOnlyPixels())
        {
          ColorAssert.AreEqual(Color.Yellow, pixels.GetPixel(0, 0));
        }
      }
    }
Ejemplo n.º 11
0
    private static void Test_Separate_Composite(MagickImage image, ColorSpace colorSpace, int value)
    {
      Assert.AreEqual(colorSpace, image.ColorSpace);

      using (PixelCollection pixels = image.GetReadOnlyPixels())
      {
        Pixel pixel = pixels.GetPixel(340, 260);
        ColorAssert.AreEqual(Color.FromArgb(value, value, value), pixel.ToColor());
      }
    }
Ejemplo n.º 12
0
    public void Test_FillColor()
    {
      using (MagickImage image = new MagickImage(MagickColor.Transparent, 100, 100))
      {
        image.FillColor = null;

        Pixel pixelA;
        image.FillColor = Color.Red;
        image.Read("caption:Magick.NET");
        using (PixelCollection pixels = image.GetReadOnlyPixels())
        {
          pixelA = pixels.GetPixel(69, 6);
        }

        Pixel pixelB;
        image.FillColor = Color.Yellow;
        image.Read("caption:Magick.NET");
        using (PixelCollection pixels = image.GetReadOnlyPixels())
        {
          pixelB = pixels.GetPixel(69, 6);
        }

        ColorAssert.AreNotEqual(pixelA, pixelB);
      }
    }
Ejemplo n.º 13
0
    public void Test_ColorAlpha()
    {
      using (MagickImage image = new MagickImage(Files.MagickNETIconPNG))
      {
        MagickColor purple = new MagickColor("purple");

        image.ColorAlpha(purple);

        using (PixelCollection pixels = image.GetReadOnlyPixels())
        {
          ColorAssert.AreNotEqual(purple, pixels.GetPixel(45, 75).ToColor());
          ColorAssert.AreEqual(purple, pixels.GetPixel(100, 60).ToColor());
        }
      }
    }
Ejemplo n.º 14
0
    public void Test_SparseColors()
    {
      MagickReadSettings settings = new MagickReadSettings();
      settings.Width = 600;
      settings.Height = 60;

      using (MagickImage image = new MagickImage("xc:", settings))
      {
        ExceptionAssert.Throws<ArgumentNullException>(delegate ()
        {
          image.SparseColor(Channels.Red, SparseColorMethod.Barycentric, null);
        });

        List<SparseColorArg> args = new List<SparseColorArg>();

        ExceptionAssert.Throws<ArgumentException>(delegate ()
        {
          image.SparseColor(Channels.Blue, SparseColorMethod.Barycentric, args);
        });

        using (PixelCollection pixels = image.GetReadOnlyPixels())
        {
          ColorAssert.AreEqual(pixels.GetPixel(0, 0), pixels.GetPixel(599, 59));
        }

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

        args.Add(new SparseColorArg(0, 0, new MagickColor("skyblue")));
        args.Add(new SparseColorArg(-600, 60, new MagickColor("skyblue")));
        args.Add(new SparseColorArg(600, 60, new MagickColor("black")));

        image.SparseColor(SparseColorMethod.Barycentric, args);

        using (PixelCollection pixels = image.GetReadOnlyPixels())
        {
          ColorAssert.AreNotEqual(pixels.GetPixel(0, 0), pixels.GetPixel(599, 59));
        }

        ExceptionAssert.Throws<ArgumentException>(delegate ()
        {
          image.SparseColor(Channels.Black, SparseColorMethod.Barycentric, args);
        });
      }
    }
Ejemplo n.º 15
0
    public void Test_Shadow()
    {
      using (MagickImage image = new MagickImage())
      {
        image.BackgroundColor = MagickColor.Transparent;
        image.FontPointsize = 60;
        image.Read("label:Magick.NET");

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

        image.Shadow(2, 2, 1, new ImageMagick.Percentage(50), Color.Red);

        Assert.AreEqual(width + 4, image.Width);
        Assert.AreEqual(height + 4, image.Height);

        using (PixelCollection pixels = image.GetReadOnlyPixels())
        {
          Pixel pixel = pixels.GetPixel(90, 9);
          Assert.AreEqual(0, pixel.ToColor().A);
        }
      }
    }
    public void Test_Image_Read()
    {
      MagickReadSettings settings = CreateSettings();

      using (MagickImage image = new MagickImage())
      {
        byte[] data = new byte[]
          {
            0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0xf0, 0x3f,
            0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0xf0, 0x3f,
            0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0,
          };
        image.Read(data, settings);

        Assert.AreEqual(2, image.Width);
        Assert.AreEqual(1, image.Height);

        using (PixelCollection pixels = image.GetReadOnlyPixels())
        {
          Pixel pixel = pixels.GetPixel(0, 0);
          Assert.AreEqual(4, pixel.Channels);
          Assert.AreEqual(0, pixel.GetChannel(0));
          Assert.AreEqual(0, pixel.GetChannel(1));
          Assert.AreEqual(0, pixel.GetChannel(2));
          Assert.AreEqual(Quantum.Max, pixel.GetChannel(3));

          pixel = pixels.GetPixel(1, 0);
          Assert.AreEqual(4, pixel.Channels);
          Assert.AreEqual(0, pixel.GetChannel(0));
          Assert.AreEqual(Quantum.Max, pixel.GetChannel(1));
          Assert.AreEqual(0, pixel.GetChannel(2));
          Assert.AreEqual(0, pixel.GetChannel(3));

          ExceptionAssert.Throws<ArgumentException>(delegate ()
          {
            pixels.GetPixel(0, 1);
          });
        }
      }
    }
Ejemplo n.º 17
0
    public void Test_Ping()
    {
      using (MagickImage image = new MagickImage())
      {
        image.Ping(Files.FujiFilmFinePixS1ProJPG);

        ExceptionAssert.Throws<InvalidOperationException>(delegate ()
        {
          image.GetReadOnlyPixels();
        });

        ImageProfile profile = image.Get8BimProfile();
        Assert.IsNotNull(profile);
      }
    }
    public void Test_Set()
    {
      using (MagickImage image = new MagickImage(Color.Red, 5, 10))
      {
        using (WritablePixelCollection pixels = image.GetWritablePixels())
        {
          ExceptionAssert.Throws<ArgumentNullException>(delegate ()
          {
            pixels.Set((QuantumType[])null);
          });

          ExceptionAssert.Throws<ArgumentNullException>(delegate ()
          {
            pixels.Set((Pixel)null);
          });

          ExceptionAssert.Throws<ArgumentNullException>(delegate ()
          {
            pixels.Set((Pixel[])null);
          });

          Assert.AreEqual(3, pixels.Channels);
          Test_Set(pixels, new QuantumType[] { });
          Test_Set(pixels, new QuantumType[] { 0 });
          Test_Set(pixels, new QuantumType[] { 0, 0 });

          pixels.Set(new QuantumType[] { 0, 0, 0 });
          Test_PixelColor(pixels, Color.Black);
          pixels.Write();
        }

        using (PixelCollection pixels = image.GetReadOnlyPixels())
        {
          Test_PixelColor(pixels, Color.Black);
        }

        using (WritablePixelCollection pixels = image.GetWritablePixels())
        {
          pixels.Set(new uint[] { 4294967295, 0, 0 });
          Test_PixelColor(pixels, Color.Red);
          pixels.Set(new ushort[] { 0, 0, 65535 });
          Test_PixelColor(pixels, Color.Blue);
          pixels.Set(new byte[] { 0, 255, 0 });
          Test_PixelColor(pixels, Color.Lime);
        }

        using (WritablePixelCollection pixels = image.GetWritablePixels())
        {
          for (int x = 0; x < pixels.Width; x++)
          {
            for (int y = 0; y < pixels.Height; y++)
            {
              pixels.Set(x, y, new QuantumType[] { 0, 0, 0 });
            }
          }
        }
      }
    }
Ejemplo n.º 19
0
 static void Main(string[] args)
 {
     if (args.Length != 9) {
         Console.WriteLine("ImageMerge: Version 1.0");
         displaySyntax();
     } else {
         for (int i = 0; i < 50; i++)
             Placed[i] = false;
         canvasSizeX = Convert.ToInt32(args[0]);
         canvasSizeY = Convert.ToInt32(args[1]);
         int canvasStartX = Convert.ToInt32(args[2]);
         int canvasStartY = Convert.ToInt32(args[3]);
         string bgColor = "xc:" + args[4]; // canvas color name or hex color
         accuracy = Convert.ToInt32(args[5]);
         shortCut = Convert.ToInt32(args[6]);
         nearness = Convert.ToInt32(args[7]);
         double overlap = Convert.ToDouble(args[8]);
         Console.WriteLine("\nCanvas Size: " + canvasSizeX.ToString() + "x" + canvasSizeY.ToString());
         Console.WriteLine("Canvas Start: " + canvasStartX.ToString() + "x" + canvasStartY.ToString());
         Console.WriteLine("Background color: " + args[4]);
         Console.WriteLine("Accuracy: " + accuracy.ToString());
         Console.WriteLine("Nearness: " + nearness.ToString());
         Console.WriteLine("Overlap: " + overlap.ToString() + "\n");
         if (overlap != 0) // 0 means use default 20 which is 20% overlap for nearness=1
             overlapPercent = overlap;
         overlapPercent = overlapPercent / 100 / accuracy / accuracy;
         if (!((shortCut == 0) || (shortCut == 1))) {
             Console.WriteLine("shortCut value is " + shortCut.ToString() + ". It must be 0 or 1. Setting to 0.\n\n");
             displaySyntax();
             Console.WriteLine("\n\n");
             shortCut = 0;
         }
         if (nearness < 0) {
             Console.WriteLine("nearness value is " + nearness.ToString() + ". It must be greater than 0. Setting to 0.\n\n");
             displaySyntax();
             Console.WriteLine("\n\n");
             nearness = 0;
         }
         if ((nearness > 0) && (shortCut != 1)) {
             Console.WriteLine("If nearness equal 1 then shortcut must equal 1.  Setting shortcut = 1.\n\n");
             displaySyntax();
             Console.WriteLine("\n\n");
             shortCut = 1;
         }
         UsedPixel = new bool[canvasSizeX, canvasSizeY]; // remembers which pixels are used in the canvas
         canvasR = new ushort[canvasSizeX, canvasSizeY];
         canvasG = new ushort[canvasSizeX, canvasSizeY];
         canvasB = new ushort[canvasSizeX, canvasSizeY];
         for (int y = 0; y < canvasSizeY; y++)
             for (int x = 0; x < canvasSizeX; x++) {
                 UsedPixel[x, y] = false;
         }
         using (MagickImage canvas = new MagickImage(bgColor, canvasSizeX, canvasSizeY)) {
             string[] files = Directory.GetFiles(".", "*.png", SearchOption.TopDirectoryOnly);
             int maxWidth = 0, maxHeight = 0;
             int len = files.Length;
             if (len < 2) {
                 Console.WriteLine("Need at least 2 images to run.");
                 return;
             }
             for (int i = 0; i < len; i++) {
                 image[i] = new MagickImage(files[i]);
                 imagePC[i] = image[i].GetReadOnlyPixels();
                 if (image[i].Width > maxWidth)
                     maxWidth = image[i].Width;
                 if (image[i].Height > maxHeight)
                     maxHeight = image[i].Height;
             }
             // record RGB pixel values for each image to reduce calls to GetPixel() to improve performance
             imageR = new ushort[len, maxWidth, maxHeight];
             imageG = new ushort[len, maxWidth, maxHeight];
             imageB = new ushort[len, maxWidth, maxHeight];
             for (int i = 1; i < len; i++) {
                 for (int y = 0; y < image[i].Height; y++) {
                     for (int x = 0; x < image[i].Width; x++) {
                         Pixel p = imagePC[i].GetPixel(x, y); // image pixel
                         imageR[i, x, y] = p.GetChannel(0);
                         imageG[i, x, y] = p.GetChannel(1);
                         imageB[i, x, y] = p.GetChannel(2);
                     }
                 }
             }
             canvas.Composite(image[0], new MagickGeometry(canvasStartX, canvasStartY, image[0].Width, image[0].Height), CompositeOperator.Over);
             canvas.Write(@"output\output-1.png");
             canvasPC = canvas.GetReadOnlyPixels();
             getCanvasRGB();
             Placed[0] = true; // first image has been placed on canvas
             // top/left of active-canvas
             activeCanvasX1 = lastImageX1 = canvasStartX;
             activeCanvasY1 = lastImageY1 = canvasStartY;
             // bottom/right of active-canvas
             activeCanvasX2 = lastImageX2 = canvasStartX + image[0].Width - 1;
             activeCanvasY2 = lastImageY2 = canvasStartY + image[0].Height - 1;
             setUsedPixels(activeCanvasX1, activeCanvasY1, activeCanvasX2, activeCanvasY2);
             int outputImageNum = 2;
             bool imagePlaced = false;
             canvasX = canvasSizeX - 1; // right column of pixels
             canvasY = canvasSizeY - 1; // bottom row of pixels
             Console.WriteLine(files[0] + " -> output-1: " + canvasStartX.ToString() + "x" + canvasStartY.ToString());
             do {
                 imagePlaced = false;
                 for (int i = 1; i < len; i++) {
                     if (!Placed[i]) { // if image has not been placed on canvas
                         Console.Write(files[i]);
                         findCorrectOverlap(i);
                         if ((nearness == 0) && (overlapMax == 0))
                             Console.WriteLine(": NO MERGE");
                         else {
                             Console.WriteLine(" -> output-" + outputImageNum.ToString() + ": " + maxX1.ToString() + "x" + maxY1.ToString());
                             int w = image[i].Width;
                             int h = image[i].Height;
                             canvas.Composite(image[i], new MagickGeometry(maxX1, maxY1, w, h), CompositeOperator.Over);
                             canvas.Write(@"output\output-" + (outputImageNum++).ToString() + ".png");
                             canvasPC = canvas.GetReadOnlyPixels(); // update canvas pixels
                             getCanvasRGB();
                             // record canvas background for comparison later
                             canvasBackR = canvasR[0, 0];
                             canvasBackG = canvasG[0, 0];
                             canvasBackB = canvasB[0, 0];
                             Placed[i] = true; // image has bee placed on canvas
                             maxX2 = maxX1 + w - 1;
                             maxY2 = maxY1 + h - 1;
                             // prevent bottom right from going outside of canvas
                             if (maxX2 > canvasX)
                                 maxX2 = canvasX;
                             if (maxY2 > canvasY)
                                 maxY2 = canvasY;
                             setUsedPixels(maxX1, maxY1, maxX2, maxY2); // mark pixels used
                             // update Active Canvas
                             if (maxX1 < activeCanvasX1)
                                 activeCanvasX1 = maxX1;
                             if (maxY1 < activeCanvasY1)
                                 activeCanvasY1 = maxY1;
                             if (maxX2 > activeCanvasX2)
                                 activeCanvasX2 = maxX2;
                             if (maxY2 > activeCanvasY2)
                                 activeCanvasY2 = maxY2;
                             imagePlaced = true;
                             if (shortCut == 1) {
                                 lastImageX1 = maxX1;
                                 lastImageY1 = maxY1;
                                 lastImageX2 = maxX2;
                                 lastImageY2 = maxY2;
                             }
                         }
                     }
                 }
             } while (imagePlaced); // repeat until no images are placed
         }
     }
 }