ext/RMagick/rmutil.cpp in rmagick-5.4.4 vs ext/RMagick/rmutil.cpp in rmagick-5.5.0

- old
+ new

@@ -358,63 +358,55 @@ * * No Ruby usage (internal function) * * @param arg the argument * @param max the maximum allowed value + * @param only_positive Accept whether only positive numbers? * @return a double */ double -rm_percentage(VALUE arg, double max) +rm_percentage2(VALUE arg, double max, bool only_positive) { double pct; char *end; if (!rm_check_num2dbl(arg)) { - char *pct_str; - long pct_long; - - arg = rb_rescue(RESCUE_FUNC(rb_str_to_str), arg, RESCUE_EXCEPTION_HANDLER_FUNC(rescue_not_str), arg); - pct_str = StringValueCStr(arg); - errno = 0; - pct_long = strtol(pct_str, &end, 10); - if (errno == ERANGE) - { - rb_raise(rb_eRangeError, "`%s' out of range", pct_str); - } - if (*end != '\0' && *end != '%') - { - rb_raise(rb_eArgError, "expected percentage, got `%s'", pct_str); - } - - if (*end == '%' && pct_long != 0) - { - pct = (((double)pct_long) / 100.0) * max; - } - else - { - pct = (double) pct_long; - } - if (pct < 0.0) - { - rb_raise(rb_eArgError, "percentages may not be negative (got `%s')", pct_str); - } + pct = rm_str_to_pct(arg, only_positive) * max; } else { pct = NUM2DBL(arg); - if (pct < 0.0) + if (pct < 0.0 && only_positive) { rb_raise(rb_eArgError, "percentages may not be negative (got `%g')", pct); } } return pct; } /** + * Return a double between 0.0 and max (the second argument), inclusive. If the + * argument is a number convert to a Float object, otherwise it's supposed to be + * a string in the form * "NN%". Convert to a number and then to a Float. + * + * No Ruby usage (internal function) + * + * @param arg the argument + * @param max the maximum allowed value + * @return a double + */ +double +rm_percentage(VALUE arg, double max) +{ + return rm_percentage2(arg, max, true); +} + + +/** * Return 0 if rb_num2dbl doesn't raise an exception. * * No Ruby usage (internal function) * * @param obj the object to convert to a double @@ -462,14 +454,15 @@ * Given a string in the form NN% return the corresponding double. * * No Ruby usage (internal function) * * @param str the string + * @param only_positive Accept whether only positive numbers? * @return a double */ double -rm_str_to_pct(VALUE str) +rm_str_to_pct(VALUE str, bool only_positive) { long pct; char *pct_str, *end; str = rb_rescue(RESCUE_FUNC(rb_str_to_str), str, RESCUE_EXCEPTION_HANDLER_FUNC(rescue_not_str), str); @@ -483,11 +476,11 @@ } if (*end != '%') { rb_raise(rb_eArgError, "expected percentage, got `%s'", pct_str); } - if (pct < 0L) + if (pct < 0L && only_positive) { rb_raise(rb_eArgError, "percentages may not be negative (got `%s')", pct_str); } return pct / 100.0; @@ -506,49 +499,11 @@ * @see Image_fuzz_eq */ double rm_fuzz_to_dbl(VALUE fuzz_arg) { - double fuzz; - char *end; - - if (!rm_check_num2dbl(fuzz_arg)) - { - char *fuzz_str; - - // Convert to string, issue error message if failure. - fuzz_arg = rb_rescue(RESCUE_FUNC(rb_str_to_str), fuzz_arg, RESCUE_EXCEPTION_HANDLER_FUNC(rescue_not_str), fuzz_arg); - fuzz_str = StringValueCStr(fuzz_arg); - errno = 0; - fuzz = strtod(fuzz_str, &end); - if (errno == ERANGE) - { - rb_raise(rb_eRangeError, "`%s' out of range", fuzz_str); - } - if(*end == '%') - { - if (fuzz < 0.0) - { - rb_raise(rb_eArgError, "percentages may not be negative (got `%s')", fuzz_str); - } - fuzz = (fuzz * QuantumRange) / 100.0; - } - else if(*end != '\0') - { - rb_raise(rb_eArgError, "expected percentage, got `%s'", fuzz_str); - } - } - else - { - fuzz = NUM2DBL(fuzz_arg); - if (fuzz < 0.0) - { - rb_raise(rb_eArgError, "fuzz may not be negative (got `%g')", fuzz); - } - } - - return fuzz; + return rm_percentage(fuzz_arg, QuantumRange); } /** * Convert a application-supplied number to a Quantum. If the object is a Float, @@ -1260,9 +1215,14 @@ image->units = info->units; } copy_options(image, info); + +#if defined(IMAGEMAGICK_7) + // The value of info->compression is referenced when saving the image in ImageMagick 7. + info->compression = image->compression; +#endif } /** * Replicate old (ImageMagick < 6.3.2) EXIF:* functionality using