Skip to content

Commit d05dc65

Browse files
authored
DAS-2280: Merge TRANSPARENT_IDX and NODATA_IDX to increase available … (#41)
1 parent 3d474ec commit d05dc65

File tree

5 files changed

+36
-27
lines changed

5 files changed

+36
-27
lines changed

CHANGELOG.md

+19-5
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,31 @@ HyBIG follows semantic versioning. All notable changes to this project will be
44
documented in this file. The format is based on [Keep a
55
Changelog](http://keepachangelog.com/en/1.0.0/).
66

7+
## [v2.2.0] - 2024-12-19
8+
9+
### Changed
10+
11+
* NODATA and TRANSPARENT values are merged. [[#41](https://github.com/nasa/harmony-browse-image-generator/pull/41)]
12+
- User visible change: paletted PNG outupt images will have up to 254 color
13+
values and a 255th value that is transparent.
14+
- Internal code changes: removes `TRANSPARENT_IDX` (254) and uses
15+
`NODATA_IDX` (255) in its stead. A color of (0,0,0,0) was previosly set to
16+
both the indexes (254 and 255) in the ouput PNGs and now only 255 will have
17+
this value. This change ensures the roundtrip from single band to RGBA to
18+
paletted PNG is consistent.
19+
720
## [v2.1.0] - 2024-12-13
821

922
### Changed
1023

11-
* Input GeoTIFF RGB[A] images are **no longer palettized** when converted to a PNG. The new resulting output browse images are now 3 or 4 band PNG retaining the color information of the input image.[#39](https://github.com/nasa/harmony-browse-image-generator/pull/39)
12-
* Changed pre-commit configuration to remove `black-jupyter` dependency [#38](https://github.com/nasa/harmony-browse-image-generator/pull/38)
13-
* Updates service image's python to 3.12 [#38](https://github.com/nasa/harmony-browse-image-generator/pull/38)
14-
* Simplifies test scripts to run with pytest and pytest plugins [#38](https://github.com/nasa/harmony-browse-image-generator/pull/38)
24+
* Input GeoTIFF RGB[A] images are **no longer palettized** when converted to a PNG. The new resulting output browse images are now 3 or 4 band PNG retaining the color information of the input image.[[#39](https://github.com/nasa/harmony-browse-image-generator/pull/39)]
25+
* Changed pre-commit configuration to remove `black-jupyter` dependency [[#38](https://github.com/nasa/harmony-browse-image-generator/pull/38)]
26+
* Updates service image's python to 3.12 [[#38](https://github.com/nasa/harmony-browse-image-generator/pull/38)]
27+
* Simplifies test scripts to run with pytest and pytest plugins [[#38](https://github.com/nasa/harmony-browse-image-generator/pull/38)]
1528

1629
### Removed
1730

18-
* Removes `test_code_format.py` in favor of `ruff` pre-commit configuration [#38](https://github.com/nasa/harmony-browse-image-generator/pull/38)
31+
* Removes `test_code_format.py` in favor of `ruff` pre-commit configuration [[#38](https://github.com/nasa/harmony-browse-image-generator/pull/38)]
1932

2033

2134
## [v2.0.2] - 2024-10-15
@@ -92,6 +105,7 @@ For more information on internal releases prior to NASA open-source approval,
92105
see legacy-CHANGELOG.md.
93106

94107
[unreleased]: https://github.com/nasa/harmony-browse-image-generator/
108+
[v2.2.0]: https://github.com/nasa/harmony-browse-image-generator/releases/tag/2.2.0
95109
[v2.1.0]: https://github.com/nasa/harmony-browse-image-generator/releases/tag/2.1.0
96110
[v2.0.2]: https://github.com/nasa/harmony-browse-image-generator/releases/tag/2.0.2
97111
[v2.0.1]: https://github.com/nasa/harmony-browse-image-generator/releases/tag/2.0.1

docker/service_version.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
2.1.0
1+
2.2.0

hybig/browse.py

+6-8
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828
NODATA_RGBA,
2929
OPAQUE,
3030
TRANSPARENT,
31-
TRANSPARENT_IDX,
32-
TRANSPARENT_RGBA,
3331
all_black_color_map,
3432
get_color_palette,
3533
palette_from_remote_colortable,
@@ -299,7 +297,7 @@ def convert_gray_1band_to_raster(data_array: DataArray) -> ndarray:
299297
"""Convert a 1-band raster without a color association."""
300298
band = data_array[0, :, :]
301299
cmap = matplotlib.colormaps['Greys_r']
302-
cmap.set_bad(TRANSPARENT_RGBA)
300+
cmap.set_bad(NODATA_RGBA)
303301
norm = Normalize(vmin=np.nanmin(band), vmax=np.nanmax(band))
304302
scalar_map = ScalarMappable(cmap=cmap, norm=norm)
305303

@@ -403,9 +401,9 @@ def palettize_raster(raster: ndarray) -> tuple[ndarray, dict]:
403401
written to the final raster as 254 and add the mapped RGBA value to the
404402
color palette.
405403
"""
406-
# reserves 254 for transparent images and 255 for off grid fill values
407-
# 0 to 253
408-
max_colors = 254
404+
# reserves 255 for transparent and off grid fill values
405+
# 0 to 254
406+
max_colors = 255
409407
rgb_raster, alpha = remove_alpha(raster)
410408

411409
multiband_image = Image.fromarray(reshape_as_image(rgb_raster))
@@ -427,8 +425,8 @@ def add_alpha(
427425
"""
428426
if alpha is not None and np.any(alpha != OPAQUE):
429427
# Set any alpha to the transparent index value
430-
quantized_array = np.where(alpha != OPAQUE, TRANSPARENT_IDX, quantized_array)
431-
color_map[TRANSPARENT_IDX] = TRANSPARENT_RGBA
428+
quantized_array = np.where(alpha != OPAQUE, NODATA_IDX, quantized_array)
429+
color_map[NODATA_IDX] = NODATA_RGBA
432430
return quantized_array, color_map
433431

434432

hybig/color_utility.py

-3
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@
2121
# Applied to transparent pixels where alpha < 255
2222
TRANSPARENT = np.uint8(0)
2323
OPAQUE = np.uint8(255)
24-
TRANSPARENT_RGBA = (0, 0, 0, 0)
25-
TRANSPARENT_IDX = 254
26-
2724
# Applied to off grid areas during reprojection
2825
NODATA_RGBA = (0, 0, 0, 0)
2926
NODATA_IDX = 255

tests/unit/test_browse.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,7 @@ def test_convert_5_multiband_to_raster(self):
654654
)
655655

656656
def test_standardize_raster_for_writing_jpeg_3band(self):
657-
raster = self.random.integers(255, size=(3, 5, 6))
657+
raster = self.random.integers(255, size=(3, 5, 6), dtype='uint8')
658658
count = 'irrelevant'
659659
driver = 'JPEG'
660660
expected_raster = np.copy(raster)
@@ -667,7 +667,7 @@ def test_standardize_raster_for_writing_jpeg_3band(self):
667667
np.testing.assert_array_equal(expected_raster, actual_raster, strict=True)
668668

669669
def test_standardize_raster_for_writing_jpeg_4band(self):
670-
raster = self.random.integers(255, size=(4, 7, 8))
670+
raster = self.random.integers(255, size=(4, 7, 8), dtype='uint8')
671671
driver = 'JPEG'
672672
count = 'irrelevant'
673673
expected_raster = np.copy(raster[0:3, :, :])
@@ -680,7 +680,7 @@ def test_standardize_raster_for_writing_jpeg_4band(self):
680680

681681
@patch('hybig.browse.palettize_raster')
682682
def test_standardize_raster_for_writing_png_4band(self, palettize_mock):
683-
raster = self.random.integers(255, size=(4, 7, 8))
683+
raster = self.random.integers(255, size=(4, 7, 8), dtype='uint8')
684684
driver = 'PNG'
685685
count = 'not 1'
686686

@@ -690,7 +690,7 @@ def test_standardize_raster_for_writing_png_4band(self, palettize_mock):
690690

691691
@patch('hybig.browse.palettize_raster')
692692
def test_standardize_raster_for_writing_png_3band(self, palettize_mock):
693-
raster = self.random.integers(255, size=(3, 7, 8))
693+
raster = self.random.integers(255, size=(3, 7, 8), dtype='uint8')
694694
driver = 'PNG'
695695
count = 'not 1'
696696

@@ -700,7 +700,7 @@ def test_standardize_raster_for_writing_png_3band(self, palettize_mock):
700700

701701
@patch('hybig.browse.palettize_raster')
702702
def test_prepare_1band_raster_for_writing_png(self, palettize_mock):
703-
raster = self.random.integers(255, size=(1, 7, 8))
703+
raster = self.random.integers(255, size=(1, 7, 8), dtype='uint8')
704704
driver = 'PNG'
705705
count = 1
706706
palettize_mock.return_value = (None, None)
@@ -711,7 +711,7 @@ def test_prepare_1band_raster_for_writing_png(self, palettize_mock):
711711
@patch('hybig.browse.get_color_map_from_image')
712712
def test_palettize_raster_no_alpha_layer(self, get_color_map_mock, image_mock):
713713
"""Test that the quantize function is called by a correct image."""
714-
raster = self.random.integers(255, dtype='uint8', size=(3, 10, 11))
714+
raster = self.random.integers(255, size=(3, 10, 11), dtype='uint8')
715715

716716
quantized_output = Image.fromarray(
717717
self.random.integers(254, size=(10, 11), dtype='uint8')
@@ -724,7 +724,7 @@ def test_palettize_raster_no_alpha_layer(self, get_color_map_mock, image_mock):
724724

725725
out_raster, out_map = palettize_raster(raster)
726726

727-
multiband_image_mock.quantize.assert_called_once_with(colors=254)
727+
multiband_image_mock.quantize.assert_called_once_with(colors=255)
728728
get_color_map_mock.assert_called_once_with(quantized_output)
729729

730730
np.testing.assert_array_equal(expected_out_raster, out_raster, strict=True)
@@ -733,7 +733,7 @@ def test_palettize_raster_no_alpha_layer(self, get_color_map_mock, image_mock):
733733
@patch('hybig.browse.get_color_map_from_image')
734734
def test_palettize_raster_with_alpha_layer(self, get_color_map_mock, image_mock):
735735
"""Test that the quantize function is called by a correct image."""
736-
raster = self.random.integers(255, dtype='uint8', size=(4, 10, 11))
736+
raster = self.random.integers(255, size=(4, 10, 11), dtype='uint8')
737737
# No transparent pixels
738738
raster[3, :, :] = 255
739739

@@ -748,11 +748,11 @@ def test_palettize_raster_with_alpha_layer(self, get_color_map_mock, image_mock)
748748
multiband_image_mock.quantize.return_value = quantized_output
749749

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

753753
out_raster, out_map = palettize_raster(raster)
754754

755-
multiband_image_mock.quantize.assert_called_once_with(colors=254)
755+
multiband_image_mock.quantize.assert_called_once_with(colors=255)
756756
get_color_map_mock.assert_called_once_with(quantized_output)
757757

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

0 commit comments

Comments
 (0)