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

[转载发布] PNG文件输出[发布预备]

[复制链接]
累计送礼:
0 个
累计收礼:
0 个
  • TA的每日心情
    开心
    昨天 18:01
  • 签到天数: 114 天

    连续签到: 4 天

    [LV.6]常住居民II

    2338

    主题

    403

    回帖

    1万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    VIP
    6
    卡币
    10622
    OK点
    16
    推广点
    0
    同能卷
    0
    积分
    13391

    灌水之王

    发表于 2024-4-19 16:29:23 | 显示全部楼层 |阅读模式
    1. #==============================================================================# 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  endend#==============================================================================# 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  endend#==============================================================================# 快速存储Bitmap的Marshal(修改版)By 柳之一#==============================================================================class Font  def marshal_dump;end  def marshal_load(obj);endendclass 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  endend#==============================================================================# 回收站~#===============================================================================beginclass 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  endend=end复制代码
    复制代码
    用到了某柳的东西,感觉轮子这个还是不错的{/hx}
                 本帖来自P1论坛作者小传子,因Project1站服务器在国外有时候访问缓慢不方便作者交流学习,经联系P1站长fux2同意署名转载一起分享游戏制作经验,共同为国内独立游戏作者共同创造良好交流环境,原文地址:https://rpg.blue/forum.php?mod=viewthread&tid=89989  若有侵权,发帖作者可联系底部站长QQ在线咨询功能删除,谢谢。
    天天去同能,天天有童年!
    回复 送礼论坛版权

    使用道具 举报

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

    本版积分规则

    关闭

    幸运抽奖

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

    立即查看

    聊天机器人
    Loading...

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

    GMT+8, 2025-3-15 00:00 , Processed in 0.085317 second(s), 58 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2020, Tencent Cloud.

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