def render_as_pixbuf(surface, *rect, **kwargs): """Renders a surface within a given rectangle as a GdkPixbuf :param lib.surface.TileBlittable surface: source surface :param *rect: x, y, w, h positional args defining the render rectangle :param **kwargs: Keyword args are passed to ``surface.blit_tile_into()`` :rtype: GdkPixbuf :raises: lib.errors.AllocationError :raises: MemoryError The keyword args ``alpha``, ``mipmap_level``, and ``feedback_cb`` are consumed here and removed from `**kwargs` before it is passed to the Surface's `blit_tile_into()`. """ alpha = kwargs.pop('alpha', False) mipmap_level = kwargs.pop('mipmap_level', 0) feedback_cb = kwargs.pop('feedback_cb', None) if not rect: rect = surface.get_bbox() x, y, w, h, = rect s = Surface(x, y, w, h) tn = 0 for tx, ty in s.get_tiles(): with s.tile_request(tx, ty, readonly=False) as dst: surface.blit_tile_into(dst, alpha, tx, ty, mipmap_level=mipmap_level, **kwargs) if feedback_cb and tn % lib.surface.TILES_PER_CALLBACK == 0: feedback_cb() tn += 1 return s.pixbuf
def render_as_pixbuf(surface, x=None, y=None, w=None, h=None, alpha=False, mipmap_level=0, progress=None, **kwargs): """Renders a surface within a given rectangle as a GdkPixbuf :param lib.surface.TileBlittable surface: source surface :param int x: :patrm int y: :param int w: :param int h: coords of the rendering rectangle. Must all be set. :param bool alpha: :param int mipmap_level: :param progress: Unsized UI progress feedback obj. :type progress: lib.feedback.Progress or None :param **kwargs: Keyword args are passed to ``surface.blit_tile_into()`` :rtype: GdkPixbuf :raises: lib.errors.AllocationError :raises: MemoryError """ if None in (x, y, w, h): x, y, w, h = surface.get_bbox() s = Surface(x, y, w, h) s_tiles = list(s.get_tiles()) if not progress: progress = lib.feedback.Progress() progress.items = len(s_tiles) tn = 0 for tx, ty in s_tiles: with s.tile_request(tx, ty, readonly=False) as dst: surface.blit_tile_into(dst, alpha, tx, ty, mipmap_level=mipmap_level, **kwargs) if tn % lib.surface.TILES_PER_CALLBACK == 0: progress.completed(tn) tn += 1 progress.close() return s.pixbuf
def __init__(self, surface, filename, rect, alpha, single_tile_pattern=False, save_srgb_chunks=False, **kwargs): super(PNGFileUpdateTask, self).__init__() self._final_filename = filename # Sizes. Save at least one tile to allow empty docs to be written if not rect: rect = surface.get_bbox() x, y, w, h = rect if w == 0 or h == 0: x, y, w, h = (0, 0, 1, 1) rect = (x, y, w, h) # Snapshot and recreate clone_surface = Surface( looped=surface.looped, looped_size=surface.looped_size, ) clone_surface.load_snapshot(surface.save_snapshot()) # Open a tempfile for writing tmp_filename = filename + ".tmp" if os.path.exists(tmp_filename): os.unlink(tmp_filename) tmp_fp = open(tmp_filename, "wb") self._png_writer = mypaintlib.ProgressivePNGWriter( tmp_fp, w, h, alpha, save_srgb_chunks, ) self._tmp_filename = tmp_filename self._tmp_fp = tmp_fp # What to write self._strips_iter = lib.surface.scanline_strips_iter( clone_surface, rect, alpha=alpha, single_tile_pattern=single_tile_pattern, **kwargs) logger.debug("autosave: scheduled update of %r", self._final_filename)
def __init__(self, surface, filename, rect, alpha, single_tile_pattern=False, save_srgb_chunks=False, **kwargs): super(PNGFileUpdateTask, self).__init__() self._final_filename = filename # Sizes. Save at least one tile to allow empty docs to be written if not rect: rect = surface.get_bbox() x, y, w, h = rect if w == 0 or h == 0: x, y, w, h = (0, 0, 1, 1) rect = (x, y, w, h) # Snapshot and recreate clone_surface = Surface( looped=surface.looped, looped_size=surface.looped_size, ) clone_surface.load_snapshot(surface.save_snapshot()) # Open a tempfile for writing tmp_filename = filename + ".tmp" if os.path.exists(tmp_filename): os.unlink(tmp_filename) tmp_fp = open(tmp_filename, "wb") self._png_writer = mypaintlib.ProgressivePNGWriter( tmp_fp, w, h, alpha, save_srgb_chunks, ) self._tmp_filename = tmp_filename self._tmp_fp = tmp_fp # What to write self._strips_iter = lib.surface.scanline_strips_iter( clone_surface, rect, alpha=alpha, single_tile_pattern=single_tile_pattern, **kwargs ) logger.debug("autosave: scheduled update of %r", self._final_filename)