diff --git a/CHANGES.md b/CHANGES.md index 27b5fddf..fc4a8ab9 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -4,14 +4,15 @@ ### titiler.core -* Add layer control to map viewer template (author @hrodmn, https://github.com/developmentseed/titiler/pull/1051) +* add layer control to map viewer template (author @hrodmn, https://github.com/developmentseed/titiler/pull/1051) * improve query string handling in LowerCaseQueryStringMiddleware using urlencode (author @pratapvardhan, https://github.com/developmentseed/titiler/pull/1050) +* add `titiler.core.utils.bounds_to_geometry` and reduce code duplication in factories (author @PratapVardhan, https://github.com/developmentseed/titiler/pull/1047) +* simplify image format dtype validation in `render_image` (author @PratapVardhan, https://github.com/developmentseed/titiler/pull/1046) ### titiler.application * update `/healthz` endpoint to return dependencies versions (titiler, rasterio, gdal, ...) (author @scottyhq, https://github.com/developmentseed/titiler/pull/1056) * migrate `templates/index.html` to bootstrap5, remove unused css, reuse bs classes (author @PratapVardhan, https://github.com/developmentseed/titiler/pull/1048) -* add `titiler.core.utils.bounds_to_geometry` and reduce code duplication in factories (author @PratapVardhan, https://github.com/developmentseed/titiler/pull/1047) ## 0.19.2 (2024-11-28) diff --git a/src/titiler/core/titiler/core/utils.py b/src/titiler/core/titiler/core/utils.py index 96c91d90..ea1da2a5 100644 --- a/src/titiler/core/titiler/core/utils.py +++ b/src/titiler/core/titiler/core/utils.py @@ -69,33 +69,20 @@ def render_image( if not output_format: output_format = ImageType.jpeg if mask.all() else ImageType.png - if output_format == ImageType.png and data.dtype not in ["uint8", "uint16"]: + # format-specific valid dtypes + format_dtypes = { + ImageType.png: ["uint8", "uint16"], + ImageType.jpeg: ["uint8"], + ImageType.jpg: ["uint8"], + ImageType.webp: ["uint8"], + ImageType.jp2: ["uint8", "int16", "uint16"], + } + + valid_dtypes = format_dtypes.get(output_format, []) + if valid_dtypes and data.dtype not in valid_dtypes: warnings.warn( - f"Invalid type: `{data.dtype}` for the `{output_format}` driver. Data will be rescaled using min/max type bounds or dataset_statistics.", - InvalidDatatypeWarning, - stacklevel=1, - ) - data = rescale_array(data, mask, in_range=datatype_range) - - elif output_format in [ - ImageType.jpeg, - ImageType.jpg, - ImageType.webp, - ] and data.dtype not in ["uint8"]: - warnings.warn( - f"Invalid type: `{data.dtype}` for the `{output_format}` driver. Data will be rescaled using min/max type bounds or dataset_statistics.", - InvalidDatatypeWarning, - stacklevel=1, - ) - data = rescale_array(data, mask, in_range=datatype_range) - - elif output_format == ImageType.jp2 and data.dtype not in [ - "uint8", - "int16", - "uint16", - ]: - warnings.warn( - f"Invalid type: `{data.dtype}` for the `{output_format}` driver. Data will be rescaled using min/max type bounds or dataset_statistics.", + f"Invalid type: `{data.dtype}` for the `{output_format}` driver. " + "Data will be rescaled using min/max type bounds or dataset_statistics.", InvalidDatatypeWarning, stacklevel=1, )