/* A picture shrunk once to the size it's shown at, for the switcher's and
* the share picker's captures: scaling a whole screen on every draw is
* slow. Never enlarged. */
#ifndef C8_SHELL_SHRINK_H
#define C8_SHELL_SHRINK_H
#include <math.h>
#include <cairo.h>
/* image is w x h; the result fits max_w x max_h */
static inline cairo_surface_t *image_shrunk(cairo_surface_t *image, int w,
int h, double max_w, double max_h) {
double s = fmin(1, fmin(max_w / w, max_h / h));
int sw = (int)fmax(1, round(w * s)), sh = (int)fmax(1, round(h * s));
cairo_surface_t *out = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
sw, sh);
cairo_t *cr = cairo_create(out);
cairo_scale(cr, (double)sw / w, (double)sh / h);
cairo_set_source_surface(cr, image, 0, 0);
cairo_pattern_set_filter(cairo_get_source(cr), CAIRO_FILTER_GOOD);
cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
cairo_paint(cr);
cairo_destroy(cr);
return out;
}
#endif