Sha256: 9e7c89af71f196a071a154e8338536703ff6a804bcdbfcc27ba64e6493d2594d

Contents?: true

Size: 1.46 KB

Versions: 3

Compression:

Stored size: 1.46 KB

Contents

class PSD::Image
  module Format
    # Parses an RLE compressed image
    module RLE
      private

      def parse_rle!
        @byte_counts = parse_byte_counts!
        parse_channel_data!
      end

      def parse_byte_counts!
        byte_counts = []
        channels.times do |i|
          height.times do |j|
            byte_counts << @file.read_short
          end
        end

        return byte_counts
      end

      def parse_channel_data!
        chan_pos = 0
        line_index = 0

        channels.times do |i|
          chan_pos = decode_rle_channel(chan_pos, line_index)
          line_index += height
        end
      end

      def decode_rle_channel(chan_pos, line_index)
        height.times do |j|
          byte_count = @byte_counts[line_index]
          line_index += 1
          start = @file.tell

          while @file.tell < start + byte_count
            len = @file.read(1).bytes.to_a[0]

            if len < 128
              len += 1
              (chan_pos...chan_pos+len).each do |k|
                @channel_data[k] = @file.read(1).bytes.to_a[0]
              end

              chan_pos += len
            elsif len > 128
              len ^= 0xff
              len += 2

              val = @file.read(1).bytes.to_a[0]
              (chan_pos...chan_pos+len).each do |k|
                @channel_data[k] = val
              end

              chan_pos += len
            end
          end
        end

        return chan_pos
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
psd-0.3.4 lib/psd/image_formats/rle.rb
psd-0.3.3 lib/psd/image_formats/rle.rb
psd-0.3.2 lib/psd/image_formats/rle.rb