Skip to content

Add TIFF compression and JPEG quality options to Save Plot as Image - #19115

Merged
kevinushey merged 4 commits into
mainfrom
feature/plot-export-compression
Oct 16, 2026
Merged

kevinushey merged 4 commits into
mainfrom
feature/plot-export-compression

Conversation

@kevinushey

Copy link
Copy Markdown
Collaborator

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.

  • Save Plot as Image now has a Compression menu for TIFFs (LZW by default, or ZIP, each with or without a predictor where R offers them) and a Quality menu for JPEGs (100, the previous behaviour, by default). Each is shown for its format only, and both are remembered in export_plot_options alongside the format and resolution.
  • The compressions are listed by the session from the running R's tiff() formals (.rs.graphics.tiffCompressionTypes()), restricted to the lossless ones ragg's agg_tiff() also maps. rle is left out because it fails for colour images, jpeg because it is lossy at a fixed quality, and the recent lerc / lzma / zstd / webp because libtiff and viewers may not support them and ragg doesn't know them.
  • The values go to grDevices::tiff(compression = ) / jpeg(quality = ) and to ragg::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 optional compression and quality arguments.

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) through kCGImagePropertyTIFFDictionary, and Quartz's jpeg() always writes at ImageIO's default quality, whatever quality says.

So the ImageIO rewrite that already records the resolution (now RGraphicsQuartzImage.cpp, replacing RGraphicsImageResolution.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; windows is the default device there, cairo is Linux's):

Output 300 DPI 600 DPI
TIFF, none 9.01 MB 36.05 MB
TIFF, LZW (windows) 0.10 MB 0.28 MB
TIFF, LZW (cairo) 0.29 MB 0.73 MB
TIFF, ZIP (cairo) 0.29 MB 0.70 MB
JPEG, quality 100 0.51 MB 1.35 MB
JPEG, quality 95 0.31 MB 0.82 MB

Testing

  • R (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.
  • e2e (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.
  • The existing plots_pane.test.ts and fixed_plot_size.test.ts specs pass on Windows (desktop-dev).
  • Not covered: the macOS code path (ImageIO's behaviour was measured on GitHub runners with a stand-alone program, but this build was not run on a Mac); the Viewer pane's export, which is an Electron screenshot and cannot produce TIFF.

Addresses #19114.

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
@kevinushey kevinushey added this to the 2026.11 (Montauk Daisy) milestone Oct 15, 2026
@kevinushey
kevinushey requested a review from gtritchie October 15, 2026 05:04
@github-actions

github-actions Bot commented Oct 15, 2026 •

Copy link
Copy Markdown

RStudio E2E - all platforms

All tests passed!

#################### 100% pass rate

Passed Failed Skipped Flaky
2733 0 135 1
Platform ✅ Passed ❌ Failed ⚠️ Skipped 🔁 Flaky
✅ auth-teardown 2 0 4 0
✅ desktop-linux 919 0 42 0
✅ desktop-macos 924 0 37 1
✅ server-linux 872 0 44 0
✅ setup 16 0 8 0
View / download the full report

View report online -- opens in the browser, no download needed. Available for 14 days.

Download playwright-report.zip -- extract and open index.html

Includes screenshots, traces, and step-by-step details for every test. Expires in 15 days.


All platforms - commit 91be5be4865e1190c90dfa1003c54a70bf6e3621 - Run #1494

@gtritchie

Copy link
Copy Markdown
Member

Code review findings

Actionable 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

  1. src/cpp/r/session/graphics/RGraphicsPlotManager.cpp:341 — the TIFF compression is validated before the format is known, so a bad value fails non-TIFF saves with a TIFF error: .rs.api.savePlotAsImage(f, "png", 480, 288, compression = "LZW") → "Invalid TIFF compression: LZW". Only the API can hit this; the dialog always sends a value from its list. Fix: validate only when bitmapFileType == kTiffFormat.

  2. src/cpp/session/modules/SessionPlots.cpp:1048 — quality is documented as 1 to 100 but is clamped, not rejected. A numeric NA passes the is.numeric check in Api.R:230; Rf_asInteger turns it into NA_INTEGER (INT_MIN), which is clamped to 1, so the JPEG is written at quality 1 with no warning. 0, 150 and 95.7 quietly become 1, 100 and 95. Fix: in Api.R, reject NA, non-integer and out-of-range values.

  3. src/cpp/r/session/graphics/RGraphicsPlotManager.cpp:429 — with !recordResolution gone from the early return, every Quartz PNG save now calls finishQuartzImage (line 444). That opens the file through ImageIO and reads its properties, only to find nothing to do (dpi 0, no compression). This affects the Zoom window (SessionPlots.cpp:684-685, re-requested on resize), the export preview (:713) and the RPubs saves (:366, :375). The cost per call is small. Fix: skip the call when dpi is 0 and the compression is empty.

Suggestions

  1. src/cpp/r/session/graphics/RGraphicsPlotManager.cpp:340 — the comment says an empty compression comes from "an older stored option", but the generated UserStateAccessor.getTiffCompression() already maps a missing key to the schema's "lzw", and the dialog sends its list value anyway. Only an explicit API compression = "" reaches this branch. The default also lives in RGraphics.hpp:149 and SavePlotAsImageDialog.java:410. Fix: let BitmapOptions own the default and drop the fallback, or correct the comment.

  2. src/cpp/r/session/graphics/RGraphicsUtils.cpp:251 — the set of compressions is hard-coded in four places: SessionGraphics.R:44, this Quartz filter, tiffCompressionTag() (RGraphicsQuartzImage.cpp:114) and tiffCompressionText() (SavePlotAsImageDialog.java:231). A type added to the filter but not to tiffCompressionTag() maps to tag 0, and the TIFF then silently stays uncompressed. Fix: derive the Quartz filter from tiffCompressionTag() != 0.

  3. src/cpp/tests/testthat/test-graphics.R:216 — the new Api.R argument errors (Api.R:228, :230) and the quality clamping have no tests. Add expect_error cases for e.g. compression = 1, compression = c("lzw", "zip") and quality = "high".

  4. src/cpp/r/session/graphics/RGraphicsPlotManager.cpp:403 — the intermediate PNG for a Quartz JPEG is a dotfile next to the target. It overwrites any existing .<name>.png there, and it is left behind if dev.off() fails (the early return at line 429). That window is narrow, because copyToActiveDevice() discards replay errors (RGraphicsDevice.cpp:811). Fix: use module_context::tempFile or a temp directory instead.

  5. e2e/rstudio/tests/panes/plots/plots_pane.test.ts:209 — export_plot_options.format is reset only on the test's last line. On Desktop that's harmless: each launch gets a fresh config (desktop.fixture.ts:276), and describe.serial skips the rest of the group after a failure. In Server mode the data home is shared across specs (server.fixture.ts:203), so a failure would leave TIFF remembered for later specs. Fix: reset in try/finally.

  6. e2e/rstudio/tests/panes/plots/plots_pane.test.ts:192 — the inline TIFF tag parser duplicates tiffCompression() in test-graphics.R:164, and it reads only the first 1e7 bytes. Consider sharing one helper, or checking only that the file is smaller than an uncompressed save.

  7. src/cpp/r/session/graphics/RGraphicsPlotManager.cpp:403 — a design question, not a defect. The Quartz path now adds a JPEG detour (draw a PNG, re-encode through ImageIO), a TIFF rewrite and a Quartz-only compression list. R documents tiff()'s compression as ignored only for type = "quartz", so passing type = "cairo" for TIFF/JPEG when capabilities("cairo") is TRUE would honour compression and quality natively and record res. The cost is not drawing with the user's chosen backend. Worth weighing against keeping the ImageIO code untested on macOS.

@gtritchie gtritchie left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Feedback posted

- 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
@kevinushey

kevinushey commented Oct 15, 2026 •

Copy link
Copy Markdown
Collaborator Author

Thanks for the review. Addressed in the follow-up commit:

  1. Compression validated for TIFFs only -- a PNG or JPEG save ignores compression, so savePlotAsImage(f, "png", ..., compression = "LZW") now succeeds (covered by a new test).
  2. quality rejected rather than clamped -- .rs.api.savePlotAsImage() now errors for NA, non-integer and out-of-range values ("quality argument must be a whole number from 1 to 100"); the C++ clamp stays as a backstop for the RPC. compression = NA is rejected too.
  3. No ImageIO round trip for RStudio's own images -- finishQuartzImage() is skipped when neither a resolution nor a compression is wanted, so the pane, zoom window and RPubs saves leave the file as the device wrote it.
  4. Default owned by BitmapOptions -- the empty-string fallback (and its misleading comment) is gone; an empty compression for a TIFF is now an error like any other invalid value. The dialog's constant is only its initial selection.
  5. Quartz list derived from tiffCompressionTag() -- the function is now shared through RGraphicsUtils.hpp, so a type the tag map can't apply is never offered on Quartz.
  6. Argument tests -- test-graphics.R covers compression = 1, c("lzw", "zip"), NA, and quality = "high", NA, 0, 150, 95.7, c(90, 95).
  7. Temp file for the Quartz JPEG's PNG -- FilePath::tempFilePath(".png") instead of a dotfile beside the target, and it is removed when the draw fails as well.
  8. e2e reset in finally -- the remembered format is restored even when the test fails.
  9. e2e parser -- it now reads the whole file; the helper in test-graphics.R isn't reachable from the e2e session, so the parser stays inline with a note pointing at it.

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 antialias handling all differ between cairo and Quartz). The ImageIO path is also exercised now: the new e2e test saves a TIFF through the dialog on the macOS CI runner and checks its compression tag, and ImageIO's compression and quality handling was measured directly on macOS 14, 15 and 26 before this was written.

…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.
@kevinushey
kevinushey merged commit 5dee0dd into main Oct 16, 2026
1 of 7 checks passed
@kevinushey
kevinushey deleted the feature/plot-export-compression branch October 16, 2026 16:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants