static bool paint_region(openslide_t *osr, cairo_t *cr,
                         int64_t x, int64_t y,
                         struct _openslide_level *level,
                         int32_t w, int32_t h,
                         GError **err) {
  struct generic_tiff_ops_data *data = osr->data;
  struct level *l = (struct level *) level;
  bool success = false;

  TIFF *tiff = _openslide_tiffcache_get(data->tc, err);
  if (tiff == NULL) {
    return false;
  }

  if (TIFFSetDirectory(tiff, l->tiffl.dir)) {
    success = _openslide_grid_paint_region(l->grid, cr, tiff,
                                           x / l->base.downsample,
                                           y / l->base.downsample,
                                           level, w, h,
                                           err);
  } else {
    g_set_error(err, OPENSLIDE_ERROR, OPENSLIDE_ERROR_FAILED,
                "Cannot set TIFF directory");
  }
  _openslide_tiffcache_put(data->tc, tiff);

  return success;
}
static bool render_missing_tile(struct level *l,
                                TIFF *tiff,
                                uint32_t *dest,
                                int64_t tile_col, int64_t tile_row,
                                GError **err) {
  bool success = true;

  int64_t tw = l->tiffl.tile_w;
  int64_t th = l->tiffl.tile_h;

  // always fill with transparent (needed for SATURATE)
  memset(dest, 0, tw * th * 4);

  if (l->prev) {
    // recurse into previous level
    double relative_ds = l->prev->base.downsample / l->base.downsample;

    cairo_surface_t *surface =
      cairo_image_surface_create_for_data((unsigned char *) dest,
                                          CAIRO_FORMAT_ARGB32,
                                          tw, th, tw * 4);
    cairo_t *cr = cairo_create(surface);
    cairo_surface_destroy(surface);
    cairo_set_operator(cr, CAIRO_OPERATOR_SATURATE);
    cairo_translate(cr, -1, -1);
    cairo_scale(cr, relative_ds, relative_ds);

    // For the usual case that we are on a tile boundary in the previous
    // level, extend the region by one pixel in each direction to ensure we
    // paint the surrounding tiles.  This reduces the visible seam that
    // would otherwise occur with non-integer downsamples.
    success = _openslide_grid_paint_region(l->prev->grid, cr, tiff,
                                           (tile_col * tw - 1) / relative_ds,
                                           (tile_row * th - 1) / relative_ds,
                                           (struct _openslide_level *) l->prev,
                                           ceil((tw + 2) / relative_ds),
                                           ceil((th + 2) / relative_ds),
                                           err);
    if (success) {
      success = _openslide_check_cairo_status(cr, err);
    }
    cairo_destroy(cr);
  }

  return success;
}
static bool paint_region(openslide_t *osr, cairo_t *cr,
			 int64_t x, int64_t y,
			 struct _openslide_level *level,
			 int32_t w, int32_t h,
			 GError **err) {
  struct aperio_ops_data *data = osr->data;
  struct level *l = (struct level *) level;

  TIFF *tiff = _openslide_tiffcache_get(data->tc, err);
  if (tiff == NULL) {
    return false;
  }

  bool success = _openslide_grid_paint_region(l->grid, cr, tiff,
                                              x / l->base.downsample,
                                              y / l->base.downsample,
                                              level, w, h,
                                              err);
  _openslide_tiffcache_put(data->tc, tiff);

  return success;
}