-
Notifications
You must be signed in to change notification settings - Fork 0
/
contrast.cr
25 lines (24 loc) · 830 Bytes
/
contrast.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# Increases contrast of an image
#
# Taking sample `image`:
#
# <img src="https://raw.githubusercontent.com/Vici37/cr-image/master/docs/images/sample.jpg" alt="Woman with black turtleneck and white background"/>
#
# ```
# image.contrast(20)
# ```
# <img src="https://raw.githubusercontent.com/Vici37/cr-image/master/docs/images/contrasted_20_sample.jpg" alt="Contrasted image with value of 10"/>
module CrImage::Operation::Contrast
def contrast(value : Float64) : self
clone.contrast!(value)
end
def contrast!(value : Float64) : self
factor = (259 * (value + 255)) / (255 * (259 - value))
each_color_channel do |channel|
size.times do |index|
channel.unsafe_put(index, Math.min(255, Math.max(0, factor * (channel.unsafe_fetch(index).to_i - 128) + 128)).to_u8)
end
end
self
end
end