#=================================================  ==============================
#=================================================  ==============================
# Vx To Xp Char Converter
# Version 1.0
# Author game_guy
#-------------------------------------------------------------------------------
# Intro
# Converts the little vx guys to xp format.
#
# Features
# Converts the 12x8 frame chars (ones with 8 people)
# Converts the 3x4 frame chars (the single person)
#
# Instructions
# Just place character files in the Chars/12x8 folder for 12x8 chars.
# Place files in Chars/3x4 folder for 3x4 chars. 
# Then run the game, it'll convert it for you.
#
# Credits
# game_guy ~ for making it
# Fantasist ~ teaching me how to get files from folder, and how to use bitmap to
#             png code
# 66rpg ~ for their bitmap to png code
# 
#=================================================  ==============================
=begin
==================================================  ============================
                        Bitmap to PNG By 轮回者
==================================================  ============================
 对Bitmap对象直接使用
 
 bitmap_obj.make_png(name[, path])
 
 name:保存文件名
 path:保存路径
 感谢66、夏娜、金圭子的提醒和帮助!
   
==================================================  ============================
=end
module Zlib
  class Png_File < GzipWriter
    #--------------------------------------------------------------------------
    # ● 主处理
    #-------------------------------------------------------------------------- 
    def make_png(bitmap_Fx,mode)
      @mode = mode
      @bitmap_Fx = bitmap_Fx
      self.write(make_header)
      self.write(make_ihdr)
      self.write(make_idat)
      self.write(make_iend)
    end
    #--------------------------------------------------------------------------
    # ● PNG文件头数据块
    #--------------------------------------------------------------------------
    def make_header
      return [0x89,0x50,0x4e,0x47,0x0d,0x0a,0x1a,0x0a].pack("C*")
    end
    #--------------------------------------------------------------------------
    # ● PNG文件情报头数据块(IHDR)
    #-------------------------------------------------------------------------- 
    def make_ihdr
      ih_size = [13].pack("N")
      ih_sign = "IHDR"
      ih_width = [@bitmap_Fx.width].pack("N")
      ih_height = [@bitmap_Fx.height].pack("N")
      ih_bit_depth = [8].pack("C")
      ih_color_type = [6].pack("C")
      ih_compression_method = [0].pack("C")
      ih_filter_method = [0].pack("C")
      ih_interlace_method = [0].pack("C")
      string = ih_sign + ih_width + ih_height + ih_bit_depth + ih_color_type +
               ih_compression_method + ih_filter_method + ih_interlace_method
      ih_crc = [Zlib.crc32(string)].pack("N")
      return ih_size + string + ih_crc
    end
    #--------------------------------------------------------------------------
    # ● 生成图像数据(IDAT)
    #-------------------------------------------------------------------------- 
    def make_idat
      header = "\x49\x44\x41\x54"
      case @mode # 请54~
      when 1
        data = make_bitmap_data#1
      else
        data = make_bitmap_data
      end
      data = Zlib:

eflate.deflate(data, 8)
      crc = [Zlib.crc32(header + data)].pack("N")
      size = [data.length].pack("N")
      return size + header + data + crc
    end
    #--------------------------------------------------------------------------
    # ● 从Bitmap对象中生成图像数据 mode 1(请54~)
    #-------------------------------------------------------------------------- 
    def make_bitmap_data1
      w = @bitmap_Fx.width
      h = @bitmap_Fx.height
      data = []
      for y in 0...h
        data.push(0)
        for x in 0...w
          color = @bitmap_Fx.get_pixel(x, y)
          red = color.red
          green = color.green
          blue = color.blue
          alpha = color.alpha
          data.push(red)
          data.push(green)
          data.push(blue)
          data.push(alpha)
        end
      end
      return data.pack("C*")
    end
    #--------------------------------------------------------------------------
    # ● 从Bitmap对象中生成图像数据 mode 0
    #-------------------------------------------------------------------------- 
    def make_bitmap_data
      gz = Zlib::GzipWriter.open('hoge.gz')
      t_Fx = 0
      w = @bitmap_Fx.width
      h = @bitmap_Fx.height
      data = []
      for y in 0...h
        data.push(0)
        for x in 0...w
          t_Fx += 1
          if t_Fx % 10000 == 0
            Graphics.update
          end
          if t_Fx % 100000 == 0
            s = data.pack("C*")
            gz.write(s)
            data.clear
            #GC.start
          end
          color = @bitmap_Fx.get_pixel(x, y)
          red = color.red
          green = color.green
          blue = color.blue
          alpha = color.alpha
          data.push(red)
          data.push(green)
          data.push(blue)
          data.push(alpha)
        end
      end
      s = data.pack("C*")
      gz.write(s)
      gz.close    
      data.clear
      gz = Zlib::GzipReader.open('hoge.gz')
      data = gz.read
      gz.close
      File.delete('hoge.gz') 
      return data
    end
    #--------------------------------------------------------------------------
    # ● PNG文件尾数据块(IEND)
    #-------------------------------------------------------------------------- 
    def make_iend
      ie_size = [0].pack("N")
      ie_sign = "IEND"
      ie_crc = [Zlib.crc32(ie_sign)].pack("N")
      return ie_size + ie_sign + ie_crc
    end
  end
