-
Notifications
You must be signed in to change notification settings - Fork 26
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
Changes from 6 commits
4b17256
4f00589
7bea627
958e96f
b1c923c
6be8ccc
637dfe3
3a808ec
5d5eab2
4c2ba0d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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') | ||
if (is.null(res)) res = NA; | ||
if (is.na(res)) interpolate = FALSE; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No, now I see that There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, maybe a comment explaining what I'm doing would help? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Couldn't this be handled by a single call to There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
# we can create very small PNGs with this code, so suppress | ||
# warnings about this | ||
suppressWarnings(png(filename = raster_file, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Well, couldn't the test just use There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)) | ||
) | ||
|
||
} |
There was a problem hiding this comment.
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 belowThere was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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.