赞 | 0 |
VIP | 298 |
好人卡 | 70 |
积分 | 8 |
经验 | 135648 |
最后登录 | 2018-10-11 |
在线时间 | 6267 小时 |
Lv2.观梦者
- 梦石
- 0
- 星屑
- 799
- 在线时间
- 6267 小时
- 注册时间
- 2006-6-7
- 帖子
- 8462
|
加入我们,或者,欢迎回来。
您需要 登录 才可以下载或查看,没有帐号?注册会员
x
- #==============================================================================
- # 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
复制代码 用到了某柳的东西,感觉轮子这个还是不错的{/hx} |
|