-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement image rotation, default value for color channels, fix bug w…
…ith alpha
- Loading branch information
Showing
8 changed files
with
113 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
require "../../spec_helper" | ||
|
||
Spectator.describe CrImage::Operation::Rotate, :focus do | ||
include SpecHelper | ||
|
||
specs_for_operator(rotate(45), | ||
gray_hash: "b23e83666e8046bd28396b3c5d7e4c7874671cfd", | ||
rgba_hash: "e21797526425cd7e9473ca479f45bbdb8e049d1e" | ||
) | ||
|
||
specs_for_operator(rotate(90), | ||
gray_hash: "124795c3ed7e003750d024f610626c3b22b26206", | ||
rgba_hash: "fd20c71bb52ff5a148dfcea9f4fe49ab92c80804" | ||
) | ||
|
||
specs_for_operator(rotate(-30), | ||
gray_hash: "a14c40f3c480330e4ec8aa9d8441d3e08d6e3864", | ||
rgba_hash: "edafe8f2472918dcdad5e72187665fcc32e14851" | ||
) | ||
|
||
specs_for_operator(rotate(45, center_x: 100, center_y: 200), | ||
gray_hash: "89f91776edc2c8814d235c29d0a50eaa3f015753", | ||
rgba_hash: "fb4a52471956cd6a48aee2f4470477a4b295587c" | ||
) | ||
|
||
specs_for_operator(rotate(45, center_x: 375, center_y: 250, radius: 20), | ||
gray_hash: "171f3aa256a46a3aeec94cddec477ed01fdb658e", | ||
rgba_hash: "ba9d444ed5f4567830d6037990231aae0f3431ca" | ||
) | ||
|
||
# specs_for_operator(crop(0, 0, 750, 500), | ||
# gray_hash: "62d6101d60ee8da38d1b9d8e809091099cec5994", | ||
# rgba_hash: "d764459f778b4839972367f78197bf9a96cd11fd" | ||
# ) | ||
|
||
let(image) { gray_moon_ppm } | ||
|
||
it "rotates" do | ||
image.to_rgba | ||
.draw_square(0, 0, image.width - 1, image.height - 1, CrImage::Color.random) | ||
.draw_circle(375, 250, 5, CrImage::Color.of("#ff0000"), fill: true) | ||
.save("original.jpg") | ||
.rotate(-45, center_x: 375, center_y: 250, radius: 20, pad: true) | ||
.save("rotated.jpg") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,8 @@ enum CrImage::ChannelType | |
Blue | ||
Gray | ||
Alpha | ||
|
||
def default : UInt8 | ||
alpha? ? 255u8 : 0u8 | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
module CrImage::Operation::Rotate | ||
def rotate(degrees : Float64, *, center_x : Int32 = width // 2, center_y : Int32 = height // 2, radius : Int32 = -1, pad : Bool = false) : self | ||
clone.rotate!(degrees, center_x: center_x, center_y: center_y, radius: radius, pad: pad) | ||
end | ||
|
||
def rotate!(degrees : Float64, *, center_x : Int32 = width // 2, center_y : Int32 = height // 2, radius : Int32 = -1, pad : Bool = false) : self | ||
# TODO: use pad | ||
|
||
# Rotate backwards, so that we can "look back" from the output pixel location into the input pixel location | ||
radians = -Math::PI * degrees / 180 | ||
sin = Math.sin(radians) | ||
cos = Math.cos(radians) | ||
radius_sq = radius * radius | ||
|
||
new_size = size | ||
out_center_x = center_x | ||
out_center_y = center_y | ||
each_channel do |channel, channel_type| | ||
new_x = -1 | ||
new_y = -1 | ||
new_channel = Array.new(new_size) do |i| | ||
new_x += 1 | ||
new_x %= width | ||
new_y += 1 if new_x == 0 | ||
|
||
if radius >= 0 | ||
next channel[i] if ((new_x - center_x) ** 2 + (new_y - center_y) ** 2) > radius_sq | ||
end | ||
|
||
orig_x = (cos * (new_x - center_x) - sin * (new_y - center_y) + out_center_x).round.to_i | ||
orig_y = (sin * (new_x - center_x) + cos * (new_y - center_y) + out_center_y).round.to_i | ||
|
||
next channel_type.default if orig_x < 0 || orig_x >= width || orig_y < 0 || orig_y >= height | ||
|
||
channel.unsafe_fetch(orig_y * width + orig_x) | ||
end | ||
|
||
self[channel_type] = new_channel | ||
end | ||
|
||
self | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters