Esempio n. 1
0
 static GValue()
 {
     if (NetVips.AtLeastLibvips(8, 6))
     {
         BlendModeType = Vips.BlendModeGetType();
     }
 }
Esempio n. 2
0
        public Resize()
        {
            if (RuntimeInformation.OSArchitecture is Architecture.X86 or Architecture.X64)
            {
                // Workaround ImageMagick issue
                OpenCL.IsEnabled = false;
            }

            // Disable libvips operations cache
            NetVipsUtil.CacheSetMax(0);
        }
Esempio n. 3
0
        static GValue()
        {
            Vips.BandFormatGetType();
            BandFormatType = NetVips.TypeFromName("VipsBandFormat");

            if (NetVips.AtLeastLibvips(8, 6))
            {
                Vips.BlendModeGetType();
                BlendModeType = NetVips.TypeFromName("VipsBlendMode");
            }
        }
Esempio n. 4
0
        static void Main(string[] args)
        {
            if (!ModuleInitializer.VipsInitialized)
            {
                Console.WriteLine("Error: Unable to init libvips. Please check your PATH environment variable.");
                Console.ReadLine();
                return;
            }

            Console.WriteLine($"libvips {NetVips.Version(0)}.{NetVips.Version(1)}.{NetVips.Version(2)}");

            Console.WriteLine(
                $"Type a number (1-{Samples.Count}) to execute a sample of your choice. Press <Enter> or type 'Q' to quit.");

            DisplayMenu();

            string input;

            do
            {
                string[] sampleArgs = Array.Empty <string>();
                if (args.Length > 0)
                {
                    var sampleId = Samples.Select((value, index) => new { Index = index + 1, value.Name })
                                   .FirstOrDefault(s => s.Name.Equals(args[0]))?.Index;
                    input      = sampleId != null ? $"{sampleId}" : "0";
                    sampleArgs = args.Skip(1).ToArray();
                }
                else
                {
                    input = Console.ReadLine();
                }

                if (int.TryParse(input, out var userChoice) && TryGetSample(userChoice, out var sample))
                {
                    Console.WriteLine($"Executing sample: {sample.Name}");
                    var result = sample.Execute(sampleArgs);
                    Console.WriteLine("Sample successfully executed!");
                    if (result != null)
                    {
                        Console.WriteLine($"Result: {result}");
                    }
                }
                else
                {
                    Console.WriteLine("Sample doesn't exists, try again");
                }

                // Clear any arguments
                args = Array.Empty <string>();
            } while (!string.IsNullOrEmpty(input) && !string.Equals(input, "Q", StringComparison.OrdinalIgnoreCase));
        }
Esempio n. 5
0
        static GValue()
        {
            if (NetVips.AtLeastLibvips(8, 6))
            {
                BlendModeType = Vips.BlendModeGetType();
            }

            if (NetVips.AtLeastLibvips(8, 9))
            {
                SourceType = NetVips.TypeFromName("VipsSource");
                TargetType = NetVips.TypeFromName("VipsTarget");
            }
        }
Esempio n. 6
0
 /// <summary>
 /// Initializes the module.
 /// </summary>
 public static void Initialize()
 {
     try
     {
         VipsInitialized = NetVips.Init();
         if (VipsInitialized)
         {
             Version = NetVips.Version(0, false);
             Version = (Version << 8) + NetVips.Version(1, false);
             Version = (Version << 8) + NetVips.Version(2, false);
         }
         else
         {
             Exception = new VipsException("unable to initialize libvips");
         }
     }
     catch (Exception e)
     {
         VipsInitialized = false;
         Exception       = e;
     }
 }
