Skip to content

Commit

Permalink
misc refined
Browse files Browse the repository at this point in the history
  • Loading branch information
mhfan committed Aug 21, 2024
1 parent 1155f93 commit 78fe26a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 18 deletions.
11 changes: 6 additions & 5 deletions src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,16 +228,17 @@ impl PathBuilderExt for skia::PathBuilder {
}
}

// https://github.com/TinyVG/sdk/blob/ed01b9112/src/lib/rendering.zig#L1133
/* fn lerp_sRGB(c0: RGBA8888, c1: RGBA8888, f_unchecked: f32) -> RGBA8888 { // blend
// Color interpolation is needed in gradients and must performed in linear
// color space. This means that the value from the color table needs to be
// converted to linear color space, then each color component is
// interpolated linearly and the final color is then determined by
// converting the color back to the specified color space.
let f = f32::clamp(f_unchecked, 0.0, 1.0); RGBA8888 {
r: gamma2linear(lerp(linear2gamma(c0.r), linear2gamma(c1.r), f)),
g: gamma2linear(lerp(linear2gamma(c0.g), linear2gamma(c1.g), f)),
b: gamma2linear(lerp(linear2gamma(c0.b), linear2gamma(c1.b), f)),
r: linear2gamma(lerp(gamma2linear(c0.r), gamma2linear(c1.r), f)),
g: linear2gamma(lerp(gamma2linear(c0.g), gamma2linear(c1.g), f)),
b: linear2gamma(lerp(gamma2linear(c0.b), gamma2linear(c1.b), f)),
a: (lerp(c0.a as f32 / 255.0, c1.a as f32 / 255.0, f) * 255.0) as _,
}
}
Expand All @@ -250,7 +251,7 @@ fn lerp_value(src: f32, dst: f32, src_alpha: f32, dst_alpha: f32, fin_alpha: f32
const sRGB_gamma: f32 = 2.2;
#[inline] fn mapToLinear(v: f32) -> f32 { f32::powf(v, sRGB_gamma) }
#[inline] fn mapToGamma (v: f32) -> f32 { f32::powf(v, 1.0 / sRGB_gamma) }
#[inline] fn gamma2linear(v: f32) -> u8 { (255.0 * mapToGamma(v)) as _ }
#[inline] fn linear2gamma(v: u8) -> f32 { mapToLinear(v as f32 / 255.0) }
#[inline] fn linear2gamma(v: f32) -> u8 { (255.0 * mapToGamma(v)) as _ }
#[inline] fn gamma2linear(v: u8) -> f32 { mapToLinear(v as f32 / 255.0) }
#[inline] fn lerp(a: f32, b: f32, f: f32) -> f32 { a + (b - a) * f } */

24 changes: 11 additions & 13 deletions src/tinyvg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,21 @@ impl From<TryFromIntError> for TVGError {
/// and a sequence of commands terminated by a _end of file_ command.
///
/// - All integers are assumed to be encoded in little-endian byte order
/// if not specified otherwise.
/// if not specified otherwise.
///
/// - The _Type_ fields have no padding bits in between. If a field
/// does not align to a byte boundary, the next field will be offset
/// into the byte by the current fields bit offset + bit size.
/// This means, that two consecutive fields A (u3) and B (u5) can be
/// extracted from the byte by using (byte & 0x7) >> 0 for A and
/// (byte & 0xF8) >> 3 for B.
/// - The _Type_ fields have no padding bits in between. If a field does not align to
/// a byte boundary, the next field will be offset into the byte by the current
/// fields bit offset + bit size. This means, that two consecutive fields A (u3) and
/// B (u5) can be extracted from the byte by using (byte & 0x7) >> 0 for A and
/// (byte & 0xF8) >> 3 for B.
///
/// - If not specified otherwise, all coordinates in TinyVG are absolute
/// coordinates, including path nodes and gradients.
/// coordinates, including path nodes and gradients.
///
/// - A lot of encoded integers are encoded off-by-one, thus mapping 0 to
/// 1, 1 to 2 and so on. This is done as encoding these integers as 0
/// would be equivalent to removing the element from the file.
/// Thus, this can be used to encode some more elements with less bytes.
/// If this is the case, this is signaled by the use of value+1.
/// - A lot of encoded integers are encoded off-by-one, thus mapping 0 to 1, 1 to 2 and so on.
/// This is done as encoding these integers as 0 would be equivalent to removing the element
/// from the file. Thus, this can be used to encode some more elements with less bytes.
/// If this is the case, this is signaled by the use of value+1.
///
/// https://tinyvg.tech/download/specification.txt, https://github.com/TinyVG/sdk,
/// https://github.com/lily-mara/tinyvg-rs, https://github.com/dataphract/tinyvg-rs
Expand Down

0 comments on commit 78fe26a

Please sign in to comment.