66RPG
本站首页
制作教程
文章作品
制作素材
原创游戏区
周边下载
本站论坛
 ※ 站内搜索
栏 目:
方 式:
关键词:
  
 ※ 教程分类
 RMXP 图文教学
 初级教学
 中级教学
 高级教学
 个人创意与研究
 RMXP 录像教学
 新人入门录像
 零散录像教学
 商业素材使用录像 ★
 周边教学
 美工 与 音乐
 RMXP 脚本发布
 游戏系统修改
 地图效果类
 战斗系统相关
 全新系统类
 API与高难度类
 RMVX 制作教学
 RMVX 初级教学
 RMVX 中级教程
 RMVX 高级教程
 RMVX 综合制作展
 ※ 无图目录 (按点击量横排序)


-- 66RPG全内容文字目录 --


 ※ 近期特色教学
PNG文件输出 ( 66RPG, RPG MAKER XP教程 )
 本站首页→制作教程→高级教学

PNG文件输出


教程作者:传说VS天涯
首发网址:点此进入本教学的原始帖
适宜用户:???
技术通用度:100
技术应用复杂度:70 (满分150分)
学习的理解难度:50 (满分150分)

 教学正文:

#==============================================================================
# PNG文件输出
#   用法:bitmap.save2png(filename)
# BY:轮回者
#==============================================================================

class Bitmap  
 
 # 是否自动颠倒上下?
 #   false时输出图像上下会颠倒,但并不能节省很多时间
 SWITCH_UP2DOWN = true
 
 # 存入PNG文件
 def save2png(filename)
   file = File.open(filename,"wb")
   file.write(make_png_header)
   file.write(make_png_ihdr)
   file.write(make_png_idat)
   file.write(make_png_iend)
   file.close
 end
 
 # PNG文件头数据块
 def make_png_header
   return [0x89,0x50,0x4e,0x47,0x0d,0x0a,0x1a,0x0a].pack("C*")
 end

 # PNG文件情报头数据块(IHDR)
 def make_png_ihdr
   ih_size = [13].pack("N")
   ih_sign = "IHDR"
   ih_width = [width].pack("N")
   ih_height = [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
 
 # PNG图像数据(IDAT)
 def make_png_idat
   header = "\x49\x44\x41\x54"
   data = SWITCH_UP2DOWN ? make_png_data : make_png_data2
   data = Zlib::Deflate.deflate(data, 8)
   crc = [Zlib.crc32(header + data)].pack("N")
   size = [data.length].pack("N")
   return size + header + data + crc
 end
 
 # PNG图像数据(点阵);自动颠倒上下
 def make_png_data
   data = get_data.unpack('x2aX2aX2ax2a'*height*width).to_s
   len = width * 4      
   for y in 0...height
     break if 2*y >= height - 1
     nth1 = y * len      
     nth2 = (height - 1 - y) * len      
     tStr = data[nth1,len]      
     data[nth1, len] = data[nth2, len]      
     data[nth2, len] = tStr
   end
   
   for y in 0...height
     nth = (height - 1 - y) * width * 4
     data.insert(nth,"\000")
   end
   return data
 end
 
 # PNG图像数据(点阵);不自动颠倒上下
 def make_png_data2
   data = get_data.unpack('x2aX2aX2ax2a'*height*width).to_s
   for y in 0...height
     nth = (height - 1 - y) * width * 4
     data.insert(nth,"\000")
   end
   return data
 end  
 
 # PNG文件尾数据块(IEND)
 def make_png_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
 
 # 获取数据
 def get_data
   data = "rgba" * width * height
   RtlMoveMemory_pi.call(data, address, data.length)
   return data
 end
end

#==============================================================================
# Bitmap类修改尝试
#==============================================================================

class Bitmap
 # 取得点(x,y)的颜色(Color)
 def get_pixel_plus(x, y)
   data = "rgba"
   nth = ((height - 1 - y) * width + x) * data.length
   RtlMoveMemory_pi.call(data, address + nth, data.length)    
   clr_ary = data.unpack('c*')
   return Color.new(clr_ary[2],clr_ary[1],clr_ary[0],clr_ary[3])
 end
 
 # 设定点(x,y)的颜色为 color(Color)
 def set_pixel_plus(x, y, color)
   data = [color.blue,color.green,color.red,color.alpha].pack('c*')
   nth = ((height - 1 - y) * width + x) * data.length
   RtlMoveMemory_ip.call(address + nth, data, data.length)
   return self
 end
end

#==============================================================================
# 快速存储Bitmap的Marshal(修改版)By 柳之一
#==============================================================================
class Font
 def marshal_dump;end
 def marshal_load(obj);end
end

class Bitmap
 attr_accessor :address
 # 初始化传送到内存的API函数
 RtlMoveMemory_pi = Win32API.new('kernel32', 'RtlMoveMemory', 'pii', 'i')
 RtlMoveMemory_ip = Win32API.new('kernel32', 'RtlMoveMemory', 'ipi', 'i')
 
 # 保存
 def _dump(limit)
   data = "rgba" * width * height
   RtlMoveMemory_pi.call(data, address, data.length)
   [width, height, Zlib::Deflate.deflate(data)].pack("LLa*") # 压缩
 end
 
 # 读取
 def self._load(str)
   w, h, zdata = str.unpack("LLa*"); b = new(w, h)
   RtlMoveMemory_ip.call(b.address, Zlib::Inflate.inflate(zdata), w * h * 4); b
 end
 
 # [[[bitmap.object_id * 2 + 16] + 8] + 16] == 数据的开头
 #
 def address
   @address = ini_address if @address.nil?
   return @address
 end
 
 def ini_address
   buffer, ad = "xxxx", object_id * 2 + 16
   RtlMoveMemory_pi.call(buffer, ad, 4); ad = buffer.unpack("L")[0] + 8
   RtlMoveMemory_pi.call(buffer, ad, 4); ad = buffer.unpack("L")[0] + 16
   RtlMoveMemory_pi.call(buffer, ad, 4); ad = buffer.unpack("L")[0]
   return ad
 end
end

#==============================================================================
# 回收站~
#==============================================================================

=begin
class Color_Plus
 # 初始化实例变量
 attr_accessor :red
 attr_accessor :green
 attr_accessor :blue
 attr_accessor :alpha

 # 初始化
 def initialize(red, green, blue, alpha=255)
   @red   = red
   @green = green
   @blue  = blue
   @alpha = alpha
 end
 
 # 设定所有属性
 def set(red, green, blue, alpha=255)
   @red   = red
   @green = green
   @blue  = blue
   @alpha = alpha
 end
end
=end


关键字:PNG 输出

发布日期:2008-6-27 13:42:06 点击量:1


 上一篇:低调发布 输入法即时消息显示脚本
 下一篇:没有下一条记录
关于我们
支援本站
友情连接
站点目录
站内搜索



WWW.66RPG.COM,2005-2013 ^o^

备案序号:京ICP备05035415号



 
Web www.66rpg.com bbs.66rpg.com