Пример #1
0
        /// <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>
        public static string GTypeToCSharp(IntPtr gtype)
        {
            var fundamental = GType.Fundamental(gtype);

            if (GTypeToCSharpDict.ContainsKey(gtype))
            {
                return(GTypeToCSharpDict[gtype]);
            }

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

            return("object");
        }
Пример #2
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);
                }
            }
Пример #3
0
 /// <summary>
 /// Extract the fundamental type ID portion.
 /// </summary>
 /// <param name="type">A valid type ID.</param>
 /// <returns>Fundamental type ID.</returns>
 public static IntPtr FundamentalType(IntPtr type)
 {
     return(GType.Fundamental(type));
 }