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

Lossy mask band causes artifacts #11535

Closed
zdila opened this issue Dec 22, 2024 · 4 comments
Closed

Lossy mask band causes artifacts #11535

zdila opened this issue Dec 22, 2024 · 4 comments

Comments

@zdila
Copy link
Contributor

zdila commented Dec 22, 2024

What is the bug?

VRT file produces transparent pixels on mask edge.

image

Steps to reproduce the issue

Please download input data (54MB) from https://drive.google.com/file/d/1BgUZ2snzYeK3_7EpN4YCIZHe6niaywyJ/view?usp=sharing and unpack it. It contains one orthophoto image (SNINA_3-0.tif) and two overlapping masks (mask-left.tif and mask-right.tif).

  1. create 2 vrt files:

    gdalbuildvrt -a_srs EPSG:8353 left.vrt SNINA_3-0.tif
    gdalbuildvrt -a_srs EPSG:8353 right.vrt SNINA_3-0.tif
  2. edit left.vrt and right.vrt and add:

    <MaskBand>
      <VRTRasterBand dataType="Byte">
        <SimpleSource>
          <SourceFilename relativeToVRT="1">mask-left.tif</SourceFilename>
          <SourceBand>1</SourceBand>
        </SimpleSource>
      </VRTRasterBand>
    </MaskBand>

    (use mask-left.tif for left.vrt and mask-right.tif for right.vrt)

  3. warp left and right:

    gdalwarp -s_srs 'EPSG:8353' -t_srs 'EPSG:3857' -tr 0.29858214173896972948 0.29858214173896972948 -tap -r lanczos -of GTiff -co COMPRESS=JXL -co JXL_DISTANCE=1 -co JXL_LOSSLESS=NO -dstalpha left.vrt left.tif
    gdalwarp -s_srs 'EPSG:8353' -t_srs 'EPSG:3857' -tr 0.29858214173896972948 0.29858214173896972948 -tap -r lanczos -of GTiff -co COMPRESS=JXL -co JXL_DISTANCE=1 -co JXL_LOSSLESS=NO -dstalpha right.vrt right.tif
  4. produce final VRT:

    gdalbuildvrt final.vrt left.tif right.tif

Issue is not reproducible if JXL_LOSSLESS=YES (but I want it to be lossy because of the size).

Versions and provenance

Linux bono 6.11.7-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.11.7-1 (2024-11-09) x86_64 GNU/Linux

GDAL 3.11.0dev-051a853584, released 2024/10/31

Additional context

I have multiple huge jpeg-compressed geotiff orthophoto files which I want to reproject to pseudomercator and create a single VRT file from them. Each file covers a part of the country and they have some small overlap. For every JPEG I also have a cutline (vector mask) which I rasterize and use, because JPEG files have no alpha channel or mask.

I expect the resulting VRT to render without any gaps between parts.

I am using JXL because JPEG can't have an alpha channel.

@rouault
Copy link
Member

rouault commented Dec 22, 2024

maybe try -co JXL_ALPHA_DISTANCE=0 so the alpha channel is saved as lossless

@zdila
Copy link
Contributor Author

zdila commented Dec 22, 2024

@rouault this works for me. Thanks! Feel free to close this issue if it is actually not a bug.

Note that I was also trying to use alpha channel not as a mask, but as a classic alpha channel, but I also did not succeed.

Offtopic, feel free to ignore: it is possible to achieve the same with classic JPEG compression and with some external mask - all with overview support per a part (using gdaladdo)?

@rouault
Copy link
Member

rouault commented Dec 22, 2024

Note that I was also trying to use alpha channel not as a mask, but as a classic alpha channel, but I also did not succeed.

not sure to understand the difference between both

: it is possible to achieve the same with classic JPEG compression and with some external mask - all with overview support per a part (using gdaladdo)?

gdal_translate rgba.tif rgb_mask.tif -b 1 -b 2 -b 3 -mask 4 -co COMPRESS=JPEG [-co PHOTOMETRIC=YCBCR]

will generate a file with RGB JPEG compressed and an internal TIFF mask using lossless 1-bit Deflate lossless compression. You can use gdaladdo to add overviews to both RGB and mask channels

@rouault rouault closed this as completed Dec 22, 2024
@zdila
Copy link
Contributor Author

zdila commented Dec 22, 2024

I've tried to use JPEG codec with internal tiff mask. First I replaced mask in my vrt files with alpha channels:

<VRTRasterBand dataType="Byte" band="4">
    <ColorInterp>Alpha</ColorInterp>
    <SimpleSource>
      <SourceFilename relativeToVRT="1">mask-left.tif</SourceFilename>
      <SourceBand>1</SourceBand>
    </SimpleSource>
  </VRTRasterBand>

Then:

gdalwarp -s_srs 'EPSG:8353' -t_srs 'EPSG:3857' -tr 0.29858214173896972948 0.29858214173896972948 -tap -r lanczos -of GTiff -co COMPRESS=ZSTD -b 1 -b 2 -b 3 -dstalpha right.vrt right-tmp.tif
gdalwarp -s_srs 'EPSG:8353' -t_srs 'EPSG:3857' -tr 0.29858214173896972948 0.29858214173896972948 -tap -r lanczos -of GTiff -co COMPRESS=ZSTD -b 1 -b 2 -b 3 -dstalpha left.vrt left-tmp.tif

gdal_translate left-tmp.tif left.tif -b 1 -b 2 -b 3 -mask 4 -co COMPRESS=JPEG -co PHOTOMETRIC=YCBCR
gdal_translate right-tmp.tif right.tif -b 1 -b 2 -b 3 -mask 4 -co COMPRESS=JPEG -co PHOTOMETRIC=YCBCR

gdaladdo -r lanczos --config COMPRESS_OVERVIEW JPEG --config PHOTOMETRIC_OVERVIEW=YCBCR left.tif
gdaladdo -r lanczos --config COMPRESS_OVERVIEW JPEG --config PHOTOMETRIC_OVERVIEW=YCBCR right.tif

gdalbuildvrt final.vrt left.tif right.tif

Unfortunately overviews contain similar artifacts. If I omit gdaladdo step then image is OK, just loads slowly as it is missing overviews.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants