Jump to content

Grayscale image: Difference between revisions

Line 395:
implement {}
rgb24_to_gray8 rgb =
(* There is no need for floating point here, although equivalent
integer calculations are a bit longer to write out. *)
let
extern castfn u8_to_dbli2u32 : uint8int -<> doubleuint32
extern castfn dbl_to_u8u8_to_u32 : doubleuint8 -<> uint8uint32
extern castfn u32_to_u8 : uint32 -<> uint8
 
val @(r, g, b) = rgb24_values rgb
val r : double = u8_to_dblu8_to_u32 r
and g : double = u8_to_dblu8_to_u32 g
and b : double = u8_to_dblu8_to_u32 b
 
val Y = (0.i2u32 2126 * r) + (0.i2u32 7152 * g) + (0.0722i2u32 722 * b)
val YiY1 = dbl_to_u8Y ($extfcall/ (double,i2u32 "rint", Y))10000
and Y0 = Y mod (i2u32 10000)
in
if Y0 < i2u32 5000 then
gray8_make Yi(u32_to_u8 Y1)
else if i2u32 5000 < Y0 then
gray8_make (succ (u32_to_u8 Y1))
else if Y0 mod (i2u32 2) = i2u32 0 then
gray8_make (u32_to_u8 Y1)
else
gray8_make (succ (u32_to_u8 Y1))
end
 
1,448

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.