Skip to content

Commit

Permalink
COG: add NBITS creation option (fixes OSGeo#7361)
Browse files Browse the repository at this point in the history
  • Loading branch information
rouault authored and tschaub committed Dec 7, 2023
1 parent b19406d commit ce0db84
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
21 changes: 21 additions & 0 deletions autotest/gcore/cog.py
Original file line number Diff line number Diff line change
Expand Up @@ -1560,3 +1560,24 @@ def test_cog_copy_mdd():
ds = None

gdal.Unlink(filename)


###############################################################################
# Test NBITS creation option


def test_cog_NBITS():

src_ds = gdal.GetDriverByName("MEM").Create("", 1, 1)
drv = gdal.GetDriverByName("COG")
filename = "/vsimem/test_cog_NBITS.tif"
drv.CreateCopy(
filename,
src_ds,
options=["NBITS=7"],
)
ds = gdal.Open(filename)
assert ds.GetRasterBand(1).GetMetadataItem("NBITS", "IMAGE_STRUCTURE") == "7"
ds = None

gdal.Unlink(filename)
6 changes: 6 additions & 0 deletions doc/source/drivers/raster/cog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,12 @@ General creation options
or TARGET_SRS creation options. (Overview generation is also multithreaded since
GDAL 3.2)

- **NBITS=n**: (GDAL >= 3.7) Create a file with less than 8 bits per sample by
passing a value from 1 to 7. The apparent pixel type should be Byte.
Values of n=9...15 (UInt16 type) and n=17...31
(UInt32 type) are also accepted. From GDAL 2.2, n=16 is accepted for
Float32 type to generate half-precision floating point values.

- **PREDICTOR=[YES/NO/STANDARD/FLOATING_POINT]**: Set the predictor for LZW,
DEFLATE and ZSTD compression. The default is NO. If YES is specified, then
standard predictor (Predictor=2) is used for integer data type,
Expand Down
4 changes: 4 additions & 0 deletions frmts/gtiff/cogdriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1091,6 +1091,7 @@ GDALDataset *GDALCOGCreator::Create(const char *pszFilename,
CSLFetchNameValue(papszOptions, "GEOTIFF_VERSION"));
aosOptions.SetNameValue("SPARSE_OK",
CSLFetchNameValue(papszOptions, "SPARSE_OK"));
aosOptions.SetNameValue("NBITS", CSLFetchNameValue(papszOptions, "NBITS"));

if (EQUAL(osOverviews, "NONE"))
{
Expand Down Expand Up @@ -1315,6 +1316,9 @@ void GDALCOGDriver::InitializeCreationOptionList()
" <Option name='NUM_THREADS' type='string' "
"description='Number of worker threads for compression. "
"Can be set to ALL_CPUS' default='1'/>"
" <Option name='NBITS' type='int' description='BITS for sub-byte "
"files (1-7), sub-uint16_t (9-15), sub-uint32_t (17-31), or float32 "
"(16)'/>"
" <Option name='BLOCKSIZE' type='int' "
"description='Tile size in pixels' min='128' default='512'/>"
" <Option name='BIGTIFF' type='string-select' description='"
Expand Down

0 comments on commit ce0db84

Please sign in to comment.