private static bool CheckType(Type dataType, FREE_IMAGE_MDTYPE type)
        {
            if (dataType != null)
            {
                switch (type)
                {
                case FREE_IMAGE_MDTYPE.FIDT_ASCII:
                    return(dataType == typeof(string));

                case FREE_IMAGE_MDTYPE.FIDT_BYTE:
                    return(dataType == typeof(byte));

                case FREE_IMAGE_MDTYPE.FIDT_DOUBLE:
                    return(dataType == typeof(double));

                case FREE_IMAGE_MDTYPE.FIDT_FLOAT:
                    return(dataType == typeof(float));

                case FREE_IMAGE_MDTYPE.FIDT_IFD:
                    return(dataType == typeof(uint));

                case FREE_IMAGE_MDTYPE.FIDT_LONG:
                    return(dataType == typeof(uint));

                case FREE_IMAGE_MDTYPE.FIDT_NOTYPE:
                    return(false);

                case FREE_IMAGE_MDTYPE.FIDT_PALETTE:
                    return(dataType == typeof(RGBQUAD));

                case FREE_IMAGE_MDTYPE.FIDT_RATIONAL:
                    return(dataType == typeof(FIURational));

                case FREE_IMAGE_MDTYPE.FIDT_SBYTE:
                    return(dataType == typeof(sbyte));

                case FREE_IMAGE_MDTYPE.FIDT_SHORT:
                    return(dataType == typeof(ushort));

                case FREE_IMAGE_MDTYPE.FIDT_SLONG:
                    return(dataType == typeof(int));

                case FREE_IMAGE_MDTYPE.FIDT_SRATIONAL:
                    return(dataType == typeof(FIRational));

                case FREE_IMAGE_MDTYPE.FIDT_SSHORT:
                    return(dataType == typeof(short));

                case FREE_IMAGE_MDTYPE.FIDT_UNDEFINED:
                    return(dataType == typeof(byte));
                }
            }

            return(false);
        }
        /// <summary>
        /// Sets the value of this tag to the value of <paramref name="value"/>
        /// using the given type.
        /// </summary>
        /// <param name="value">New value of the tag.</param>
        /// <param name="type">Data-type of the tag.</param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="value"/> is a null reference.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// <paramref name="type"/> is FIDT_ASCII and
        /// <paramref name="value"/> is not String.
        /// <paramref name="type"/> is not FIDT_ASCII and
        /// <paramref name="value"/> is not Array.</exception>
        /// <exception cref="NotSupportedException">
        /// <paramref name="type"/> is FIDT_NOTYPE.</exception>
        private unsafe bool SetArrayValue(object value, FREE_IMAGE_MDTYPE type)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            byte[] data = null;

            if (type == FREE_IMAGE_MDTYPE.FIDT_ASCII)
            {
                string tempValue = value as string;
                if (tempValue == null)
                {
                    throw new ArgumentException("value");
                }

                Type   = type;
                Length = Count = (uint)tempValue.Length;
                data   = new byte[Length];

                for (int i = 0; i < tempValue.Length; i++)
                {
                    data[i] = (byte)tempValue[i];
                }
            }
            else if (type == FREE_IMAGE_MDTYPE.FIDT_NOTYPE)
            {
                throw new NotSupportedException("type is not supported.");
            }
            else
            {
                Array array = value as Array;
                if (array == null)
                {
                    throw new ArgumentException("value");
                }

                if (array.Length != 0)
                {
                    if (!CheckType(array.GetValue(0).GetType(), type))
                    {
                        throw new ArgumentException("The type of value is incorrect.");
                    }
                }

                Type   = type;
                Count  = (uint)array.Length;
                Length = (uint)(array.Length * Marshal.SizeOf(idList[type]));
                data   = new byte[Length];
                FreeImage.CopyMemory(data, array, Length);
            }

            return(FreeImage.SetTagValue(tag, data));
        }
 /// <summary>
 /// Sets the value of the metadata.
 /// </summary>
 /// <param name="value">New data of the metadata.</param>
 /// <param name="type">Type of the data.</param>
 /// <returns>True on success, false on failure.</returns>
 /// <exception cref="NotSupportedException">
 /// The data type is not supported.</exception>
 /// <exception cref="ArgumentNullException">
 /// <paramref name="value"/> is null.</exception>
 /// <exception cref="ArgumentException">
 /// <paramref name="value"/> and <paramref name="type"/> to not fit.</exception>
 public bool SetValue(object value, FREE_IMAGE_MDTYPE type)
 {
     CheckDisposed();
     if ((!value.GetType().IsArray) && (!(value is string)))
     {
         Array array = Array.CreateInstance(value.GetType(), 1);
         array.SetValue(value, 0);
         return(SetArrayValue(array, type));
     }
     return(SetArrayValue(value, type));
 }
Пример #4
0
        /// <summary>
        /// Sets the value of this tag to the value of <paramref name="value"/>
        /// using the given type.
        /// </summary>
        /// <param name="value">New value of the tag.</param>
        /// <param name="type">Data-type of the tag.</param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="value"/> is a null reference.
        /// </exception>
        /// <exception cref="ArgumentException">
        /// <paramref name="type"/> is FIDT_ASCII and
        /// <paramref name="value"/> is not String.
        /// <paramref name="type"/> is not FIDT_ASCII and
        /// <paramref name="value"/> is not Array.</exception>
        /// <exception cref="NotSupportedException">
        /// <paramref name="type"/> is FIDT_NOTYPE.</exception>
        private unsafe bool SetArrayValue(object value, FREE_IMAGE_MDTYPE type)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            byte[] data = null;

            if (type == FREE_IMAGE_MDTYPE.FIDT_ASCII)
            {
                string tempValue = value as string;
                if (tempValue == null)
                {
                    throw new ArgumentException("value");
                }
                Type   = type;
                Count  = (uint)(tempValue.Length + 1);
                Length = (uint)((tempValue.Length * sizeof(byte)) + 1);
                data   = new byte[Length + 1];

                for (int i = 0; i < tempValue.Length; i++)
                {
                    data[i] = (byte)tempValue[i];
                }
                data[data.Length - 1] = 0;
            }
            else if (type == FREE_IMAGE_MDTYPE.FIDT_NOTYPE)
            {
                throw new NotSupportedException();
            }
            else
            {
                Array array = value as Array;
                if (array == null)
                {
                    throw new ArgumentException("value");
                }
                Type   = type;
                Count  = (uint)array.Length;
                Length = (uint)(array.Length * Marshal.SizeOf(idList[type]));
                data   = new byte[Length];
                GCHandle handle = GCHandle.Alloc(array, GCHandleType.Pinned);
                void *   src    = (void *)Marshal.UnsafeAddrOfPinnedArrayElement(array, 0);
                fixed(byte *dst = data)
                {
                    FreeImage.CopyMemory(dst, src, Length);
                }

                handle.Free();
            }

            return(FreeImage.SetTagValue(tag, data));
        }
 public static extern bool SetTagType(FITAG tag, FREE_IMAGE_MDTYPE type);
Пример #6
0
 private static extern bool SetTagTypeWindows(FITAG tag, FREE_IMAGE_MDTYPE type);
Пример #7
0
 private static extern bool SetTagTypeLinux(FITAG tag, FREE_IMAGE_MDTYPE type);