Class: PSD::ChannelImage
- Includes:
- ImageFormat::LayerRAW, ImageFormat::LayerRLE
- Defined in:
- lib/psd/channel_image.rb
Overview
Represents an image for a single layer, which differs slightly in format from the full size preview image.
Constant Summary
Constants inherited from Image
Instance Attribute Summary collapse
-
#height ⇒ Object
readonly
Returns the value of attribute height.
-
#mask_data ⇒ Object
readonly
Returns the value of attribute mask_data.
-
#width ⇒ Object
readonly
Returns the value of attribute width.
Attributes inherited from Image
#has_mask, #opacity, #pixel_data
Instance Method Summary collapse
- #channels ⇒ Object
-
#initialize(file, header, layer) ⇒ ChannelImage
constructor
A new instance of ChannelImage.
- #parse ⇒ Object
- #parse_image_data! ⇒ Object
- #parse_user_mask ⇒ Object
- #skip ⇒ Object
Methods included from Image::Export::PNG
#mask_to_png, #save_as_png, #to_png, #to_png_with_mask
Constructor Details
#initialize(file, header, layer) ⇒ ChannelImage
Returns a new instance of ChannelImage.
12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/psd/channel_image.rb', line 12 def initialize(file, header, layer) @layer = layer @width = @layer.width @height = @layer.height super(file, header) @channels_info = @layer.channels_info @has_mask = @layer.mask.width * @layer.mask.height > 0 @opacity = @layer.opacity / 255.0 @mask_data = [] end |
Instance Attribute Details
#height ⇒ Object (readonly)
Returns the value of attribute height.
10 11 12 |
# File 'lib/psd/channel_image.rb', line 10 def height @height end |
#mask_data ⇒ Object (readonly)
Returns the value of attribute mask_data.
10 11 12 |
# File 'lib/psd/channel_image.rb', line 10 def mask_data @mask_data end |
#width ⇒ Object (readonly)
Returns the value of attribute width.
10 11 12 |
# File 'lib/psd/channel_image.rb', line 10 def width @width end |
Instance Method Details
#channels ⇒ Object
33 34 35 |
# File 'lib/psd/channel_image.rb', line 33 def channels @layer.channels end |
#parse ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/psd/channel_image.rb', line 37 def parse PSD.logger.debug "Layer = #{@layer.name}, Size = #{width}x#{height}" PSD.logger.debug @channels_info @chan_pos = 0 @channels_info.each do |ch_info| @ch_info = ch_info if ch_info[:length] <= 0 parse_compression! and next end # If the ID of this current channel is -2, then we assume the dimensions # of the layer mask. if ch_info[:id] == -2 @width = @layer.mask.width @height = @layer.mask.height else @width = @layer.width @height = @layer.height end start = @file.tell PSD.logger.debug "Channel ##{ch_info[:id]}, length = #{ch_info[:length]}" parse_image_data! finish = @file.tell if finish != start + ch_info[:length] PSD.logger.error "Read incorrect number of bytes for channel ##{ch_info[:id]}. Expected = #{ch_info[:length]}, Actual = #{finish - start}" @file.seek start + ch_info[:length] end end @width = @layer.width @height = @layer.height if @channel_data.length != @length PSD.logger.error "#{@channel_data.length} read; expected #{@length}" end parse_user_mask process_image_data end |
#parse_image_data! ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/psd/channel_image.rb', line 82 def parse_image_data! @compression = parse_compression! case @compression when 0 then parse_raw! when 1 then parse_rle! when 2, 3 then parse_zip! else PSD.logger.error "Unknown image compression: #{@compression}. Attempting to skip." @file.seek(@end_pos) end end |
#parse_user_mask ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/psd/channel_image.rb', line 95 def parse_user_mask return unless has_mask? channel = @channels_info.select { |c| c[:id] == -2 }.first index = @channels_info.index { |c| c[:id] == -2 } return if channel.nil? start = @channel_length * index length = @layer.mask.width * @layer.mask.height PSD.logger.debug "Copying user mask: #{length} bytes at #{start}" @mask_data = @channel_data[start, length] if @mask_data.length != length PSD.logger.error "Mask length is incorrect. Expected = #{length}, Actual = #{@mask_data.length}" end end |
#skip ⇒ Object
26 27 28 29 30 31 |
# File 'lib/psd/channel_image.rb', line 26 def skip PSD.logger.debug "Skipping channel image data. Layer = #{@layer.name}" @channels_info.each do |ch| @file.seek ch[:length], IO::SEEK_CUR end end |