Class: PSD

Inherits:
Object
  • Object
show all
Includes:
Helpers, Logger, Slices
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/slice.rb,
lib/psd/header.rb,
lib/psd/logger.rb,
lib/psd/slices.rb,
lib/psd/helpers.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/nodes/root.rb,
lib/psd/nodes/group.rb,
lib/psd/nodes/layer.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/nodes/locking.rb,
lib/psd/renderer/mask.rb,
lib/psd/nodes/ancestry.rb,
lib/psd/resources/base.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/curves.rb,
lib/psd/layer/info/invert.rb,
lib/psd/layer/info/levels.rb,
lib/psd/layer/info/locked.rb,
lib/psd/nodes/layer_comps.rb,
lib/psd/layer/info/pattern.rb,
lib/psd/layer/channel_image.rb,
lib/psd/layer/info/exposure.rb,
lib/psd/layer/info/knockout.rb,
lib/psd/layer/info/layer_id.rb,
lib/psd/layer/info/typetool.rb,
lib/psd/layer/info/vibrance.rb,
lib/psd/nodes/build_preview.rb,
lib/psd/resources/work_path.rb,
lib/psd/layer/info/posterize.rb,
lib/psd/layer/info/threshold.rb,
lib/psd/renderer/mask_canvas.rb,
lib/psd/resources/saved_path.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/renderer/vector_shape.rb,
lib/psd/resources/layer_comps.rb,
lib/psd/layer/info/black_white.rb,
lib/psd/layer/info/layer_group.rb,
lib/psd/layer/info/sheet_color.rb,
lib/psd/layer/info/solid_color.rb,
lib/psd/layer/info/vector_mask.rb,
lib/psd/renderer/cairo_helpers.rb,
lib/psd/renderer/clipping_mask.rb,
lib/psd/resources/xmp_metadata.rb,
lib/psd/image_formats/layer_raw.rb,
lib/psd/image_formats/layer_rle.rb,
lib/psd/layer/info/color_lookup.rb,
lib/psd/layer/info/fill_opacity.rb,
lib/psd/layer/info/gradient_map.rb,
lib/psd/layer/info/pattern_fill.rb,
lib/psd/layer/info/photo_filter.rb,
lib/psd/layer/info/placed_layer.rb,
lib/psd/layer/info/unicode_name.rb,
lib/psd/layer/info/channel_mixer.rb,
lib/psd/layer/info/color_balance.rb,
lib/psd/layer/info/effects_layer.rb,
lib/psd/layer/info/gradient_fill.rb,
lib/psd/layer/info/layer_effects.rb,
lib/psd/layer/info/vector_stroke.rb,
lib/psd/layer/info/hue_saturation.rb,
lib/psd/layer/info/object_effects.rb,
lib/psd/resources/resolution_info.rb,
lib/psd/layer/info/legacy_typetool.rb,
lib/psd/layer/info/reference_point.rb,
lib/psd/layer/info/selective_color.rb,
lib/psd/renderer/canvas_management.rb,
lib/psd/layer/info/metadata_setting.rb,
lib/psd/layer/position_and_channels.rb,
lib/psd/layer/info/content_generator.rb,
lib/psd/layer/info/layer_name_source.rb,
lib/psd/layer/info/vector_origination.rb,
lib/psd/layer/info/brightness_contrast.rb,
lib/psd/layer/info/layer_section_divider.rb,
lib/psd/layer/info/vector_stroke_content.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,
lib/psd/layer/info/layer_mask_as_global_mask.rb,
lib/psd/layer/info/transparency_shapes_layer.rb,
lib/psd/layer/info/vector_mask_as_global_mask.rb,
lib/psd/layer/info/channel_blending_restrictions.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, Helpers, ImageFormat, ImageMode, Logger, Node, Slices, Util Classes: BlackWhite, BlendClippingElements, BlendInteriorElements, BlendMode, BrightnessContrast, ChannelBlendingRestrictions, ChannelImage, ChannelMixer, ColorBalance, ColorLookup, ContentGenerator, Curves, Descriptor, DisabledLogger, EffectsLayer, Exposure, File, FillOpacity, GradientFill, GradientMap, Header, HueSaturation, Image, Invert, Knockout, Layer, LayerEffects, LayerID, LayerInfo, LayerMask, LayerMaskAsGlobalMask, LayerNameSource, LayerSectionDivider, LayerStyles, LazyExecute, LegacyTypeTool, Levels, Locked, Mask, MetadataSetting, NestedLayerDivider, ObjectEffects, PathRecord, Pattern, PatternFill, PhotoFilter, PlacedLayer, Posterize, ReferencePoint, Renderer, Resource, Resources, SelectiveColor, SheetColor, Slice, SolidColor, Threshold, TransparencyShapesLayer, TypeTool, UnicodeName, VectorMask, VectorMaskAsGlobalMask, VectorOrigination, VectorStroke, VectorStrokeContent, Vibrance

Constant Summary collapse

VERSION =
"3.9.0"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Slices

#slice_by_id, #slices_by_name

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


58
59
60
61
62
63
64
65
66
67
# File 'lib/psd.rb', line 58

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


36
37
38
# File 'lib/psd.rb', line 36

def file
  @file
end

#optsObject (readonly) Also known as: options

Returns the value of attribute opts


36
37
38
# File 'lib/psd.rb', line 36

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


42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/psd.rb', line 42

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


70
71
72
# File 'lib/psd.rb', line 70

def close
  file.close unless file.closed?
end

#headerObject

Get the Header, parsing it if needed.


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

def header
  return @header if @header

  @header = Header.new(@file)
  @header.parse!

  PSD.logger.debug @header.inspect
end

#imageObject

Get the full size flattened preview Image.


125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/psd.rb', line 125

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.


117
118
119
120
121
122
# File 'lib/psd.rb', line 117

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.


77
78
79
80
81
82
83
84
85
86
# File 'lib/psd.rb', line 77

def parse!
  header
  resources
  layer_mask
  image
  
  @parsed = true

  return true
end

#parsed?Boolean

Has our PSD been parsed yet?

Returns:

  • (Boolean)

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

def parsed?
  @parsed
end

#resourcesObject

Get the Resources section, parsing if needed.


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

def resources
  return @resources unless @resources.nil?

  ensure_header

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

  return @resources
end