Screenshot editor: annotate, blur, pixelate, redact and crop before saving
CI / test (push) Canceled after 12m58s
CI / test (push) Canceled after 12m58s
Every screenshot now opens in an editor after KDE Spectacle's annotation mode: Arrow, Line, Rectangle, Ellipse, Pen, Highlighter, Text, Number markers, Blur, Pixelate, Redact and Crop, 8 colours, 3 sizes, undo/redo. Enter saves and copies, Ctrl+C copies only, Esc discards. - Drawn by the compositor (shot_editor.mbt), keyboard owned by it while up. - Annotations are drawn into the picture's pixels at full size (annotate.mbt), so what is shown is what is saved. Blur = 6x6 block average + 3 box-blur passes; redact is opaque; both touch only their box (pixel tests). - The picture goes to the renderer as <=2 MB strips: one 8 MB image-pixels call exhausts wasmtime's hostcall fuel and killed the wasm host. - Frame callbacks are held while the overlay/editor freeze the screen, so apps behind stop drawing (0% CPU vs 52% with a full-screen animating app). - wasmhost: error buffer 16 KB so trap causes aren't cut off; image-pixels limit 1024 -> 16384 a side (both hosts). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01DPL5hh3TaFJh7nhCxAjV4Q
This commit is contained in:
14 files changed
+2127
-67
No files matched your search
@@ -0,0 +1,501 @@
|
||||
///| Screenshot annotations: what each tool of the screenshot editor (shot_editor.mbt) leaves on
|
||||
/// the picture, and the pixel work that puts it there.
|
||||
///
|
||||
/// Every annotation is drawn into the picture's own pixels at full size the moment it is made
|
||||
/// (`apply_annotation`), and the editor shows that picture, so what is on screen is what gets
|
||||
/// saved. Blur, Pixelate and Redact replace the pixels under them: Redact paints an opaque box,
|
||||
/// Pixelate averages blocks, and Blur first averages small blocks and then box-blurs them three
|
||||
/// times, so nothing of the detail underneath is left in the file. Crop doesn't touch pixels:
|
||||
/// the last crop picks the part of the picture that is saved.
|
||||
|
||||
///| An editor tool.
|
||||
pub(all) enum Tool {
|
||||
Arrow
|
||||
Line
|
||||
Rectangle
|
||||
Ellipse
|
||||
/// Freehand.
|
||||
Pen
|
||||
/// Freehand, wide and translucent.
|
||||
Highlighter
|
||||
Text
|
||||
/// A numbered disc (1, 2, 3, ... in the order they are placed).
|
||||
Number
|
||||
Blur
|
||||
Pixelate
|
||||
/// An opaque box in the chosen colour.
|
||||
Redact
|
||||
Crop
|
||||
} derive(Eq, Debug)
|
||||
|
||||
///| One annotation, in picture pixels. Shapes and regions use the first and last point; Pen
|
||||
/// and Highlighter every point; Text and Number the first (Text: its left end, vertically
|
||||
/// centred; Number: the disc's centre).
|
||||
pub(all) struct Annotation {
|
||||
tool : Tool
|
||||
color : @scene.Rgba
|
||||
/// Stroke width; text size, marker size and pixel blocks follow from it.
|
||||
width : Int
|
||||
points : Array[(Int, Int)]
|
||||
mut text : String
|
||||
number : Int
|
||||
} derive(Debug)
|
||||
|
||||
///| Text size for stroke width `w`.
|
||||
pub fn annotation_text_size(w : Int) -> Int {
|
||||
10 + w * 3
|
||||
}
|
||||
|
||||
///| A number marker's radius for stroke width `w`.
|
||||
pub fn annotation_marker_radius(w : Int) -> Int {
|
||||
10 + w * 2
|
||||
}
|
||||
|
||||
///| Pixelate's block size for stroke width `w`.
|
||||
pub fn annotation_pixel_block(w : Int) -> Int {
|
||||
8 + w * 4
|
||||
}
|
||||
|
||||
///| Half the highlighter's width for stroke width `w`.
|
||||
fn highlighter_half(w : Int) -> Double {
|
||||
(w * 3).max(8).to_double()
|
||||
}
|
||||
|
||||
///| The highlighter's alpha (out of 255).
|
||||
let highlighter_alpha : Int = 110
|
||||
|
||||
///| The rectangle spanned by the first and last point (the last point's pixel included).
|
||||
pub fn Annotation::rect(self : Annotation) -> @scene.Rect {
|
||||
let (x0, y0) = self.points[0]
|
||||
let (x1, y1) = self.points[self.points.length() - 1]
|
||||
@scene.Rect::new(x0.min(x1), y0.min(y1), (x1 - x0).abs(), (y1 - y0).abs())
|
||||
}
|
||||
|
||||
///| `r` clipped to `p`.
|
||||
fn clip_rect(p : @renderer.Pixmap, r : @scene.Rect) -> @scene.Rect {
|
||||
let x0 = r.x.max(0)
|
||||
let y0 = r.y.max(0)
|
||||
let x1 = (r.x + r.w).min(p.width)
|
||||
let y1 = (r.y + r.h).min(p.height)
|
||||
@scene.Rect::new(x0, y0, (x1 - x0).max(0), (y1 - y0).max(0))
|
||||
}
|
||||
|
||||
///| A copy of `p`.
|
||||
pub fn copy_pixmap(p : @renderer.Pixmap) -> @renderer.Pixmap {
|
||||
let data = FixedArray::make(p.data.length(), b'\x00')
|
||||
FixedArray::blit_to(p.data, data, len=data.length())
|
||||
{ width: p.width, height: p.height, data }
|
||||
}
|
||||
|
||||
///| `r` of `p` as a new pixmap (clipped to `p`; at least 1 x 1).
|
||||
pub fn crop_pixmap(p : @renderer.Pixmap, r : @scene.Rect) -> @renderer.Pixmap {
|
||||
let c = clip_rect(p, r)
|
||||
let out = @renderer.Pixmap::new(c.w, c.h)
|
||||
for y = 0; y < c.h; y = y + 1 {
|
||||
FixedArray::blit_to(p.data, out.data, len=c.w * 4, src_offset=((c.y + y) * p.width + c.x) * 4, dst_offset=y * c.w * 4)
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
///| Paint `r` of `p` opaque `color`.
|
||||
pub fn redact_region(p : @renderer.Pixmap, r : @scene.Rect, color : @scene.Rgba) -> Unit {
|
||||
let c = clip_rect(p, r)
|
||||
for y = c.y; y < c.y + c.h; y = y + 1 {
|
||||
for x = c.x; x < c.x + c.w; x = x + 1 {
|
||||
let o = (y * p.width + x) * 4
|
||||
p.data[o] = color.r
|
||||
p.data[o + 1] = color.g
|
||||
p.data[o + 2] = color.b
|
||||
p.data[o + 3] = b'\xff'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///| Replace each `block` x `block` square of `r` (counted from its top-left corner) with its
|
||||
/// average colour.
|
||||
pub fn pixelate_region(p : @renderer.Pixmap, r : @scene.Rect, block : Int) -> Unit {
|
||||
let c = clip_rect(p, r)
|
||||
let b = block.max(1)
|
||||
let mut by = c.y
|
||||
while by < c.y + c.h {
|
||||
let bh = b.min(c.y + c.h - by)
|
||||
let mut bx = c.x
|
||||
while bx < c.x + c.w {
|
||||
let bw = b.min(c.x + c.w - bx)
|
||||
let mut sr = 0
|
||||
let mut sg = 0
|
||||
let mut sb = 0
|
||||
for y = by; y < by + bh; y = y + 1 {
|
||||
for x = bx; x < bx + bw; x = x + 1 {
|
||||
let o = (y * p.width + x) * 4
|
||||
sr = sr + p.data[o].to_int()
|
||||
sg = sg + p.data[o + 1].to_int()
|
||||
sb = sb + p.data[o + 2].to_int()
|
||||
}
|
||||
}
|
||||
let n = bw * bh
|
||||
let ar = ((sr + n / 2) / n).to_byte()
|
||||
let ag = ((sg + n / 2) / n).to_byte()
|
||||
let ab = ((sb + n / 2) / n).to_byte()
|
||||
for y = by; y < by + bh; y = y + 1 {
|
||||
for x = bx; x < bx + bw; x = x + 1 {
|
||||
let o = (y * p.width + x) * 4
|
||||
p.data[o] = ar
|
||||
p.data[o + 1] = ag
|
||||
p.data[o + 2] = ab
|
||||
p.data[o + 3] = b'\xff'
|
||||
}
|
||||
}
|
||||
bx = bx + b
|
||||
}
|
||||
by = by + b
|
||||
}
|
||||
}
|
||||
|
||||
///| One box-blur pass of radius `k` along a line of `n` pixels starting at byte offset `start`,
|
||||
/// `step` bytes apart, reading only those pixels (the ends repeat), through `tmp`.
|
||||
fn box_line(p : @renderer.Pixmap, start : Int, step : Int, n : Int, k : Int, tmp : FixedArray[Int]) -> Unit {
|
||||
for ch = 0; ch < 3; ch = ch + 1 {
|
||||
let at = fn(i : Int) { p.data[start + i.clamp(min=0, max=n - 1) * step + ch].to_int() }
|
||||
let mut sum = 0
|
||||
for i = -k; i <= k; i = i + 1 {
|
||||
sum = sum + at(i)
|
||||
}
|
||||
let d = 2 * k + 1
|
||||
for i = 0; i < n; i = i + 1 {
|
||||
tmp[i] = (sum + d / 2) / d
|
||||
sum = sum + at(i + k + 1) - at(i - k)
|
||||
}
|
||||
for i = 0; i < n; i = i + 1 {
|
||||
p.data[start + i * step + ch] = tmp[i].to_byte()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///| The blur radius for a region: strong, and stronger for big regions.
|
||||
pub fn blur_radius(r : @scene.Rect) -> Int {
|
||||
(r.w.min(r.h) / 6).clamp(min=6, max=24)
|
||||
}
|
||||
|
||||
///| Blur `r` of `p` so its content can't be read back: 6 x 6 block averages first (fine detail is
|
||||
/// gone for good), then three box-blur passes each way. Only pixels of `r` are read or written.
|
||||
pub fn blur_region(p : @renderer.Pixmap, r : @scene.Rect) -> Unit {
|
||||
let c = clip_rect(p, r)
|
||||
if c.w <= 0 || c.h <= 0 {
|
||||
return
|
||||
}
|
||||
pixelate_region(p, c, 6)
|
||||
let k = blur_radius(c)
|
||||
let tmp = FixedArray::make(c.w.max(c.h), 0)
|
||||
for _ in 0..<3 {
|
||||
for y = c.y; y < c.y + c.h; y = y + 1 {
|
||||
box_line(p, (y * p.width + c.x) * 4, 4, c.w, k, tmp)
|
||||
}
|
||||
for x = c.x; x < c.x + c.w; x = x + 1 {
|
||||
box_line(p, (c.y * p.width + x) * 4, p.width * 4, c.h, k, tmp)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///| Antialiased coverage (0-255) over a box of the picture: strokes and shapes add to it (the
|
||||
/// highest coverage wins), then it is blended once, so overlapping parts of a translucent stroke
|
||||
/// aren't darker.
|
||||
priv struct Mask {
|
||||
x0 : Int
|
||||
y0 : Int
|
||||
w : Int
|
||||
h : Int
|
||||
cov : FixedArray[Int]
|
||||
}
|
||||
|
||||
///| A mask over (x0, y0)-(x1, y1) of `p`, clipped to it.
|
||||
fn Mask::new(p : @renderer.Pixmap, x0 : Int, y0 : Int, x1 : Int, y1 : Int) -> Mask {
|
||||
let a = x0.max(0)
|
||||
let b = y0.max(0)
|
||||
let w = (x1.min(p.width) - a).max(0)
|
||||
let h = (y1.min(p.height) - b).max(0)
|
||||
{ x0: a, y0: b, w, h, cov: FixedArray::make((w * h).max(1), 0) }
|
||||
}
|
||||
|
||||
///|
|
||||
fn Mask::put(self : Mask, x : Int, y : Int, c : Double) -> Unit {
|
||||
if c <= 0.0 {
|
||||
return
|
||||
}
|
||||
let v = if c >= 1.0 { 255 } else { (c * 255.0).to_int() }
|
||||
let i = (y - self.y0) * self.w + (x - self.x0)
|
||||
if v > self.cov[i] {
|
||||
self.cov[i] = v
|
||||
}
|
||||
}
|
||||
|
||||
///| The pixels of the mask within `pad` of the box (ax, ay)-(bx, by): (x0, y0, x1, y1), ends excluded.
|
||||
fn Mask::span(self : Mask, ax : Double, ay : Double, bx : Double, by : Double, pad : Double) -> (Int, Int, Int, Int) {
|
||||
let x0 = ((ax.min(bx) - pad).floor().to_int()).max(self.x0)
|
||||
let y0 = ((ay.min(by) - pad).floor().to_int()).max(self.y0)
|
||||
let x1 = ((ax.max(bx) + pad).ceil().to_int() + 1).min(self.x0 + self.w)
|
||||
let y1 = ((ay.max(by) + pad).ceil().to_int() + 1).min(self.y0 + self.h)
|
||||
(x0, y0, x1, y1)
|
||||
}
|
||||
|
||||
///| A stroke from (ax, ay) to (bx, by) with round ends, `half` its half width.
|
||||
fn Mask::segment(self : Mask, ax : Double, ay : Double, bx : Double, by : Double, half : Double) -> Unit {
|
||||
let (x0, y0, x1, y1) = self.span(ax, ay, bx, by, half + 1.0)
|
||||
let dx = bx - ax
|
||||
let dy = by - ay
|
||||
let len2 = dx * dx + dy * dy
|
||||
for y = y0; y < y1; y = y + 1 {
|
||||
let py = y.to_double() + 0.5
|
||||
for x = x0; x < x1; x = x + 1 {
|
||||
let px = x.to_double() + 0.5
|
||||
let t = if len2 <= 0.0 {
|
||||
0.0
|
||||
} else {
|
||||
(((px - ax) * dx + (py - ay) * dy) / len2).clamp(min=0.0, max=1.0)
|
||||
}
|
||||
let qx = ax + t * dx - px
|
||||
let qy = ay + t * dy - py
|
||||
self.put(x, y, half + 0.5 - (qx * qx + qy * qy).sqrt())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///| A filled triangle.
|
||||
fn Mask::triangle(self : Mask, ax : Double, ay : Double, bx : Double, by : Double, cx : Double, cy : Double) -> Unit {
|
||||
let area = (bx - ax) * (cy - ay) - (by - ay) * (cx - ax)
|
||||
if area == 0.0 {
|
||||
return
|
||||
}
|
||||
let s = if area > 0.0 { 1.0 } else { -1.0 }
|
||||
// Signed distance of (px, py) from the edge p -> q, positive inside.
|
||||
let edge = fn(px : Double, py : Double, ex : Double, ey : Double, fx : Double, fy : Double) {
|
||||
let l = ((fx - ex) * (fx - ex) + (fy - ey) * (fy - ey)).sqrt()
|
||||
s * ((fx - ex) * (py - ey) - (fy - ey) * (px - ex)) / l
|
||||
}
|
||||
let (x0, y0, x1, y1) = self.span(ax.min(bx).min(cx), ay.min(by).min(cy), ax.max(bx).max(cx), ay.max(by).max(cy), 1.0)
|
||||
for y = y0; y < y1; y = y + 1 {
|
||||
let py = y.to_double() + 0.5
|
||||
for x = x0; x < x1; x = x + 1 {
|
||||
let px = x.to_double() + 0.5
|
||||
let d = edge(px, py, ax, ay, bx, by).min(edge(px, py, bx, by, cx, cy)).min(edge(px, py, cx, cy, ax, ay))
|
||||
self.put(x, y, d + 0.5)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///| Blend `color` over `p` through the mask.
|
||||
fn Mask::blend(self : Mask, p : @renderer.Pixmap, color : @scene.Rgba) -> Unit {
|
||||
let cr = color.r.to_int()
|
||||
let cg = color.g.to_int()
|
||||
let cb = color.b.to_int()
|
||||
let ca = color.a.to_int()
|
||||
for y = 0; y < self.h; y = y + 1 {
|
||||
for x = 0; x < self.w; x = x + 1 {
|
||||
let m = self.cov[y * self.w + x]
|
||||
if m > 0 {
|
||||
let a = ca * m / 255
|
||||
let o = ((self.y0 + y) * p.width + self.x0 + x) * 4
|
||||
let blend = fn(i : Int, c : Int) {
|
||||
let d = p.data[o + i].to_int()
|
||||
p.data[o + i] = ((d * (255 - a) + c * a + 127) / 255).to_byte()
|
||||
}
|
||||
blend(0, cr)
|
||||
blend(1, cg)
|
||||
blend(2, cb)
|
||||
let da = p.data[o + 3].to_int()
|
||||
p.data[o + 3] = (da + (255 - da) * a / 255).to_byte()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
///| A point's centre.
|
||||
fn centre(pt : (Int, Int)) -> (Double, Double) {
|
||||
(pt.0.to_double() + 0.5, pt.1.to_double() + 0.5)
|
||||
}
|
||||
|
||||
///| A mask covering points `pts` plus `pad` around them.
|
||||
fn mask_around(p : @renderer.Pixmap, pts : Array[(Int, Int)], pad : Int) -> Mask {
|
||||
let mut x0 = pts[0].0
|
||||
let mut y0 = pts[0].1
|
||||
let mut x1 = x0
|
||||
let mut y1 = y0
|
||||
for pt in pts {
|
||||
x0 = x0.min(pt.0)
|
||||
y0 = y0.min(pt.1)
|
||||
x1 = x1.max(pt.0)
|
||||
y1 = y1.max(pt.1)
|
||||
}
|
||||
Mask::new(p, x0 - pad, y0 - pad, x1 + pad + 1, y1 + pad + 1)
|
||||
}
|
||||
|
||||
///| A stroke through `pts` (a dot when there is one).
|
||||
fn Mask::polyline(self : Mask, pts : Array[(Int, Int)], half : Double) -> Unit {
|
||||
if pts.length() == 1 {
|
||||
let (x, y) = centre(pts[0])
|
||||
self.segment(x, y, x, y, half)
|
||||
return
|
||||
}
|
||||
for i = 1; i < pts.length(); i = i + 1 {
|
||||
let (ax, ay) = centre(pts[i - 1])
|
||||
let (bx, by) = centre(pts[i])
|
||||
self.segment(ax, ay, bx, by, half)
|
||||
}
|
||||
}
|
||||
|
||||
///| The points of an ellipse inscribed in `r` (a closed loop).
|
||||
pub fn ellipse_points(r : @scene.Rect) -> Array[(Int, Int)] {
|
||||
let cx = r.x.to_double() + r.w.to_double() / 2.0
|
||||
let cy = r.y.to_double() + r.h.to_double() / 2.0
|
||||
let rx = r.w.to_double() / 2.0
|
||||
let ry = r.h.to_double() / 2.0
|
||||
let n = 96
|
||||
let pts : Array[(Int, Int)] = []
|
||||
for i = 0; i <= n; i = i + 1 {
|
||||
let t = 2.0 * @math.pi * i.to_double() / n.to_double()
|
||||
pts.push(((cx + rx * @math.cos(t)).round().to_int(), (cy + ry * @math.sin(t)).round().to_int()))
|
||||
}
|
||||
pts
|
||||
}
|
||||
|
||||
///| An arrow's head: (tip, the two corners of its base, where the shaft ends), for stroke width
|
||||
/// `w`, as doubles.
|
||||
pub fn arrow_head(
|
||||
x0 : Double,
|
||||
y0 : Double,
|
||||
x1 : Double,
|
||||
y1 : Double,
|
||||
w : Int,
|
||||
) -> ((Double, Double), (Double, Double), (Double, Double), (Double, Double)) {
|
||||
let dx = x1 - x0
|
||||
let dy = y1 - y0
|
||||
let len = (dx * dx + dy * dy).sqrt()
|
||||
let (ux, uy) = if len > 0.0 { (dx / len, dy / len) } else { (1.0, 0.0) }
|
||||
let l = (w * 4 + 8).to_double().min(len * 0.6).max(4.0)
|
||||
let hw = l * 0.5
|
||||
let bx = x1 - ux * l
|
||||
let by = y1 - uy * l
|
||||
((x1, y1), (bx - uy * hw, by + ux * hw), (bx + uy * hw, by - ux * hw), (x1 - ux * l * 0.5, y1 - uy * l * 0.5))
|
||||
}
|
||||
|
||||
///| Black or white, whichever reads better on `c`.
|
||||
pub fn contrast_on(c : @scene.Rgba) -> @scene.Rgba {
|
||||
let lum = c.r.to_int() * 299 + c.g.to_int() * 587 + c.b.to_int() * 114
|
||||
if lum > 160 * 1000 {
|
||||
@scene.Rgba::new(0, 0, 0, 255)
|
||||
} else {
|
||||
@scene.Rgba::new(255, 255, 255, 255)
|
||||
}
|
||||
}
|
||||
|
||||
///| Draw annotation `a` into `p`.
|
||||
pub fn apply_annotation(p : @renderer.Pixmap, a : Annotation) -> Unit {
|
||||
let half = a.width.to_double() / 2.0
|
||||
match a.tool {
|
||||
Line | Arrow => {
|
||||
let (x0, y0) = centre(a.points[0])
|
||||
let (x1, y1) = centre(a.points[a.points.length() - 1])
|
||||
let pad = a.width * 4 + 12
|
||||
let m = mask_around(p, [a.points[0], a.points[a.points.length() - 1]], pad)
|
||||
if a.tool == Line {
|
||||
m.segment(x0, y0, x1, y1, half)
|
||||
} else {
|
||||
let (tip, b1, b2, shaft_end) = arrow_head(x0, y0, x1, y1, a.width)
|
||||
m.segment(x0, y0, shaft_end.0, shaft_end.1, half)
|
||||
m.triangle(tip.0, tip.1, b1.0, b1.1, b2.0, b2.1)
|
||||
}
|
||||
m.blend(p, a.color)
|
||||
}
|
||||
Rectangle => {
|
||||
let r = a.rect()
|
||||
let c = [(r.x, r.y), (r.x + r.w, r.y), (r.x + r.w, r.y + r.h), (r.x, r.y + r.h), (r.x, r.y)]
|
||||
let m = mask_around(p, c, a.width + 2)
|
||||
m.polyline(c, half)
|
||||
m.blend(p, a.color)
|
||||
}
|
||||
Ellipse => {
|
||||
let pts = ellipse_points(a.rect())
|
||||
let m = mask_around(p, pts, a.width + 2)
|
||||
m.polyline(pts, half)
|
||||
m.blend(p, a.color)
|
||||
}
|
||||
Pen => {
|
||||
let m = mask_around(p, a.points, a.width + 2)
|
||||
m.polyline(a.points, half)
|
||||
m.blend(p, a.color)
|
||||
}
|
||||
Highlighter => {
|
||||
let hh = highlighter_half(a.width)
|
||||
let m = mask_around(p, a.points, hh.to_int() + 2)
|
||||
m.polyline(a.points, hh)
|
||||
m.blend(p, a.color.with_alpha(highlighter_alpha))
|
||||
}
|
||||
Text =>
|
||||
if a.text.length() > 0 {
|
||||
let (x, y) = a.points[0]
|
||||
@renderer.draw_ops(
|
||||
p,
|
||||
[@scene.DrawOp::Text(x, y, a.text, annotation_text_size(a.width), a.color, @scene.TextAlign::Left, false)],
|
||||
0,
|
||||
0,
|
||||
)
|
||||
}
|
||||
Number => {
|
||||
let r = annotation_marker_radius(a.width)
|
||||
let m = mask_around(p, [a.points[0]], r + 2)
|
||||
m.polyline([a.points[0]], r.to_double())
|
||||
m.blend(p, a.color.with_alpha(255))
|
||||
let (x, y) = a.points[0]
|
||||
@renderer.draw_ops(
|
||||
p,
|
||||
[
|
||||
@scene.DrawOp::Text(
|
||||
x,
|
||||
y,
|
||||
a.number.to_string(),
|
||||
r * 6 / 5,
|
||||
contrast_on(a.color),
|
||||
@scene.TextAlign::Center,
|
||||
true,
|
||||
),
|
||||
],
|
||||
0,
|
||||
0,
|
||||
)
|
||||
}
|
||||
Blur => blur_region(p, a.rect())
|
||||
Pixelate => pixelate_region(p, a.rect(), annotation_pixel_block(a.width))
|
||||
Redact => redact_region(p, a.rect(), a.color)
|
||||
Crop => ()
|
||||
}
|
||||
}
|
||||
|
||||
///| The part of the picture that is saved: the last crop's rectangle (clipped), else all of it.
|
||||
pub fn final_crop(w : Int, h : Int, anns : Array[Annotation]) -> @scene.Rect {
|
||||
let full = @scene.Rect::new(0, 0, w, h)
|
||||
let mut i = anns.length() - 1
|
||||
while i >= 0 {
|
||||
if anns[i].tool == Crop {
|
||||
let r = anns[i].rect()
|
||||
let x0 = r.x.max(0)
|
||||
let y0 = r.y.max(0)
|
||||
let x1 = (r.x + r.w).min(w)
|
||||
let y1 = (r.y + r.h).min(h)
|
||||
if x1 > x0 && y1 > y0 {
|
||||
return @scene.Rect::new(x0, y0, x1 - x0, y1 - y0)
|
||||
}
|
||||
return full
|
||||
}
|
||||
i = i - 1
|
||||
}
|
||||
full
|
||||
}
|
||||
|
||||
///| `base` with `anns` drawn into it in order, cropped by the last crop: the picture to save.
|
||||
pub fn render_annotations(base : @renderer.Pixmap, anns : Array[Annotation]) -> @renderer.Pixmap {
|
||||
let p = copy_pixmap(base)
|
||||
for a in anns {
|
||||
apply_annotation(p, a)
|
||||
}
|
||||
crop_pixmap(p, final_crop(p.width, p.height, anns))
|
||||
}
|
||||
Reference in new issue
Block a user