Class: PSD::Renderer::Mask

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(canvas, options = {}) ⇒ Mask

Returns a new instance of Mask.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/psd/renderer/mask.rb', line 6

def initialize(canvas, options = {})
  @canvas = canvas
  @layer = canvas.node
  @options = options

  @pixel_data = @canvas.pixels
  @mask_data = @layer.image.mask_data

  @layer_width = (@layer.folder? ? @layer.mask.width : @layer.width).to_i
  @layer_height = (@layer.folder? ? @layer.mask.height : @layer.height).to_i
end

Instance Attribute Details

#layer_heightObject

Returns the value of attribute layer_height.



4
5
6
# File 'lib/psd/renderer/mask.rb', line 4

def layer_height
  @layer_height
end

#layer_widthObject

Returns the value of attribute layer_width.



4
5
6
# File 'lib/psd/renderer/mask.rb', line 4

def layer_width
  @layer_width
end

#mask_dataObject

Returns the value of attribute mask_data.



4
5
6
# File 'lib/psd/renderer/mask.rb', line 4

def mask_data
  @mask_data
end

#pixel_dataObject

Returns the value of attribute pixel_data.



4
5
6
# File 'lib/psd/renderer/mask.rb', line 4

def pixel_data
  @pixel_data
end

Instance Method Details

#apply!Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/psd/renderer/mask.rb', line 18

def apply!
  PSD.logger.debug "Applying mask to #{@layer.name}"
  
  # We generate the preview at the document size instead to make applying the mask
  # significantly easier.
  width = @layer.header.width.to_i
  height = @layer.header.height.to_i
  png = ChunkyPNG::Canvas.new(width, height, ChunkyPNG::Color::TRANSPARENT)

  i = 0
  @layer_height.times do |y|
    @layer_width.times do |x|
      offset_x = x + @layer.left
      offset_y = y + @layer.top

      i +=1 and next if offset_x < 0 || offset_y < 0 || offset_x >= png.width || offset_y >= png.height

      png[offset_x, offset_y] = @pixel_data[i]
      i += 1
    end
  end
  
  # Now we apply the mask
  i = 0
  @layer.mask.height.times do |y|
    @layer.mask.width.times do |x|
      offset_x = @layer.mask.left + x
      offset_y = @layer.mask.top + y

      i += 1 and next if offset_x < 0 || offset_y < 0 || offset_x >= png.width || offset_y >= png.height

      color = ChunkyPNG::Color.to_truecolor_alpha_bytes(png[offset_x, offset_y])
      color[3] = color[3] * @mask_data[i] / 255

      png[offset_x, offset_y] = ChunkyPNG::Color.rgba(*color)
      i += 1
    end
  end

  crop_left = PSD::Util.clamp(@layer.left, 0, png.width)
  crop_top = PSD::Util.clamp(@layer.top, 0, png.height)
  crop_width = PSD::Util.clamp(@layer_width, 0, png.width - crop_left)
  crop_height = PSD::Util.clamp(@layer_height, 0, png.height - crop_top)

  png.crop!(crop_left, crop_top, crop_width, crop_height)

  @canvas.canvas = png
end