Module: Pura::Image::Operations
- Included in:
- Wrapper
- Defined in:
- lib/pura/image/operations.rb
Instance Method Summary collapse
- #grayscale ⇒ Object
- #resize_and_pad(target_width, target_height, background: [0, 0, 0]) ⇒ Object
- #resize_to_cover(cover_width, cover_height) ⇒ Object
- #resize_to_fill(fill_width, fill_height) ⇒ Object
- #resize_to_fit(max_width, max_height) ⇒ Object
- #resize_to_limit(max_width, max_height) ⇒ Object
- #rotate(degrees) ⇒ Object
- #strip ⇒ Object
Instance Method Details
#grayscale ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/pura/image/operations.rb', line 21 def grayscale new_pixels = String.new(encoding: Encoding::BINARY) i = 0 size = @pixels.bytesize while i < size r = @pixels.getbyte(i) g = @pixels.getbyte(i + 1) b = @pixels.getbyte(i + 2) gray = ((r + g + b) / 3.0).round new_pixels << gray << gray << gray i += 3 end self.class.new(@width, @height, new_pixels) end |
#resize_and_pad(target_width, target_height, background: [0, 0, 0]) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/pura/image/operations.rb', line 56 def resize_and_pad(target_width, target_height, background: [0, 0, 0]) # First resize_to_fit scale = [@width.to_f / target_width, @height.to_f / target_height].max new_w = [(@width / scale).round, 1].max new_h = [(@height / scale).round, 1].max resized = resize(new_w, new_h) # Create padded canvas r, g, b = background bg_row = (String.new(encoding: Encoding::BINARY) << r << g << b) * target_width canvas = bg_row * target_height # Center the resized image on the canvas offset_x = (target_width - resized.width) / 2 offset_y = (target_height - resized.height) / 2 resized.height.times do |y| src_start = y * resized.width * 3 dst_start = (((offset_y + y) * target_width) + offset_x) * 3 canvas[dst_start, resized.width * 3] = resized.pixels[src_start, resized.width * 3] end self.class.new(target_width, target_height, canvas) end |
#resize_to_cover(cover_width, cover_height) ⇒ Object
81 82 83 84 85 86 |
# File 'lib/pura/image/operations.rb', line 81 def resize_to_cover(cover_width, cover_height) scale = [@width.to_f / cover_width, @height.to_f / cover_height].min new_w = [(@width / scale).round, 1].max new_h = [(@height / scale).round, 1].max resize(new_w, new_h) end |
#resize_to_fill(fill_width, fill_height) ⇒ Object
52 53 54 |
# File 'lib/pura/image/operations.rb', line 52 def resize_to_fill(fill_width, fill_height) resize_fill(fill_width, fill_height) end |
#resize_to_fit(max_width, max_height) ⇒ Object
45 46 47 48 49 50 |
# File 'lib/pura/image/operations.rb', line 45 def resize_to_fit(max_width, max_height) scale = [@width.to_f / max_width, @height.to_f / max_height].max new_w = [(@width / scale).round, 1].max new_h = [(@height / scale).round, 1].max resize(new_w, new_h) end |
#resize_to_limit(max_width, max_height) ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/pura/image/operations.rb', line 36 def resize_to_limit(max_width, max_height) return dup_image if @width <= max_width && @height <= max_height scale = [@width.to_f / max_width, @height.to_f / max_height].max new_w = [(@width / scale).round, 1].max new_h = [(@height / scale).round, 1].max resize(new_w, new_h) end |
#rotate(degrees) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/pura/image/operations.rb', line 6 def rotate(degrees) case degrees % 360 when 0 dup_image when 90 rotate90 when 180 rotate180 when 270 rotate270 else raise ArgumentError, "Only 90, 180, 270 degree rotations are supported" end end |
#strip ⇒ Object
88 89 90 |
# File 'lib/pura/image/operations.rb', line 88 def strip dup_image end |