Skip to content

Commit

Permalink
Merge pull request #102 from lencioni/croptimization
Browse files Browse the repository at this point in the history
Optimize crop! when only cropping top/bottom
  • Loading branch information
wvanbergen committed Oct 28, 2015
2 parents fb00a5e + f97f57e commit e4ac4dd
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 15 deletions.
15 changes: 11 additions & 4 deletions lib/chunky_png/canvas/operations.rb
Expand Up @@ -175,11 +175,18 @@ def crop!(x, y, crop_width, crop_height)
raise ChunkyPNG::OutOfBounds, 'Original image height is too small!'
end

new_pixels = []
for cy in 0...crop_height do
new_pixels.concat pixels.slice((cy + y) * width + x, crop_width)
if crop_width == width && x == 0
# We only need to crop off the top and/or bottom, so we can take a
# shortcut.
replace_canvas!(crop_width, crop_height,
pixels.slice(y * width, width * crop_height))
else
new_pixels = []
for cy in 0...crop_height do
new_pixels.concat pixels.slice((cy + y) * width + x, crop_width)
end
replace_canvas!(crop_width, crop_height, new_pixels)
end
replace_canvas!(crop_width, crop_height, new_pixels)
end

# Flips the image horizontally, leaving the original intact.
Expand Down
46 changes: 35 additions & 11 deletions spec/chunky_png/canvas/operations_spec.rb
Expand Up @@ -48,22 +48,46 @@
end

describe '#crop!' do
it "should crop the right pixels from the original canvas" do
subject.crop!(10, 5, 4, 8)
expect(subject).to eql reference_canvas('cropped')
end
context 'when cropping both width and height' do
let(:crop_opts) { [10, 5, 4, 8] }

it "should crop the right pixels from the original canvas" do
subject.crop!(*crop_opts)
expect(subject).to eql reference_canvas('cropped')
end

it "should have a new width and height" do
expect { subject.crop!(10, 5, 4, 8) }.to change { subject.dimension }.
from(ChunkyPNG::Dimension('16x16')).to(ChunkyPNG::Dimension('4x8'))
it "should have a new width and height" do
expect { subject.crop!(*crop_opts) }.to change { subject.dimension }.
from(ChunkyPNG::Dimension('16x16')).to(ChunkyPNG::Dimension('4x8'))
end

it "should return itself" do
expect(subject.crop!(*crop_opts)).to equal(subject)
end
end

it "should raise an exception when the cropped image falls outside the oiginal image" do
expect { subject.crop!(16, 16, 2, 2) }.to raise_error(ChunkyPNG::OutOfBounds)
context "when cropping just the height" do
let(:crop_opts) { [0, 5, 16, 8] }

it "should crop the right pixels from the original canvas" do
subject.crop!(*crop_opts)
expect(subject).to eql reference_canvas('cropped_height')
end

it "should have a new width and height" do
expect { subject.crop!(*crop_opts) }.to change { subject.dimension }.
from(ChunkyPNG::Dimension('16x16')).to(ChunkyPNG::Dimension('16x8'))
end

it "should return itself" do
expect(subject.crop!(*crop_opts)).to equal(subject)
end
end

it "should return itself" do
expect(subject.crop!(10, 5, 4, 8)).to equal(subject)
context "when the cropped image falls outside the original image" do
it "should raise an exception" do
expect { subject.crop!(16, 16, 2, 2) }.to raise_error(ChunkyPNG::OutOfBounds)
end
end
end

Expand Down
Binary file added spec/resources/cropped_height.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit e4ac4dd

Please sign in to comment.