Esempio n. 7
0
        /// <summary>
        /// Set a GValue.
        /// </summary>
        /// <remarks>
        /// The value is converted to the type of the GValue, if possible, and
        /// assigned.
        /// </remarks>
        /// <param name="value">Value to be set.</param>
        public void Set(object value)
        {
            // logger.Debug($"Set: value = {value}");
            var gtype       = GetTypeOf();
            var fundamental = GType.Fundamental(gtype);

            if (gtype == GBoolType)
            {
                Internal.GValue.SetBoolean(ref Struct, Convert.ToBoolean(value));
            }
            else if (gtype == GIntType)
            {
                Internal.GValue.SetInt(ref Struct, Convert.ToInt32(value));
            }
            else if (gtype == GUint64Type)
            {
                Internal.GValue.SetUint64(ref Struct, Convert.ToUInt64(value));
            }
            else if (gtype == GDoubleType)
            {
                Internal.GValue.SetDouble(ref Struct, Convert.ToDouble(value));
            }
            else if (fundamental == GEnumType)
            {
                Internal.GValue.SetEnum(ref Struct, Convert.ToInt32(value));
            }
            else if (fundamental == GFlagsType)
            {
                Internal.GValue.SetFlags(ref Struct, Convert.ToUInt32(value));
            }
            else if (gtype == GStrType)
            {
                var bytes = Encoding.UTF8.GetBytes(Convert.ToString(value) + char.MinValue); // Ensure null-terminated string
                Internal.GValue.SetString(ref Struct, bytes);
            }
            else if (gtype == RefStrType)
            {
                var bytes = Encoding.UTF8.GetBytes(Convert.ToString(value) + char.MinValue); // Ensure null-terminated string
                VipsValue.SetRefString(ref Struct, bytes);
            }
            else if (fundamental == GObjectType && value is GObject gObject)
            {
                AddMemoryPressure(gObject.MemoryPressure);
                Internal.GValue.SetObject(ref Struct, gObject);
            }
            else if (gtype == ArrayIntType)
            {
                if (!(value is IEnumerable))
                {
                    value = new[] { value };
                }

                int[] integers;
                switch (value)
                {
                case int[] ints:
                    integers = ints;
                    break;

                case double[] doubles:
                    integers = Array.ConvertAll(doubles, Convert.ToInt32);
                    break;

                case object[] objects:
                    integers = Array.ConvertAll(objects, Convert.ToInt32);
                    break;

                default:
                    throw new Exception(
                              $"unsupported value type {value.GetType()} for gtype {NetVips.TypeName(gtype)}");
                }

                VipsValue.SetArrayInt(ref Struct, integers, integers.Length);
            }
            else if (gtype == ArrayDoubleType)
            {
                if (!(value is IEnumerable))
                {
                    value = new[] { value };
                }

                double[] doubles;
                switch (value)
                {
                case double[] dbls:
                    doubles = dbls;
                    break;

                case int[] ints:
                    doubles = Array.ConvertAll(ints, Convert.ToDouble);
                    break;

                case object[] objects:
                    doubles = Array.ConvertAll(objects, Convert.ToDouble);
                    break;

                default:
                    throw new Exception(
                              $"unsupported value type {value.GetType()} for gtype {NetVips.TypeName(gtype)}");
                }

                VipsValue.SetArrayDouble(ref Struct, doubles, doubles.Length);
            }
            else if (gtype == ArrayImageType && value is Image[] images)
            {
                var size = images.Length;
                VipsValue.SetArrayImage(ref Struct, size);

                var ptrArr = VipsValue.GetArrayImage(in Struct, out _);

                for (var i = 0; i < size; i++)
                {
                    ref var image = ref images[i];

                    // the gvalue needs a ref on each of the images
                    Marshal.WriteIntPtr(ptrArr, i * IntPtr.Size, image.ObjectRef());

                    AddMemoryPressure(image.MemoryPressure);
                }
            }
Esempio n. 8
0
        /// <summary>
        /// Get all arguments for an operation.
        /// </summary>
        /// <remarks>
        /// Not quick! Try to call this infrequently.
        /// </remarks>
        /// <param name="operation">Operation to lookup.</param>
        /// <returns>Arguments for the operation.</returns>
        private IEnumerable <KeyValuePair <string, Enums.ArgumentFlags> > GetArgs(Operation operation)
        {
            var args = new List <KeyValuePair <string, Enums.ArgumentFlags> >();

            void AddArg(string name, Enums.ArgumentFlags flags)
            {
                // libvips uses '-' to separate parts of arg names, but we
                // need '_' for C#
                name = name.Replace("-", "_");

                args.Add(new KeyValuePair <string, Enums.ArgumentFlags>(name, flags));
            }

            // vips_object_get_args was added in 8.7
            if (NetVips.AtLeastLibvips(8, 7))
            {
                var result = Internal.VipsObject.GetArgs(operation, out var names, out var flags, out var nArgs);

                if (result != 0)
                {
                    throw new VipsException("unable to get arguments from operation");
                }

                for (var i = 0; i < nArgs; i++)
                {
                    var flag = (Enums.ArgumentFlags)Marshal.PtrToStructure(flags + i * sizeof(int), typeof(int));
                    if ((flag & Enums.ArgumentFlags.CONSTRUCT) == 0)
                    {
                        continue;
                    }

                    var name = Marshal.PtrToStringAnsi(Marshal.ReadIntPtr(names, i * IntPtr.Size));

                    AddArg(name, flag);
                }
            }
            else
            {
                IntPtr AddConstruct(IntPtr self, IntPtr pspec, IntPtr argumentClass, IntPtr argumentInstance,
                                    IntPtr a,
                                    IntPtr b)
                {
                    var flags = argumentClass.Dereference <VipsArgumentClass>().Flags;

                    if ((flags & Enums.ArgumentFlags.CONSTRUCT) == 0)
                    {
                        return(IntPtr.Zero);
                    }

                    var name = Marshal.PtrToStringAnsi(pspec.Dereference <GParamSpec.Struct>().Name);

                    AddArg(name, flags);

                    return(IntPtr.Zero);
                }

                Vips.ArgumentMap(operation, AddConstruct, IntPtr.Zero, IntPtr.Zero);
            }

            return(args);
        }