-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.cr
70 lines (61 loc) · 2.28 KB
/
image.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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# Common base class for `GrayscaleImage` and `RGBAImage`. All `Image`s are readable and saveable
# to the filesystem or `IO` stream.
abstract class CrImage::Image
macro subsclasses_include(mod)
{% for sub in @type.subclasses %}
class ::{{sub}}
include {{mod}}
end
{% end %}
end
macro inherited
include Format::PPM
include Format::PNG
include Operation::BilinearResize
include Operation::BoxBlur
include Operation::Brightness
include Operation::ChannelSwap
include Operation::Contrast
include Operation::Crop
include Operation::Draw
include Operation::GaussianBlur
include Operation::HistogramEqualize
include Operation::HorizontalBlur
include Operation::Rotate
include Operation::Pad
include Operation::VerticalBlur
include Format::Save
extend Format::Open
include Operation::MaskApply
end
# Return the red channel
abstract def red : Array(UInt8)
# Return the green channel
abstract def green : Array(UInt8)
# Return the blue channel
abstract def blue : Array(UInt8)
# Return the alpha channel
abstract def alpha : Array(UInt8)
# Width of image
abstract def width : Int32
# Height of image
abstract def height : Int32
# Size (total pixels) in image (`width` * `height`)
abstract def size : Int32
# Run provided block on each channel supported by this image.
abstract def each_color_channel(& : (Array(UInt8), ChannelType) -> Nil) : Nil
# Run provided block on each channel supported by this image.
abstract def each_channel(& : (Array(UInt8), ChannelType) -> Nil) : Nil
# Get the `Array(UInt8)` corresponding to `channel_type`)
abstract def [](channel_type : ChannelType) : Array(UInt8)
# Set the `Array(UInt8)` corresponding to `channel_type`) to `channel`
abstract def []=(channel_type : ChannelType, channel : Array(UInt8)) : Array(UInt8)
private def index_of(x : Int, y : Int) : Int
y * width + x
end
private def resolve_to_start_and_count(range, size) : Tuple(Int32, Int32)
start, count = Indexable.range_to_index_and_count(range, size) || raise IndexError.new("Unable to resolve range #{range} for image dimension of #{size}")
raise IndexError.new("Range #{range} exceeds bounds of #{size}") if (start + count) > size
{start, count}
end
end