Class: ComicWalker::ItemDecoder

Inherits:
Object
  • Object
show all
Defined in:
lib/comic_walker/item_decoder.rb,
lib/comic_walker/item_decoder/unknown.rb

Defined Under Namespace

Modules: Unknown

Instance Method Summary collapse

Constructor Details

#initialize(configuration_pack) ⇒ ItemDecoder

Returns a new instance of ItemDecoder.



6
7
8
9
10
11
12
13
14
# File 'lib/comic_walker/item_decoder.rb', line 6

def initialize(configuration_pack)
  fname = '0.dat'
  ct = configuration_pack['ct']
  st = configuration_pack['st']
  et = configuration_pack['et']
  @key1 = (ct + st + fname).unpack('C*')
  @key2 = (ct + fname + et).unpack('C*')
  @key3 = (fname + st + et).unpack('C*')
end

Instance Method Details

#decode(dat_path, img_path, b64data) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/comic_walker/item_decoder.rb', line 16

def decode(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*')

  src = Magick::Image.from_blob(blob).first
  width = src.columns
  height = src.rows
  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, 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