Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stop raster output from being resampled #55

Merged
merged 10 commits into from
Feb 21, 2014
78 changes: 46 additions & 32 deletions R/tikzInternal.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,63 +16,77 @@ getTikzDeviceVersion <- function() {
as.character(packageVersion('tikzDevice'))
}

tikz_writeRaster <-
function(
fileName, rasterCount, rasterData, nrows, ncols,
finalDims, interpolate
){

raster_file <- basename(tools::file_path_sans_ext(fileName))
raster_file <- file.path(dirname(fileName),
paste(raster_file, '_ras', rasterCount, '.png', sep = '')
)

tikz_writeRaster <- function(fileName, rasterCount, rasterData,
nrows, ncols, finalDims, interpolate) {
# Convert the 4 vectors of RGBA data contained in rasterData to a raster
# image.
rasterData[['maxColorValue']] = 255
rasterData = do.call( grDevices::rgb, rasterData )
rasterData = as.raster(
matrix( rasterData, nrow = nrows, ncol = ncols, byrow = TRUE ) )

raster_file <- paste0(
tools::file_path_sans_ext(fileName),
'_ras', rasterCount, '.png')

res = getOption('tikzRasterResolution')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can use res = getOption('tikzRasterResolution', NA) and delete the three lines below

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hadn't noticed that parameter before—thanks! Have pushed an appropriate change to my noresample repo

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please open a new pull request?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, I'm sure when I get people hacking my repos I'll appreciate all these ways of moving code around!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sure you will ;-) Merging with a few clicks is very comfortable indeed.

if (is.null(res)) res = NA;
if (is.na(res)) interpolate = FALSE;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

else if?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, now I see that else would be wrong. Sorry.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, maybe a comment explaining what I'm doing would help?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would make things clearer. On the other hand, does the setting of interpolate make a difference at all if res is NA?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it seems to make a difference. I get something very different from what I put in without this


# Write the image to a PNG file.

# On OS X there is a problem with png() not respecting antialiasing options.
# So, we have to use quartz instead. Also, we cannot count on X11 or Cairo
# being compiled into OS X binaries. Technically, cannot count on Aqua/Quartz
# either but you would have to be a special kind of special to leave it out.
# Using type='Xlib' also causes a segfault for me on OS X 10.6.4
if ( Sys.info()['sysname'] == 'Darwin' && capabilities('aqua') ){

grDevices::quartz( file = raster_file, type = 'png',
width = finalDims$width, height = finalDims$height, antialias = FALSE,
dpi = getOption('tikzRasterResolution') )

if ( Sys.info()['sysname'] == 'Darwin' && capabilities('aqua') ) {
if (is.na(res)) {
grDevices::quartz(
file = raster_file, type = 'png',
width = ncols, height = nrows,
antialias = FALSE, dpi = 1 )
} else {
grDevices::quartz(
file = raster_file, type = 'png',
width = finalDims$width, height = finalDims$height,
antialias = FALSE, dpi = res )
}
} else if (Sys.info()['sysname'] == 'Windows') {

png( filename = raster_file, width = finalDims$width, height = finalDims$height,
units = 'in', res = getOption('tikzRasterResolution') )

if (is.na(res)) {
# we can create very small PNGs with this code, so suppress
# warnings about this
suppressWarnings(png(filename = raster_file,
width = ncols, height = nrows, units = 'px'))
} else {
png(filename = raster_file,
width = finalDims$width, height = finalDims$height,
units = 'in', res = res )
}
} else {

# Linux/UNIX and OS X without Aqua.
png( filename = raster_file, width = finalDims$width, height = finalDims$height,
type = 'Xlib', units = 'in', antialias = 'none',
res = getOption('tikzRasterResolution') )

if (is.na(res)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't this be handled by a single call to png()? The parameters seem the same otherwise, and if res is omitted it's NA.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, I've reworked the code a bit and am submitting a patch that tidies this all up a lot. there's still a divide between grDevices::quartz vs basic png call, which I don't quite understand, but am leaving in

# we can create very small PNGs with this code, so suppress
# warnings about this
suppressWarnings(png(filename = raster_file,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if we really should suppress warnings here. Is there potential for other kinds of warnings? Could this not be of interest to the user?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, I was annoyed about this as well. seems the x11 code always warns about low resolutions which causes the tests to fail. I hope you'll get errors on important things, but haven't looked too closely.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, couldn't the test just use suppressWarnings?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was looking for an option that would disable these checks, but couldn't see anything. The closest I could find was just suppressing all warnings for this function call. I suppose just suppressing them in the test would work as well.

width = ncols, height = nrows, units = 'px',
type = 'Xlib', antialias = 'none' ))
} else {
png(filename = raster_file,
width = finalDims$width, height = finalDims$height,
type = 'Xlib', antialias = 'none',
units = 'in', res=res )
}
}

par( mar = c(0,0,0,0) )
par(mar=c(0,0,0,0))
plot.new()

plotArea = par('usr')

rasterImage(rasterData, plotArea[1], plotArea[3],
plotArea[2], plotArea[4], interpolate = interpolate )

plotArea[2], plotArea[4], interpolate = interpolate)
dev.off()

return(
basename(tools::file_path_sans_ext(raster_file))
)

}
Binary file not shown.
12 changes: 12 additions & 0 deletions inst/tests/test_graphics.R
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,18 @@ test_graphs <- list(
})
),

list(
short_name = 'base_raster_noresample',
description = 'Test noresampling raster support in base graphics',
tags = c('base', 'raster'),
graph_options = list(
tikzRasterResolution = NA),
graph_code = quote({
plot.new()
rasterImage(as.raster(matrix(seq(0,1,len=9),3)),0,0,1,1,interpolate=TRUE)
})
),

# New pdfLaTeX tests go here
#list(
# short_name = 'something_suitable_as_a_filename',
Expand Down