예제 #1
1
    public void Test_FillColor()
    {
      using (MagickImage image = new MagickImage(MagickColors.Transparent, 100, 100))
      {
        ColorAssert.AreEqual(MagickColors.Black, image.Settings.FillColor);

        Pixel pixelA;
        image.Settings.FillColor = MagickColors.Red;
        image.Read("caption:Magick.NET");

        Assert.AreEqual(100, image.Width);
        Assert.AreEqual(100, image.Height);

        using (PixelCollection pixels = image.GetPixels())
        {
          pixelA = pixels.GetPixel(69, 6);
        }

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

        ColorAssert.AreNotEqual(pixelA.ToColor(), pixelB.ToColor());
      }
    }
예제 #2
0
 public static void AreEqual(MagickColor expected, MagickImage image, int x, int y)
 {
   using (PixelCollection pixels = image.GetPixels())
   {
     AreEqual(expected, pixels.GetPixel(x, y));
   }
 }
예제 #3
0
    private static void TestPixels(MagickImage image, MagickColor firstRow, MagickColor secondRow)
    {
      using (PixelCollection pixels = image.GetPixels())
      {
        for (int y = 0; y < 2; y++)
          for (int x = 0; x < 10; x++)
            ColorAssert.AreEqual(y == 0 ? firstRow : secondRow, pixels.GetPixel(x, y).ToColor());
      }

      using (MemoryStream memStream = new MemoryStream())
      {
        image.Format = MagickFormat.Bmp;
        image.Write(memStream);
        memStream.Position = 0;

        using (MagickImage output = new MagickImage(memStream))
        {
          using (PixelCollection pixels = output.GetPixels())
          {
            for (int y = 0; y < 2; y++)
              for (int x = 0; x < 10; x++)
                ColorAssert.AreEqual(y == 0 ? firstRow : secondRow, pixels.GetPixel(x, y).ToColor());
          }
        }
      }
    }
예제 #4
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.Bgra;
      using (MagickImage image = new MagickImage(bytes, settings))
      {
        using (PixelCollection pixels = image.GetPixels())
        {
          Pixel pixel = pixels.GetPixel(0, 0);
          Assert.AreEqual(4, pixel.Channels);
          Assert.AreEqual(3, pixel.GetChannel(0));
          Assert.AreEqual(2, pixel.GetChannel(1));
          Assert.AreEqual(1, pixel.GetChannel(2));
          Assert.AreEqual(4, pixel.GetChannel(3));
        }
      }

      settings.Format = MagickFormat.Bgro;
      using (MagickImage image = new MagickImage(bytes, settings))
      {
        using (PixelCollection pixels = image.GetPixels())
        {
          Pixel pixel = pixels.GetPixel(0, 0);
          Assert.AreEqual(4, pixel.Channels);
          Assert.AreEqual(3, pixel.GetChannel(0));
          Assert.AreEqual(2, pixel.GetChannel(1));
          Assert.AreEqual(1, pixel.GetChannel(2));
          Assert.AreEqual(Quantum.Max - 4, pixel.GetChannel(3));
        }
      }
    }
예제 #5
0
    public void Test_IndexOutOfRange()
    {
      using (MagickImage image = new MagickImage(MagickColors.Red, 5, 10))
      {
        using (PixelCollection pixels = image.GetPixels())
        {
          ExceptionAssert.Throws<ArgumentOutOfRangeException>(delegate ()
          {
            pixels.GetArea(4, 0, 2, 1);
          });

          ExceptionAssert.Throws<ArgumentOutOfRangeException>(delegate ()
          {
            pixels.GetArea(new MagickGeometry(0, 9, 1, 2));
          });

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

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

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

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

          ExceptionAssert.Throws<ArgumentOutOfRangeException>(delegate ()
          {
            pixels.GetValue(5, 0);
          });

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

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

          ExceptionAssert.Throws<ArgumentOutOfRangeException>(delegate ()
          {
            pixels.GetValue(0, 10);
          });
        }
      }
    }
예제 #6
0
 public void Test_IEnumerable()
 {
   using (MagickImage image = new MagickImage(MagickColors.Red, 5, 10))
   {
     using (PixelCollection pixels = image.GetPixels())
     {
       Assert.AreEqual(50, pixels.Count());
     }
   }
 }
예제 #7
0
    public void Test_GetValues()
    {
      using (MagickImage image = new MagickImage(MagickColors.PowderBlue, 1, 1))
      {
        Assert.AreEqual(3, image.ChannelCount);

        using (PixelCollection pixels = image.GetPixels())
        {
          var values = pixels.GetValues();
          Assert.AreEqual(3, values.Length);

          MagickColor color = new MagickColor(values[0], values[1], values[2]);
          ColorAssert.AreEqual(MagickColors.PowderBlue, color);
        }
      }
    }
예제 #8
0
    public void Test_GetValue()
    {
      using (MagickImage image = new MagickImage(MagickColors.Red, 5, 10))
      {
        using (PixelCollection pixels = image.GetPixels())
        {
          var values = pixels.GetValue(0, 0);
          Assert.AreEqual(3, values.Length);

          MagickColor color = new MagickColor(values[0], values[1], values[2]);
          ColorAssert.AreEqual(MagickColors.Red, color);
        }
      }
    }
예제 #9
0
    public void Test_GetArea()
    {
      using (MagickImage image = new MagickImage(MagickColors.Fuchsia, 10, 10))
      {
        using (PixelCollection pixels = image.GetPixels())
        {
          pixels.Set(3, 3, new QuantumType[] { 0, 0, 0 });

          var valuesA = pixels.GetArea(2, 2, 4, 4);
          Assert.AreEqual(48, valuesA.Length);

          var pixelB = pixels.GetArea(new MagickGeometry(3, 3, 1, 1));
          Assert.AreEqual(3, pixelB.Length);

          var pixelA = valuesA.Skip(15).Take(3).ToArray();

          CollectionAssert.AreEqual(pixelA, pixelB);
        }
      }
    }
예제 #10
0
    public void Test_Ping()
    {
      MagickImage image = new MagickImage();

      ExceptionAssert.Throws<ArgumentException>(delegate ()
      {
        image.Ping(new byte[0]);
      });

      ExceptionAssert.Throws<ArgumentNullException>(delegate ()
      {
        image.Ping((byte[])null);
      });

      ExceptionAssert.Throws<ArgumentNullException>(delegate ()
      {
        image.Ping((Stream)null);
      });

      ExceptionAssert.Throws<ArgumentNullException>(delegate ()
      {
        image.Ping((string)null);
      });

      ExceptionAssert.Throws<ArgumentException>(delegate ()
      {
        image.Ping(Files.Missing);
      });

      image.Ping(Files.FujiFilmFinePixS1ProJPG);
      Test_Ping(image);
      Assert.AreEqual(600, image.Width);
      Assert.AreEqual(400, image.Height);

      image.Ping(new FileInfo(Files.FujiFilmFinePixS1ProJPG));
      Test_Ping(image);
      Assert.AreEqual(600, image.Width);
      Assert.AreEqual(400, image.Height);

      image.Ping(File.ReadAllBytes(Files.FujiFilmFinePixS1ProJPG));
      Test_Ping(image);
      Assert.AreEqual(600, image.Width);
      Assert.AreEqual(400, image.Height);

      image.Read(Files.SnakewarePNG);
      Assert.AreEqual(286, image.Width);
      Assert.AreEqual(67, image.Height);
      using (PixelCollection pixels = image.GetPixels())
      {
        Assert.AreEqual(38324, pixels.ToArray().Length);
      }

      image.Dispose();
    }
예제 #11
0
    public void Test_ToByteArray()
    {
      using (MagickImage image = new MagickImage(MagickColors.Red, 10, 10))
      {
        using (PixelCollection pixels = image.GetPixels())
        {
          var bytes = pixels.ToByteArray(0, 0, 1, 1, "BGR");
          Assert.AreEqual(3, bytes.Length);
          CollectionAssert.AreEqual(new byte[] { 0, 0, 255 }, bytes);

          bytes = pixels.ToByteArray(0, 0, 1, 1, "BG");
          Assert.AreEqual(2, bytes.Length);
          CollectionAssert.AreEqual(new byte[] { 0, 0 }, bytes);
        }
      }
    }
    public void Test_OptimizeTransparency()
    {
      using (MagickImageCollection collection = new MagickImageCollection())
      {
        ExceptionAssert.Throws<InvalidOperationException>(delegate ()
        {
          collection.OptimizeTransparency();
        });

        collection.Add(new MagickImage(MagickColors.Red, 11, 11));

        MagickImage image = new MagickImage(MagickColors.Red, 11, 11);
        using (var pixels = image.GetPixels())
        {
          pixels.Set(5, 5, new QuantumType[] { 0, Quantum.Max, 0 });
        }
        collection.Add(image);
        collection.OptimizeTransparency();

        Assert.AreEqual(11, collection[1].Width);
        Assert.AreEqual(11, collection[1].Height);
        Assert.AreEqual(0, collection[1].Page.X);
        Assert.AreEqual(0, collection[1].Page.Y);
        ColorAssert.AreEqual(MagickColors.Lime, collection[1], 5, 5);
        ColorAssert.AreEqual(new MagickColor("#f000"), collection[1], 4, 4);
      }
    }
    public void Test_OptimizePlus()
    {
      using (MagickImageCollection collection = new MagickImageCollection())
      {
        ExceptionAssert.Throws<InvalidOperationException>(delegate ()
        {
          collection.OptimizePlus();
        });

        collection.Add(new MagickImage(MagickColors.Red, 11, 11));
        /* the second image will not be removed if it is a duplicate so we
           need to add an extra one. */
        collection.Add(new MagickImage(MagickColors.Red, 11, 11));
        collection.Add(new MagickImage(MagickColors.Red, 11, 11));

        MagickImage image = new MagickImage(MagickColors.Red, 11, 11);
        using (var pixels = image.GetPixels())
        {
          pixels.Set(5, 5, new QuantumType[] { 0, Quantum.Max, 0 });
        }
        collection.Add(image);
        collection.OptimizePlus();

        Assert.AreEqual(3, collection.Count);

        Assert.AreEqual(1, collection[1].Width);
        Assert.AreEqual(1, collection[1].Height);
        Assert.AreEqual(-1, collection[1].Page.X);
        Assert.AreEqual(-1, collection[1].Page.Y);
        ColorAssert.AreEqual(MagickColors.Red, collection[1], 0, 0);

        Assert.AreEqual(1, collection[2].Width);
        Assert.AreEqual(1, collection[2].Height);
        Assert.AreEqual(5, collection[2].Page.X);
        Assert.AreEqual(5, collection[2].Page.Y);
        ColorAssert.AreEqual(MagickColors.Lime, collection[2], 0, 0);
      }
    }
    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.GetPixels())
        {
          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<ArgumentOutOfRangeException>(delegate ()
          {
            pixels.GetPixel(0, 1);
          });
        }
      }
    }
예제 #15
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.GetPixels())
        {
          ColorAssert.AreEqual(pixels.GetPixel(0, 0).ToColor(), pixels.GetPixel(599, 59).ToColor());
        }

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

        args.Add(new SparseColorArg(0, 0, MagickColors.SkyBlue));
        args.Add(new SparseColorArg(-600, 60, MagickColors.SkyBlue));
        args.Add(new SparseColorArg(600, 60, MagickColors.Black));

        image.SparseColor(SparseColorMethod.Barycentric, args);

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

        ExceptionAssert.Throws<ArgumentException>(delegate ()
        {
          image.SparseColor(Channels.Black, SparseColorMethod.Barycentric, args);
        });
      }
    }
예제 #16
0
    public void Test_Shadow()
    {
      using (MagickImage image = new MagickImage())
      {
        image.Settings.BackgroundColor = MagickColors.Transparent;
        image.Settings.FontPointsize = 60;
        image.Read("label:Magick.NET");

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

        image.Shadow(2, 2, 5, new Percentage(50), MagickColors.Red);

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

        using (PixelCollection pixels = image.GetPixels())
        {
          Pixel pixel = pixels.GetPixel(90, 9);
#if Q8 || Q16
          Assert.AreEqual(0, pixel.ToColor().A);
#elif Q16HDRI
          Assert.AreEqual(OpenCLValue.Get(0.5, 0.0), pixel.ToColor().A);
#else
#error Not implemented!
#endif
          pixel = pixels.GetPixel(34, 55);

#if Q8
          Assert.AreEqual(72, pixel.ToColor().A);
#elif Q16 || Q16HDRI
          Assert.AreEqual(18096, (int)pixel.ToColor().A);
#else
#error Not implemented!
#endif
        }
      }
    }
예제 #17
0
    public void Test_Set()
    {
      using (MagickImage image = new MagickImage(MagickColors.Red, 5, 10))
      {
        using (PixelCollection pixels = image.GetPixels())
        {
          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, MagickColors.Black);
        }

        using (PixelCollection pixels = image.GetPixels())
        {
          Test_PixelColor(pixels, MagickColors.Black);
        }

        using (PixelCollection pixels = image.GetPixels())
        {
          pixels.Set(new uint[] { 100000, 0, 0 });
          Test_PixelColor(pixels, MagickColors.Red);
          pixels.Set(new ushort[] { 0, 0, 65535 });
          Test_PixelColor(pixels, MagickColors.Blue);
          pixels.Set(new byte[] { 0, 255, 0 });
          Test_PixelColor(pixels, MagickColors.Lime);
        }

        using (PixelCollection pixels = image.GetPixels())
        {
          pixels.SetArea(3, 3, 1, 1, new uint[] { 100000, 0, 0 });
          Test_PixelColor(pixels, 3, 3, MagickColors.Red);
          pixels.SetArea(3, 3, 1, 1, new ushort[] { 0, 0, 65535 });
          Test_PixelColor(pixels, 3, 3, MagickColors.Blue);
          pixels.SetArea(3, 3, 1, 1, new byte[] { 0, 255, 0 });
          Test_PixelColor(pixels, 3, 3, MagickColors.Lime);
        }

        using (PixelCollection pixels = image.GetPixels())
        {
          for (int x = 0; x < image.Width; x++)
          {
            for (int y = 0; y < image.Height; y++)
            {
              pixels.Set(x, y, new QuantumType[] { 0, 0, 0 });
            }
          }
        }
      }
    }
