Class: PSD::LayerMask

Inherits:
Object
  • Object
show all
Includes:
Section
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

Attributes included from Section

#section_end, #section_start

Instance Method Summary collapse

Methods included from Section

#end_of_section, #end_section, #start_of_section, #start_section

Constructor Details

#initialize(file, header, options) ⇒ LayerMask

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



10
11
12
13
14
15
16
17
18
19
# File 'lib/psd/layer_mask.rb', line 10

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

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

Instance Attribute Details

#global_maskObject (readonly)

Returns the value of attribute global_mask.



7
8
9
# File 'lib/psd/layer_mask.rb', line 7

def global_mask
  @global_mask
end

#layersObject (readonly)

Returns the value of attribute layers.



7
8
9
# File 'lib/psd/layer_mask.rb', line 7

def layers
  @layers
end

Instance Method Details

#export(outfile) ⇒ Object

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



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/psd/layer_mask.rb', line 77

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. Once implemented, this will also trigger parsing of the channel images for each layer.



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
67
68
69
70
71
72
73
74
# File 'lib/psd/layer_mask.rb', line 30

def parse
  start_section

  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
      raise "Unlikely number of layers parsed: #{layer_count}"
    end

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

    layers.each do |layer|
      @file.seek 8, IO::SEEK_CUR and next if layer.folder? || layer.folder_end?
      layer.parse_channel_image!(@header, @options[:parse_layer_images])
    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
  end_section

  return self
end

#skipObject

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



23
24
25
26
# File 'lib/psd/layer_mask.rb', line 23

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