Class: PSD::LayerMask

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

Overview

Covers parsing the global mask and controls parsing of all the layers/folders in the document.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, header, options) ⇒ LayerMask

Store a reference to the file and the header and initialize the defaults.



8
9
10
11
12
13
14
15
16
# File 'lib/psd/layer_mask.rb', line 8

def initialize(file, header, options)
  @file = file
  @header = header
  @options = options

  @layers = []
  @merged_alpha = false
  @global_mask = nil      
end

Instance Attribute Details

#global_maskObject (readonly)

Returns the value of attribute global_mask.



5
6
7
# File 'lib/psd/layer_mask.rb', line 5

def global_mask
  @global_mask
end

#layersObject (readonly)

Returns the value of attribute layers.



5
6
7
# File 'lib/psd/layer_mask.rb', line 5

def layers
  @layers
end

Instance Method Details

#export(outfile) ⇒ Object

Export the mask and all the children layers to a file.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/psd/layer_mask.rb', line 69

def export(outfile)
  if @layers.size == 0
    # No data, just read whatever's here.
    return outfile.write @file.read(@section_end[:all] - start_of_section)
  end

  # Read the initial mask data since it won't change
  outfile.write @file.read(@layer_section_start - @file.tell)

  @layers.reverse.each do |layer|
    layer.export(outfile)
  end

  outfile.write @file.read(end_of_section - @file.tell)
end

#parseObject

Parse this section, including all of the layers and folders.



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
66
# File 'lib/psd/layer_mask.rb', line 26

def parse
  mask_size = @file.read_int
  finish = @file.tell + mask_size

  return self if mask_size <= 0

  layer_info_size = Util.pad2(@file.read_int)

  if layer_info_size > 0
    layer_count = @file.read_short

    if layer_count < 0
      layer_count = layer_count.abs
      @merged_alpha = true
    end

    if layer_count * (18 + 6 * @header.channels) > layer_info_size
      PSD.logger.error "Unlikely number of layers parsed: #{layer_count}"
    end

    @layer_section_start = @file.tell
    layer_count.times do
      @layers << PSD::Layer.new(@file, @header).parse
    end

    layers.each do |layer|
      layer.parse_channel_image(@header)
    end
  end

  # Layers are parsed in reverse order
  layers.reverse!
  group_layers

  parse_global_mask

  # Ensure we're at the end of this section
  @file.seek finish

  return self
end

#skipObject

Allows us to skip this section because it starts with the length of the section stored as an integer.



20
21
22
23
# File 'lib/psd/layer_mask.rb', line 20

def skip
  @file.seek @file.read_int, IO::SEEK_CUR
  return self
end