Repository navigation
Add TIFF compression and JPEG quality options to Save Plot as Image - #19115
Conversation
TIFFs saved from the Plots pane were uncompressed (36 MB for a 7 x 5 in plot at 600 DPI), and JPEGs were always saved at quality 100. The Save Plot as Image dialog now offers a compression for TIFFs (LZW by default, or ZIP, each with or without a predictor where R offers them) and a quality for JPEGs. Both are remembered in the export options and passed to grDevices' tiff() / jpeg() and to ragg's agg_tiff() / agg_jpeg(). The compressions are listed from the running R's tiff() formals, restricted to the lossless ones ragg also maps. The Quartz devices ignore both arguments: a TIFF is compressed afterwards through the ImageIO rewrite that already records its resolution, and a JPEG is drawn as a PNG and encoded through ImageIO at the quality, since Quartz's own JPEG output (always at ImageIO's default quality) can't be re-encoded without losing more. .rs.api.savePlotAsImage() gains optional compression and quality arguments. Addresses #19114
RStudio E2E - all platformsAll tests passed!
View / download the full reportView report online -- opens in the browser, no download needed. Available for 14 days. Download playwright-report.zip -- extract and open
All platforms - commit |
Code review findingsActionable items from an automated review, each re-checked against 5345a6f. None of them block merging; the macOS (Quartz) path was reviewed by reading the code only. Important
Suggestions
|
- validate the TIFF compression only for TIFF saves, and let BitmapOptions own the default rather than mapping an empty value - reject NA, non-integer and out-of-range quality (and NA compression) in .rs.api.savePlotAsImage(), with tests - skip the ImageIO round trip for RStudio's own PNGs (the pane, the zoom window, RPubs), which want neither a resolution nor a compression - derive the Quartz compression list from tiffCompressionTag(), now shared through RGraphicsUtils.hpp - draw the Quartz JPEG's intermediate PNG in a temp file, removed even when the draw fails - e2e: restore the remembered format in a finally block, and read the whole TIFF
|
Thanks for the review. Addressed in the follow-up commit:
10 (drawing TIFF/JPEG with cairo on macOS instead): not taken, deliberately. Switching the device would make a saved TIFF or JPEG look different from the PNG saved next to it, and from the Plots pane, which draw with the backend the user chose (fonts, antialiasing, and |
…s from quartz The Quartz list offered LZW/ZIP with predictor, which ImageIO wrote as plain LZW/ZIP; a compression Quartz can't apply is now an error instead of being silently skipped. usesQuartzBitmapDevice() now applies the same unsupported-backend fallback as extraBitmapParams(), so a migrated backend setting no longer bypasses the Quartz encoding path.
Summary
Plots saved as TIFF from the Plots pane were always uncompressed, and JPEGs were always saved at quality 100. With the fixed plot size and export resolution options from #18895, a 7 x 5 in plot at 600 DPI was a 36 MB TIFF.
export_plot_optionsalongside the format and resolution.tiff()formals (.rs.graphics.tiffCompressionTypes()), restricted to the lossless ones ragg'sagg_tiff()also maps.rleis left out because it fails for colour images,jpegbecause it is lossy at a fixed quality, and the recentlerc/lzma/zstd/webpbecause libtiff and viewers may not support them and ragg doesn't know them.grDevices::tiff(compression = )/jpeg(quality = )and toragg::agg_tiff()/agg_jpeg(), so no new native dependency is needed. The compression name is validated before it is interpolated into R code..rs.api.savePlotAsImage()gains optionalcompressionandqualityarguments.Quartz (macOS)
R's Quartz devices ignore both arguments (
tiff()warns,jpeg()silently drops the quality) and write through ImageIO with no properties. Measured on macOS 14, 15 and 26: ImageIO writes TIFFs uncompressed by default, honours compression 5 (LZW) and 8 (ZIP) throughkCGImagePropertyTIFFDictionary, and Quartz'sjpeg()always writes at ImageIO's default quality, whateverqualitysays.So the ImageIO rewrite that already records the resolution (now
RGraphicsQuartzImage.cpp, replacingRGraphicsImageResolution.cpp) also compresses TIFFs, and a JPEG is drawn as a PNG and encoded through ImageIO at the chosen quality (re-encoding Quartz's own JPEG would lose more). On macOS, "100" therefore now genuinely means 100, which is larger than the default-quality file Quartz used to write. ImageIO has no predictor option, so the Quartz list offers None, LZW and ZIP.Measurements
The same 7 x 5 in plot (R-devel on Windows;
windowsis the default device there,cairois Linux's):windows)cairo)cairo)Testing
test-graphics.R): every compression the session offers writes the matching TIFF compression tag (259); the default is LZW and under half the size of an uncompressed save; an invalid compression name is an error; a JPEG at quality 50 is smaller than at 100 and still records its resolution.plots_pane.test.ts): the Compression and Quality menus appear for their formats only, with LZW and 100 selected; a TIFF saved through the dialog carries compression tag 5.plots_pane.test.tsandfixed_plot_size.test.tsspecs pass on Windows (desktop-dev).Addresses #19114.