Skip to content

Commit

Permalink
Add artificial alpha channel for NODATA masks
Browse files Browse the repository at this point in the history
When no alpha channel is available and NODATA values are present (as
mask_flags), use gdalwarp to create an artificial one in order to
prevent NODATA values from being subject to [JPEG] compression
artifacts.

Refs hotosm/OpenAerialMap#129
  • Loading branch information
mojodna committed Sep 12, 2018
1 parent 547a504 commit 9e1005d
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion bin/transcode.sh
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ ext=${input##*.}
failed=0
to_clean=()
unzipped=""
vrt=""

if [[ "$ext" == "zip" ]]; then
# assume it's a zipped TIFF
Expand Down Expand Up @@ -127,6 +128,8 @@ height=$(jq .height <<< $info)
width=$(jq .width <<< $info)
zoom=$(get_zoom.py $input)
colorinterp=$(jq .colorinterp <<< $info)
mask_flags=$(jq -c .mask_flags <<< $info)
nodata=$(jq -r .nodata <<< $info)
overviews=""
mask=""
opts=""
Expand Down Expand Up @@ -173,6 +176,19 @@ for b in $(seq 1 $count); do
fi
done

if [ -z "$mask" ] && grep -q nodata <<< $mask_flags; then
# no alpha channel mask was found; creating one from NODATA values
# without this, NODATA values are preserved but are subject to compression artifacts
>&2 echo "Creating an artificial alpha channel for NODATA masks"

vrt="${input}.vrt"
gdalwarp -q -srcnodata $nodata -dstalpha -of VRT $input $vrt
input="$vrt"
mask="-mask $[count + 1]"

to_clean+=($input)
fi

>&2 echo "Transcoding ${count} band(s)..."
update_status status "Transcoding ${count} band(s)..."
# TODO make timeout configurable
Expand Down Expand Up @@ -243,4 +259,4 @@ timeout --foreground 2h gdal_translate \
--config GDAL_TIFF_OVR_BLOCKSIZE 512 \
$intermediate $output

rm -f $intermediate $intermediate.msk ${intermediate}.aux.xml $unzipped
rm -f $intermediate $intermediate.msk ${intermediate}.aux.xml $unzipped $vrt

0 comments on commit 9e1005d

Please sign in to comment.