--- a/gradia/backend/tool_config.py +++ b/gradia/backend/tool_config.py @@ -344,7 +344,7 @@ icon="checkerboard-big-symbolic", column=3, row=1, - has_scale=False, + has_scale=True, has_primary_color=False, ), ToolConfig( --- a/gradia/overlay/drawing_actions.py +++ b/gradia/overlay/drawing_actions.py @@ -21,6 +21,7 @@ from gi.repository import Gtk, Gdk, Gio, Pango, PangoCairo, GdkPixbuf from enum import Enum import math +import random from gradia.backend.logger import Logger from gradia.utils.colors import has_visible_color import time @@ -640,19 +641,25 @@ class CensorAction(RectAction): + # Secure blur: the area is averaged down to cells (4x the size slider, in + # screen pixels), every cell gets random noise, then it is scaled back up + # smoothly. Averaging throws the detail away; the noise means the cells no + # longer equal the true averages, which is what depixelation tools match on. + NOISE_SIGMA = 10 + def __init__(self, start: tuple[int, int], end: tuple[int, int], background_pixbuf: GdkPixbuf.Pixbuf, options): super().__init__(start, end, False, options) self.original_scale = 1.0 - self.base_block_size = 8 self.background_pixbuf = background_pixbuf + self.noise_seed = random.SystemRandom().getrandbits(64) def set_original_scale(self, scale: float): self.original_scale = scale def _get_scaled_block_size(self, current_scale: float) -> float: scale_ratio = current_scale / self.original_scale - return self.base_block_size * scale_ratio + return max(3, self.options.size) * 4 * scale_ratio def draw(self, cr: cairo.Context, image_to_widget_coords: Callable[[int, int], tuple[float, float]], scale: float): crop = self._get_image_crop() @@ -675,37 +682,59 @@ return scaled_block_size = self._get_scaled_block_size(scale) - self._draw_pixelation(cr, crop, x, y, width, height, scaled_block_size) + self._draw_secure_blur(cr, crop, x, y, width, height, scaled_block_size) + + def _draw_secure_blur(self, cr: cairo.Context, crop: dict, x: float, y: float, width: float, height: float, block_size: float): + blocks_x = max(2, round(width / block_size)) + blocks_y = max(2, round(height / block_size)) + + cells = cairo.ImageSurface(cairo.FORMAT_ARGB32, blocks_x, blocks_y) + cells_cr = cairo.Context(cells) + cells_cr.scale(blocks_x / crop['width'], blocks_y / crop['height']) + cells_cr.translate(-crop['x'], -crop['y']) + Gdk.cairo_set_source_pixbuf(cells_cr, self.background_pixbuf, 0, 0) + cells_cr.get_source().set_filter(cairo.FILTER_GOOD) # box-average when shrinking + cells_cr.paint() + cells.flush() + self._add_noise(cells) + cells.mark_dirty() + + # Two smooth upscales (cells -> 4x -> area) read as a blur, not a mosaic. + mid_w, mid_h = blocks_x * 4, blocks_y * 4 + mid = cairo.ImageSurface(cairo.FORMAT_ARGB32, mid_w, mid_h) + mid_cr = cairo.Context(mid) + mid_cr.scale(4, 4) + self._set_smooth_source(mid_cr, cells) + mid_cr.paint() - def _draw_pixelation(self, cr: cairo.Context, crop: dict, x: float, y: float, width: float, height: float, block_size: float): cr.save() cr.rectangle(x, y, width, height) cr.clip() - - blocks_x = max(1, int(width / block_size)) - blocks_y = max(1, int(height / block_size)) - - tiny_surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, blocks_x, blocks_y) - tiny_cr = cairo.Context(tiny_surface) - - tiny_cr.set_operator(cairo.OPERATOR_CLEAR) - tiny_cr.paint() - tiny_cr.set_operator(cairo.OPERATOR_OVER) - - tiny_cr.scale(blocks_x / crop['width'], blocks_y / crop['height']) - tiny_cr.translate(-crop['x'], -crop['y']) - Gdk.cairo_set_source_pixbuf(tiny_cr, self.background_pixbuf, 0, 0) - tiny_cr.paint() - cr.translate(x, y) - cr.scale(width / blocks_x, height / blocks_y) - pattern = cairo.SurfacePattern(tiny_surface) - pattern.set_filter(cairo.FILTER_NEAREST) - cr.set_source(pattern) + cr.scale(width / mid_w, height / mid_h) + self._set_smooth_source(cr, mid) cr.paint() - cr.restore() + @staticmethod + def _set_smooth_source(cr: cairo.Context, surface: cairo.ImageSurface): + pattern = cairo.SurfacePattern(surface) + pattern.set_filter(cairo.FILTER_BILINEAR) + pattern.set_extend(cairo.EXTEND_PAD) # no see-through fringe at the edges + cr.set_source(pattern) + + def _add_noise(self, surface: cairo.ImageSurface): + rng = random.Random(self.noise_seed) # fixed per box, so it does not flicker + data = surface.get_data() + stride = surface.get_stride() + for row in range(surface.get_height()): + for col in range(surface.get_width()): + i = row * stride + col * 4 + alpha = data[i + 3] + shift = rng.gauss(0, self.NOISE_SIGMA) # brightness only, so it reads as grain + for c in range(3): # premultiplied B, G, R stay within alpha + data[i + c] = min(alpha, max(0, round(data[i + c] + shift))) + def _get_image_crop(self) -> dict | None: img_w, img_h = self.background_pixbuf.get_width(), self.background_pixbuf.get_height() x1 = int(self.start[0] + img_w / 2)