private static unsafe HeifIntegerEncoderParameter CreateIntegerParameter(SafeHeifEncoder encoder,
                                                                                 heif_encoder_parameter nativeParameter,
                                                                                 string name,
                                                                                 bool hasDefaultValue)
        {
            int defaultValue = 0;

            if (hasDefaultValue)
            {
                // The error value is ignored because some encoders return an error
                // when getting the value of a valid command.
                _ = LibHeifNative.heif_encoder_get_parameter_integer(encoder, name, out defaultValue);
            }

            int minimum = 0;
            int maximum = 0;

            var error = LibHeifNative.heif_encoder_parameter_get_valid_integer_range(nativeParameter,
                                                                                     out bool haveMinimumMaximum,
                                                                                     ref minimum,
                                                                                     ref maximum);

            error.ThrowIfError();

            return(new HeifIntegerEncoderParameter(name, hasDefaultValue, defaultValue, haveMinimumMaximum, minimum, maximum));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="HeifEncoder"/> class.
        /// </summary>
        /// <param name="encoder">The encoder.</param>
        internal HeifEncoder(SafeHeifEncoder encoder)
        {
            Validate.IsNotNull(encoder, nameof(encoder));

            this.encoder = encoder;
            this.encoderParameterList  = new Lazy <ReadOnlyCollection <IHeifEncoderParameter> >(GetEncoderParameterList);
            this.encoderParameterTypes = new Lazy <IReadOnlyDictionary <string, HeifEncoderParameterType> >(GetEncoderParameterTypes);
        }
        private static HeifBooleanEncoderParameter CreateBooleanParameter(SafeHeifEncoder encoder,
                                                                          string name,
                                                                          bool hasDefaultValue)
        {
            bool defaultValue = false;

            if (hasDefaultValue)
            {
                // The error value is ignored because some encoders return an error
                // when getting the value of a valid command.
                _ = LibHeifNative.heif_encoder_get_parameter_boolean(encoder, name, out defaultValue);
            }

            return(new HeifBooleanEncoderParameter(name, hasDefaultValue, defaultValue));
        }
        private static unsafe HeifStringEncoderParameter CreateStringParameter(SafeHeifEncoder encoder,
                                                                               heif_encoder_parameter nativeParameter,
                                                                               string name,
                                                                               bool hasDefaultValue)
        {
            string defaultValue = string.Empty;

            if (hasDefaultValue)
            {
                byte[] bytes = new byte[256];

                fixed(byte *ptr = bytes)
                {
                    // The error value is ignored because some encoders return an error
                    // when getting the value of a valid command.
                    _ = LibHeifNative.heif_encoder_get_parameter_string(encoder, name, ptr, bytes.Length);
                }

                int count = bytes.Length;

                // Look for the NUL terminator at the end of the string.
                for (int i = 0; i < bytes.Length; i++)
                {
                    if (bytes[i] == 0)
                    {
                        count = i;
                        break;
                    }
                }

                defaultValue = System.Text.Encoding.ASCII.GetString(bytes, 0, count);
            }

            var error = LibHeifNative.heif_encoder_parameter_get_valid_string_values(nativeParameter, out var stringArray);

            error.ThrowIfError();

            var validItems = stringArray.ToReadOnlyCollection();

            return(new HeifStringEncoderParameter(name, hasDefaultValue, defaultValue, validItems));
        }
        /// <summary>
        /// Creates the specified encoder parameter.
        /// </summary>
        /// <param name="encoder">The encoder.</param>
        /// <param name="nativeParameter">The native encoder parameter.</param>
        /// <returns></returns>
        /// <exception cref="HeifException">An error occurred when creating the encoder parameter.</exception>
        public static unsafe IHeifEncoderParameter Create(SafeHeifEncoder encoder, heif_encoder_parameter nativeParameter)
        {
            string name = LibHeifNative.heif_encoder_parameter_get_name(nativeParameter).GetStringValue();
            var    type = LibHeifNative.heif_encoder_parameter_get_type(nativeParameter);

            bool hasDefaultValue = LibHeifNative.heif_encoder_has_default(encoder, name);

            switch (type)
            {
            case heif_encoder_parameter_type.Integer:
                return(CreateIntegerParameter(encoder, nativeParameter, name, hasDefaultValue));

            case heif_encoder_parameter_type.Boolean:
                return(CreateBooleanParameter(encoder, name, hasDefaultValue));

            case heif_encoder_parameter_type.String:
                return(CreateStringParameter(encoder, nativeParameter, name, hasDefaultValue));

            default:
                throw new HeifException($"Unknown { nameof(heif_encoder_parameter_type)}: { type }.");
            }
        }
        private static unsafe HeifIntegerEncoderParameter CreateIntegerParameter(SafeHeifEncoder encoder,
                                                                                 heif_encoder_parameter nativeParameter,
                                                                                 string name,
                                                                                 bool hasDefaultValue)
        {
            int defaultValue = 0;

            if (hasDefaultValue)
            {
                // The error value is ignored because some encoders return an error
                // when getting the value of a valid command.
                _ = LibHeifNative.heif_encoder_get_parameter_integer(encoder, name, out defaultValue);
            }

            bool haveMinimumMaximum;
            int  minimum     = 0;
            int  maximum     = 0;
            var  validValues = new List <int>();

            if (LibHeifVersion.Is1Point10OrLater)
            {
                var error = LibHeifNative.heif_encoder_parameter_get_valid_integer_values(nativeParameter,
                                                                                          out bool haveMinimum,
                                                                                          out bool haveMaximum,
                                                                                          ref minimum,
                                                                                          ref maximum,
                                                                                          out int numValidValues,
                                                                                          out var validValuesArray);
                error.ThrowIfError();

                haveMinimumMaximum = haveMinimum && haveMaximum;

                if (numValidValues > 0 && validValuesArray != IntPtr.Zero)
                {
                    validValues.Capacity = numValidValues;

                    int *integerArray = (int *)validValuesArray;

                    for (int i = 0; i < numValidValues; i++)
                    {
                        validValues.Add(integerArray[i]);
                    }
                }
            }
            else
            {
                var error = LibHeifNative.heif_encoder_parameter_get_valid_integer_range(nativeParameter,
                                                                                         out haveMinimumMaximum,
                                                                                         ref minimum,
                                                                                         ref maximum);
                error.ThrowIfError();
            }

            return(new HeifIntegerEncoderParameter(name,
                                                   hasDefaultValue,
                                                   defaultValue,
                                                   haveMinimumMaximum,
                                                   minimum,
                                                   maximum,
                                                   validValues.AsReadOnly()));
        }