Sha256: bb403311d596c9847519114533ef00b10cb59ae3c1d42d0673ffe04f9215ce5e

Contents?: true

Size: 1.22 KB

Versions: 8

Compression:

Stored size: 1.22 KB

Contents

module ChunkyPNG
  class Canvas
    module Operations
      def compose(new_foreground, dx = 0, dy = 0)
        check_size_constraints!(new_foreground, dx, dy)

        for y in 0...new_foreground.height do
          for x in 0...new_foreground.width do
            self[x+dx, y+dy] = ChunkyPNG::Color.compose(new_foreground[x, y], self[x+dx, y+dy])
          end
        end
        self
      end

      def replace(other, offset_x = 0, offset_y = 0)
        check_size_constraints!(other, offset_x, offset_y)

        for y in 0...other.height do
          pixels[(y + offset_y) * width + offset_x, other.width] = other.pixels[y * other.width, other.width]
        end
        self
      end

      def crop(x, y, crop_width, crop_height)
        new_pixels = []
        for cy in 0...crop_height do
          new_pixels += pixels.slice((cy + y) * width + x, crop_width)
        end
        ChunkyPNG::Canvas.new(crop_width, crop_height, new_pixels)
      end

      protected

      def check_size_constraints!(other, offset_x, offset_y)
        raise "Background image width is too small!"  if width  < other.width  + offset_x
        raise "Background image height is too small!" if height < other.height + offset_y
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
chunky_png-0.5.8 lib/chunky_png/canvas/operations.rb
chunky_png-0.5.7 lib/chunky_png/canvas/operations.rb
chunky_png-0.5.6 lib/chunky_png/canvas/operations.rb
chunky_png-0.5.5 lib/chunky_png/canvas/operations.rb
chunky_png-0.5.4 lib/chunky_png/canvas/operations.rb
chunky_png-0.5.3 lib/chunky_png/canvas/operations.rb
chunky_png-0.5.2 lib/chunky_png/canvas/operations.rb
chunky_png-0.5.1 lib/chunky_png/canvas/operations.rb