/* This Source Code Form is subject to the terms of the Mozilla Public *License,v.2.0.IfacopyoftheMPLwasnotdistributedwiththis
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
/// Color space responsible for converting between lumas and luminances. #[derive(Clone, Copy, Debug, PartialEq)] pubenum LuminanceColorSpace { /// Linear space - no conversion involved.
Linear, /// Simple gamma space - uses the `luminance ^ gamma` function.
Gamma(f32), /// Srgb space.
Srgb,
}
// Computes the luminance from the given r, g, and b in accordance with // SK_LUM_COEFF_X. For correct results, r, g, and b should be in linear space. fn compute_luminance(r: u8, g: u8, b: u8) -> u8 { // The following is // r * SK_LUM_COEFF_R + g * SK_LUM_COEFF_G + b * SK_LUM_COEFF_B // with SK_LUM_COEFF_X in 1.8 fixed point (rounding adjusted to sum to 256). let val: u32 = r as u32 * 54 + g as u32 * 183 + b as u32 * 19;
assert!(val < 0x10000);
(val >> 8) as u8
}
// Skia uses 3 bits per channel for luminance. const LUM_BITS: u8 = 3; // Mask of the highest used bits. const LUM_MASK: u8 = ((1 << LUM_BITS) - 1) << (8 - LUM_BITS);
impl ColorLut for ColorU { // Compute a canonical color that is equivalent to the input color // for preblend table lookups. The alpha channel is never used for // preblending, so overwrite it with opaque. fn quantize(&self) -> ColorU {
ColorU::new(
scale255(LUM_BITS, self.r >> (8 - LUM_BITS)),
scale255(LUM_BITS, self.g >> (8 - LUM_BITS)),
scale255(LUM_BITS, self.b >> (8 - LUM_BITS)), 255,
)
}
// Quantize to the smallest value that yields the same table index. fn quantized_floor(&self) -> ColorU {
ColorU::new( self.r & LUM_MASK, self.g & LUM_MASK, self.b & LUM_MASK, 255,
)
}
// Quantize to the largest value that yields the same table index. fn quantized_ceil(&self) -> ColorU {
ColorU::new( self.r | !LUM_MASK, self.g | !LUM_MASK, self.b | !LUM_MASK, 255,
)
}
// Compute a luminance value suitable for grayscale preblend table // lookups. fn luminance(&self) -> u8 {
compute_luminance(self.r, self.g, self.b)
}
// Make a grayscale color from the computed luminance. fn luminance_color(&self) -> ColorU { let lum = self.luminance();
ColorU::new(lum, lum, lum, self.a)
}
}
// This will invert the gamma applied by CoreGraphics, // so we can get linear values. // CoreGraphics obscurely defaults to 2.0 as the smoothing gamma value. // The color space used does not appear to affect this choice. #[cfg(any(target_os="macos", target_os = "ios"))] fn get_inverse_gamma_table_coregraphics_smoothing() -> [u8; 256] { letmut table = [0u8; 256];
for (i, v) in table.iter_mut().enumerate() { let x = i as f32 / 255.0;
*v = round_to_u8(x * x * 255.0);
}
table
}
// A value of 0.5 for SK_GAMMA_CONTRAST appears to be a good compromise. // With lower values small text appears washed out (though correctly so). // With higher values lcd fringing is worse and the smoothing effect of // partial coverage is diminished. fn apply_contrast(srca: f32, contrast: f32) -> f32 {
srca + ((1.0 - srca) * contrast * srca)
}
// The approach here is not necessarily the one with the lowest error // See https://bel.fi/alankila/lcd/alpcor.html for a similar kind of thing // that just search for the adjusted alpha value pubfn build_gamma_correcting_lut(table: &mut [u8; 256], src: u8, contrast: f32,
src_space: LuminanceColorSpace,
dst_convert: LuminanceColorSpace) { let src = src as f32 / 255.0; let lin_src = src_space.to_luma(src); // Guess at the dst. The perceptual inverse provides smaller visual // discontinuities when slight changes to desaturated colors cause a channel // to map to a different correcting lut with neighboring srcI. // See https://code.google.com/p/chromium/issues/detail?id=141425#c59 . let dst = 1.0 - src; let lin_dst = dst_convert.to_luma(dst);
// Contrast value tapers off to 0 as the src luminance becomes white let adjusted_contrast = contrast * lin_dst;
// Remove discontinuity and instability when src is close to dst. // The value 1/256 is arbitrary and appears to contain the instability. if (src - dst).abs() < (1.0 / 256.0) { letmut ii : f32 = 0.0; for v in table.iter_mut() { let raw_srca = ii / 255.0; let srca = apply_contrast(raw_srca, adjusted_contrast);
*v = round_to_u8(255.0 * srca);
ii += 1.0;
}
} else { // Avoid slow int to float conversion. letmut ii : f32 = 0.0; for v in table.iter_mut() { // 'raw_srca += 1.0f / 255.0f' and even // 'raw_srca = i * (1.0f / 255.0f)' can add up to more than 1.0f. // When this happens the table[255] == 0x0 instead of 0xff. // See http://code.google.com/p/chromium/issues/detail?id=146466 let raw_srca = ii / 255.0; let srca = apply_contrast(raw_srca, adjusted_contrast);
assert!(srca <= 1.0); let dsta = 1.0 - srca;
// Calculate the output we want. let lin_out = lin_src * srca + dsta * lin_dst;
assert!(lin_out <= 1.0); let out = dst_convert.from_luma(lin_out);
// Undo what the blit blend will do. // i.e. given the formula for OVER: out = src * result + (1 - result) * dst // solving for result gives: let result = (out - dst) / (src - dst);
*v = round_to_u8(255.0 * result);
debug!("Setting {:?} to {:?}", ii as u8, *v);
impl GammaLut { // Skia actually makes 9 gamma tables, then based on the luminance color, // fetches the RGB gamma table for that color. fn generate_tables(&mutself, contrast: f32, paint_gamma: f32, device_gamma: f32) { let paint_color_space = LuminanceColorSpace::new(paint_gamma); let device_color_space = LuminanceColorSpace::new(device_gamma);
for (i, entry) inself.tables.iter_mut().enumerate() { let luminance = scale255(LUM_BITS, i as u8);
build_gamma_correcting_lut(entry,
luminance,
contrast,
paint_color_space,
device_color_space);
}
}
// Assumes pixels are in BGRA format. Assumes pixel values are in linear space already. pubfn preblend(&self, pixels: &mut [u8], color: ColorU) { let table_r = self.get_table(color.r); let table_g = self.get_table(color.g); let table_b = self.get_table(color.b);
for pixel in pixels.chunks_mut(4) { let (b, g, r) = (table_b[pixel[0] as usize], table_g[pixel[1] as usize], table_r[pixel[2] asusize]);
pixel[0] = b;
pixel[1] = g;
pixel[2] = r;
pixel[3] = max(max(b, g), r);
}
}
// Assumes pixels are in BGRA format. Assumes pixel values are in linear space already. pubfn preblend_scaled(&self, pixels: &mut [u8], color: ColorU, percent: u8) { if percent >= 100 { self.preblend(pixels, color); return;
}
let table_r = self.get_table(color.r); let table_g = self.get_table(color.g); let table_b = self.get_table(color.b); let scale = (percent as i32 * 256) / 100;
for pixel in pixels.chunks_mut(4) { let (mut b, g, mut r) = (
table_b[pixel[0] as usize] as i32,
table_g[pixel[1] as usize] as i32,
table_r[pixel[2] as usize] as i32,
);
b = g + (((b - g) * scale) >> 8);
r = g + (((r - g) * scale) >> 8);
pixel[0] = b as u8;
pixel[1] = g as u8;
pixel[2] = r as u8;
pixel[3] = max(max(b, g), r) as u8;
}
}
#[cfg(any(target_os="macos", target_os="ios"))] pubfn coregraphics_convert_to_linear(&self, pixels: &e='color:red'>mut [u8]) { for pixel in pixels.chunks_mut(4) {
pixel[0] = self.cg_inverse_gamma[pixel[0] as usize];
pixel[1] = self.cg_inverse_gamma[pixel[1] as usize];
pixel[2] = self.cg_inverse_gamma[pixel[2] as usize];
}
}
// Assumes pixels are in BGRA format. Assumes pixel values are in linear space already. pubfn preblend_grayscale(&self, pixels: &mut [u8], color: ColorU) { let table_g = self.get_table(color.g);
for pixel in pixels.chunks_mut(4) { let luminance = compute_luminance(pixel[2], pixel[1], pixel[0]); let alpha = table_g[luminance as usize];
pixel[0] = alpha;
pixel[1] = alpha;
pixel[2] = alpha;
pixel[3] = alpha;
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.