Class: PSD

Inherits:
Object
  • Object
show all
Includes:
Helpers, Logger, NodeExporting
Defined in:
lib/psd.rb,
lib/psd/file.rb,
lib/psd/mask.rb,
lib/psd/node.rb,
lib/psd/util.rb,
lib/psd/color.rb,
lib/psd/image.rb,
lib/psd/layer.rb,
lib/psd/header.rb,
lib/psd/logger.rb,
lib/psd/helpers.rb,
lib/psd/section.rb,
lib/psd/version.rb,
lib/psd/renderer.rb,
lib/psd/resource.rb,
lib/psd/resources.rb,
lib/psd/blend_mode.rb,
lib/psd/descriptor.rb,
lib/psd/layer/info.rb,
lib/psd/layer/mask.rb,
lib/psd/layer/name.rb,
lib/psd/layer_info.rb,
lib/psd/layer_mask.rb,
lib/psd/path_record.rb,
lib/psd/lazy_execute.rb,
lib/psd/nodes/search.rb,
lib/psd/channel_image.rb,
lib/psd/layer/helpers.rb,
lib/psd/renderer/mask.rb,
lib/psd/node_exporting.rb,
lib/psd/nodes/ancestry.rb,
lib/psd/image_modes/rgb.rb,
lib/psd/layer/exporting.rb,
lib/psd/renderer/canvas.rb,
lib/psd/image_modes/cmyk.rb,
lib/psd/renderer/blender.rb,
lib/psd/renderer/compose.rb,
lib/psd/resource_section.rb,
lib/psd/resources/guides.rb,
lib/psd/resources/slices.rb,
lib/psd/image_formats/raw.rb,
lib/psd/image_formats/rle.rb,
lib/psd/layer/blend_modes.rb,
lib/psd/layer_info/locked.rb,
lib/psd/nodes/has_children.rb,
lib/psd/layer/channel_image.rb,
lib/psd/layer_info/layer_id.rb,
lib/psd/layer_info/typetool.rb,
lib/psd/nodes/build_preview.rb,
lib/psd/image_modes/greyscale.rb,
lib/psd/layer/blending_ranges.rb,
lib/psd/layer/path_components.rb,
lib/psd/renderer/layer_styles.rb,
lib/psd/resources/layer_comps.rb,
lib/psd/layer_info/layer_group.rb,
lib/psd/layer_info/vector_mask.rb,
lib/psd/renderer/clipping_mask.rb,
lib/psd/image_formats/layer_raw.rb,
lib/psd/image_formats/layer_rle.rb,
lib/psd/layer_info/fill_opacity.rb,
lib/psd/layer_info/placed_layer.rb,
lib/psd/layer_info/unicode_name.rb,
lib/psd/layer_info/vector_mask_2.rb,
lib/psd/layer_info/vector_stroke.rb,
lib/psd/layer_info/object_effects.rb,
lib/psd/layer_info/legacy_typetool.rb,
lib/psd/layer_info/reference_point.rb,
lib/psd/renderer/canvas_management.rb,
lib/psd/layer/position_and_channels.rb,
lib/psd/layer_info/metadata_setting.rb,
lib/psd/layer_info/layer_name_source.rb,
lib/psd/layer_info/layer_section_divider.rb,
lib/psd/layer_info/vector_stroke_content.rb,
lib/psd/renderer/layer_styles/drop_shadow.rb,
lib/psd/layer_info/blend_clipping_elements.rb,
lib/psd/layer_info/blend_interior_elements.rb,
lib/psd/renderer/layer_styles/color_overlay.rb

Overview

Internal structure to help us build trees of a Photoshop documents. A lot of method names borrowed from the Ruby ancestry gem.

Defined Under Namespace

Modules: Color, Compose, HasChildren, Helpers, ImageFormat, ImageMode, Logger, NodeExporting, Section, Util Classes: BlendClippingElements, BlendInteriorElements, BlendMode, ChannelImage, Descriptor, DisabledLogger, File, FillOpacity, Header, Image, Layer, LayerID, LayerInfo, LayerMask, LayerNameSource, LayerSectionDivider, LayerStyles, LazyExecute, LegacyTypeTool, Locked, Mask, MetadataSetting, NestedLayerDivider, Node, ObjectEffects, PathRecord, PlacedLayer, ReferencePoint, Renderer, Resource, Resources, TypeTool, UnicodeName, VectorMask, VectorMask2, VectorStroke, VectorStrokeContent

