Skip to content

Commit

Permalink
version 1.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric Kort authored and gaborcsardi committed Dec 5, 2006
1 parent f2f5a2e commit 8e5f78b
Show file tree
Hide file tree
Showing 11 changed files with 190 additions and 84 deletions.
24 changes: 12 additions & 12 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
Package: rtiff
Type: Package
Title: A tiff reader for R.
Version: 1.0
Date: 2005-12-20
Depends: pixmap
Author: Eric Kort
Maintainer: Eric Kort <eric.kort@vai.org>
SystemRequirements: libtiff
Description: This package will read TIFF format images and return them as a pixmap object. Because the resulting object can be very large for even modestly sized TIFF images, images can be reduced as they are read for improved performance. This package is a wrapper around libtiff (www.libtiff.org), on which it depends (i.e. the libtiff shared library must be on your PATH for the binary to work, and tiffio.h must be on your system to build the package from source). By using libtiff's highlevel TIFFReadRGBAImage function, this package inherently support a wide range of image formats and compression schemes. This package also provides an implementation of the Ridler Autothresholding algorithm for easy generation of binary masks. NOTE TO SOURCE COMPILERS: Due to what appears to be a bug in GCC optimization, this package uses a simple custom Makefile in the src directory to ensure that compiler optimization is skipped. The Makefile is not sophisticated, and assumes you are using gcc as your compiler. Please contact the author if this is problematic.
License: GPL
Packaged: Tue Dec 27 13:43:32 2005; erikor
Package: rtiff
Type: Package
Title: A tiff reader for R.
Version: 1.1
Date: 2006-12-05
Depends: pixmap
Author: Eric Kort
Maintainer: Eric Kort <eric.kort@vai.org>
SystemRequirements: libtiff
Description: This package will read (and, as of version 1.1, write) TIFF format images and return them as a pixmap object. Because the resulting object can be very large for even modestly sized TIFF images, images can be reduced as they are read for improved performance. This package is a wrapper around libtiff (www.libtiff.org), on which it depends (i.e. the libtiff shared library must be on your PATH for the binary to work, and tiffio.h must be on your system to build the package from source). By using libtiff's highlevel TIFFReadRGBAImage function, this package inherently support a wide range of image formats and compression schemes. This package also provides an implementation of the Ridler Autothresholding algorithm for easy generation of binary masks.
License: GPL
Packaged: Tue Dec 5 16:56:06 2006; eric.kort
3 changes: 1 addition & 2 deletions NAMESPACE
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@

export(
readTiff,
writeTiff,
autoThreshold,
newPixmapRGB,
plot.matrix)

S3method(plot, matrix)


3 changes: 1 addition & 2 deletions R/autoThreshold.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,5 @@ function(d.m, est=0.5) {
t2 <- mean(d.m[d.m > est], na.rm=TRUE)
est <- mean(c(t1, t2), na.rm=TRUE)
}
return(c(t1, est, mean(c(t1, est)), mean(c(t2,est)), t2, ))
return(c(t1, mean(c(t1, est)), est, mean(c(t2,est)), t2))
}

