Class: PSD::ClippingMask

Inherits:
Object
  • Object
show all
Defined in:
lib/psd/clipping_mask.rb

Instance Method Summary collapse

Constructor Details

#initialize(layer, png = nil) ⇒ ClippingMask

Returns a new instance of ClippingMask.



3
4
5
6
# File 'lib/psd/clipping_mask.rb', line 3

def initialize(layer, png=nil)
  @layer = layer
  @png = png
end

Instance Method Details

#applyObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/psd/clipping_mask.rb', line 8

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