#!/usr/bin/ruby $: << File::join([File::dirname(__FILE__), "..", "..", "common", "ruby"]) require "bitmap.rb" class BitMapExtractor SUPPORT_SIZES = [ [128, 96], # subQCIF [160, 120], # QQVGA [176, 144], # QCIF [320, 240], # QVGA [352, 288], # CIF [640, 480], # VGA [1280, 1024], # SXGA [1600, 1200]] # UXGA def reset @cont_page_count = 0 @bitmap = nil @start_time = 0 @bitmaps = {} # Žž‚Ɖ摜 @raw_datas = {} end def initialize(options = {}) @max_cont_page = options[:max_cont_page].to_i || 7 @debug_out = options[:debug_out] || $stderr reset end attr_reader :bitmaps, :raw_datas def process(page) if @cont_page_count > 0 then if @bitmap then @bitmap.pixels << page.unpack("C*") end @cont_page_count -= 1 elsif page[0] == ?I then case page[1] when ?H @bitmap = Bitmap::new @cont_page_count = @max_cont_page @start_time = page[3..6].unpack("V").first @bitmap.pixels << page[7..-1].unpack("C*") when ?C @cont_page_count = @max_cont_page if @bitmap then @bitmap.pixels << page[2..-1].unpack("C*") end when ?F byte_count = page[7..10].unpack("V").first @debug_out.puts byte_count if @bitmap then @bitmap.pixels.flatten! if @bitmap.pixels.size > byte_count then raw_data = true SUPPORT_SIZES.each{|w, h| if byte_count == w * h * 2 then raw_data = false @bitmap.width = w @bitmap.height = h break end } unless raw_data then pixels_mod = [] (byte_count / 2).times{|i| pixel = [@bitmap.pixels.shift, @bitmap.pixels.shift] pixel16 = (pixel[1] << 8) | pixel[0] b = (pixel16 & 0b11111) * (1 << 3) pixel16 >>= 5 g = (pixel16 & 0b111111) * (1 << 2) pixel16 >>= 6 r = pixel16 * (1 << 3) pixels_mod << [r, g, b] #sum = (r + g + b) / 3 #pixels_mod << [sum, sum, sum] } @bitmap.pixels = pixels_mod @bitmap.bits = 24 @bitmaps[@start_time] = @bitmap.flip_vertical! else @raw_datas[@start_time] = @bitmap.pixels end end end @bitmap = nil end else return false end return true end end if __FILE__ == $0 then $stderr.puts "Usage #{__FILE__} log.dat" if ARGV.size < 1 then exit end options = { :raw_ext => "raw", :suffix => "time", :max_cont_page => 7} ARGV.each{|arg| next if arg !~ /--([^=]+)=/ options[$1.to_sym] = $' } file_name = ARGV.shift extractor = BitMapExtractor::new(options) open(file_name){|io| io.binmode page_count = -1 while !io.eof? page_count += 1 extractor.process(io.read(32)) end base_name = File.basename(file_name, ".*") extractor.bitmaps.each{|time, bitmap| open("#{base_name}_#{sprintf('%09d', time)}.bmp", "w"){|io_bitmap| io_bitmap.print bitmap } } i = 0 extractor.raw_datas.to_a.sort{|a, b| a[0] <=> b[0] }.each{|time, raw_data| suffix = sprintf('t_%09d', time) case options[:suffix] when "num" suffix = sprintf('_%05d', i) i += 1 end open("#{base_name}#{suffix}.#{options[:raw_ext]}", "w"){|io_raw| io_raw.print raw_data.pack("C*") } } } =begin open(file_name){|io| io.binmode count_table = {} while !io.eof? page = io.read(32) if count_table.include?(page[0].chr) then count_table[page[0].chr] += 1 else count_table[page[0].chr] = 1 end end p count_table } =end end