add BlendFunction blend method variant for unpacked colors

also renamed the existing method to be more explicit
This commit is contained in:
Gered 2023-04-19 15:44:37 -04:00
parent 02349d09a2
commit f6283a2634
4 changed files with 36 additions and 19 deletions

View file

@ -146,7 +146,7 @@ impl RgbaBitmap {
dest_x,
dest_y,
|src_pixels, dest_pixels| {
*dest_pixels = blend.blend(*src_pixels, *dest_pixels);
*dest_pixels = blend.blend_1u32(*src_pixels, *dest_pixels);
},
);
}
@ -170,7 +170,7 @@ impl RgbaBitmap {
horizontal_flip,
vertical_flip,
|src_pixels, dest_pixels| {
*dest_pixels = blend.blend(*src_pixels, *dest_pixels);
*dest_pixels = blend.blend_1u32(*src_pixels, *dest_pixels);
},
);
}
@ -239,7 +239,7 @@ impl RgbaBitmap {
dest_y,
|src_pixels, dest_pixels| {
if *src_pixels != transparent_color {
*dest_pixels = blend.blend(*src_pixels, *dest_pixels);
*dest_pixels = blend.blend_1u32(*src_pixels, *dest_pixels);
}
},
);
@ -293,7 +293,7 @@ impl RgbaBitmap {
vertical_flip,
|src_pixels, dest_pixels| {
if *src_pixels != transparent_color {
*dest_pixels = blend.blend(*src_pixels, *dest_pixels);
*dest_pixels = blend.blend_1u32(*src_pixels, *dest_pixels);
}
},
);
@ -347,7 +347,7 @@ impl RgbaBitmap {
scale_y,
|src_pixel, dest_bitmap, draw_x, draw_y| {
if let Some(dest_pixel) = dest_bitmap.get_pixel(draw_x, draw_y) {
dest_bitmap.set_pixel(draw_x, draw_y, blend.blend(src_pixel, dest_pixel))
dest_bitmap.set_pixel(draw_x, draw_y, blend.blend_1u32(src_pixel, dest_pixel))
}
},
);
@ -406,7 +406,7 @@ impl RgbaBitmap {
|src_pixel, dest_bitmap, draw_x, draw_y| {
if transparent_color != src_pixel {
if let Some(dest_pixel) = dest_bitmap.get_pixel(draw_x, draw_y) {
dest_bitmap.set_pixel(draw_x, draw_y, blend.blend(src_pixel, dest_pixel))
dest_bitmap.set_pixel(draw_x, draw_y, blend.blend_1u32(src_pixel, dest_pixel))
}
}
},

View file

@ -8,7 +8,7 @@ impl RgbaBitmap {
self.set_custom_pixel(
x, //
y,
|dest_color| blend.blend(color, dest_color),
|dest_color| blend.blend_1u32(color, dest_color),
);
}
@ -20,7 +20,7 @@ impl RgbaBitmap {
self.set_custom_pixel_unchecked(
x, //
y,
|dest_color| blend.blend(color, dest_color),
|dest_color| blend.blend_1u32(color, dest_color),
);
}
@ -31,7 +31,7 @@ impl RgbaBitmap {
y1,
x2,
y2,
|dest_color| blend.blend(color, dest_color),
|dest_color| blend.blend_1u32(color, dest_color),
);
}
@ -42,7 +42,7 @@ impl RgbaBitmap {
x1, //
x2,
y,
|dest_color| blend.blend(color, dest_color),
|dest_color| blend.blend_1u32(color, dest_color),
);
}
@ -53,7 +53,7 @@ impl RgbaBitmap {
x, //
y1,
y2,
|dest_color| blend.blend(color, dest_color),
|dest_color| blend.blend_1u32(color, dest_color),
);
}
@ -66,7 +66,7 @@ impl RgbaBitmap {
y1,
x2,
y2,
|dest_color| blend.blend(color, dest_color),
|dest_color| blend.blend_1u32(color, dest_color),
);
}
@ -79,7 +79,7 @@ impl RgbaBitmap {
y1,
x2,
y2,
|dest_color| blend.blend(color, dest_color),
|dest_color| blend.blend_1u32(color, dest_color),
);
}
}

