返回 CodeWhale
pixels.rs
1 //! The GPUI cove/contour paint order in a bounded RGBA plane. The existing
2 //! Sprite adapter handles terminal half-blocks, alpha and color capabilities.
3
4 use super::{CoveScene, Layer, Path, Role, ink::Fill, rig::holes_for, scene::segments};
5
6 pub(crate) const MAX_SIDE: usize = 160;
7
8 struct Canvas {
9 side: usize,
10 // Premultiplied RGBA: averaging transparent edges must not darken them.
11 pixels: Vec<[f64; 4]>,
12 }
13
14 impl Canvas {
15 fn fill(&mut self, paths: &[Path], fill: Fill, alpha: f64) {
16 if !alpha.is_finite() || alpha <= 0. {
17 return;
18 }
19 let alpha = alpha.min(1.);
20 let side = self.side as f64;
21 let scale = side / 124.;
22 let edges: Vec<_> = paths
23 .iter()
24 .filter(|path| {
25 path.iter()
26 .flat_map(super::rig::Cmd::coords)
27 .all(|v| v.is_finite())
28 })
29 .flat_map(segments)
30 .map(|[a, b, c, d]| {
31 [
32 side / 2. + a * scale,
33 side / 2. + b * scale,
34 side / 2. + c * scale,
35 side / 2. + d * scale,
36 ]
37 })
38 .collect();
39 let mut crossings = Vec::new();
40 for y in 0..self.side {
41 let cy = y as f64 + 0.5;
42 crossings.clear();
43 for &[x1, y1, x2, y2] in &edges {
44 if (y1 > cy) != (y2 > cy) {
45 crossings.push(x1 + (cy - y1) * (x2 - x1) / (y2 - y1));
46 }
47 }
48 crossings.sort_by(f64::total_cmp);
49 for pair in crossings.as_chunks::<2>().0 {
50 let left = (pair[0] - 0.5).ceil().clamp(0., side) as usize;
51 let right = (pair[1] - 0.5).ceil().clamp(0., side) as usize;
52 for x in left..right {
53 let t = (((x as f64 + cy + 0.5 - side) / scale + 100.) / 200.).clamp(0., 1.);
54 let channel = |hex: u32, shift: u32| f64::from((hex >> shift) & 255_u32);
55 let color = match fill {
56 Fill::Solid(hex) => [16, 8, 0].map(|s| channel(hex, s)),
57 Fill::Gradient([a, b]) => {
58 [16, 8, 0].map(|s| channel(a, s) + (channel(b, s) - channel(a, s)) * t)
59 }
60 };
61 let pixel = &mut self.pixels[y * self.side + x];
62 for (dst, src) in pixel[..3].iter_mut().zip(color) {
63 *dst = src * alpha + *dst * (1. - alpha);
64 }
65 pixel[3] = alpha + pixel[3] * (1. - alpha);
66 }
67 }
68 }
69 }
70
71 fn layers(&mut self, layers: &[Layer], opacity: f64, current_ink: Option<u32>) {
72 for layer in layers {
73 let color = if layer.color == 0xf7fcfb {
74 current_ink.unwrap_or(layer.color)
75 } else {
76 layer.color
77 };
78 self.fill(&layer.paths, Fill::Solid(color), layer.alpha * opacity);
79 }
80 }
81 }
82
83 /// Square design space at one sample per pixel, so shape edges stay hard on a
84 /// half-block grid instead of blending into the terminal background.
85 /// Public terminal rectangles never control an unbounded allocation.
86 pub(crate) fn render(scene: &CoveScene, side: usize, dark: bool) -> Vec<u8> {
87 if !(1..=MAX_SIDE).contains(&side) {
88 return Vec::new();
89 }
90 let samples = 1;
91 let mut canvas = Canvas {
92 side: side * samples,
93 pixels: vec![[0.; 4]; side * side * samples * samples],
94 };
95 canvas.layers(&scene.behind, if dark { 0.68 } else { 0.9 }, None);
96 for shape in &scene.parts.shapes {
97 if shape.role == Role::Cutout {
98 continue;
99 }
100 if let Some(fill) = super::ink::fill(shape.role, dark) {
101 let mut paths = vec![shape.path.clone()];
102 paths.extend(holes_for(&shape.id, &scene.parts.shapes, false));
103 canvas.fill(&paths, fill, shape.opacity);
104 }
105 }
106 canvas.layers(&scene.front, 0.82, (!dark).then_some(0x3e7789));
107 let mut out = Vec::with_capacity(side * side * 4);
108 for y in 0..side {
109 for x in 0..side {
110 let mut rgba = [0.; 4];
111 for dy in 0..samples {
112 for dx in 0..samples {
113 let p = canvas.pixels[(y * samples + dy) * canvas.side + x * samples + dx];
114 for (dst, src) in rgba.iter_mut().zip(p) {
115 *dst += src / (samples * samples) as f64;
116 }
117 }
118 }
119 for c in &rgba[..3] {
120 out.push(if rgba[3] > 0. {
121 (c / rgba[3]).round() as u8
122 } else {
123 0
124 });
125 }
126 out.push((rgba[3] * 255.).round() as u8);
127 }
128 }
129 out
130 }
131
131 lines RUST