-
Notifications
You must be signed in to change notification settings - Fork 330
/
scale.rb
47 lines (37 loc) · 1.51 KB
/
scale.rb
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
module RedArtisan
module CoreImage
module Filters
module Scale
def resize(width, height)
create_core_image_context(width, height)
scale_x, scale_y = scale(width, height)
@original.affine_clamp :inputTransform => OSX::NSAffineTransform.transform do |clamped|
clamped.lanczos_scale_transform :inputScale => scale_x > scale_y ? scale_x : scale_y, :inputAspectRatio => scale_x / scale_y do |scaled|
scaled.crop :inputRectangle => vector(0, 0, width, height) do |cropped|
@target = cropped
end
end
end
end
def thumbnail(width, height)
create_core_image_context(width, height)
transform = OSX::NSAffineTransform.transform
transform.scaleXBy_yBy *scale(width, height)
@original.affine_transform :inputTransform => transform do |scaled|
@target = scaled
end
end
def fit(size)
original_size = @original.extent.size
scale = size.to_f / (original_size.width > original_size.height ? original_size.width : original_size.height)
resize (original_size.width * scale).to_i, (original_size.height * scale).to_i
end
private
def scale(width, height)
original_size = @original.extent.size
return width.to_f / original_size.width.to_f, height.to_f / original_size.height.to_f
end
end
end
end
end