| 赞 | 2 |
| VIP | |
| 好人卡 | |
| 积分 | 1 |
| 经验 | |
| 最后登录 | 2020-7-27 |
| 在线时间 | 1379 小时 |
- 梦石
- 0
- 星屑
- 76
- 在线时间
- 1379 小时
- 注册时间
- 2012-7-5
- 回帖
- 1643

|
发表于 2015-4-22 22:39:01
|
显示全部楼层
本帖最后由 kuerlulu 于 2015-4-23 12:37 编辑
[fold=CACAO 的 save_png(必须)]
- #==============================================================================
- # ■ Bitmap
- #------------------------------------------------------------------------------
- # 位图的类。所谓位图即表示图像其本身。
- #==============================================================================
- class Bitmap
- #--------------------------------------------------------------------------
- # ● 保存成 png 格式
- # filename : 文件名
- # alpha : 透明通道有效
- #--------------------------------------------------------------------------
- def save_png(filename, alpha = true)
- sgnt = "\x89PNG\r\n\x1a\n"
- ihdr = _chunk('IHDR', [width,height,8,(alpha ? 6 : 2),0,0,0].pack('N2C5'))
- data = []
- height.times do |y|
- data << 0
- width.times do |x|
- color = self.get_pixel(x, y)
- data << color.red << color.green << color.blue
- data << color.alpha if alpha
- end
- end
- idat = _chunk('IDAT', Zlib::Deflate.deflate(data.pack('C*')))
- iend = _chunk('IEND', "")
- File.open(filename, 'wb') do |file|
- file.write(sgnt)
- file.write(ihdr)
- file.write(idat)
- file.write(iend)
- end
- end
- private
- #--------------------------------------------------------------------------
- # ● チャンクの作成
- # name : チャンク名
- # data : チャンクデータ
- #--------------------------------------------------------------------------
- def _chunk(name, data)
- return [data.size, name, data, Zlib.crc32(name + data)].pack('NA4A*N')
- end
- end
复制代码 [/fold]
[fold=主代码]- # 2x2 转 3x3
- def conv_2x2_3x3(b)
- a = Bitmap.new 96, 96 # 3x3
- a.blt( 0, 0, b, Rect.new( 0, 0, 48, 48))
- a.blt(48, 0, b, Rect.new(16, 0, 48, 48))
- a.blt( 0, 48, b, Rect.new( 0, 16, 48, 48))
- a.blt(48, 48, b, Rect.new(16, 16, 48, 48))
- return a
- end
- # (动画)自动地图元件(vx/a -> xp) # n 帧数
- def conv_tileset(c, n = 1)
- w = c.width / n
- a = Bitmap.new 96 * n, 128 # 3x4xn
- b = Bitmap.new 64, 64 # 2x2
- n.times do |x|
- a.blt(96 * x, 0, c, Rect.new(64 * x, 0, 32, 32)) # A
- a.fill_rect(96 * x + 32, 0, 32, 32, c.get_pixel(64 * x, 0))# B
- a.blt(96 * x + 64, 0, c, Rect.new(32, 0, 32, 32)) # C
- b.blt(0, 0, c, Rect.new(64 * x, 32, 64, 64)) # 2x2
- a.blt(96 * x, 32, conv_2x2_3x3(b), Rect.new(0, 0, 96, 96)) # combine
- b.clear
- end
- b.dispose
- return a
- end
- =begin 为了替换"砖块"随手写的【最终目的是装逼+省字数
- class Bitmap
- def reblt(x, y, b, r)
- self.fill_rect(x, y, r.width, r.height, Color.new(0, 0, 0, 0))
- self.blt(x, y, b, r)
- end
- end
- a = Bitmap.new("di.png")
- b = Bitmap.new("ss.png")
- 3.times do |x|
- b.reblt(64 * x, 0, a, Rect.new(0, 24, 32, 8))
- b.reblt(64 * x, 32, a, Rect.new(0, 88, 64, 8))
- end
- #b.save_png("mm.png")
- =end
- conv_tileset(Bitmap.new("mm.png"), 3).save_png("ssss.png")
复制代码 [/fold]
[fold=主代码的食用姿势]
[pre lang="ruby"]conv_tileset(Bitmap.new("文件路径"), 帧数(默认为1)).save_png("输出路径")[/pre]
[/fold]
另外这是我在水区发的一些已经转好的素材和代码 |
评分
-
查看全部评分
|