Sha256: 27911831b0f0118dc47203959ac8a9ff80f9a11d4afbdac4aa16b3a3ab4b8938

Contents?: true

Size: 1.26 KB

Versions: 6

Compression:

Stored size: 1.26 KB

Contents

class PSD
  class ClippingMask
    def initialize(layer, png=nil)
      @layer = layer
      @png = png
    end

    def apply
      return @png unless @layer.clipped?

      PSD.logger.debug "Applying clipping mask #{mask.name} to #{@layer.name}"

      width, height = @layer.document_dimensions
      full_png = compose_to_full

      height.times do |y|
        width.times do |x|
          if y < mask.top || y > mask.bottom || x < mask.left || x > mask.right
            alpha = 0
          else
            mask_x = x - mask.left
            mask_y = y - mask.top

            pixel = mask.image.pixel_data[mask_y * mask.width + mask_x]
            alpha = pixel.nil? ? 0 : ChunkyPNG::Color.a(pixel)
          end
          
          color = full_png[x, y]
          full_png[x, y] = (color & 0xffffff00) | (ChunkyPNG::Color.a(color) * alpha / 255)
        end
      end

      full_png.crop!(@layer.left, @layer.top, @layer.width, @layer.height)
    end

    private

    def mask
      @mask ||= @layer.next_sibling
    end

    def compose_to_full
      width, height = @layer.document_dimensions
      full_png = ChunkyPNG::Canvas.new(width.to_i, height.to_i, ChunkyPNG::Color::TRANSPARENT)
      full_png.compose!(@png, @layer.left, @layer.top)
      full_png
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
psd-1.5.0 lib/psd/clipping_mask.rb
psd-1.4.5 lib/psd/clipping_mask.rb
psd-1.4.4 lib/psd/clipping_mask.rb
psd-1.4.3 lib/psd/clipping_mask.rb
psd-1.4.2 lib/psd/clipping_mask.rb
psd-1.4.1 lib/psd/clipping_mask.rb