Esempio n. 1
0
        /// <summary>
        /// Sets a string encoder parameter.
        /// </summary>
        /// <param name="name">The parameter name.</param>
        /// <param name="value">The parameter value.</param>
        /// <param name="coerceParameterType">
        /// <see langword="true"/> if the parameter should be coerced to the correct type; otherwise, <see langword="false"/>.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// <paramref name="name"/> is null.
        ///
        /// -or-
        ///
        /// <paramref name="value"/> is null.
        /// </exception>
        /// <exception cref="HeifException">
        /// A LibHeif error occurred.
        ///
        /// -or-
        ///
        /// Unable to convert the parameter to the correct type.
        /// </exception>
        /// <exception cref="ObjectDisposedException">The object has been disposed.</exception>
        private void SetStringParameter(string name, string value, bool coerceParameterType)
        {
            Validate.IsNotNull(name, nameof(name));
            Validate.IsNotNull(value, nameof(value));
            VerifyNotDisposed();

            if (coerceParameterType)
            {
                // Some encoders expect the method that is called to match the parameter type.
                // Attempting to set a Boolean or Integer parameter as a string will cause an invalid parameter error.
                if (this.encoderParameterTypes.Value.TryGetValue(name, out var type))
                {
                    switch (type)
                    {
                    case HeifEncoderParameterType.Boolean:
                        if (TryConvertStringToBoolean(value, out bool boolValue))
                        {
                            SetBooleanParameter(name, boolValue);
                        }
                        else
                        {
                            throw new HeifException(string.Format(CultureInfo.CurrentCulture,
                                                                  Resources.CoerceStringValueToBooleanFailedFormat,
                                                                  nameof(value)));
                        }
                        return;

                    case HeifEncoderParameterType.Integer:
                        if (int.TryParse(value, NumberStyles.Integer, CultureInfo.InvariantCulture, out int intValue))
                        {
                            SetIntegerParameter(name, intValue);
                        }
                        else
                        {
                            throw new HeifException(string.Format(CultureInfo.CurrentCulture,
                                                                  Resources.CoerceStringValueToIntegerFailedFormat,
                                                                  nameof(value)));
                        }
                        return;

                    case HeifEncoderParameterType.String:
                        // The parameter is the correct type.
                        break;

                    default:
                        throw new HeifException($"Unknown { nameof(HeifEncoderParameterType) }: { type }.");
                    }
                }
            }

            var error = LibHeifNative.heif_encoder_set_parameter_string(this.encoder, name, value);

            error.ThrowIfError();
        }