Constant Summary collapse

VERSION =
"2.1.2"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from NodeExporting

#export_node, #hide_all_nodes

Methods included from Helpers

#actual_layers, #folders, #guides, #height, #layer_comps, #layers, #resource, #slices, #tree, #width

Methods included from Logger

included

Constructor Details

#initialize(file, opts = {}) ⇒ PSD

Create and store a reference to our PSD file



50
51
52
53
54
55
56
57
58
59
# File 'lib/psd.rb', line 50

def initialize(file, opts={})
  @file = PSD::File.new(file, 'rb')
  @file.seek 0 # If the file was previously used and not closed

  @opts = opts
  @header = nil
  @resources = nil
  @layer_mask = nil
  @parsed = false
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



28
29
30
# File 'lib/psd.rb', line 28

def file
  @file
end

#optsObject (readonly) Also known as: options

Returns the value of attribute opts.



28
29
30
# File 'lib/psd.rb', line 28

def opts
  @opts
end

Class Method Details

.open(filename, opts = {}, &block) ⇒ PSD

Opens the named file, parses it, and makes it available for reading. Then, closes it after you’re finished.

Parameters:

  • filename (String)

    the name of the file to open

Returns:

  • (PSD)

    the PSD object if no block was given, otherwise the value of the block



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/psd.rb', line 34

def self.open(filename, opts={}, &block)
  raise "Must supply a block. Otherwise, use PSD.new." unless block_given?

  psd = PSD.new(filename, opts)
  psd.parse!

  if 0 == block.arity
    psd.instance_eval(&block)
  else
    yield psd
  end
ensure
  psd.close if psd
end

Instance Method Details

#closeObject

Close the PSD file



62
63
64
# File 'lib/psd.rb', line 62

def close
  file.close unless file.closed?
end

#export(file) ⇒ Object

Export the current file to a new PSD. This may or may not work.



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/psd.rb', line 131

def export(file)
  parse! unless parsed?

  # Create our file for writing
  outfile = File.open(file, 'w')

  # Reset the file pointer
  @file.seek 0
  @header.write outfile
  @file.seek @header.num_bytes, IO::SEEK_CUR

  # Nothing in the header or resources we want to bother with changing
  # right now. Write it straight to file.
  outfile.write @file.read(@resources.end_of_section - @file.tell)

  # Now, the changeable part. Layers and masks.
  layer_mask.export(outfile)

  # And the rest of the file (merged image data)
  outfile.write @file.read
  outfile.flush
end

#headerObject

Get the Header, parsing it if needed.



86
87
88
89
90
91
# File 'lib/psd.rb', line 86

def header
  return @header if @header

  @header = Header.read(@file)
  PSD.logger.debug @header.inspect
end

#imageObject

Get the full size flattened preview Image.



115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/psd.rb', line 115

def image
  ensure_header
  ensure_resources
  ensure_layer_mask

  @image ||= (
    # The image is the last section in the file, so we don't have to
    # bother with skipping over the bytes to read more data.
    image = Image.new(@file, @header)
    LazyExecute.new(image, @file)
      .later(:parse)
      .ignore(:width, :height)
  )
end

#layer_maskObject

Get the LayerMask section. Ensures the header and resources have been parsed first since they are required.



107
108
109
110
111
112
# File 'lib/psd.rb', line 107

def layer_mask
  ensure_header
  ensure_resources

  @layer_mask ||= LayerMask.new(@file, @header, @opts).parse
end

#parse!Object

There is a specific order that must be followed when parsing the PSD. Sections can be skipped if needed. This method will parse all sections of the PSD.



69
70
71
72
73
74
75
76
77
78
# File 'lib/psd.rb', line 69

def parse!
  header
  resources
  layer_mask
  image
  
  @parsed = true

  return true
end

#parsed?Boolean

Has our PSD been parsed yet?

Returns:

  • (Boolean)


81
82
83
# File 'lib/psd.rb', line 81

def parsed?
  @parsed
end

#resourcesObject

Get the Resources section, parsing if needed.



94
95
96
97
98
99
100
101
102
103
# File 'lib/psd.rb', line 94

def resources
  return @resources unless @resources.nil?

  ensure_header

  @resources = Resources.new(@file)
  @resources.parse

  return @resources
end