Skip to content

Commit c0cac2c

Browse files
committed
🥎 Only using rayon for compositing larger images.
1 parent e2d0a43 commit c0cac2c

File tree

1 file changed

+95
-42
lines changed

1 file changed

+95
-42
lines changed

‎src/composite/compositor.rs‎

Lines changed: 95 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -61,51 +61,104 @@ pub fn draw_layer_over_image(image: &mut Image, layer: &Layer) {
6161
0
6262
};
6363

64-
image
65-
.data
66-
.par_chunks_mut(image.bytes_per_row as usize)
67-
.enumerate()
68-
.skip(target_y_offset as usize) // Skip rows outside the target area
69-
.take(required_height as usize) // Only process the necessary rows
70-
.for_each(|(y, row)| {
71-
let y = y as u32;
72-
let y_position = y as i32 - location.y;
73-
let y_position = (y_position as f32 * pixel_ratio_y).floor() as u32;
74-
let offset = (y_position * layer_bytes_per_row) as usize;
75-
76-
for x in 0..required_width {
77-
let alpha = layer.mask_alpha(Point {
78-
x: x as u32,
79-
y: y - target_y_offset,
80-
});
81-
if alpha == 0 {
82-
continue;
83-
}
84-
let mask_opacity = alpha as f32 / u8::MAX as f32;
85-
let x = x * 4;
86-
let x_position = x + x_offset;
87-
let x_position = (x_position as f32 * pixel_ratio_x).floor() as usize;
88-
let start = offset + x_position;
89-
let blend_color = pixel_data(&layer.image.data, start);
90-
let blend_color: Color = blend_color.into();
91-
92-
let start = start_x as usize * 4 + x;
93-
let base_color = pixel_data(&row, start);
94-
let mut base_color: Color = base_color.into();
95-
96-
blend_colors(
97-
&mut base_color,
98-
&blend_color,
99-
layer.blend_mode,
100-
layer.opacity * mask_opacity,
64+
if image.size.height > 100 {
65+
image
66+
.data
67+
.par_chunks_mut(image.bytes_per_row as usize)
68+
.enumerate()
69+
.skip(target_y_offset as usize) // Skip rows outside the target area
70+
.take(required_height as usize) // Only process the necessary rows
71+
.for_each(|(y, row)| {
72+
process_row(
73+
y,
74+
row,
75+
layer,
76+
layer_bytes_per_row,
77+
location,
78+
pixel_ratio_x,
79+
pixel_ratio_y,
80+
required_width,
81+
start_x,
82+
target_y_offset,
83+
x_offset,
10184
);
85+
});
86+
} else {
87+
image
88+
.data
89+
.chunks_mut(image.bytes_per_row as usize)
90+
.enumerate()
91+
.skip(target_y_offset as usize) // Skip rows outside the target area
92+
.take(required_height as usize) // Only process the necessary rows
93+
.for_each(|(y, row)| {
94+
process_row(
95+
y,
96+
row,
97+
layer,
98+
layer_bytes_per_row,
99+
location,
100+
pixel_ratio_x,
101+
pixel_ratio_y,
102+
required_width,
103+
start_x,
104+
target_y_offset,
105+
x_offset,
106+
);
107+
});
108+
}
109+
}
102110

103-
row[start + 0] = base_color.red;
104-
row[start + 1] = base_color.green;
105-
row[start + 2] = base_color.blue;
106-
row[start + 3] = base_color.alpha;
107-
}
111+
/// Processes the rows of data.
112+
fn process_row(
113+
y: usize,
114+
row: &mut [u8],
115+
layer: &Layer<'_>,
116+
layer_bytes_per_row: u32,
117+
location: Point<i32>,
118+
pixel_ratio_x: f32,
119+
pixel_ratio_y: f32,
120+
required_width: usize,
121+
start_x: u32,
122+
target_y_offset: u32,
123+
x_offset: usize,
124+
) {
125+
let y = y as u32;
126+
let y_position = y as i32 - location.y;
127+
let y_position = (y_position as f32 * pixel_ratio_y).floor() as u32;
128+
let offset = (y_position * layer_bytes_per_row) as usize;
129+
130+
for x in 0..required_width {
131+
let alpha = layer.mask_alpha(Point {
132+
x: x as u32,
133+
y: y - target_y_offset,
108134
});
135+
if alpha == 0 {
136+
continue;
137+
}
138+
let mask_opacity = alpha as f32 / u8::MAX as f32;
139+
let x = x * 4;
140+
let x_position = x + x_offset;
141+
let x_position = (x_position as f32 * pixel_ratio_x).floor() as usize;
142+
let start = offset + x_position;
143+
let blend_color = pixel_data(&layer.image.data, start);
144+
let blend_color: Color = blend_color.into();
145+
146+
let start = start_x as usize * 4 + x;
147+
let base_color = pixel_data(&row, start);
148+
let mut base_color: Color = base_color.into();
149+
150+
blend_colors(
151+
&mut base_color,
152+
&blend_color,
153+
layer.blend_mode,
154+
layer.opacity * mask_opacity,
155+
);
156+
157+
row[start + 0] = base_color.red;
158+
row[start + 1] = base_color.green;
159+
row[start + 2] = base_color.blue;
160+
row[start + 3] = base_color.alpha;
161+
}
109162
}
110163

111164
impl Image {

0 commit comments

Comments
 (0)