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

[Backport release/3.9] JP2KAK: fix data corruption when creating multi-band tiled with the stripe compressor code path #10636

Merged
merged 1 commit into from
Aug 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions autotest/gdrivers/jp2kak.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,51 @@ def test_jp2kak_lossless_uint32_nbits_20():
gdal.GetDriverByName("JP2KAK").Delete(tmpfilename)


###############################################################################
# Test lossless copying of multi band with tiling (to cause a stripe_height != 1)


@pytest.mark.parametrize("use_stripe_compressor", ["YES", "NO"])
def test_jp2kak_lossless_multiband(tmp_vsimem, use_stripe_compressor):

src_ds = gdal.Open("data/rgbsmall.tif")
out_filename = str(tmp_vsimem / "out.jp2")
with gdaltest.config_option("JP2KAK_USE_STRIPE_COMPRESSOR", use_stripe_compressor):
gdal.GetDriverByName("JP2KAK").CreateCopy(
out_filename,
src_ds,
options=["QUALITY=100", "BLOCKXSIZE=32", "BLOCKYSIZE=24"],
)
ds = gdal.Open(out_filename)
assert [ds.GetRasterBand(i + 1).Checksum() for i in range(3)] == [
src_ds.GetRasterBand(i + 1).Checksum() for i in range(3)
]


###############################################################################
# Test lossless copying of multi band with tiling (to cause a stripe_height != 1)


@pytest.mark.parametrize("use_stripe_compressor", ["YES", "NO"])
def test_jp2kak_lossless_multiband_non_byte(tmp_vsimem, use_stripe_compressor):

src_ds = gdal.Open("data/rgbsmall.tif")
src_ds = gdal.Translate(
"", src_ds, options="-f MEM -ot UInt16 -scale 0 255 0 65535"
)
out_filename = str(tmp_vsimem / "out.jp2")
with gdaltest.config_option("JP2KAK_USE_STRIPE_COMPRESSOR", use_stripe_compressor):
gdal.GetDriverByName("JP2KAK").CreateCopy(
out_filename,
src_ds,
options=["QUALITY=100", "BLOCKXSIZE=32", "BLOCKYSIZE=24"],
)
ds = gdal.Open(out_filename)
assert [ds.GetRasterBand(i + 1).Checksum() for i in range(3)] == [
src_ds.GetRasterBand(i + 1).Checksum() for i in range(3)
]


###############################################################################
# Test lossy copying of Int32

Expand Down
7 changes: 6 additions & 1 deletion frmts/jp2kak/jp2kakdataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2904,7 +2904,9 @@ static GDALDataset *JP2KAKCreateCopy(const char *pszFilename,
std::vector<int> precisions(num_components);
for (int i = 0; i < num_components; ++i)
{
stripe_bufs[i] = pBuffer + nXSize * nDataTypeSizeBytes * i;
stripe_bufs[i] = pBuffer + static_cast<size_t>(nXSize) *
nDataTypeSizeBytes * i *
stripe_height;
is_signed[i] = CPL_TO_BOOL(GDALDataTypeIsSigned(eType));
precisions[i] = nBits;
}
Expand All @@ -2919,6 +2921,9 @@ static GDALDataset *JP2KAKCreateCopy(const char *pszFilename,
for (int i = 0; i < num_components; ++i)
{
stripe_heights[i] = nHeight;
stripe_bufs[i] = pBuffer + static_cast<size_t>(nXSize) *
nDataTypeSizeBytes * i *
nHeight;
}
}
if (poSrcDS->RasterIO(GF_Read, 0, iY, nXSize, nHeight, pBuffer,
Expand Down
Loading