Skip to content

Commit

Permalink
Internal libtiff: fix writing a Predictor=3 file with non-native endi…
Browse files Browse the repository at this point in the history
…anness
  • Loading branch information
rouault committed Dec 18, 2024
1 parent 50194b6 commit 42e8bb7
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
Binary file not shown.
1 change: 1 addition & 0 deletions autotest/gcore/tiff_read.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
("empty1bit.tif", 1, 0),
("gtiff/int64_full_range.tif", 1, 65535),
("gtiff/uint64_full_range.tif", 1, 1),
("gtiff/float32_lzw_predictor_3_big_endian.tif", 1, 4672),
]


Expand Down
20 changes: 20 additions & 0 deletions autotest/gcore/tiff_write.py
Original file line number Diff line number Diff line change
Expand Up @@ -11868,3 +11868,23 @@ def test_tiff_write_band_IMAGERY(tmp_vsimem):
)
with gdal.Open(filename2) as ds:
assert ds.GetRasterBand(1).GetMetadata_Dict("IMAGERY") == {"foo": "bar"}


###############################################################################
#


@pytest.mark.skipif(
not check_libtiff_internal_or_at_least(4, 7, 1),
reason="libtiff internal or >= 4.7.1 needed",
)
def test_tiff_write_float32_predictor_3_endianness(tmp_path):

out_filename = str(tmp_path / "out.tif")
gdal.GetDriverByName("GTiff").CreateCopy(
out_filename,
gdal.Open("data/float32.tif"),
options=["COMPRESS=LZW", "ENDIANNESS=INVERTED", "PREDICTOR=3"],
)
with gdal.Open(out_filename) as ds:
assert ds.GetRasterBand(1).Checksum() == 4672
15 changes: 10 additions & 5 deletions frmts/gtiff/libtiff/tif_predict.c
Original file line number Diff line number Diff line change
Expand Up @@ -208,16 +208,12 @@ static int PredictorSetupDecode(TIFF *tif)
/*
* The data should not be swapped outside of the floating
* point predictor, the accumulation routine should return
* byres in the native order.
* bytes in the native order.
*/
if (tif->tif_flags & TIFF_SWAB)
{
tif->tif_postdecode = _TIFFNoPostDecode;
}
/*
* Allocate buffer to keep the decoded bytes before
* rearranging in the right order
*/
}

return 1;
Expand Down Expand Up @@ -305,6 +301,15 @@ static int PredictorSetupEncode(TIFF *tif)
sp->encodetile = tif->tif_encodetile;
tif->tif_encodetile = PredictorEncodeTile;
}
/*
* The data should not be swapped outside of the floating
* point predictor, the differentiation routine should return
* bytes in the native order.
*/
if (tif->tif_flags & TIFF_SWAB)
{
tif->tif_postdecode = _TIFFNoPostDecode;
}
}

return 1;
Expand Down

0 comments on commit 42e8bb7

Please sign in to comment.