5 changes: 1 addition & 4 deletions R/readTiff.R
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
"readTiff" <-
function(fn, page=1, reduce=0) {
function(fn, page=0, reduce=0) {
w <- integer(1)
h <- integer(1)

Expand Down Expand Up @@ -46,9 +46,6 @@ function(fn, page=1, reduce=0) {
rm(b)
gc()

pmap$rmax <- rmx
pmap$gmax <- gmx
pmap$bmax <- bmx
return(pmap)
} else {
cat("Could not open", fn, ". File corrupted or missing.\n")
Expand Down
13 changes: 13 additions & 0 deletions R/writeTiff.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"writeTiff" <-
function(pixmap, fn) {
if(class(pixmap) == "pixmapRGB"){
.Call("writeTiff", pixmap@red, pixmap@green, pixmap@blue, fn, PACKAGE="rtiff")
} else if(class(pixmap) == "matrix") {
pixmap = newPixmapRGB(pixmap, pixmap, pixmap);
.Call("writeTiff", pixmap@red, pixmap@green, pixmap@blue, fn, PACKAGE="rtiff")
} else {
stop(paste("writeTiff expects a pixmapRGB or matrix, got ", class(pixmap)))
}
gc();
return();
}
5 changes: 3 additions & 2 deletions man/autoThreshold.Rd
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
\alias{autoThreshold}
\title{ Suggests threshold levels to use in binarizing an image channel. }
\description{
This is an implementation of the Ridler method for binarization (see references).
This is an implementation of the Ridler method for binarization (see references). NOTE: The order of the values returned did
not match what is described in the "value" section below. This is fixed in this version.
}
\usage{
autoThreshold(d.m, est = 0.5)
Expand All @@ -15,7 +16,7 @@ autoThreshold(d.m, est = 0.5)
}
\value{
A vector (v) of estimates, the 3rd element of which is the true Ridler estimate. However, experience demonstrates
that sometimes a lower (elements 1 or 2) or higher (elements 4 or 4) estimate performs better for a given application.
that sometimes a lower (elements 1 or 2) or higher (elements 4 or 5) estimate performs better for a given application.
The Ridler estimate is the mean between the average intensity of bright regions in the raster and the average intensity
of dim regions. This is v[3]. v[1] is the average of dim regions, v[5] is the average of dim regions, and v[2] and v[4]
are the mean between the Ridler estimate and v[1] and v[2], respectively.
Expand Down
1 change: 0 additions & 1 deletion man/newPixmapRGB.Rd
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
\name{newPixmapRGB}
\alias{newPixmapRGB}
%- Also NEED an '\alias' for EACH other topic documented here.
\title{ Create an RGB pixmap }
\description{
A utility function to create an RGB pixmap object out of 3 rasters.
Expand Down
5 changes: 2 additions & 3 deletions man/plot.matrix.Rd
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
\alias{plot.matrix}
\title{ Overload the plot function for matrices }
\description{
This is a simple overloader for the plot function which takes a matrix,
converts it to and RGB pixmap (by replicating it 3 times, once for each
This is a simple S3 method for the plot function which takes a matrix,
converts it to an RGB pixmap (by replicating it 3 times, once for each
channel), and then plotting the resulting image.
}
\usage{
Expand All @@ -23,7 +23,6 @@ plot.matrix(x, ...)
library(rtiff)
tif <- readTiff(paste(.path.package("rtiff"), "/tiff/jello.tif", sep=""))

#note the overload...specifying plot.matrix is not necessary.
plot(tif@red)

}
Expand Down
116 changes: 58 additions & 58 deletions man/readTiff.Rd
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,58 +1,58 @@
\name{readTiff}
\alias{readTiff}
\title{ A function to load TIFF images into a pixmap. }
\description{
Loads a TIFF image from a file and returns the image as a pixmap object, with optional scaling.
}
\usage{
readTiff(fn, page = 1, reduce = 0)
}
%- maybe also 'usage' for other objects documented here.
\arguments{
\item{fn}{ Filename (the tiff image to load) }
\item{page}{ In the case of multi-page tiffs, which page do you want? }
\item{reduce}{ Optional scaling factor to improve performance with large images,
should be a value between 0 and 1 (i.e. a decimal representation of
a percentage). See details. }
}
\details{


This package is a wrapper around libtiff (www.libtiff.org), on which it depends. By using libtiff's
highlevel TIFFReadRGBAImage function, this package inherently support a wide range of image formats
and compression schemes (interestingly, thanks to libtiff, this package can load a number of TIFF formats
that tools like Window's Paint or the open source Gimp application will not load).

High resolution images (by which I mean images that contain a "large" number of pixels) will occupy
quite a bit of memory and will also plot very slowly. If you do not need all the resolution for your
purposes, you scan specify a scaling factor (reduce=x) to downsample the image. The factor is the
amount you want the image scaled BY, not TO. In otherwords, reduce=.90 will reduce the image by 90%,
yielding an image 10% the size of the orginal. The scaling is not sophisticated...a nearest neighbor
approach is used. Since we are reducing and not enlarging, I hope this will be suitable for your
applications.

For simplicity, an RGB pixmap is generated regardless of the colorspace of the original image (including
grayscale images, in which case the R, G, and B rasters are identical). The pixmap object requires
pixel intensities to be between 0 and 1, so the intensities in the orginal image file are scaled
accordingly.

}
\value{
A pixmap object containing the image rasters.
}

\author{ Eric Kort <eric.kort@vai.org> }


\examples{

library(rtiff)
tif <- readTiff(paste(.path.package("rtiff"), "/tiff/jello.tif", sep=""))
plot(tif)


}

\keyword{ utilities }
\keyword{ IO }
\keyword{ file }
\name{readTiff}
\alias{readTiff}
\title{ A function to load TIFF images into a pixmap. }
\description{
Loads a TIFF image from a file and returns the image as a pixmap object, with optional scaling.
}
\usage{
readTiff(fn, page = 0, reduce = 0)
}
%- maybe also 'usage' for other objects documented here.
\arguments{
\item{fn}{ Filename (the tiff image to load) }
\item{page}{ In the case of multi-page tiffs, which page do you want? }
\item{reduce}{ Optional scaling factor to improve performance with large images,
should be a value between 0 and 1 (i.e. a decimal representation of
a percentage). See details. }
}
\details{


This package is a wrapper around libtiff (www.libtiff.org), on which it depends. By using libtiff's
highlevel TIFFReadRGBAImage function, this package inherently support a wide range of image formats
and compression schemes (interestingly, thanks to libtiff, this package can load a number of TIFF formats
that tools like Window's Paint or the open source Gimp application will not load).

High resolution images (by which I mean images that contain a "large" number of pixels) will occupy
quite a bit of memory and will also plot very slowly. If you do not need all the resolution for your
purposes, you scan specify a scaling factor (reduce=x) to downsample the image. The factor is the
amount you want the image scaled BY, not TO. In otherwords, reduce=.90 will reduce the image by 90%,
yielding an image 10% the size of the orginal. The scaling is not sophisticated...a nearest neighbor
approach is used. Since we are reducing and not enlarging, I hope this will be suitable for your
applications.

For simplicity, an RGB pixmap is generated regardless of the colorspace of the original image (including
grayscale images, in which case the R, G, and B rasters are identical). The pixmap object requires
pixel intensities to be between 0 and 1, so the intensities in the orginal image file are scaled
accordingly.

}
\value{
A pixmap object containing the image rasters.
}

\author{ Eric Kort <eric.kort@vai.org> }


\examples{

library(rtiff)
tif <- readTiff(paste(.path.package("rtiff"), "/tiff/jello.tif", sep=""))
plot(tif)


}

\keyword{ utilities }
\keyword{ IO }
\keyword{ file }
41 changes: 41 additions & 0 deletions man/writeTiff.Rd
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
\name{writeTiff}
\alias{writeTiff}
\title{ A function to load TIFF images into a pixmap. }
\description{
Loads a TIFF image from a file and returns the image as a pixmap object, with optional scaling.
}
\usage{
writeTiff(pixmap, fn)
}
%- maybe also 'usage' for other objects documented here.
\arguments{
\item{pixmap}{ Either a pixmapRGB or matrix containing the image to save. In the case of a matrix,
a new pixmapRGB will be created from it. The resulting TIFF file will be RGB, but
will appear grey because each channel will be identical. }
\item{fn}{ What to call the new tiff file. }
}
\details{

This function saves the given pixmap or matrix raster as an unencrypted TIFF image, utilizing libtiff's
TIFFWriteEncodedStrip, with the entire raster in a single strip (for simplicity).
}
\value{
None. Used for its handy side effect of creating a tiff file.
}
\author{ Eric Kort <eric.kort@vai.org> }
\examples{
library(rtiff)
tif <- readTiff(paste(.path.package("rtiff"), "/tiff/jello.tif", sep=""))
writeTiff(tif@red, "atesttif.tif")
}
\keyword{ utilities }
\keyword{ IO }
\keyword{ file }
58 changes: 58 additions & 0 deletions src/rtiff.c
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#include <math.h>
#include <tiffio.h>
#include <R.h>
#include <Rdefines.h>
#include <Rinternals.h>

/*============================================================================
* tiffRead --
*
Expand Down Expand Up @@ -119,6 +123,60 @@ void TiffReadTIFFRGBA (char** fileName, int* dir, int* r, int* g, int* b) {
return;
}

void writeTiff(SEXP mr, SEXP mg, SEXP mb, SEXP fn)
{
TIFF *output;
char *raster;
int x, y;
int h = INTEGER(GET_DIM(mr))[0];
int w = INTEGER(GET_DIM(mr))[1];
double *r = REAL(mr);
double *g = REAL(mg);
double *b = REAL(mb);
char* fileName = CHAR(STRING_ELT(fn, 0)) ;

// Open the output image
if((output = TIFFOpen(fileName, "w")) == NULL){
fprintf(stderr, "Could not open outgoing image\n");
exit(42);
}

if((raster = (char*) malloc(sizeof(char*) * w * h * 3)) == NULL){
fprintf(stderr, "Could not allocate enough memory\n");
exit(42);
}

for (x=0; x<w; x++)
{
for (y=0; y<h; y++)
{
int index = 3 * (y * w + x);
raster[index] = (char)(255.0 * r[y + x*h]);
raster[index + 1] = (char)(255.0 * g[y + x*h]);
raster[index + 2] = (char)(255.0 * b[y + x*h]);
}
}

// Write the tiff tags to the file
TIFFSetField(output, TIFFTAG_IMAGEWIDTH, w);
TIFFSetField(output, TIFFTAG_IMAGELENGTH, h);
TIFFSetField(output, TIFFTAG_COMPRESSION, 1);
TIFFSetField(output, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
TIFFSetField(output, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
TIFFSetField(output, TIFFTAG_BITSPERSAMPLE, 8);
TIFFSetField(output, TIFFTAG_SAMPLESPERPIXEL, 3);

// Actually write the image
if(TIFFWriteEncodedStrip(output, 0, raster, w * h * 3) == 0){
fprintf(stderr, "Could not write image\n");
exit(42);
}

TIFFClose(output);
free(raster);
return;
}

void reduce(int* r, int* nr, int* w, int* h, double* p)
{
int nw, nh, x, y, i;
Expand Down

0 comments on commit 8e5f78b

Please sign in to comment.