public string Execute(string[] args)
        {
            NetVips.CacheSetMax(0);

            Image b;

            using (var a = Image.NewFromMemory(Enumerable.Repeat((byte)255, 200).ToArray(), 20, 10, 1, "uchar"))
            {
                b = a / 2;
            } // g_object_unref

            Console.WriteLine($"Reference count b: {b.RefCount}");

            var average = b.Avg();

            Console.WriteLine($"Before GC: {average}");

            GC.Collect();
            GC.WaitForPendingFinalizers();

            average = b.Avg();

            Console.WriteLine($"After GC: {average}");

            return("All done!");
        }
Beispiel #2
0
        public void TestMatrixinvert()
        {
            // matrixinvert was added in libvips 8.10.
            Skip.IfNot(NetVips.AtLeastLibvips(8, 10), "requires libvips >= 8.10");

            // 4x4 matrix to check if PLU decomposition works
            var mat = Image.NewFromArray(new[, ]
            {
                { 4, 0, 0, 0 },
                { 0, 0, 2, 0 },
                { 0, 1, 2, 0 },
                { 1, 0, 0, 1 }
            });
            var im = mat.Matrixinvert();

            Assert.Equal(4, im.Width);
            Assert.Equal(4, im.Height);
            Assert.Equal(1, im.Bands);
            Assert.Equal(Enums.BandFormat.Double, im.Format);

            var p = im[0, 0];

            Assert.Equal(0.25, p[0]);
            p = im[3, 3];
            Assert.Equal(1.0, p[0]);
        }
        public void TestSwitch()
        {
            // switch was added in libvips 8.9.
            Skip.IfNot(NetVips.AtLeastLibvips(8, 9), "requires libvips >= 8.9");

            var x = Image.Grey(256, 256, uchar: true);

            // slice into two at 128, we should get 50% of pixels in each half
            var index = Image.Switch(x < 128, x >= 128);

            Assert.Equal(0.5, index.Avg());

            // slice into four
            index = Image.Switch(
                x < 64,
                x >= 64 && x < 128,
                x >= 128 && x < 192,
                x >= 192
                );
            Assert.Equal(1.5, index.Avg());

            // no match should return n + 1
            index = Image.Switch(x.Equal(1000), x.Equal(2000));
            Assert.Equal(2, index.Avg());
        }
Beispiel #4
0
        public void TestCase()
        {
            // case was added in libvips 8.9.
            Skip.IfNot(NetVips.AtLeastLibvips(8, 9), "requires libvips >= 8.9");

            var x = Image.Grey(256, 256, uchar: true);

            // slice into two at 128, we should get 50% of pixels in each half
            var index = Image.Switch(x < 128, x >= 128);
            var y     = index.Case(10, 20);

            Assert.Equal(15, y.Avg());

            // slice into four
            index = Image.Switch(
                x < 64,
                x >= 64 && x < 128,
                x >= 128 && x < 192,
                x >= 192
                );
            Assert.Equal(25, index.Case(10, 20, 30, 40).Avg());

            // values over N should use the last value
            Assert.Equal(22.5, index.Case(10, 20, 30).Avg());
        }
Beispiel #5
0
        public void TestSplit7()
        {
            string[] Split(string path)
            {
                var filename7 = NetVips.PathFilename7(path);
                var mode7     = NetVips.PathMode7(path);

                return(new[] { filename7, mode7 });
            }

            var cases = new Dictionary <string, string[]>
            {
                {
                    "c:\\silly:dir:name\\fr:ed.tif:jpeg:95,,,,c:\\icc\\srgb.icc",
                    new[]
                    {
                        "c:\\silly:dir:name\\fr:ed.tif",
                        "jpeg:95,,,,c:\\icc\\srgb.icc"
                    }
                },
                {
                    "I180:",
                    new[]
                    {
                        "I180",
                        ""
                    }
                },
                {
                    "c:\\silly:",
                    new[]
                    {
                        "c:\\silly",
                        ""
                    }
                },
                {
                    "c:\\program files\\x:hello",
                    new[]
                    {
                        "c:\\program files\\x",
                        "hello"
                    }
                },
                {
                    "C:\\fixtures\\2569067123_aca715a2ee_o.jpg",
                    new[]
                    {
                        "C:\\fixtures\\2569067123_aca715a2ee_o.jpg",
                        ""
                    }
                }
            };

            foreach (var entry in cases)
            {
                Assert.Equal(entry.Value, Split(entry.Key));
            }
        }
