Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #===============================================================================
- # ** PNG Writer
- #===============================================================================
- module Zlib
- class PNG_Writer < 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
- #-------------------------------------------------------------------------------
- def make_header
- return [0x89,0x50,0x4e,0x47,0x0d,0x0a,0x1a,0x0a].pack("C*")
- end
- #-------------------------------------------------------------------------------
- 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
- #-------------------------------------------------------------------------------
- def make_idat
- header = "\x49\x44\x41\x54"
- data = make_bitmap_data
- data = Zlib::Deflate.deflate(data, 8)
- crc = [Zlib.crc32(header + data)].pack("N")
- size = [data.length].pack("N")
- return size + header + data + crc
- end
- #-------------------------------------------------------------------------------
- def make_bitmap_data1
- data = []
- (0...@bitmap_Fx.height).each {|y|
- data.push(0)
- (0...@bitmap_Fx.width).each {|x|
- color = @bitmap_Fx.get_pixel(x, y)
- data.push(color.red)
- data.push(color.green)
- data.push(color.blue)
- data.push(color.alpha)
- }
- }
- return data.pack("C*")
- end
- #-------------------------------------------------------------------------------
- def make_bitmap_data
- gz = Zlib::GzipWriter.open('hoge.gz')
- t_Fx = 0
- data = []
- (0...@bitmap_Fx.height).each {|y|
- data.push(0)
- (0...@bitmap_Fx.width).each {|x|
- t_Fx += 1
- Graphics.update if t_Fx % 10000 == 0
- if t_Fx % 100000 == 0
- s = data.pack("C*")
- gz.write(s)
- data.clear
- end
- color = @bitmap_Fx.get_pixel(x, y)
- data.push(color.red)
- data.push(color.green)
- data.push(color.blue)
- data.push(color.alpha)
- end
- end
- gz.write(data.pack("C*"))
- gz.close
- data.clear
- gz = Zlib::GzipReader.open('hoge.gz')
- data = gz.read
- gz.close
- File.delete('hoge.gz')
- return data
- end
- #-------------------------------------------------------------------------------
- def make_iend
- return [0].pack("N") + 'IEND' + [Zlib.crc32('IEND')].pack("N")
- end
- end
- end
- #===============================================================================
- # ** Bitmap
- #===============================================================================
- class Bitmap
- def make_png(name = 'bitmap', path = '', mode = 0)
- make_dir(path) if path != ''
- Zlib::PNG_Writer.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("/")
- dir.each_index {|i|
- unless dir == "."
- add_dir = dir[0..i].join("/")
- begin
- Dir.mkdir(add_dir)
- rescue
- end
- end
- }
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement