readChar in R is unable to read the tiff tag

68 views Asked by At

I am trying to read a a tiff file that is in Little endian format. In the Image File directory the first tag value is 0100(in hex). When i am trying to read these 2 bytes it gives the following result.

>readChar(fptr,nchars=2,TRUE)
[1] ""
But when i read single bytes then it correctly gives
>readChar(fptr,nchars=1,TRUE)
[1] ""
>readChar(fptr,nchars=1,TRUE)
[1] "\001"
>
1

There are 1 answers

0
Richie Cotton On BEST ANSWER

Here's a tiff file:

filename <- "test.tiff"
tiff(filename)
plot(1)
dev.off()

You can read it in as raw bytes using readBin.

n <- file.info(filename)$size
bytes <- readBin(filename, raw(), n = n)

You may prefer to read it in using tiff::readTIFF.

library(tiff)
the_plot <- readTIFF(filename)

Then you can include it inside your other plots using rasterImage.

plot(1, 1, 'n')
rasterImage(the_plot, 0.8, 0.8, 1.2, 1.2)