Beispiel #6
0
 public GenerateImageClass()
 {
     if (NetVips.AtLeastLibvips(8, 9))
     {
         _gTypeToCSharpDict.Add(GValue.SourceType, "Source");
         _gTypeToCSharpDict.Add(GValue.TargetType, "Target");
     }
 }
Beispiel #7
0
        public string Execute(string[] args)
        {
            using var fileStream = File.OpenRead(Filename);
            using var image      = Image.NewFromStream(fileStream);

            Parallel.For(0, 1000, new ParallelOptions {
                MaxDegreeOfParallelism = NetVips.ConcurrencyGet()
            },
                         i => RandomCrop(image, TileSize).WriteToFile($"x_{i}.png"));

            return("Done!");
        }
Beispiel #8
0
        public void TestGetFields()
        {
            Skip.IfNot(NetVips.AtLeastLibvips(8, 5), "requires libvips >= 8.5");

            var im     = Image.Black(10, 10);
            var fields = im.GetFields();

            // we might add more fields later
            Assert.True(fields.Length > 10);

            Assert.Equal("width", fields[0]);
        }
        public GenerateImageClass()
        {
            // Classes
            _gTypeToCSharpDict.Add(GValue.SourceType, "Source");
            _gTypeToCSharpDict.Add(GValue.TargetType, "Target");

            // Enums
            _gTypeToCSharpDict.Add(NetVips.TypeFromName("VipsAccess"), "Enums.Access");
            _gTypeToCSharpDict.Add(NetVips.TypeFromName("VipsAlign"), "Enums.Align");
            _gTypeToCSharpDict.Add(NetVips.TypeFromName("VipsAngle"), "Enums.Angle");
            _gTypeToCSharpDict.Add(NetVips.TypeFromName("VipsAngle45"), "Enums.Angle45");
            _gTypeToCSharpDict.Add(NetVips.TypeFromName("VipsBandFormat"), "Enums.BandFormat");
            _gTypeToCSharpDict.Add(NetVips.TypeFromName("VipsBlendMode"), "Enums.BlendMode");
            _gTypeToCSharpDict.Add(NetVips.TypeFromName("VipsCoding"), "Enums.Coding");
            _gTypeToCSharpDict.Add(NetVips.TypeFromName("VipsCombine"), "Enums.Combine");
            _gTypeToCSharpDict.Add(NetVips.TypeFromName("VipsCombineMode"), "Enums.CombineMode");
            _gTypeToCSharpDict.Add(NetVips.TypeFromName("VipsCompassDirection"), "Enums.CompassDirection");
            _gTypeToCSharpDict.Add(NetVips.TypeFromName("VipsDemandStyle"), "Enums.DemandStyle");
            _gTypeToCSharpDict.Add(NetVips.TypeFromName("VipsDirection"), "Enums.Direction");
            _gTypeToCSharpDict.Add(NetVips.TypeFromName("VipsExtend"), "Enums.Extend");
            _gTypeToCSharpDict.Add(FailOnType, "Enums.FailOn");
            _gTypeToCSharpDict.Add(NetVips.TypeFromName("VipsForeignDzContainer"), "Enums.ForeignDzContainer");
            _gTypeToCSharpDict.Add(NetVips.TypeFromName("VipsForeignDzDepth"), "Enums.ForeignDzDepth");
            _gTypeToCSharpDict.Add(NetVips.TypeFromName("VipsForeignDzLayout"), "Enums.ForeignDzLayout");
            _gTypeToCSharpDict.Add(NetVips.TypeFromName("VipsForeignHeifCompression"), "Enums.ForeignHeifCompression");
            _gTypeToCSharpDict.Add(NetVips.TypeFromName("VipsForeignPpmFormat"), "Enums.ForeignPpmFormat");
            _gTypeToCSharpDict.Add(NetVips.TypeFromName("VipsForeignSubsample"), "Enums.ForeignSubsample");
            _gTypeToCSharpDict.Add(NetVips.TypeFromName("VipsForeignTiffCompression"), "Enums.ForeignTiffCompression");
            _gTypeToCSharpDict.Add(NetVips.TypeFromName("VipsForeignTiffPredictor"), "Enums.ForeignTiffPredictor");
            _gTypeToCSharpDict.Add(NetVips.TypeFromName("VipsForeignTiffResunit"), "Enums.ForeignTiffResunit");
            _gTypeToCSharpDict.Add(NetVips.TypeFromName("VipsForeignWebpPreset"), "Enums.ForeignWebpPreset");
            _gTypeToCSharpDict.Add(NetVips.TypeFromName("VipsIntent"), "Enums.Intent");
            _gTypeToCSharpDict.Add(NetVips.TypeFromName("VipsInteresting"), "Enums.Interesting");
            _gTypeToCSharpDict.Add(NetVips.TypeFromName("VipsInterpretation"), "Enums.Interpretation");
            _gTypeToCSharpDict.Add(NetVips.TypeFromName("VipsKernel"), "Enums.Kernel");
            _gTypeToCSharpDict.Add(NetVips.TypeFromName("VipsOperationBoolean"), "Enums.OperationBoolean");
            _gTypeToCSharpDict.Add(NetVips.TypeFromName("VipsOperationComplex"), "Enums.OperationComplex");
            _gTypeToCSharpDict.Add(NetVips.TypeFromName("VipsOperationComplex2"), "Enums.OperationComplex2");
            _gTypeToCSharpDict.Add(NetVips.TypeFromName("VipsOperationComplexget"), "Enums.OperationComplexget");
            _gTypeToCSharpDict.Add(NetVips.TypeFromName("VipsOperationMath"), "Enums.OperationMath");
            _gTypeToCSharpDict.Add(NetVips.TypeFromName("VipsOperationMath2"), "Enums.OperationMath2");
            _gTypeToCSharpDict.Add(NetVips.TypeFromName("VipsOperationMorphology"), "Enums.OperationMorphology");
            _gTypeToCSharpDict.Add(NetVips.TypeFromName("VipsOperationRelational"), "Enums.OperationRelational");
            _gTypeToCSharpDict.Add(NetVips.TypeFromName("VipsOperationRound"), "Enums.OperationRound");
            _gTypeToCSharpDict.Add(NetVips.TypeFromName("VipsPCS"), "Enums.PCS");
            _gTypeToCSharpDict.Add(NetVips.TypeFromName("VipsPrecision"), "Enums.Precision");
            _gTypeToCSharpDict.Add(NetVips.TypeFromName("VipsRegionShrink"), "Enums.RegionShrink");
            _gTypeToCSharpDict.Add(NetVips.TypeFromName("VipsSize"), "Enums.Size");

            // Flags
            _gTypeToCSharpDict.Add(NetVips.TypeFromName("VipsForeignFlags"), "Enums.ForeignFlags");
            _gTypeToCSharpDict.Add(NetVips.TypeFromName("VipsForeignPngFilter"), "Enums.ForeignPngFilter");
        }
Beispiel #10
0
        public void TestGetSuffixes()
        {
            Skip.IfNot(NetVips.AtLeastLibvips(8, 8), "requires libvips >= 8.8");

            var suffixes = NetVips.GetSuffixes();

            // vips supports these file types by default
            // (without being dependent on external dependencies):
            // - Native file format (`*.v`, `*.vips`).
            // - PPM images (`*.ppm`, `*.pgm`, `*.pbm`, `*.pfm`).
            // - Analyze images (`*.hdr`).
            Assert.True(suffixes.Length >= 7);
        }
Beispiel #11
0
        public void TestCmyk()
        {
            Skip.IfNot(NetVips.AtLeastLibvips(8, 8), "requires libvips >= 8.8");

            // even without lcms, we should have a working approximation
            var test = Image.NewFromFile(Helper.JpegFile);
            var im   = test.Colourspace(Enums.Interpretation.Cmyk).Colourspace(Enums.Interpretation.Srgb);

            var before = test[582, 210];
            var after  = im[582, 210];

            Helper.AssertAlmostEqualObjects(before, after, 10);
        }
Beispiel #12
0
        public void TestFlags()
        {
            // the OperationFlags enum is created when the first op is made --
            // make it ourselves in case we are run before that
            NetVips.VipsOperationFlagsGetType();
            var operationflagsGtype = NetVips.TypeFromName("VipsOperationFlags");
            var gv = new GValue();

            gv.SetType(operationflagsGtype);
            gv.Set(12);
            var value = gv.Get();

            Assert.Equal(12u, value);
        }
