rspatial/terra

JPG images are flipped when imported with rast()

Closed this issue · 7 comments

Hello,

This behavior occurs on both CRAN and dev versions.

library(terra)
#> terra 1.8.0
library(curl)
#> Using libcurl 7.88.1 with OpenSSL/3.0.14
filename <- tempfile(fileext = ".jpg")
curl_download(url = "https://upload.wikimedia.org/wikipedia/commons/thumb/2/28/JPG_Test.jpg/477px-JPG_Test.jpg", 
              destfile = filename, quiet = FALSE, mode = "wb")
testjpg <- terra::rast(filename)
#> Warning: [rast] unknown extent
plotRGB(testjpg)

terra::gdal(lib="all")
#>     gdal     proj     geos 
#>  "3.6.2"  "9.1.1" "3.11.1"
sessionInfo()
#> R version 4.4.1 (2024-06-14)
#> Platform: x86_64-pc-linux-gnu
#> Running under: Debian GNU/Linux 12 (bookworm)
#> 
#> Matrix products: default
#> BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.11.0 
#> LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.11.0
#> 
#> locale:
#>  [1] LC_CTYPE=fr_FR.UTF-8       LC_NUMERIC=C              
#>  [3] LC_TIME=fr_FR.UTF-8        LC_COLLATE=fr_FR.UTF-8    
#>  [5] LC_MONETARY=fr_FR.UTF-8    LC_MESSAGES=fr_FR.UTF-8   
#>  [7] LC_PAPER=fr_FR.UTF-8       LC_NAME=C                 
#>  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
#> [11] LC_MEASUREMENT=fr_FR.UTF-8 LC_IDENTIFICATION=C       
#> 
#> time zone: Europe/Paris
#> tzcode source: system (glibc)
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] curl_5.2.3  terra_1.8-0
#> 
#> loaded via a namespace (and not attached):
#>  [1] digest_0.6.37     codetools_0.2-20  fastmap_1.2.0     xfun_0.48        
#>  [5] glue_1.8.0        knitr_1.48        htmltools_0.5.8.1 rmarkdown_2.28   
#>  [9] lifecycle_1.0.4   cli_3.6.3         reprex_2.1.1      withr_3.0.1      
#> [13] compiler_4.4.1    rstudioapi_0.17.0 tools_4.4.1       evaluate_1.0.1   
#> [17] Rcpp_1.0.13       yaml_2.3.10       rlang_1.1.4       fs_1.6.4
library(terra)
#> terra 1.7.78
library(curl)
#> Using libcurl 7.88.1 with OpenSSL/3.0.14
filename <- tempfile(fileext = ".jpg")
curl_download(url = "https://upload.wikimedia.org/wikipedia/commons/thumb/2/28/JPG_Test.jpg/477px-JPG_Test.jpg", 
              destfile = filename, quiet = FALSE, mode = "wb")
testjpg <- terra::rast(filename)
#> Warning: [rast] unknown extent
plotRGB(testjpg)

terra::gdal(lib="all")
#>     gdal     proj     geos 
#>  "3.6.2"  "9.1.1" "3.11.1"
sessionInfo()
#> R version 4.4.1 (2024-06-14)
#> Platform: x86_64-pc-linux-gnu
#> Running under: Debian GNU/Linux 12 (bookworm)
#> 
#> Matrix products: default
#> BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.11.0 
#> LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.11.0
#> 
#> locale:
#>  [1] LC_CTYPE=fr_FR.UTF-8       LC_NUMERIC=C              
#>  [3] LC_TIME=fr_FR.UTF-8        LC_COLLATE=fr_FR.UTF-8    
#>  [5] LC_MONETARY=fr_FR.UTF-8    LC_MESSAGES=fr_FR.UTF-8   
#>  [7] LC_PAPER=fr_FR.UTF-8       LC_NAME=C                 
#>  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
#> [11] LC_MEASUREMENT=fr_FR.UTF-8 LC_IDENTIFICATION=C       
#> 
#> time zone: Europe/Paris
#> tzcode source: system (glibc)
#> 
#> attached base packages:
#> [1] stats     graphics  grDevices utils     datasets  methods   base     
#> 
#> other attached packages:
#> [1] curl_5.2.3   terra_1.7-78
#> 
#> loaded via a namespace (and not attached):
#>  [1] digest_0.6.37     codetools_0.2-20  fastmap_1.2.0     xfun_0.48        
#>  [5] glue_1.8.0        knitr_1.48        htmltools_0.5.8.1 rmarkdown_2.28   
#>  [9] lifecycle_1.0.4   cli_3.6.3         reprex_2.1.1      withr_3.0.1      
#> [13] compiler_4.4.1    rstudioapi_0.17.0 tools_4.4.1       evaluate_1.0.1   
#> [17] Rcpp_1.0.13       yaml_2.3.10       rlang_1.1.4       fs_1.6.4

Created on 2024-10-22 with reprex v2.1.1

Maybe this is regression from 049111e (if it worked in terra 1.7.78)?

Sorry, a0c09d9 is not related to this issue.

This problem is not exclusive of the jpg format:
Given
https://www.dropbox.com/scl/fi/qv7gph4sszd48beu43da1/DI17_2.tif?rlkey=1qo1i4p8h2xlx8gv17b6vgdyp&dl=0
image

> library(terra)
terra 1.7.83

plot(rast(file.path(dirdata, "DI17_2.tif")), col=gray.colors(256))

image

But, on the other hand:
plot(rast("logo.tif"))
image

I have the same probleme. On my machine (ubuntu), it works well, on a virtual machine (windows and ubuntu) it is flipped. The only difference is the gdal library version
gdal proj geos "3.4.1" "8.2.1" "3.10.2"

This version works well.

This one does not : gdal proj geos "3.9.3" "9.4.1" "3.12.2"

This now works as expected:

library(terra)
url <- "https://upload.wikimedia.org/wikipedia/commons/thumb/2/28/JPG_Test.jpg/477px-JPG_Test.jpg"
x <- terra::rast(url)
plot(x)

This behavior was caused by a fix for a case where flipping was needed. The problem is that these non-georeferenced images really are flipped in the way that GDAL returns the coordinates, but I won't correct for that. If need be I can add an option for files where flipping is required. .

Thank you.

I confirm it works, thanks.

If need be I can add an option for files where flipping is required. .

I think this would be best. Is there any way to check that a given file requires the vertical flip without displaying it?
(eg, some missing or wrong tag in the header).