扫描二维码关注官方公众号
返回列表
+ 发新帖
查看: 117|回复: 0

[转载发布] 将Bitmap对象输出成PNG文件 优化版

[复制链接]
累计送礼:
0 个
累计收礼:
0 个
  • TA的每日心情
    开心
    5 小时前
  • 签到天数: 114 天

    连续签到: 4 天

    [LV.6]常住居民II

    2338

    主题

    403

    回帖

    1万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    VIP
    6
    卡币
    10607
    OK点
    16
    推广点
    0
    同能卷
    0
    积分
    13376

    灌水之王

    发表于 2024-4-19 15:38:06 | 显示全部楼层 |阅读模式
    http://ftp2.66rpg.com/3/美术与技术/轮回者/TilesetMaker_Fx4.rar

    128m的机器2000*1000的图输出大约30s,
    应该不会再出现“脚本已备份”了。
    多谢66、金圭子和夏娜的提醒{/cy}
    现在主要是get_pixel的效率问题……管不了,不管了。
    1. #==============================================================================#               本脚本出自www.66rpg.com,转载请注明。#===============================================================================begin==============================================================================                        Bitmap to PNG By 轮回者============================================================================== 对Bitmap对象直接使用 bitmap_obj.make_png(name[, path]) name:保存文件名 path:保存路径 感谢66、夏娜、金圭子的提醒和帮助!   ===============================================================================endmodule Zlib  class Png_File < 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    #--------------------------------------------------------------------------    # ● PNG文件头数据块    #--------------------------------------------------------------------------    def make_header      return [0x89,0x50,0x4e,0x47,0x0d,0x0a,0x1a,0x0a].pack("C*")    end    #--------------------------------------------------------------------------    # ● PNG文件情报头数据块(IHDR)    #--------------------------------------------------------------------------     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    #--------------------------------------------------------------------------    # ● 生成图像数据(IDAT)    #--------------------------------------------------------------------------     def make_idat      header = "\x49\x44\x41\x54"      case @mode # 请54~      when 1        data = make_bitmap_data#1      else        data = make_bitmap_data      end      data = Zlib::Deflate.deflate(data, 8)      crc = [Zlib.crc32(header + data)].pack("N")      size = [data.length].pack("N")      return size + header + data + crc    end    #--------------------------------------------------------------------------    # ● 从Bitmap对象中生成图像数据 mode 1(请54~)    #--------------------------------------------------------------------------     def make_bitmap_data1      w = @bitmap_Fx.width      h = @bitmap_Fx.height      data = []      for y in 0...h        data.push(0)        for x in 0...w          color = @bitmap_Fx.get_pixel(x, y)          red = color.red          green = color.green          blue = color.blue          alpha = color.alpha          data.push(red)          data.push(green)          data.push(blue)          data.push(alpha)        end      end      return data.pack("C*")    end    #--------------------------------------------------------------------------    # ● 从Bitmap对象中生成图像数据 mode 0    #--------------------------------------------------------------------------     def make_bitmap_data      gz = Zlib::GzipWriter.open('hoge.gz')      t_Fx = 0      w = @bitmap_Fx.width      h = @bitmap_Fx.height      data = []      for y in 0...h        data.push(0)        for x in 0...w          t_Fx += 1          if t_Fx % 10000 == 0            Graphics.update          end          if t_Fx % 100000 == 0            s = data.pack("C*")            gz.write(s)            data.clear            #GC.start          end          color = @bitmap_Fx.get_pixel(x, y)          red = color.red          green = color.green          blue = color.blue          alpha = color.alpha          data.push(red)          data.push(green)          data.push(blue)          data.push(alpha)        end      end      s = data.pack("C*")      gz.write(s)      gz.close          data.clear      gz = Zlib::GzipReader.open('hoge.gz')      data = gz.read      gz.close      File.delete('hoge.gz')       return data    end    #--------------------------------------------------------------------------    # ● PNG文件尾数据块(IEND)    #--------------------------------------------------------------------------     def make_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  endend#==============================================================================# ■ Bitmap#------------------------------------------------------------------------------#  关联到Bitmap。#==============================================================================class Bitmap  #--------------------------------------------------------------------------  # ● 关联  #--------------------------------------------------------------------------   def make_png(name="like", path="",mode=0)    make_dir(path) if path != ""    Zlib::Png_File.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("/")    for i in 0...dir.size      unless dir == "."        add_dir = dir[0..i].join("/")        begin          Dir.mkdir(add_dir)        rescue        end      end    end  endend#==============================================================================#               本脚本出自www.66rpg.com,转载请注明。#==============================================================================复制代码
    复制代码


                  [本贴由 雷欧纳德 于 2006-11-28 17:05:45 进行了编辑]
                 本帖来自P1论坛作者轮回者,因Project1站服务器在国外有时候访问缓慢不方便作者交流学习,经联系P1站长fux2同意署名转载一起分享游戏制作经验,共同为国内独立游戏作者共同创造良好交流环境,原文地址:https://rpg.blue/forum.php?mod=viewthread&tid=36728  若有侵权,发帖作者可联系底部站长QQ在线咨询功能删除,谢谢。
    天天去同能,天天有童年!
    回复 送礼论坛版权

    使用道具 举报

    文明发言,和谐互动
    文明发言,和谐互动
    高级模式
    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    关闭

    幸运抽奖

    社区每日抽奖来袭,快来试试你是欧皇还是非酋~

    立即查看

    聊天机器人
    Loading...

    QQ|Archiver|手机版|小黑屋|同能RPG制作大师 ( 沪ICP备12027754号-3 )

    GMT+8, 2025-3-14 23:18 , Processed in 0.109235 second(s), 54 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2020, Tencent Cloud.

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