Beispiel #13
0
        public void TestEnum()
        {
            // the Interpretation enum is created when the first image is made --
            // make it ourselves in case we are run before the first image
            NetVips.VipsInterpretationGetType();
            var interpretationGtype = NetVips.TypeFromName("VipsInterpretation");
            var gv = new GValue();

            gv.SetType(interpretationGtype);
            gv.Set("xyz");
            var value = gv.Get();

            Assert.Equal("xyz", value);
        }
Beispiel #14
0
        public void TestGifLoad()
        {
            Skip.IfNot(Helper.Have("gifload") && File.Exists(Helper.GifFile), "no gif support, skipping test");

            void GifValid(Image im)
            {
                var a = im[10, 10];

                Assert.Equal(new double[] { 33 }, a);
                Assert.Equal(159, im.Width);
                Assert.Equal(203, im.Height);
                Assert.Equal(1, im.Bands);
            }

            FileLoader("gifload", Helper.GifFile, GifValid);
            BufferLoader("gifload_buffer", Helper.GifFile, GifValid);

            // 'n' param added in 8.5
            if (NetVips.AtLeastLibvips(8, 5))
            {
                var x1 = Image.NewFromFile(Helper.GifAnimFile);
                var x2 = Image.NewFromFile(Helper.GifAnimFile, kwargs: new VOption
                {
                    { "n", 2 }
                });
                Assert.Equal(2 * x1.Height, x2.Height);
                var pageHeight = x2.Get("page-height");
                Assert.Equal(x1.Height, pageHeight);

                x2 = Image.NewFromFile(Helper.GifAnimFile, kwargs: new VOption
                {
                    { "n", -1 }
                });
                Assert.Equal(5 * x1.Height, x2.Height);

                // delay metadata was added in libvips 8.9
                if (NetVips.AtLeastLibvips(8, 9))
                {
                    // our test gif has delay 0 for the first frame set in error
                    Assert.Equal(new[] { 0, 50, 50, 50, 50 }, x2.Get("delay"));
                }

                x2 = Image.NewFromFile(Helper.GifAnimFile, kwargs: new VOption
                {
                    { "page", 1 },
                    { "n", -1 }
                });
                Assert.Equal(4 * x1.Height, x2.Height);
            }
        }
Beispiel #15
0
        public void TestTanh()
        {
            Skip.IfNot(NetVips.AtLeastLibvips(8, 12), "requires libvips >= 8.12");

            dynamic Tanh(dynamic x)
            {
                if (x is Image image)
                {
                    return(image.Tanh());
                }

                return(Math.Tanh(x));
            }

            RunUnary(_allImages, Tanh, Helper.NonComplexFormats);
        }
        /// <summary>
        /// Map a GType to the name of the C# type we use to represent it.
        /// </summary>
        /// <param name="name">The GType identifier.</param>
        /// <param name="gtype">The GType to map.</param>
        /// <returns>The C# type we use to represent it.</returns>
        private string GTypeToCSharp(string name, IntPtr gtype)
        {
            if (_gTypeToCSharpDict.ContainsKey(gtype))
            {
                return(_gTypeToCSharpDict[gtype]);
            }

            var fundamental = NetVips.FundamentalType(gtype);

            if (_gTypeToCSharpDict.ContainsKey(fundamental))
            {
                return(_gTypeToCSharpDict[fundamental]);
            }

            throw new Exception($"Unsupported type: {gtype} name: {name}");
        }
        /// <summary>
        /// Map a GType to the name of the C# type we use to represent it.
        /// </summary>
        /// <param name="gtype">The GType to map.</param>
        /// <returns>The C# type we use to represent it.</returns>
        private string GTypeToCSharp(IntPtr gtype)
        {
            if (_gTypeToCSharpDict.ContainsKey(gtype))
            {
                return(_gTypeToCSharpDict[gtype]);
            }

            var fundamental = NetVips.FundamentalType(gtype);

            if (_gTypeToCSharpDict.ContainsKey(fundamental))
            {
                return(_gTypeToCSharpDict[fundamental]);
            }

            return("object");
        }
