From 2286e99dd80a3ceb61cbdd0aa2e31e89957d3c4e Mon Sep 17 00:00:00 2001 From: Kip Cole Date: Sat, 20 Jul 2024 09:18:21 +1000 Subject: [PATCH] Add logistic_curve_rhs/1 --- lib/image.ex | 52 +++++++++++++++++++++++++++++++++++----------- lib/image/color.ex | 6 ++++++ 2 files changed, 46 insertions(+), 12 deletions(-) diff --git a/lib/image.ex b/lib/image.ex index 17a6e7a..7e58e47 100644 --- a/lib/image.ex +++ b/lib/image.ex @@ -8666,25 +8666,53 @@ defmodule Image do use Image.Math with {:ok, options} <- Options.Vibrance.validate_options(options) do - h1 = Operation.identity!() * 1.0 threshold = options.threshold * 1.0 - - h2 = 255.0 - h3 = 6.0 * h1 / h2 - h4 = 2.0 / (1.0 + exp!(-h3)) - 1.0 - h5 = h3 / 6.0 - h6 = h4 - h5 - h7 = vibrance - h8 = h5 + h7 * h6 + curve = logistic_curve_rhs(vibrance) with_colorspace(image, :lch, fn image -> - g5 = h2 * image[1] / threshold - g7 = threshold * Operation.maplut!(g5, h8) - Image.join_bands([image[0], g7, image[2]]) + g5 = Color.max_rgb() * image[1] / threshold + chroma = threshold * Operation.maplut!(g5, curve) + Image.join_bands([image[0], chroma, image[2]]) end) end end + @doc """ + Returns a single band image representing the + right hand side (positive range) of the + [logistic function](https://en.wikipedia.org/wiki/Logistic_function). + + ### Arguments + + * `k` is a float in the range `-1.0` to `1.0` representing + the logistic growth rate (slope of the curve). + + ### Returns + + * A single band `t:Vimage.t/0` representing the right hand side + (positive numbers) of the logistic curve. + + """ + # See https://github.com/libvips/libvips/discussions/4039 + @doc since: "0.54.0" + @doc subject: "Histogram" + + @spec logistic_curve_rhs(k :: float()) :: Vimage.t() + def logistic_curve_rhs(k) when is_float(k) and k >= -1.0 and k <= 1.0 do + use Image.Math + + h1 = Operation.identity!() * 1.0 + h2 = Color.max_rgb() + h3 = 6.0 * h1 / h2 + h4 = 2.0 / (1.0 + exp!(-h3)) - 1.0 + + h5 = h3 / 6.0 + h6 = h4 - h5 + h7 = k + h8 = h5 + h7 * h6 + h8 + end + @doc """ Apply an curve adjustment to an image's saturation (chroma) such that less saturated colors are more diff --git a/lib/image/color.ex b/lib/image/color.ex index c4f6b34..2911a20 100644 --- a/lib/image/color.ex +++ b/lib/image/color.ex @@ -102,6 +102,12 @@ defmodule Image.Color do """ defguard is_inbuilt_profile(profile) when profile in @inbuilt_profiles + @doc """ + Return max rgb value. + + """ + def max_rgb, do: 255 + @doc """ Returns the list of color profiles built into `libvips`.