Class: PSD
- Inherits:
-
Object
- Object
- PSD
- 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/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/path_record.rb,
lib/psd/nodes/search.rb,
lib/psd/channel_image.rb,
lib/psd/node_exporting.rb,
lib/psd/nodes/ancestry.rb,
lib/psd/resource_section.rb,
lib/psd/nodes/has_children.rb,
lib/psd/layer_info/layer_id.rb,
lib/psd/layer_info/typetool.rb,
lib/psd/resources/layer_comps.rb,
lib/psd/layer_info/vector_mask.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/object_effects.rb,
lib/psd/layer_info/legacy_typetool.rb,
lib/psd/layer_info/reference_point.rb,
lib/psd/layer_info/layer_name_source.rb,
lib/psd/layer_info/layer_section_divider.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: HasChildren, Helpers, Logger, NodeExporting, Section Classes: BlendMode, ChannelImage, Color, Descriptor, DisabledLogger, File, FillOpacity, Header, Image, Layer, LayerID, LayerInfo, LayerMask, LayerNameSource, LayerSectionDivider, LegacyTypeTool, Mask, Node, ObjectEffects, PathRecord, PlacedLayer, ReferencePoint, Resource, Resources, TypeTool, UnicodeName, Util, VectorMask
Constant Summary collapse
- DEFAULTS =
{ parse_image: false, parse_layer_images: false }
- VERSION =
"0.4.2"
Instance Attribute Summary collapse
-
#file ⇒ Object
readonly
Returns the value of attribute file.
Class Method Summary collapse
-
.open(filename, opts = {}, &block) ⇒ PSD
Opens the named file, parses it, and makes it available for reading.
Instance Method Summary collapse
-
#close ⇒ Object
Close the PSD file.
-
#export(file) ⇒ Object
Export the current file to a new PSD.
-
#header ⇒ Object
Get the Header, parsing it if needed.
-
#image ⇒ Object
Get the full size flattened preview Image.
-
#initialize(file, opts = {}) ⇒ PSD
constructor
Create and store a reference to our PSD file.
-
#layer_mask ⇒ Object
Get the LayerMask section.
-
#parse! ⇒ Object
There is a specific order that must be followed when parsing the PSD.
-
#parsed? ⇒ Boolean
Has our PSD been parsed yet?.
-
#resources ⇒ Object
Get the Resources section, parsing if needed.
Methods included from NodeExporting
Methods included from Helpers
#actual_layers, #folders, #height, #layers, #tree, #width
Methods included from Logger
Constructor Details
#initialize(file, opts = {}) ⇒ PSD
Create and store a reference to our PSD file
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/psd.rb', line 52 def initialize(file, opts={}) @file = PSD::File.new(file, 'rb') @file.seek 0 # If the file was previously used and not closed @opts = DEFAULTS.merge(opts) @header = nil @resources = nil @layer_mask = nil @parsed = false end |
Instance Attribute Details
#file ⇒ Object (readonly)
Returns the value of attribute file.
31 32 33 |
# File 'lib/psd.rb', line 31 def file @file 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.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/psd.rb', line 36 def self.open(filename, opts={}, &block) psd = PSD.new(filename, opts) psd.parse! return psd unless block_given? if 0 == block.arity psd.instance_eval(&block) else yield psd end ensure psd.close if psd end |
Instance Method Details
#close ⇒ Object
Close the PSD file
64 65 66 |
# File 'lib/psd.rb', line 64 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.
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/psd.rb', line 128 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 |
#header ⇒ Object
Get the Header, parsing it if needed.
88 89 90 91 92 93 |
# File 'lib/psd.rb', line 88 def header return @header if @header @header = Header.read(@file) PSD.logger.debug @header.inspect end |
#image ⇒ Object
Get the full size flattened preview Image.
119 120 121 122 123 124 125 |
# File 'lib/psd.rb', line 119 def image ensure_header ensure_resources ensure_layer_mask @image ||= Image.new(@file, @header).parse end |
#layer_mask ⇒ Object
Get the LayerMask section. Ensures the header and resources have been parsed first since they are required.
111 112 113 114 115 116 |
# File 'lib/psd.rb', line 111 def layer_mask ensure_header ensure_resources @layer_mask ||= LayerMask.new(@file, @header).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.
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/psd.rb', line 71 def parse! header resources layer_mask image if @opts[:parse_image] @parsed = true return true end |
#parsed? ⇒ Boolean
Has our PSD been parsed yet?
83 84 85 |
# File 'lib/psd.rb', line 83 def parsed? @parsed end |
#resources ⇒ Object
Get the Resources section, parsing if needed.
96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/psd.rb', line 96 def resources return @resources unless @resources.nil? ensure_header @resources = Resources.new(@file) @resources.parse PSD.logger.debug @resources.data.inspect return @resources end |