Beispiel #18
0
        public void TestMagickSave()
        {
            Skip.IfNot(Helper.Have("magicksave"), "no magick support, skipping test");

            // save to a file and load again ... we can't use SaveLoadFile since
            // we want to make sure we use magickload/save
            // don't use BMP - GraphicsMagick always adds an alpha
            // don't use TIF - IM7 will save as 16-bit
            var filename = Helper.GetTemporaryFile(_tempDir, ".jpg");

            _colour.Magicksave(filename);
            var x = Image.Magickload(filename);

            Assert.Equal(_colour.Width, x.Width);
            Assert.Equal(_colour.Height, x.Height);
            Assert.Equal(_colour.Bands, x.Bands);
            Assert.Equal(_colour.Height, x.Height);

            var maxDiff = (_colour - x).Abs().Max();

            Assert.True(maxDiff <= 60);

            SaveLoadBuffer("magicksave_buffer", "magickload_buffer", _colour, 60, new VOption
            {
                { "format", "JPG" }
            });

            // try an animation
            if (Helper.Have("gifload"))
            {
                var x1 = Image.NewFromFile(Helper.GifAnimFile, kwargs: new VOption
                {
                    { "n", -1 }
                });
                var w1 = x1.MagicksaveBuffer(format: "GIF");
                var x2 = Image.NewFromBuffer(w1, kwargs: new VOption
                {
                    { "n", -1 }
                });

                var delayName = NetVips.AtLeastLibvips(8, 9) ? "delay" : "gif-delay";
                Assert.Equal(x2.Get(delayName), x1.Get(delayName));
                Assert.Equal(x2.Get("page-height"), x1.Get("page-height"));
                // magicks vary in how they handle this ... just pray we are close
                Assert.True(Math.Abs((int)x1.Get("gif-loop") - (int)x2.Get("gif-loop")) < 5);
            }
        }
Beispiel #19
0
        public void TestEnum()
        {
            // the Interpretation enum is created when the first image is made --
            // make it ourselves in case we are run before the first image
            NetVips.VipsInterpretationGetType();
            var gtype = NetVips.TypeFromName("VipsInterpretation");

            Enums.Interpretation actual;
            using (var gv = new GValue())
            {
                gv.SetType(gtype);
                gv.Set(Enums.Interpretation.Xyz);
                actual = (Enums.Interpretation)gv.Get();
            }

            Assert.Equal(Enums.Interpretation.Xyz, actual);
        }
Beispiel #20
0
        public void TestFlags()
        {
            // the OperationFlags enum is created when the first op is made --
            // make it ourselves in case we are run before that
            NetVips.VipsOperationFlagsGetType();
            var gtype = NetVips.TypeFromName("VipsOperationFlags");

            Enums.OperationFlags actual;
            using (var gv = new GValue())
            {
                gv.SetType(gtype);
                gv.Set(Enums.OperationFlags.DEPRECATED);
                actual = (Enums.OperationFlags)gv.Get();
            }

            Assert.Equal(Enums.OperationFlags.DEPRECATED, actual);
        }
Beispiel #21
0
        /// <summary>
        /// Load from memory buffer 10000 times.
        ///
        /// It runs in a fairly steady 60MB of ram for me. Watching the output, you see
        /// stuff like:
        ///
        /// memory processing &lt;NetVips.Image 4120x2747 uchar, 3 bands, srgb&gt;
        /// 24 vips objects known to net-vips
        /// memory processing &lt;NetVips.Image 4120x2747 uchar, 3 bands, srgb&gt;
        /// 21 vips objects known to net-vips
        /// memory processing &lt;NetVips.Image 4120x2747 uchar, 3 bands, srgb&gt;
        /// 23 vips objects known to net-vips
        /// memory processing &lt;NetVips.Image 4120x2747 uchar, 3 bands, srgb&gt;
        /// 16 vips objects known to net-vips
        /// memory processing &lt;NetVips.Image 4120x2747 uchar, 3 bands, srgb&gt;
        /// 7 vips objects known to net-vips
        /// memory processing &lt;NetVips.Image 4120x2747 uchar, 3 bands, srgb&gt;
        /// 7 vips objects known to net-vips
        /// memory processing &lt;NetVips.Image 4120x2747 uchar, 3 bands, srgb&gt;
        /// 9 vips objects known to net-vips
        /// memory processing &lt;NetVips.Image 4120x2747 uchar, 3 bands, srgb&gt;
        /// 4 vips objects known to net-vips
        /// memory processing &lt;NetVips.Image 4120x2747 uchar, 3 bands, srgb&gt;
        ///
        /// So when around 25 vips objects are alive, the C# gc runs and they all get
        /// flushed.
        ///
        /// If you want it to run in less ram than that, you'll need to expose the GC and
        /// trigger it manually every so often.
        /// </summary>
        /// <param name="args">Command-line arguments.</param>
        /// <returns>Result.</returns>
        public string Execute(string[] args)
        {
            NetVips.LeakSet(true);

            NetVips.CacheSetMax(0);

            var imageBytes = File.ReadAllBytes(Filename);

            for (var i = 0; i < 10000; i++)
            {
                using var img = Image.NewFromBuffer(imageBytes);
                Console.WriteLine($"memory processing {img}");
                // uncomment this line together with the `NObjects` variable in GObject
                // Console.WriteLine($"{GObject.NObjects} vips objects known to net-vips");
            }

            return("All done!");
        }
