Skip to content

Commit

Permalink
Add the ability to parse resource data. Also adds additional logging …
Browse files Browse the repository at this point in the history
…in descriptors.
  • Loading branch information
meltingice committed Aug 5, 2013
1 parent 752d94e commit 4a35c8b
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 20 deletions.
13 changes: 10 additions & 3 deletions lib/psd/descriptor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ def initialize(file)
# a variable number of items in the descriptor. We return the Hash that represents
# the full data structure.
def parse
@data[:class] = parse_class
PSD.logger.debug "Descriptor: pos = #{@file.tell}"

@data[:class] = parse_class
num_items = @file.read_int

PSD.logger.debug "Class = #{@data[:class]}, Item count = #{num_items}"

num_items.times do |i|
id, value = parse_key_item
@data[id] = value
Expand All @@ -35,23 +39,26 @@ def parse_class

def parse_id
len = @file.read_int
len == 0 ? @file.read_int : @file.read_string(len)
len == 0 ? @file.read_string(4) : @file.read_string(len)
end

def parse_key_item
id = parse_id
PSD.logger.debug "Key = #{id}"

value = parse_item

return id, value
end

def parse_item(type = nil)
type = @file.read_string(4) if type.nil?
PSD.logger.debug "Type = #{type}"

value = case type
when 'bool' then parse_boolean
when 'type', 'GlbC' then parse_class
when 'Objc', 'GlbO' then parse
when 'Objc', 'GlbO' then Descriptor.new(@file).parse
when 'doub' then parse_double
when 'enum' then parse_enum
when 'alis' then parse_alias
Expand Down
28 changes: 13 additions & 15 deletions lib/psd/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,23 @@ class PSD
#
# Most of the resources are options/preferences set by the user
# or automatically by Photoshop.
class Resource < BinData::Record
endian :big
class Resource
attr_reader :id, :name, :size
attr_accessor :data

string :type, read_length: 4
uint16 :id
uint8 :name_len
stringz :name, read_length: :name_length
uint32 :res_size
def initialize(file)
@file = file
@data = {}
end

skip length: :resource_size
def parse
@file.seek 4, IO::SEEK_CUR # Type, always 8BIM
@id = @file.read_short

#---
# Really weird padding business
def name_length
Util.pad2(name_len + 1) - 1
end
name_length = Util.pad2(@file.read(1).bytes.to_a[0] + 1) - 1
@name = @file.read_string(name_length)

def resource_size
Util.pad2(res_size)
@size = Util.pad2(@file.read_int)
end
end
end
21 changes: 21 additions & 0 deletions lib/psd/resource_section.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class PSD
class Resource
class Section
def self.factory(file, resource)
Section.constants.each do |c|
section = Section.const_get(c)
next unless section.id == resource.id

return section.new(file, resource).parse
end

return nil
end

def initialize(file, resource)
@file = file
@resource = resource
end
end
end
end
17 changes: 15 additions & 2 deletions lib/psd/resources.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Resources

def initialize(file)
@file = file
@resources = []
@resources = {}
@length = nil
end

Expand All @@ -21,7 +21,16 @@ def parse

while n > 0
pos = @file.tell
@resources << PSD::Resource.read(@file)

resource = Resource.new(@file)
resource.parse

resource_end = @file.tell + resource.size

Resource::Section.factory(@file, resource)
@resources[resource.id] = resource

@file.seek resource_end
n -= @file.tell - pos
end

Expand All @@ -37,6 +46,10 @@ def skip
@file.seek length, IO::SEEK_CUR
end

def [](id)
@resources[id]
end

private

def length
Expand Down
15 changes: 15 additions & 0 deletions lib/psd/resources/layer_comps.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class PSD
class Resource
class Section
class LayerComps < Section
def self.id; 1065; end

def parse
# Descriptor version
@file.seek 4, IO::SEEK_CUR
@resource.data = Descriptor.new(@file).parse
end
end
end
end
end

0 comments on commit 4a35c8b

Please sign in to comment.