[gdal-dev] JPEG compressed GeoTIFF ignores Nodata

Even Rouault even.rouault at mines-paris.org
Sat Jan 29 10:13:02 EST 2011


Le samedi 29 janvier 2011 15:47:43, Marius Jigmond a écrit :
> Hi Everyone,
> 
> I am having some trouble figuring out why adding JPEG compression to a
> RGB GeoTIFF results in Nodata pixel triplets becoming data pixel
> triplets. Is this expected behavior due to the noisy nature of JPEG
> compression or to the fact that these are border Nodata triplets? I know
> certain algorithms process the boundary between data and Nodata
> differently.

Marius,

Yes you did find the cause : the pixels maching nodata values will be JPEG 
compressed, so you have no guarantee that the 0 value is preserved through 
compression/decompression. I'd also note that the nodata concept in GDAL is a 
per-band value (so it should not be interpreted as a nodata "triplet")

A possibility to workaround this would be to create a "nodata mask band" (see 
http://trac.osgeo.org/gdal/wiki/rfc15_nodatabitmask), but I'm pretty sure that 
QGIS ignores those mask bands.

Anyway, if you want to try and create one, you can :

1) Use nearblack to create a nodata mask band based on (0,0,0) triplet
nearblack -setmask -of GTiff -o out_with_mask.tif in.tif

(the -setmask option is new in GDAL 1.8.0)

2) Then compress the dataset with
gdal_translate -co COMPRESS=JPEG -co TILED=YES -co JPEG_QUALITY=90 -co 
PHOTOMETRIC=YCBCR out_with_mask.tif out_compressed.tif

Only the RGB channels will be JPEG compressed : the mask band will use 
lossless compression so you won't have artifacts at the borders between 
significant areas and nodata areas.

(You can add --config GDAL_TIFF_INTERNAL_MASK YES to make the nodata mask 
internal to the GTiff file, instead of having a .tif.msk external file, which 
will not make any difference for software using GDAL API but can be more 
convenient to have a sinle file)

FYI, you can later reconvert the dataset into an uncompressed RGBA dataset :

gdal_translate out_compressed.tif out_rgba.tif -b 1 -b 2 -b 3 -b mask

Best regards,

Even


More information about the gdal-dev mailing list