Beispiel #22
0
        public void TestAtan2()
        {
            Skip.IfNot(NetVips.AtLeastLibvips(8, 12), "requires libvips >= 8.12");

            dynamic Atan2(dynamic x, dynamic y)
            {
                if (x is Image left)
                {
                    return(left.Atan2(y));
                }

                return(Math.Atan2(x[0], y[0]) * (180.0 / Math.PI));
            }

            var im = (Image.Black(100, 100) + new[] { 0, 1, 2 }) / 3.0;

            RunBinary(im.Bandsplit(), Atan2, Helper.NonComplexFormats);
        }
Beispiel #23
0
        public void TestAtanh()
        {
            Skip.IfNot(NetVips.AtLeastLibvips(8, 12), "requires libvips >= 8.12");

            dynamic Atanh(dynamic x)
            {
                if (x is Image image)
                {
                    return(image.Atanh());
                }

                return(Math.Atanh(x));
            }

            var im = (Image.Black(100, 100) + new[] { 0, 1, 2 }) / 3.0;

            RunUnary(new[] { im }, Atanh, Helper.NonComplexFormats);
        }
Beispiel #24
0
        public string Execute(string[] args)
        {
            NetVips.CacheSetMax(0);

            using var fileStream = File.OpenRead(Filename);
            using var image      = Image.NewFromStream(fileStream);

            for (var i = 0; i < 1000; i++)
            {
                using var crop = image.Crop(0, 0, 256, 256);
                var _ = crop.Avg();

                Console.WriteLine($"reference count: {image.RefCount}");

                // RefCount should not increase (i.e. operation should be freed)
                Debug.Assert(image.RefCount == 2u);
            }

            var count  = 0;
            var locker = new object();

            Parallel.For(0, 1000, new ParallelOptions {
                MaxDegreeOfParallelism = NetVips.ConcurrencyGet()
            },
                         i =>
            {
                Interlocked.Increment(ref count);

                using var crop = image.Crop(0, 0, 256, 256);
                lock (locker)
                {
                    var _ = crop.Avg();

                    Console.WriteLine($"reference count: {image.RefCount} with {count} active threads");

                    // RefCount -1 must be lower than or equal to the number of active threads
                    Debug.Assert(image.RefCount - 1 <= (uint)count);
                }

                Interlocked.Decrement(ref count);
            });

            return("All done!");
        }
Beispiel #25
0
        public void TestRegion()
        {
            Skip.IfNot(NetVips.AtLeastLibvips(8, 8), "requires libvips >= 8.8");

            var im     = Image.Black(100, 100);
            var region = Region.New(im);
            var data   = region.Fetch(0, 0, 10, 10);

            Assert.Equal(10, region.Width);
            Assert.Equal(10, region.Height);
            Assert.True(data.Length == 100);
            Assert.True(data.All(p => p == 0));

            data = region.Fetch(0, 0, 20, 10);

            Assert.Equal(20, region.Width);
            Assert.Equal(10, region.Height);
            Assert.True(data.Length == 200);
            Assert.True(data.All(p => p == 0));
        }
Beispiel #26
0
        public void TestHistLocal()
        {
            var im = Image.NewFromFile(Helper.JpegFile);

            var im2 = im.HistLocal(10, 10);

            Assert.Equal(im.Width, im2.Width);
            Assert.Equal(im.Height, im2.Height);

            Assert.True(im.Avg() < im2.Avg());
            Assert.True(im.Deviate() < im2.Deviate());

            if (NetVips.AtLeastLibvips(8, 5))
            {
                var im3 = im.HistLocal(10, 10, maxSlope: 3);
                Assert.Equal(im.Width, im3.Width);
                Assert.Equal(im.Height, im3.Height);

                Assert.True(im3.Deviate() < im2.Deviate());
            }
        }
