SHARE
TWEET

RMXP PNG Writer

ForeverZer0 Mar 9th, 2012 88 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #===============================================================================
  2. # ** PNG Writer
  3. #===============================================================================
  4.  
  5. module Zlib
  6.   class PNG_Writer < GzipWriter
  7. #-------------------------------------------------------------------------------
  8.     def make_png(bitmap_Fx,mode)
  9.       @mode = mode
  10.       @bitmap_Fx = bitmap_Fx
  11.       self.write(make_header)
  12.       self.write(make_ihdr)
  13.       self.write(make_idat)
  14.       self.write(make_iend)
  15.     end
  16. #-------------------------------------------------------------------------------
  17.     def make_header
  18.       return [0x89,0x50,0x4e,0x47,0x0d,0x0a,0x1a,0x0a].pack("C*")
  19.     end
  20. #-------------------------------------------------------------------------------
  21.     def make_ihdr
  22.       ih_size = [13].pack("N")
  23.       ih_sign = "IHDR"
  24.       ih_width = [@bitmap_Fx.width].pack("N")
  25.       ih_height = [@bitmap_Fx.height].pack("N")
  26.       ih_bit_depth = [8].pack("C")
  27.       ih_color_type = [6].pack("C")
  28.       ih_compression_method = [0].pack("C")
  29.       ih_filter_method = [0].pack("C")
  30.       ih_interlace_method = [0].pack("C")
  31.       string = ih_sign + ih_width + ih_height + ih_bit_depth + ih_color_type +
  32.                ih_compression_method + ih_filter_method + ih_interlace_method
  33.       ih_crc = [Zlib.crc32(string)].pack("N")
  34.       return ih_size + string + ih_crc
  35.     end
  36. #-------------------------------------------------------------------------------
  37.     def make_idat
  38.       header = "\x49\x44\x41\x54"
  39.       data = make_bitmap_data
  40.       data = Zlib::Deflate.deflate(data, 8)
  41.       crc = [Zlib.crc32(header + data)].pack("N")
  42.       size = [data.length].pack("N")
  43.       return size + header + data + crc
  44.     end
  45. #-------------------------------------------------------------------------------
  46.     def make_bitmap_data1
  47.       data = []
  48.       (0...@bitmap_Fx.height).each {|y|
  49.         data.push(0)
  50.         (0...@bitmap_Fx.width).each {|x|
  51.           color = @bitmap_Fx.get_pixel(x, y)
  52.           data.push(color.red)
  53.           data.push(color.green)
  54.           data.push(color.blue)
  55.           data.push(color.alpha)
  56.         }
  57.       }
  58.       return data.pack("C*")
  59.     end
  60. #-------------------------------------------------------------------------------
  61.     def make_bitmap_data
  62.       gz = Zlib::GzipWriter.open('hoge.gz')
  63.       t_Fx = 0
  64.       data = []
  65.       (0...@bitmap_Fx.height).each {|y|
  66.         data.push(0)
  67.         (0...@bitmap_Fx.width).each {|x|
  68.           t_Fx += 1
  69.           Graphics.update if t_Fx % 10000 == 0
  70.           if t_Fx % 100000 == 0
  71.             s = data.pack("C*")
  72.             gz.write(s)
  73.             data.clear
  74.           end
  75.           color = @bitmap_Fx.get_pixel(x, y)
  76.           data.push(color.red)
  77.           data.push(color.green)
  78.           data.push(color.blue)
  79.           data.push(color.alpha)
  80.         end
  81.       end
  82.       gz.write(data.pack("C*"))
  83.       gz.close    
  84.       data.clear
  85.       gz = Zlib::GzipReader.open('hoge.gz')
  86.       data = gz.read
  87.       gz.close
  88.       File.delete('hoge.gz')
  89.       return data
  90.     end
  91. #-------------------------------------------------------------------------------
  92.     def make_iend
  93.       return [0].pack("N") + 'IEND' + [Zlib.crc32('IEND')].pack("N")
  94.     end
  95.   end
  96. end
  97.  
  98. #===============================================================================
  99. # ** Bitmap
  100. #===============================================================================
  101.  
  102. class Bitmap
  103.  
  104.   def make_png(name = 'bitmap', path = '', mode = 0)
  105.     make_dir(path) if path != ''
  106.     Zlib::PNG_Writer.open('temp.gz') {|gz| gz.make_png(self, mode) }
  107.     Zlib::GzipReader.open('temp.gz') {|gz| $read = gz.read }
  108.     f = File.open(path + name + '.png','wb')
  109.     f.write($read)
  110.     f.close
  111.     File.delete('temp.gz')
  112.   end
  113. #-------------------------------------------------------------------------------
  114.   def make_dir(path)
  115.     dir = path.split("/")
  116.     dir.each_index {|i|
  117.       unless dir == "."
  118.         add_dir = dir[0..i].join("/")
  119.         begin
  120.           Dir.mkdir(add_dir)
  121.         rescue
  122.         end
  123.       end
  124.     }
  125.   end
  126. end
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top