设为首页收藏本站|繁體中文

Project1

 找回密码
 注册会员
搜索
查看: 7901|回复: 18
打印 上一主题 下一主题

[原创发布] PNG文件输出[发布预备]

 关闭 [复制链接]

Lv2.观梦者


  • 更新完成啦

梦石
0
星屑
774
在线时间
6267 小时
注册时间
2006-6-7
帖子
8462
跳转到指定楼层
1
发表于 2008-6-14 05:05:10 | 显示全部楼层 回帖奖励 |倒序浏览 |阅读模式

加入我们,或者,欢迎回来。

您需要 登录 才可以下载或查看,没有帐号?注册会员

x
  1. #==============================================================================
  2. # PNG文件输出
  3. #   用法:bitmap.save2png(filename)
  4. # BY:轮回者
  5. #==============================================================================

  6. class Bitmap  
  7.   
  8.   # 是否自动颠倒上下?
  9.   #   false时输出图像上下会颠倒,但并不能节省很多时间
  10.   SWITCH_UP2DOWN = true
  11.   
  12.   # 存入PNG文件
  13.   def save2png(filename)
  14.     file = File.open(filename,"wb")
  15.     file.write(make_png_header)
  16.     file.write(make_png_ihdr)
  17.     file.write(make_png_idat)
  18.     file.write(make_png_iend)
  19.     file.close
  20.   end
  21.   
  22.   # PNG文件头数据块
  23.   def make_png_header
  24.     return [0x89,0x50,0x4e,0x47,0x0d,0x0a,0x1a,0x0a].pack("C*")
  25.   end

  26.   # PNG文件情报头数据块(IHDR)
  27.   def make_png_ihdr
  28.     ih_size = [13].pack("N")
  29.     ih_sign = "IHDR"
  30.     ih_width = [width].pack("N")
  31.     ih_height = [height].pack("N")
  32.     ih_bit_depth = [8].pack("C")
  33.     ih_color_type = [6].pack("C")
  34.     ih_compression_method = [0].pack("C")
  35.     ih_filter_method = [0].pack("C")
  36.     ih_interlace_method = [0].pack("C")
  37.     string = ih_sign + ih_width + ih_height + ih_bit_depth + ih_color_type +
  38.             ih_compression_method + ih_filter_method + ih_interlace_method
  39.     ih_crc = [Zlib.crc32(string)].pack("N")
  40.     return ih_size + string + ih_crc
  41.   end
  42.   
  43.   # PNG图像数据(IDAT)
  44.   def make_png_idat
  45.     header = "\x49\x44\x41\x54"
  46.     data = SWITCH_UP2DOWN ? make_png_data : make_png_data2
  47.     data = Zlib::Deflate.deflate(data, 8)
  48.     crc = [Zlib.crc32(header + data)].pack("N")
  49.     size = [data.length].pack("N")
  50.     return size + header + data + crc
  51.   end
  52.   
  53.   # PNG图像数据(点阵);自动颠倒上下
  54.   def make_png_data
  55.     data = get_data.unpack('x2aX2aX2ax2a'*height*width).to_s
  56.     len = width * 4      
  57.     for y in 0...height
  58.       break if 2*y >= height - 1
  59.       nth1 = y * len      
  60.       nth2 = (height - 1 - y) * len      
  61.       tStr = data[nth1,len]      
  62.       data[nth1, len] = data[nth2, len]      
  63.       data[nth2, len] = tStr
  64.     end
  65.    
  66.     for y in 0...height
  67.       nth = (height - 1 - y) * width * 4
  68.       data.insert(nth,"\000")
  69.     end
  70.     return data
  71.   end
  72.   
  73.   # PNG图像数据(点阵);不自动颠倒上下
  74.   def make_png_data2
  75.     data = get_data.unpack('x2aX2aX2ax2a'*height*width).to_s
  76.     for y in 0...height
  77.       nth = (height - 1 - y) * width * 4
  78.       data.insert(nth,"\000")
  79.     end
  80.     return data
  81.   end  
  82.   
  83.   # PNG文件尾数据块(IEND)
  84.   def make_png_iend
  85.     ie_size = [0].pack("N")
  86.     ie_sign = "IEND"
  87.     ie_crc = [Zlib.crc32(ie_sign)].pack("N")
  88.     return ie_size + ie_sign + ie_crc
  89.   end
  90.   
  91.   # 获取数据
  92.   def get_data
  93.     data = "rgba" * width * height
  94.     RtlMoveMemory_pi.call(data, address, data.length)
  95.     return data
  96.   end
  97. end

  98. #==============================================================================
  99. # Bitmap类修改尝试
  100. #==============================================================================

  101. class Bitmap
  102.   # 取得点(x,y)的颜色(Color)
  103.   def get_pixel_plus(x, y)
  104.     data = "rgba"
  105.     nth = ((height - 1 - y) * width + x) * data.length
  106.     RtlMoveMemory_pi.call(data, address + nth, data.length)   
  107.     clr_ary = data.unpack('c*')
  108.     return Color.new(clr_ary[2],clr_ary[1],clr_ary[0],clr_ary[3])
  109.   end
  110.   
  111.   # 设定点(x,y)的颜色为 color(Color)
  112.   def set_pixel_plus(x, y, color)
  113.     data = [color.blue,color.green,color.red,color.alpha].pack('c*')
  114.     nth = ((height - 1 - y) * width + x) * data.length
  115.     RtlMoveMemory_ip.call(address + nth, data, data.length)
  116.     return self
  117.   end
  118. end

  119. #==============================================================================
  120. # 快速存储Bitmap的Marshal(修改版)By 柳之一
  121. #==============================================================================
  122. class Font
  123.   def marshal_dump;end
  124.   def marshal_load(obj);end
  125. end

  126. class Bitmap
  127.   attr_accessor :address
  128.   # 初始化传送到内存的API函数
  129.   RtlMoveMemory_pi = Win32API.new('kernel32', 'RtlMoveMemory', 'pii', 'i')
  130.   RtlMoveMemory_ip = Win32API.new('kernel32', 'RtlMoveMemory', 'ipi', 'i')
  131.   
  132.   # 保存
  133.   def _dump(limit)
  134.     data = "rgba" * width * height
  135.     RtlMoveMemory_pi.call(data, address, data.length)
  136.     [width, height, Zlib::Deflate.deflate(data)].pack("LLa*") # 压缩
  137.   end
  138.   
  139.   # 读取
  140.   def self._load(str)
  141.     w, h, zdata = str.unpack("LLa*"); b = new(w, h)
  142.     RtlMoveMemory_ip.call(b.address, Zlib::Inflate.inflate(zdata), w * h * 4); b
  143.   end
  144.   
  145.   # [[[bitmap.object_id * 2 + 16] + 8] + 16] == 数据的开头
  146.   #
  147.   def address
  148.     @address = ini_address if @address.nil?
  149.     return @address
  150.   end
  151.   
  152.   def ini_address
  153.     buffer, ad = "xxxx", object_id * 2 + 16
  154.     RtlMoveMemory_pi.call(buffer, ad, 4); ad = buffer.unpack("L")[0] + 8
  155.     RtlMoveMemory_pi.call(buffer, ad, 4); ad = buffer.unpack("L")[0] + 16
  156.     RtlMoveMemory_pi.call(buffer, ad, 4); ad = buffer.unpack("L")[0]
  157.     return ad
  158.   end
  159. end

  160. #==============================================================================
  161. # 回收站~
  162. #==============================================================================

  163. =begin
  164. class Color_Plus
  165.   # 初始化实例变量
  166.   attr_accessor :red
  167.   attr_accessor :green
  168.   attr_accessor :blue
  169.   attr_accessor :alpha

  170.   # 初始化
  171.   def initialize(red, green, blue, alpha=255)
  172.     @red   = red
  173.     @green = green
  174.     @blue  = blue
  175.     @alpha = alpha
  176.   end
  177.   
  178.   # 设定所有属性
  179.   def set(red, green, blue, alpha=255)
  180.     @red   = red
  181.     @green = green
  182.     @blue  = blue
  183.     @alpha = alpha
  184.   end
  185. end
  186. =end

复制代码
用到了某柳的东西,感觉轮子这个还是不错的{/hx}
您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

拿上你的纸笔,建造一个属于你的梦想世界,加入吧。
 注册会员
找回密码

站长信箱:[email protected]|手机版|小黑屋|无图版|Project1游戏制作

GMT+8, 2024-5-29 23:01

Powered by Discuz! X3.1

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表