end
#=================================================  =============================
# ■ Bitmap
#------------------------------------------------------------------------------
#  关联到Bitmap。
#=================================================  =============================
class Bitmap
  #--------------------------------------------------------------------------
  # ● 关联
  #-------------------------------------------------------------------------- 
  def make_png(name="like", path="",mode=0)
    make_dir(path) if path != ""
    Zlib::Png_File.open("temp.gz") {|gz|
      gz.make_png(self,mode)
    }
    Zlib::GzipReader.open("temp.gz") {|gz|
      $read = gz.read
    }
    f = File.open(path + name + ".png","wb")
    f.write($read)
    f.close
    File.delete('temp.gz') 
    end
  #--------------------------------------------------------------------------
  # ● 生成保存路径
  #-------------------------------------------------------------------------- 
  def make_dir(path)
    dir = path.split("/")
    for i in 0...dir.size
      unless dir == "."
        add_dir = dir[0..i].join("/")
        begin
          Dir.mkdir(add_dir)
        rescue
        end
      end
    end
  end
end
module GameGuy
  def self.vxconvert(file)
    begin
      char = GameGuy.character(file, 0)
    rescue
      $skipped += 1
      return
    end
    width = char.width / 4
    height = char.height / 2
    vxwidth = width / 3
    index = 0
    xx = 0
    yy = 0
    unless FileTest.directory?("Converted/#{file}/")
      Dir.mkdir("Converted/#{file}/")
    end
    loop do
      bitmap = Bitmap.new(width + vxwidth, height)
      rect1 = Rect.new(xx+vxwidth, yy, vxwidth, height)
      bitmap.blt(0, 0, char, rect1)
      rect2 = Rect.new(xx, yy, width, height)
      bitmap.blt(vxwidth, 0, char, rect2)
      bitmap.make_png("#{file} #{index}", "Converted/#{file}/")
      bitmap.dispose
      bitmap = nil
      if index == 7 
        $converted += 1
        break
      end
      index += 1
      case index
      when 0,4
        xx = width*0
      when 1,5
        xx = width*1
      when 2,6
        xx = width*2
      when 3,7
        xx = width*3
      end
      if index == 4
        yy = height
      end
    end
  end
  def self.svxconvert(file)
    begin
      char = GameGuy.scharacter(file, 0)
    rescue
      $skipped += 1
      return
    end
    width = char.width / 3
    height = char.height
    bitmap = Bitmap.new(width * 4, height)
    rect1 = Rect.new(width, 0, width, height)
    bitmap.blt(0, 0, char, rect1)
    rect2 = Rect.new(0, 0, width * 3, height)
    bitmap.blt(width, 0, char, rect2)
    unless FileTest.directory?("Converted/#{file}/")
      Dir.mkdir("Converted/#{file}/")
    end
    bitmap.make_png("#{file}", "Converted/#{file}/")
    $converted += 1
  end
end
module GameGuy
    @cache = {}
    def self.load_bitmap(folder_name, filename, hue = 0)
      path = folder_name + filename
      if not @cache.include?(path) or @cache[path].disposed?
        if filename != ""
          @cache[path] = Bitmap.new(path)
        else
          @cache[path] = Bitmap.new(32, 32)
        end
      end
      if hue == 0
        @cache[path]
      else
        key = [path, hue]
        if not @cache.include?(key) or @cache[key].disposed?
          @cache[key] = @cache[path].clone
          @cache[key].hue_change(hue)
        end
        @cache[key]
      end
    end
    def self.character(filename, hue)
      self.load_bitmap("Chars/12x8/", filename, hue)
    end
    def self.scharacter(filename, hue)
      self.load_bitmap("Chars/3x4/", filename, hue)
    end
  end
begin
  
  unless FileTest.directory?("Converted")
    Dir.mkdir("Converted")
  end
  $time = Time.now
  $converted = 0
  $skipped = 0
  @names = []
  dir = Dir.new('Chars/12x8/')
  dir.entries.each {|file| next unless file.include?('.png')
  @names.push(file); GameGuy.character(file, 0)}
  for i in 0...@names.size
    GameGuy.vxconvert(@names[i])
  end
  @names = []
  dir = Dir.new('Chars/3x4/')
  dir.entries.each {|file| next unless file.include?('.png')
  @names.push(file); GameGuy.scharacter(file, 0)}
  for i in 0...@names.size
    GameGuy.svxconvert(@names[i])
  end
  print "Converted #{$converted} files in #{Time.now - $time} seconds" + "\n" +
        "Total Skipped Files: #{$skipped}"
  exit
end
Социальные закладки