View file

@ -86,7 +86,7 @@ impl RgbaBitmap {
positions[0],
positions[1],
positions[2],
|dest_pixels, _w0, _w1, _w2| *dest_pixels = blend.blend(color, *dest_pixels),
|dest_pixels, _w0, _w1, _w2| *dest_pixels = blend.blend_1u32(color, *dest_pixels),
)
}
@ -129,7 +129,7 @@ impl RgbaBitmap {
let r = ((w0 * r1 as f32 + w1 * r2 as f32 + w2 * r3 as f32) / area) as u8;
let g = ((w0 * g1 as f32 + w1 * g2 as f32 + w2 * g3 as f32) / area) as u8;
let b = ((w0 * b1 as f32 + w1 * b2 as f32 + w2 * b3 as f32) / area) as u8;
*dest_pixels = blend.blend(to_argb32([a, r, g, b]), *dest_pixels)
*dest_pixels = blend.blend_1u32(to_argb32([a, r, g, b]), *dest_pixels)
},
)
}
@ -188,7 +188,7 @@ impl RgbaBitmap {
let u = (w0 * texcoords[0].x + w1 * texcoords[1].x + w2 * texcoords[2].x) / area;
let v = (w0 * texcoords[0].y + w1 * texcoords[1].y + w2 * texcoords[2].y) / area;
let src = multiply_argb32(bitmap.sample_at(u, v), color);
*dest_pixels = blend.blend(src, *dest_pixels)
*dest_pixels = blend.blend_1u32(src, *dest_pixels)
},
)
}
@ -245,7 +245,7 @@ impl RgbaBitmap {
let u = (w0 * texcoords[0].x + w1 * texcoords[1].x + w2 * texcoords[2].x) / area;
let v = (w0 * texcoords[0].y + w1 * texcoords[1].y + w2 * texcoords[2].y) / area;
let src = multiply_argb32(bitmap.sample_at(u, v), to_argb32([a, r, g, b]));
*dest_pixels = blend.blend(src, *dest_pixels)
*dest_pixels = blend.blend_1u32(src, *dest_pixels)
},
)
}
@ -287,7 +287,7 @@ impl RgbaBitmap {
|dest_pixels, w0, w1, w2| {
let u = (w0 * texcoords[0].x + w1 * texcoords[1].x + w2 * texcoords[2].x) / area;
let v = (w0 * texcoords[0].y + w1 * texcoords[1].y + w2 * texcoords[2].y) / area;
*dest_pixels = blend.blend(bitmap.sample_at(u, v), *dest_pixels);
*dest_pixels = blend.blend_1u32(bitmap.sample_at(u, v), *dest_pixels);
},
)
}

View file

@ -52,7 +52,7 @@ impl BlendFunction {
/// * `dest`: the destination color to blend the source color over
///
/// returns: the blended color
pub fn blend(&self, src: Color1u32, dest: Color1u32) -> Color1u32 {
pub fn blend_1u32(&self, src: Color1u32, dest: Color1u32) -> Color1u32 {
use BlendFunction::*;
match self {
Blend => blend_argb32(src, dest),
@ -67,6 +67,23 @@ impl BlendFunction {
}
}
}
#[inline]
pub fn blend_4u8(&self, src: Color4u8, dest: Color4u8) -> Color4u8 {
use BlendFunction::*;
match self {
Blend => blend_argb(src, dest),
BlendSourceWithAlpha(opacity) => blend_argb_source_by(src, dest, *opacity),
TintedBlend(tint) => {
let tinted = tint_argb(src, from_argb32(*tint));
blend_argb(tinted, dest)
}
MultipliedBlend(color) => {
let multiplied = multiply_argb(src, from_argb32(*color));
blend_argb(multiplied, dest)
}
}
}
}
/// Packs a set of individual ARGB components to a packed 32-bit color value.