예제 #18
0
    public void Test_SetResult()
    {
      using (MagickImage image = new MagickImage(MagickColors.Red, 10, 2))
      {
        using (PixelCollection pixels = image.GetPixels())
        {
          QuantumType[] newPixels = new QuantumType[20 * pixels.Channels];
          for (int i = 0; i < newPixels.Length; i++)
            newPixels[i] = Quantum.Max;

          pixels.Set(newPixels);
        }

        TestPixels(image, new MagickColor(Quantum.Max, Quantum.Max, Quantum.Max));
      }

      using (MagickImage image = new MagickImage(MagickColors.Black, 10, 2))
      {
        using (PixelCollection pixels = image.GetPixels())
        {
          Assert.AreEqual(20, pixels.Count());

          foreach (Pixel pixel in pixels.Take(10))
          {
            pixel.SetChannel(2, Quantum.Max);
          }

          foreach (Pixel pixel in pixels.Skip(10))
          {
            pixel.SetChannel(0, Quantum.Max);
          }
        }

        TestPixels(image, MagickColors.Blue, MagickColors.Red);
      }
    }
예제 #19
0
    private static void Test_Ping(MagickImage image)
    {
      ExceptionAssert.Throws<InvalidOperationException>(delegate ()
      {
        image.GetPixels();
      });

      ImageProfile profile = image.Get8BimProfile();
      Assert.IsNotNull(profile);
    }
예제 #20
0
    public void Test_ToShortArray()
    {
      using (MagickImage image = new MagickImage(MagickColors.Red, 10, 10))
      {
        using (PixelCollection pixels = image.GetPixels())
        {
          var shorts = pixels.ToShortArray(0, 0, 1, 1, "BGR");
          Assert.AreEqual(3, shorts.Length);
          CollectionAssert.AreEqual(new ushort[] { 0, 0, 65535 }, shorts);

          shorts = pixels.ToShortArray(0, 0, 1, 1, "BG");
          Assert.AreEqual(2, shorts.Length);
          CollectionAssert.AreEqual(new ushort[] { 0, 0 }, shorts);
        }
      }
    }
예제 #21
0
    private static void Test_Separate_Composite(MagickImage image, ColorSpace colorSpace, byte value)
    {
      Assert.AreEqual(colorSpace, image.ColorSpace);

      using (PixelCollection pixels = image.GetPixels())
      {
        Pixel pixel = pixels.GetPixel(340, 260);
        ColorAssert.AreEqual(MagickColor.FromRgb(value, value, value), pixel.ToColor());
      }
    }
예제 #22
0
    public void Test_Enumerator()
    {
      using (MagickImage image = new MagickImage(Files.ConnectedComponentsPNG, 10, 10))
      {
        Pixel pixel = image.GetPixels().First(p => p.ToColor() == MagickColors.Black);
        Assert.IsNotNull(pixel);

        Pixel otherPixel = null;

        using (var pixels = image.GetPixels())
        {
          for (int y = 0; y < image.Height; y++)
          {
            for (int x = 0; x < image.Width; x++)
            {
              otherPixel = pixels.GetPixel(x, y);
              if (otherPixel.ToColor() == MagickColors.Black)
                break;
            }
            if (otherPixel.ToColor() == MagickColors.Black)
              break;
          }
        }

        Assert.IsNotNull(otherPixel);

        Assert.AreEqual(pixel, otherPixel);
        Assert.AreEqual(350, pixel.X);
        Assert.AreEqual(196, pixel.Y);
        Assert.AreEqual(2, pixel.Channels);
      }
    }
예제 #23
0
 public static void AreNotEqual(MagickColor notExpected, MagickImage image, int x, int y)
 {
   using (PixelCollection collection = image.GetPixels())
   {
     AreNotEqual(notExpected, collection.GetPixel(x, y));
   }
 }