Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional gamma correction parameter #350

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
23 changes: 16 additions & 7 deletions terracotta/handlers/rgb.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def rgb(
*,
stretch_ranges: Optional[ListOfRanges] = None,
gamma_factor: Optional[float] = None,
color_transform: Optional[str] = None,
tile_size: Optional[Tuple[int, int]] = None
) -> BinaryIO:
"""Return RGB image as PNG
Expand Down Expand Up @@ -81,6 +82,7 @@ def get_band_future(band_key: str) -> Future:
futures = [get_band_future(key) for key in rgb_values]
band_items = zip(rgb_values, stretch_ranges_, futures)

out_ranges = []
out_arrays = []

for i, (band_key, band_stretch_override, band_data_future) in enumerate(
Expand All @@ -107,14 +109,21 @@ def get_band_future(band_key: str) -> Future:

band_data = band_data_future.result()

if gamma_factor:
# gamma correction is monotonic and preserves percentiles
band_stretch_range_arr = np.array(band_stretch_range, dtype=band_data.dtype)
band_stretch_range = list(image.gamma_correction(band_stretch_range_arr, gamma_factor, band_range))
# gamma correct band data
band_data = image.gamma_correction(band_data, gamma_factor, band_range)
out_ranges.append(band_stretch_range)
out_arrays.append(band_data)

out_arrays.append(image.to_uint8(band_data, *band_stretch_range))
out = np.ma.stack(out_arrays, axis=0)

if color_transform:
band_stretch_range_arr = [np.array(band_rng, dtype=band_data.dtype) for band_rng in out_ranges]
band_stretch_range_arr = np.ma.stack(band_stretch_range_arr, axis=0)

band_stretch_range_arr = image.apply_color_transform(band_stretch_range_arr, color_transform)
band_data = image.apply_color_transform(out, color_transform)

out_arrays = []
for k in range(band_data.shape[0]):
out_arrays.append(image.to_uint8(band_data[k], *band_stretch_range_arr[k]))

out = np.ma.stack(out_arrays, axis=-1)
return image.array_to_png(out)
16 changes: 16 additions & 0 deletions terracotta/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import numpy as np
from PIL import Image
from color_operations import parse_operations
from color_operations.operations import gamma
from color_operations.utils import to_math_type, scale_dtype

Expand Down Expand Up @@ -187,6 +188,21 @@ def gamma_correction(
return arr


def apply_color_transform(
masked_data: Array,
color_transform: str,
out_dtype: type = np.uint16,
) -> Array:
"""Apply gamma correction to the input array and scale it to the output dtype."""
arr = to_math_type(masked_data)

for func in parse_operations(color_transform):
arr = func(arr)
Comment on lines +182 to +183
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this assume a particular scaling already?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. It may be easier to apply stretching before color transform then. I.e., normalize to [0,1] using stretch_range and clamp spillover to 0 / 1, then apply color transform, then convert to uint8. That way we don't rely on a transformation of the stretch range?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it may not be a good idea since it would change the result.

The color stretch fills the [0, 1] range whereas the normalization to_math_type does is relative to the dtype range. So when you apply the color transform I think that would shift where values fall along the curve.

This is what chatgpt said in the case of gamma correction:

Gamma correction is typically applied before color stretching, particularly when working with image data in scientific imaging, photography, or graphic design.

Here's why:

    Gamma Correction First: Gamma correction adjusts the image data to a linear color space, which compensates for the nonlinear response of human vision and many display systems. This linearization step ensures that subsequent adjustments, like color stretching, are applied to data that more accurately represents real-world light intensities.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, but who says that the dtype range is appropriate? The user-provided stretch range is telling us how to map the values in the raster to a linear scale.

Summoning @vincentsarago in case you want to weigh in on the appropriate order of linear scaling (and clamping out of range values) vs. color correction :)


arr = scale_dtype(arr, out_dtype)
biserhong marked this conversation as resolved.
Show resolved Hide resolved
return arr


def label(data: Array, labels: Sequence[Number]) -> Array:
"""Create a labelled uint8 version of data, with output values starting at 1.

Expand Down
4 changes: 4 additions & 0 deletions terracotta/server/rgb.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ class Meta:
missing=None,
description="Gamma factor to perform gamma correction."
)
color_transform = fields.String(
missing=None,
description="Gamma factor to perform gamma correction."
)
tile_size = fields.List(
fields.Integer(),
validate=validate.Length(equal=2),
Expand Down