readTIFF | R Documentation |
Reads an image from a TIFF file/content into a raster array.
readTIFF(source, native = FALSE, all = FALSE, convert = FALSE,
info = FALSE, indexed = FALSE, as.is = FALSE,
payload = TRUE)
source |
Either name of the file to read from or a raw vector representing the TIFF file content. |
native |
logical, determines the image representation - if
|
all |
logical scalar or integer vector. TIFF files can contain
more than one image. If |
convert |
logical, if |
info |
logical, if set to |
indexed |
logical, if set to |
as.is |
logical, if |
payload |
logical, if |
Most common files decompress into RGB (3 channels), RGBA (4 channels),
Grayscale (1 channel) or GA (2 channels). Note that G and GA images
cannot be directly used in rasterImage
unless
native
is set to TRUE
because rasterImage
requires
RGB or RGBA format (nativeRaster
is always 8-bit RGBA).
TIFF images can have a wide range of internal representations, but only
the most common in image processing are directly supported (8-bit, 16-bit
integer and 32-bit float samples). Other formats (color maps, sub-8-bit
images, etc.) are only supported via convert=TRUE
which uses the
built-in facilities of the TIFF library to convert the image into RGBA
format with 8-bit samples (i.e. total of 32-bit per pixel) and then
store the relevant components from there into real arrays. This is the
same path as used by native=TRUE
and so differs only in the
output value. Note that conversion may result in different values than
direct acccess as it is intended mainly for viewing and not computation.
If native
is FALSE
then an array of the dimensions height
x width x channels. If there is only one channel the result is a
matrix. The values are reals between 0 and 1 (except for 32-bit floating
point sample storage which are unscaled reals, and for indexed and
as.is=TRUE
which are integers). If native
is
TRUE
then an object of the class nativeRaster
is
returned instead. The latter cannot be easily computed on but is the
most efficient way to draw using rasterImage
.
If all
is TRUE
or a vector of image indices,
then the result is a list of the above with
zero or more elements. If all
is a vector of indices, the result
will have exactly the same length as all
. If an index does not
appear in the file, the corresponding list entry will be NULL
.
If payload=FALSE
then the result is equivalent to
info=TRUE
but without the image data and returned as a data frame.
If all
is either TRUE
or a vector then the result will
be a data frame with each row corresponding to one image.
Some non-standard formats such as 12-bit TIFFs are partially supported (there is no standard for packing order for TIFFs beoynd 8-bit so we assume big-endian packing similar to the default fill order and only support single channel or indexed).
The as.is=TRUE
option is experimental, cannot be used with
native
or convert
and only works for integer storage
TIFFs.
Simon Urbanek Kent Johnson
rasterImage
, writeTIFF
Rlogo <- system.file("img", "Rlogo.tiff", package="tiff")
# read a sample file (R logo)
img <- readTIFF(Rlogo)
# read it also in native format
img.n <- readTIFF(Rlogo, native=TRUE)
# and also in converted
img.c <- readTIFF(Rlogo, convert=TRUE)
# read all contained images
str(readTIFF(Rlogo, all=TRUE))
# pick some images
str(readTIFF(Rlogo, all=c(5, 1, 3)))
# only show information
str(readTIFF(Rlogo, payload=FALSE))
# if your R supports it, we'll plot it
if (exists("rasterImage")) { # can plot only in R 2.11.0 and higher
plot(1:2, type='n')
if (names(dev.cur()) == "windows") {
# windows device doesn't support semi-transparency so we'll need
# to flatten the image
transparent <- img[,,4] == 0
img <- as.raster(img[,,1:3])
img[transparent] <- NA
# interpolate must be FALSE on Windows, otherwise R will
# try to interpolate transparency and fail
rasterImage(img, 1.2, 1.27, 1.8, 1.73, interpolate=FALSE)
} else {
# any reasonable device will be fine using alpha
rasterImage(img, 1.2, 1.27, 1.8, 1.73)
rasterImage(img.n, 1.5, 1.5, 1.9, 1.8)
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.