Skip to content

Commit

Permalink
Add global mask parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
meltingice committed Aug 2, 2013
1 parent 2293a2e commit 523e8ec
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 4 deletions.
3 changes: 2 additions & 1 deletion examples/export_image.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
require 'benchmark'
require './lib/psd'

psd = PSD.new('examples/images/example.psd')
file = ARGV[0] || 'examples/images/example.psd'
psd = PSD.new(file)

results = Benchmark.measure "Image exporting" do
psd.image.save_as_png 'output.png'
Expand Down
Binary file added examples/images/example-nocompat.psd
Binary file not shown.
9 changes: 6 additions & 3 deletions lib/psd.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@ def initialize(file, opts={})
# parse all sections of the PSD.
def parse!
header
PSD.logger.debug header.inspect

resources
layer_mask
image if @opts[:parse_image]
Expand All @@ -65,7 +63,10 @@ def parsed?

# Get the Header, parsing it if needed.
def header
@header ||= Header.read(@file)
return @header if @header

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

# Get the Resources section, parsing if needed.
Expand All @@ -77,6 +78,8 @@ def resources
@resources = Resources.new(@file)
@resources.parse

PSD.logger.debug @resources.inspect

return @resources.data
end

Expand Down
5 changes: 5 additions & 0 deletions lib/psd/image.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ def initialize(file, header)
@end_pos = @start_pos + @length

@pixel_data = []

PSD.logger.debug "Image: #{width}x#{height}, length = #{@length}, mode = #{@header.mode_name}, position = #{@start_pos}"
end

# Begins parsing the image by first figuring out the compression format used, and then
Expand All @@ -49,9 +51,12 @@ def parse

# ZIP not implemented
if [2, 3].include?(@compression)
PSD.logger.debug "Warning: ZIP image compression not supported yet. Skipping."
@file.seek @end_pos and return
end

PSD.logger.debug "Compression: id = #{@compression}, name = #{COMPRESSIONS[@compression]}"

parse_image_data!

return self
Expand Down
1 change: 1 addition & 0 deletions lib/psd/image_formats/rle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def parse_channel_data!
line_index = 0

channels.times do |i|
PSD.logger.debug "Parsing RLE channel ##{i}: position = #{chan_pos}, line = #{line_index}"
chan_pos = decode_rle_channel(chan_pos, line_index)
line_index += height
end
Expand Down
23 changes: 23 additions & 0 deletions lib/psd/layer_mask.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ def parse
layers.reverse!
group_layers

parse_global_mask

# Temporarily seek to the end of this section
@file.seek finish
end_section
Expand Down Expand Up @@ -102,5 +104,26 @@ def group_layers
end
end
end

def parse_global_mask
length = @file.read_int
return if length == 0

mask_end = @file.tell + length
PSD.logger.debug "Global Mask: length = #{length}"

@global_mask = {}
@global_mask[:overlay_color_space] = @file.read_short
@global_mask[:color_components] = 4.times.map { |i| @file.read_short >> 8 }
@global_mask[:opacity] = @file.read_short

# 0 = color selected, 1 = color protected, 128 = use value per layer
@global_mask[:kind] = @file.read(1).bytes.to_a[0]

PSD.logger.debug @global_mask

# Filler zeros
@file.seek mask_end
end
end
end

0 comments on commit 523e8ec

Please sign in to comment.