Sha256: 0846d92dae64760ba382cc04ea44e54a427cbc4787e66304d1ffc0f33dd2a036

Contents?: true

Size: 1.97 KB

Versions: 1

Compression:

Stored size: 1.97 KB

Contents

require 'RMagick'
require 'comic_walker/item_decoder/unknown'

module ComicWalker
  class ItemDecoder
    attr_reader :pages

    DEFAULT_CT = '20000101000000'
    DEFAULT_ST = '20000101000000'
    DEFAULT_ET = '99991231235959'

    def initialize(configuration_pack)
      fname = '0.dat'
      ct = configuration_pack['ct'] || DEFAULT_CT
      st = configuration_pack['st'] || DEFAULT_ST
      et = configuration_pack['et'] || DEFAULT_ET
      @key1 = (ct + st + fname).unpack('C*')
      @key2 = (ct + fname + et).unpack('C*')
      @key3 = (fname + st + et).unpack('C*')
      @pages = []
      configuration_pack['configuration']['contents'].each do |content|
        pages[content['index']-1] = content['file']
      end
      @file_info = {}
      @pages.each do |file|
        @file_info[file] = configuration_pack[file]
      end
    end

    def decode_b64(file, dat_path, img_path, b64data)
      bs = 128
      hs = 1024
      blob = Unknown.finish(@key1, hs,
        Unknown.decrypt(@key2, bs,
          Unknown.prepare(@key3,
            Base64.decode64(b64data).unpack('C*')
          )
        )
      ).pack('C*')
      decode(file, dat_path, img_path, blob)
    end

    def decode(file, dat_path, img_path, blob)
      src = Magick::Image.from_blob(blob).first
      width = src.columns
      height = src.rows
      page_info = @file_info[file]['FileLinkInfo']['PageLinkInfoList'].first['Page']
      dummy_height = page_info['DummyHeight']
      dummy_width = page_info['DummyWidth']
      t = dat_path.sub_ext('').to_s.chars.inject(0) { |acc, c| acc + c.ord }
      pat = t%4 + 1
      moves = Unknown.calculate_moves(width, height, 64, 64, pat)

      dst = Magick::Image.new(width - dummy_width, height - dummy_height)
      dst.format = 'jpeg'
      moves.each do |move|
        crop = src.excerpt(move[:destX], move[:destY], move[:width], move[:height])
        dst.composite!(crop, move[:srcX], move[:srcY], Magick::SrcOverCompositeOp)
      end
      dst.write(img_path.to_s)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
comic_walker-0.2.2 lib/comic_walker/item_decoder.rb