Beispiel #27
0
        public void TestThumbnail()
        {
            Skip.IfNot(NetVips.AtLeastLibvips(8, 5), "requires libvips >= 8.5");

            var im = Image.Thumbnail(Helper.JpegFile, 100);

            Assert.Equal(100, im.Width);
            Assert.Equal(3, im.Bands);

            // the average shouldn't move too much
            var imOrig = Image.NewFromFile(Helper.JpegFile);

            Assert.True(Math.Abs(imOrig.Avg() - im.Avg()) < 1);

            // make sure we always get the right width
            for (var width = 1000; width >= 1; width -= 13)
            {
                im = Image.Thumbnail(Helper.JpegFile, width);
                Assert.Equal(width, im.Width);
            }

            // should fit one of width or height
            im = Image.Thumbnail(Helper.JpegFile, 100, height: 300);
            Assert.Equal(100, im.Width);
            Assert.NotEqual(300, im.Height);
            im = Image.Thumbnail(Helper.JpegFile, 300, height: 100);
            Assert.NotEqual(300, im.Width);
            Assert.Equal(100, im.Height);

            // with @crop, should fit both width and height
            im = Image.Thumbnail(Helper.JpegFile, 100, height: 300, crop: "centre");
            Assert.Equal(100, im.Width);
            Assert.Equal(300, im.Height);

            var im1 = Image.Thumbnail(Helper.JpegFile, 100);
            var buf = File.ReadAllBytes(Helper.JpegFile);
            var im2 = Image.ThumbnailBuffer(buf, 100);

            Assert.True(Math.Abs(im1.Avg() - im2.Avg()) < 1);
        }
        /// <summary>
        /// Map a GType to the name of the C# type we use to represent it.
        /// </summary>
        /// <param name="name">The GType identifier.</param>
        /// <param name="gtype">The GType to map.</param>
        /// <returns>The C# type we use to represent it.</returns>
        private string GTypeToCSharp(string name, IntPtr gtype)
        {
            if (_gTypeToCSharpDict.ContainsKey(gtype))
            {
                return(_gTypeToCSharpDict[gtype]);
            }

            var fundamental = NetVips.FundamentalType(gtype);

            if (_gTypeToCSharpDict.ContainsKey(fundamental))
            {
                return(_gTypeToCSharpDict[fundamental]);
            }

            if (fundamental == GValue.GFlagsType)
            {
                return(name switch
                {
                    "flags" => "Enums.ForeignFlags",
                    "filter" => "Enums.ForeignPngFilter",
                    _ => throw new Exception($"Unsupported type: {gtype} name: {name}")
                });
Beispiel #29
0
        public void TestText()
        {
            Skip.IfNot(Helper.Have("text"), "no text in this vips, skipping test");

            var im = Image.Text("Hello, world!", dpi: 300);

            Assert.True(im.Width > 10);
            Assert.True(im.Height > 10);
            Assert.Equal(1, im.Bands);
            Assert.Equal(Enums.BandFormat.Uchar, im.Format);
            Assert.Equal(255, im.Max());
            Assert.Equal(0, im.Min());

            if (NetVips.AtLeastLibvips(8, 9))
            {
                // test autofit
                im = Image.Text("Hello, world!", width: 500, height: 500);

                // quite a large threshold, since we need to work with a huge range of
                // text rendering systems
                Assert.True(Math.Abs(im.Width - 500) < 50);
            }
        }
Beispiel #30
0
        public void TestOpenexrLoad()
        {
            Skip.IfNot(Helper.Have("openexrload") && File.Exists(Helper.ExrFile), "no openexr support, skipping test");

            void ExrValid(Image im)
            {
                var a = im[10, 10];

                Helper.AssertAlmostEqualObjects(new[]
                {
                    0.124512,
                    0.159668,
                    0.040375,
                    // OpenEXR alpha is scaled to 0 - 255 in libvips 8.7+
                    NetVips.AtLeastLibvips(8, 7) ? 255 : 1.0
                }, a, 0.00001);
                Assert.Equal(610, im.Width);
                Assert.Equal(406, im.Height);
                Assert.Equal(4, im.Bands);
            }

            FileLoader("openexrload", Helper.ExrFile, ExrValid);
        }