Skip to content
Prev 257727 / 398502 Next

Reading a TIFF file

Fine, looking at the code shows that readTiff read the image with using 
libtiff which should be fine. Afterwards, a pixmap obnject is generated 
via  pixmapRGB() which includes the following lines:

     datamax <- max(data)
     datamin <- min(data)
     data <- as.numeric(data)
     if (datamax > 1 || datamin < 0)
         data <- (data - datamin)/(datamax - datamin)

That means the data is perfectly fitted into the [0,1] interval.

That means we have not only rescaled but also another 0 now.

What I did is:

pic <- readTiff("test1_layer1.tif")
pic <- pic at red
ARC <- read.csv2("test1_arcgis.csv", header=TRUE)
ARC <- matrix(ARC[,2], nrow=nrow(pic), byrow=TRUE)

plot(pic)
plot(ARC)# looks *very* similar

plot(as.vector(pic) ~ as.vector(ARC))
# all on one line

summary(lm(as.vector(pic) ~ as.vector(ARC)))
# resuiduals < 10^(-14)

So the formula use to get from the ARC to the pixmap data is
pixmapdata = -0.82278 * 0.01266 ARCdata

if you want to get the original data, you can adapt the readTiff 
function for your own use for greyscales (and save memory that way) as in:


myReadTiff <- function (fn, page = 0)
{
   w <- .C("TiffGetWidth", as.character(fn), w = as.integer(0),
     PACKAGE = "rtiff")$w
   h <- .C("TiffGetHeight", as.character(fn), h = as.integer(0),
     PACKAGE = "rtiff")$h
   nw <- ceiling((1 - reduce) * w)
   nh <- ceiling((1 - reduce) * h)
   if (w > 0 && h > 0) {
     tiff <- .C("TiffReadTIFFRGBA", as.character(fn),
       page = as.integer(page),
       r = integer(w * h), g = integer(w * h), b = integer(w * h),
       PACKAGE = "rtiff")
     tiff <- tiff$r / 255
     tiff <- pixmapGrey(data = tiff, nrow = nh, ncol = nw)
     return(tiff)
   }
   stop("Could not open", fn, ".  File corrupted or missing.\n")
}

pic2 <- myReadTiff("test1_layer1.tif")


Now pic2 at grey will be exactly the same as ARC/255 from above.

Uwe Ligges
On 22.04.2011 15:10, Julio Rojas wrote: