Skip to content

Commit

Permalink
DAS-2280: Merge TRANSPARENT_IDX and NODATA_IDX to increase available …
Browse files Browse the repository at this point in the history
…colors
  • Loading branch information
flamingbear committed Dec 19, 2024
1 parent 3d474ec commit 8a67f00
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 14 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ HyBIG follows semantic versioning. All notable changes to this project will be
documented in this file. The format is based on [Keep a
Changelog](http://keepachangelog.com/en/1.0.0/).

## [v2.2.0] - 2024-12-19

### Changed

* NODATA and TRANSPARENT values are merged.
- The user visible change is that now a paletted output PNG will have up to 254 color values and a 255th value that is transparent.
- Internally, the code change removes `TRANSPARENT_IDX` (254) and uses `NODATA_IDX` in its stead. A color of (0,0,0,0) was already set to both of the indexes in the ouput PNGs. This ensures the roundtrip from single band to RGBA to Paletted PNG is consistent.

## [v2.1.0] - 2024-12-13

### Changed
Expand Down
14 changes: 6 additions & 8 deletions hybig/browse.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
NODATA_RGBA,
OPAQUE,
TRANSPARENT,
TRANSPARENT_IDX,
TRANSPARENT_RGBA,
all_black_color_map,
get_color_palette,
palette_from_remote_colortable,
Expand Down Expand Up @@ -299,7 +297,7 @@ def convert_gray_1band_to_raster(data_array: DataArray) -> ndarray:
"""Convert a 1-band raster without a color association."""
band = data_array[0, :, :]
cmap = matplotlib.colormaps['Greys_r']
cmap.set_bad(TRANSPARENT_RGBA)
cmap.set_bad(NODATA_RGBA)
norm = Normalize(vmin=np.nanmin(band), vmax=np.nanmax(band))
scalar_map = ScalarMappable(cmap=cmap, norm=norm)

Expand Down Expand Up @@ -403,9 +401,9 @@ def palettize_raster(raster: ndarray) -> tuple[ndarray, dict]:
written to the final raster as 254 and add the mapped RGBA value to the
color palette.
"""
# reserves 254 for transparent images and 255 for off grid fill values
# 0 to 253
max_colors = 254
# reserves 255 for transparent and off grid fill values
# 0 to 254
max_colors = 255
rgb_raster, alpha = remove_alpha(raster)

multiband_image = Image.fromarray(reshape_as_image(rgb_raster))
Expand All @@ -427,8 +425,8 @@ def add_alpha(
"""
if alpha is not None and np.any(alpha != OPAQUE):
# Set any alpha to the transparent index value
quantized_array = np.where(alpha != OPAQUE, TRANSPARENT_IDX, quantized_array)
color_map[TRANSPARENT_IDX] = TRANSPARENT_RGBA
quantized_array = np.where(alpha != OPAQUE, NODATA_IDX, quantized_array)
color_map[NODATA_IDX] = NODATA_RGBA
return quantized_array, color_map


Expand Down
3 changes: 0 additions & 3 deletions hybig/color_utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@
# Applied to transparent pixels where alpha < 255
TRANSPARENT = np.uint8(0)
OPAQUE = np.uint8(255)
TRANSPARENT_RGBA = (0, 0, 0, 0)
TRANSPARENT_IDX = 254

# Applied to off grid areas during reprojection
NODATA_RGBA = (0, 0, 0, 0)
NODATA_IDX = 255
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/test_browse.py
Original file line number Diff line number Diff line change
Expand Up @@ -724,7 +724,7 @@ def test_palettize_raster_no_alpha_layer(self, get_color_map_mock, image_mock):

out_raster, out_map = palettize_raster(raster)

multiband_image_mock.quantize.assert_called_once_with(colors=254)
multiband_image_mock.quantize.assert_called_once_with(colors=255)
get_color_map_mock.assert_called_once_with(quantized_output)

np.testing.assert_array_equal(expected_out_raster, out_raster, strict=True)
Expand All @@ -748,11 +748,11 @@ def test_palettize_raster_with_alpha_layer(self, get_color_map_mock, image_mock)
multiband_image_mock.quantize.return_value = quantized_output

expected_out_raster = np.array(quantized_output).reshape(1, 10, 11)
expected_out_raster[0, 0:3, 0:3] = 254
expected_out_raster[0, 0:3, 0:3] = 255

out_raster, out_map = palettize_raster(raster)

multiband_image_mock.quantize.assert_called_once_with(colors=254)
multiband_image_mock.quantize.assert_called_once_with(colors=255)
get_color_map_mock.assert_called_once_with(quantized_output)

np.testing.assert_array_equal(expected_out_raster, out_raster, strict=True)
Expand Down

0 comments on commit 8a67f00

Please sign in to comment.