-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathragg_quality.Rmd
More file actions
455 lines (359 loc) · 12.9 KB
/
ragg_quality.Rmd
File metadata and controls
455 lines (359 loc) · 12.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
---
title: "Ragg Quality"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Ragg Quality}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r}
#| include: false
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.width = 7
)
options(knitr.graphics.rel_path = FALSE)
has_xlib <- capabilities()['X11']
```
```{r}
#| label: setup
#| message: false
library(ragg)
library(grid)
library(magick)
img_zoom <- function(path) {
img <- image_read(path)
img <- image_crop(img, '300x150+150+75')
img <- image_sample(img, '600x300')
img
}
```
This vignette tries to compare the quality of the output of ragg, with that of
cairo and, if the system supports it, Xlib (Xlib is unix only). As quality is,
to a certain extend, subjective, the vignette will be short on conclusions and
filled with examples.
```{asis}
#| echo: !expr '!has_xlib'
>The present version of this vignette has been compiled on a system without the
X11 device. The benchmarkings will thus omit this device, though the text will
still refer to it.
```
## Scope
There are mainly two areas of high importance when discussing graphic device
quality: Shape rendering (fill and stroke) and text rendering. Both of these are
highly dependent on the quality of anti-aliasing if any. Apart from that we will
also look into alpha blending and raster interpolation.
## Shape rendering
The examples here are relevant for rendering of all different types of shapes,
be it lines or polygons. Lines obviously don't have fill, so only the stroke
rendering will have relevance here. We chose to use a circle for this as it
provides a nice sampling of uneven edges.
```{r}
circle_quality <- function(device, name, file, ...) {
device(file, width = 600, height = 300, ...)
grid.circle(
x = c(0.25, 0.75),
r = 0.4,
gp = gpar(col = c('black', NA), fill = c(NA, 'black'), lwd = 2)
)
grid.text(y = 0.1, label = name, gp = gpar(cex = 2))
invisible(dev.off())
}
```
```{r}
ragg_circle <- knitr::fig_path('.png')
circle_quality(agg_png, 'ragg', ragg_circle)
knitr::include_graphics(ragg_circle)
```
```{r}
#| echo: false
img_zoom(ragg_circle)
```
```{r}
#| eval: !expr has_xlib
#| include: !expr has_xlib
xlib_circle <- knitr::fig_path('.png')
circle_quality(png, 'Xlib', xlib_circle, type = 'Xlib')
knitr::include_graphics(xlib_circle)
```
```{r}
#| eval: !expr has_xlib
#| include: !expr has_xlib
#| echo: false
img_zoom(xlib_circle)
```
```{r}
cairo_none_circle <- knitr::fig_path('.png')
circle_quality(png, 'cairo no AA', cairo_none_circle,
type = 'cairo', antialias = 'none')
knitr::include_graphics(cairo_none_circle)
```
```{r}
#| echo: false
img_zoom(cairo_none_circle)
```
```{r}
cairo_gray_circle <- knitr::fig_path('.png')
circle_quality(png, 'cairo gray AA', cairo_gray_circle,
type = 'cairo', antialias = 'gray')
knitr::include_graphics(cairo_gray_circle)
```
```{r}
#| echo: false
img_zoom(cairo_gray_circle)
```
```{r}
cairo_subpixel_circle <- knitr::fig_path('.png')
circle_quality(png, 'cairo subpixel AA', cairo_subpixel_circle,
type = 'cairo', antialias = 'subpixel')
knitr::include_graphics(cairo_subpixel_circle)
```
```{r}
#| echo: false
img_zoom(cairo_subpixel_circle)
```
### Observations
ragg is the only device that provides anti-aliasing of fill, which results in
obvious quality differences. The reason for not doing so in cairo is presumably
to avoid artefacts when shapes are touching each other where anti-aliasing can
result in a thin edge between the two shapes instead of a contiguous colour.
This is a real issue, but I personally don't agree that it should be allowed to
degrade the overall quality of the device, hence the reason for not following
this approach in ragg.
Xlib (if that is available on your system), provides completely non anti-aliased
output and so does cairo with `antialias = 'none'`. It is surprising that Xlib
appears to have a better stroke rendering than cairo without anti-aliasing.
Further, the difference between `'gray'` and `'subpixel'` antialiasing is not
visible to the naked eye, nor with a 2x zoom.
## Text
Text is a difficult thing to handle for a graphic device, both in terms of
finding system fonts, and in terms of rendering. Often the rendering is
offloaded to another library (e.g. freetype), which will provide a bitmap
representation to blend into the device. This approach is often good for
horizontal or vertical text, but struggle with other rotations. Here we will
test text rendering at different sizes and at a 27° counter-clockwise
rotation. We will use the the system provided *serif* font as it provides a more
complex task than a *sans-serif* one.
```{r}
text_quality <- function(device, name, file, rotation = 0, ...) {
text <- 'The quick blue R jumped over the lazy snake'
vp <- viewport(angle = rotation)
device(file, width = 600, height = 300, ...)
pushViewport(vp)
grid.text(text, x = 0.1, y = 0.2, just = 'left', gp = gpar(fontfamily = 'serif', cex = 0.5))
grid.text(text, x = 0.1, y = 0.4, just = 'left', gp = gpar(fontfamily = 'serif', cex = 1))
grid.text(text, x = 0.1, y = 0.6, just = 'left', gp = gpar(fontfamily = 'serif', cex = 1.5))
grid.text(text, x = 0.1, y = 0.8, just = 'left', gp = gpar(fontfamily = 'serif', cex = 2))
popViewport()
grid.text(x = 0.9, y = 0.1, label = name, just = 'right', gp = gpar(cex = 2))
invisible(dev.off())
}
```
```{r}
ragg_text <- knitr::fig_path('.png')
text_quality(agg_png, 'ragg', ragg_text)
knitr::include_graphics(ragg_text)
```
```{r}
#| echo: false
img_zoom(ragg_text)
```
```{r}
ragg_text_rot <- knitr::fig_path('.png')
text_quality(agg_png, 'ragg', ragg_text_rot, rotation = 27)
knitr::include_graphics(ragg_text_rot)
```
```{r}
#| echo: false
img_zoom(ragg_text_rot)
```
```{r}
#| eval: !expr has_xlib
#| include: !expr has_xlib
xlib_text <- knitr::fig_path('.png')
text_quality(png, 'Xlib', xlib_text, type = 'Xlib')
knitr::include_graphics(xlib_text)
```
```{r}
#| eval: !expr has_xlib
#| include: !expr has_xlib
#| echo: false
img_zoom(xlib_text)
```
```{r}
#| eval: !expr has_xlib
#| include: !expr has_xlib
xlib_text_rot <- knitr::fig_path('.png')
text_quality(png, 'Xlib', xlib_text_rot, rotation = 27, type = 'Xlib')
knitr::include_graphics(xlib_text_rot)
```
```{r}
#| eval: !expr has_xlib
#| include: !expr has_xlib
#| echo: false
img_zoom(xlib_text_rot)
```
```{r}
cairo_none_text <- knitr::fig_path('.png')
text_quality(png, 'cairo no AA', cairo_none_text,
type = 'cairo', antialias = 'none')
knitr::include_graphics(cairo_none_text)
```
```{r}
#| echo: false
img_zoom(cairo_none_text)
```
```{r}
cairo_none_text_rot <- knitr::fig_path('.png')
text_quality(png, 'cairo no AA', cairo_none_text_rot, rotation = 27,
type = 'cairo', antialias = 'none')
knitr::include_graphics(cairo_none_text_rot)
```
```{r}
#| echo: false
img_zoom(cairo_none_text_rot)
```
```{r}
cairo_gray_text <- knitr::fig_path('.png')
text_quality(png, 'cairo gray AA', cairo_gray_text,
type = 'cairo', antialias = 'gray')
knitr::include_graphics(cairo_gray_text)
```
```{r}
#| echo: false
img_zoom(cairo_gray_text)
```
```{r}
cairo_gray_text_rot <- knitr::fig_path('.png')
text_quality(png, 'cairo gray AA', cairo_gray_text_rot, rotation = 27,
type = 'cairo', antialias = 'gray')
knitr::include_graphics(cairo_gray_text_rot)
```
```{r}
#| echo: false
img_zoom(cairo_gray_text_rot)
```
```{r}
cairo_subpixel_text <- knitr::fig_path('.png')
text_quality(png, 'cairo subpixel AA', cairo_subpixel_text,
type = 'cairo', antialias = 'subpixel')
knitr::include_graphics(cairo_subpixel_text)
```
```{r}
#| echo: false
img_zoom(cairo_subpixel_text)
```
```{r}
cairo_subpixel_text_rot <- knitr::fig_path('.png')
text_quality(png, 'cairo subpixel AA', cairo_subpixel_text_rot, rotation = 27,
type = 'cairo', antialias = 'subpixel')
knitr::include_graphics(cairo_subpixel_text_rot)
```
```{r}
#| echo: false
img_zoom(cairo_subpixel_text_rot)
```
### Observations
Font handling is hard... Setting the font to `'serif'` means different things to
different devices and being more specific results in some devices not being able
to find the font. ragg exclusively uses the system fonts so whatever your OS
defines as the base serif type it will pick up. Cairo goes through the internal
R database to pick a slightly different font. Xlib... well I can't comment on
what it is doing, but it appears to pick something completely different from the
X11 system. Regarding quality, I don't think I have to be mean and mention Xlib,
so let's look at ragg and cairo. Ignoring for a fact that they have used two
different fonts, we can see some differences and some interesting stuff. First,
cairo don't care about the anti-alias setting when it renders fonts. My guess is
that it will always ask for an 8-bit pixelmap from the font engine (probably
freetype) and use that. This ensures high quality fonts no matter the settings.
The text appears quite heavy, which (unless this is a feature of the font)
indicates that cairo does not gamma-correct the pixelmap before blending it into
the image. Correct gamma correction of font is quite important, so if that is
the case it is quite sad. Another thing we notice is that cairo uses the
pixelmaps even for rotated text. This results in jagged baseline and uneven
kerning when plotting rotated text. ragg only uses pixelmaps when plotting
axis-aligned text. For rotated text it will convert the glyphs to polygons and
render them using the built-in rasterizer ensuring an even baseline and kerning.
This means plotting rotated text is slightly less performant, at the cost of
looking good — I can live with that trade-off.
## Alpha blending
How transparent colours are combined is not necessarily equal among devices. The
biggest divide is on whether to use premultiplied colours or not. With
premultiplied colours the red, green, and blue intensity is weighted by the
alpha directly, instead of alpha simply being an additional value. Using
premultiplied colours is the only way to get correct alpha blending. As the Xlib
device doesn't support transparent colours it will be exempt from this
comparison even on systems that have it. Further, as alpha blending is not
related to anti-aliasing, we will simply compare ragg against a single cairo
setup.
```{r}
blend_quality <- function(device, name, file, ...) {
device(file, width = 600, height = 300, ...)
grid.rect(x = 0.35, y = 0.4, width = 0.5, height = 0.5, gp = gpar(fill = '#FF0000'))
grid.rect(x = 0.65, y = 0.6, width = 0.5, height = 0.5, gp = gpar(fill = '#00FF001A'))
grid.text(x = 0.9, y = 0.1, label = name, just = 'right', gp = gpar(cex = 2))
invisible(dev.off())
}
```
```{r}
ragg_blend <- knitr::fig_path('.png')
blend_quality(agg_png, 'ragg', ragg_blend)
knitr::include_graphics(ragg_blend)
```
```{r}
cairo_blend <- knitr::fig_path('.png')
blend_quality(png, 'cairo', cairo_blend, type = 'cairo')
knitr::include_graphics(cairo_blend)
```
### Observations
Nothing much to say — both devices handle alpha blending correctly (see [here](https://developer.nvidia.com/content/alpha-blending-pre-or-not-pre) to
understand what this test was about).
## Raster
The main way raster plotting can get influence by the device is in how the image
gets interpolated during scaling, which will be briefly compared here.
```{r}
raster_quality <- function(device, name, file, ...) {
reds <- matrix(hcl(0, 80, seq(50, 80, 10)),
nrow = 4, ncol = 5)
device(file, width = 600, height = 300, ...)
grid.raster(reds, vp = viewport(0.25, 0.25, 0.5, 0.5, angle = 27))
grid.raster(reds, interpolate = FALSE,
vp = viewport(0.75, 0.75, 0.5, 0.5, angle = 27))
grid.text(x = 0.9, y = 0.1, label = name, just = 'right', gp = gpar(cex = 2))
invisible(dev.off())
}
```
```{r}
ragg_raster <- knitr::fig_path('.png')
raster_quality(agg_png, 'ragg', ragg_raster)
knitr::include_graphics(ragg_raster)
```
```{r}
#| eval: !expr has_xlib
#| include: !expr has_xlib
xlib_raster <- knitr::fig_path('.png')
raster_quality(png, 'Xlib', xlib_raster, type = 'Xlib')
knitr::include_graphics(xlib_raster)
```
```{r}
cairo_raster <- knitr::fig_path('.png')
raster_quality(png, 'cairo', cairo_raster, type = 'cairo')
knitr::include_graphics(cairo_raster)
```
### Observations
As with blending there is nothing much to see here. All devices perform equally
and correctly.
## Conclusion
When it comes to raster quality, the only real contenders are
ragg and anti-aliased cairo, as lack of anti-aliasing has clear
detrimental effect on output quality. As there appears to be no real difference
in quality between cairo's two anti-aliasing modes, the question basically boils
down to cairo vs ragg. While for the most part the two rendering systems provide
comparable output, there are two areas where ragg takes the lead, quality-wise:
rendering of fill, and rendering of rotated text. If these areas are of interest
to you then ragg will be the obvious choice.
## Session info
```{r}
sessioninfo::session_info()
```