#--------------------------------------------------------------------------
# ● 分解gif图片
#--------------------------------------------------------------------------
def analyze_gif
@old_dir = Dir.pwd
Dir.chdir("GIF")
@total_gif = []
for g in Dir["*"]
suf = g.split(/\./)
if suf[1].is_a?(String) and suf[1].downcase == "gif"
@total_gif.push(g)
Dir.mkdir(suf[0]) unless Dir["*"].include? suf[0]
end
end
@total_gif.each{|file| self.readdata(file)}
Dir.chdir(@old_dir)
p '全部分解完毕,点击确定退出'
exit
end
#--------------------------------------------------------------------------
# ● 读取文件数据:成功返回@gif_infos,失败返回nil
#--------------------------------------------------------------------------
def readdata(filename)
# 检查是否有临时记忆文件
tmp_filename = File.basename(filename,".*")
unless Dir["~TEMP/#{tmp_filename}_infos.fmd"].empty?
begin
giffr = open("~TEMP/#{tmp_filename}_infos.fmd","rb")
tmp_infos = Marshal.load(giffr)
giffr.close
if Dir["~TEMP/#{tmp_filename}_*.png"].size == tmp_infos.total_frames
return tmp_infos
end
rescue
end
end
# 初始化数据
self.initial_var(filename)
# 打开文件
begin
@f = open(filename,"rb")
# 读取文件头
self.read_gifh
# 读取逻辑屏幕描述块
self.read_gifs
# 读取下一个字节
@temp = @f.read(1)[0]
# 循环读取每个图象描述块
while true
if @temp == 0x2c
# 读取图象描述块
self.read_gifi
end
if @temp == 0x21
# 读取图象扩展块
self.read_gife
end
if @temp == 0x3b
break
end
# 读取下一个字节
@temp = @f.read(1)[0]
end
# ※ 设置图象帧数
@gif_infos.total_frames = @frame_count
# ※ 写入图象分解数据
giffw = open("~TEMP/#{@filename}_infos.fmd","wb")
Marshal.dump(@gif_infos,giffw)
giffw.close
rescue
return nil
ensure
@f.close
end
return @gif_infos
end
#--------------------------------------------------------------------------
# ● 读取文件头
#--------------------------------------------------------------------------
def read_gifh
@gifh = @f.read(SIZE_GIFH)
if @gifh[0,3] != "GIF"
raise "不是GIF文件!"
end
if @gifh[3,3] != "87a" and @gifh[3,3] != "89a"
raise "不支持的版本!"
end
end
#--------------------------------------------------------------------------
# ● png数据块
#--------------------------------------------------------------------------
def make_png_idat
lz_data = []
if @_trans_index != nil
for i in 0...@png_data.size
lz_data.push 0
for j in @png_data
if j == @_trans_index
lz_data.push @_pal[j*3],@_pal[j*3+1],@_pal[j*3+2],0
else
lz_data.push @_pal[j*3],@_pal[j*3+1],@_pal[j*3+2],255
end
end
end
else
for i in 0...@png_data.size
lz_data.push 0
lz_data += @png_data
end
end
id_data = Zlib::Deflate.deflate(lz_data.pack("C*"),9)
id_size = [id_data.size].pack("N")
id_sign = "IDAT"
id_crc = [Zlib.crc32(id_sign + id_data)].pack("N")
return id_size + id_sign + id_data + id_crc
end
#--------------------------------------------------------------------------
# ● 预处理:建立临时文件夹
#--------------------------------------------------------------------------
unless Dir["*"].include?("~TEMP")
Dir.mkdir("~TEMP")
System.attrib("~TEMP","+h","+s")
end
#--------------------------------------------------------------------------
# ● 清空硬缓存
#--------------------------------------------------------------------------
def self.TmpFileclear
begin
Dir["~TEMP/*"].each{|filename| File.delete filename}
rescue
end
end
#--------------------------------------------------------------------------
# ● 预加载图片
#--------------------------------------------------------------------------
def self.pre_load(filename_arr)
filename_arr.each{|fn| GIF.readdata(fn)}
end
#--------------------------------------------------------------------------
# ● 获取图片信息
#--------------------------------------------------------------------------
def bitmap
@gif_infos
end
#--------------------------------------------------------------------------
# ● 设置图片文件名
#--------------------------------------------------------------------------
def bitmap=(filename)
@gif_infos = GIF.readdata(filename)
if @gif_infos != nil
@sp_arr = Array.new
basename = File.basename(filename,".*")
for i in 0...@gif_infos.total_frames
sp = Sprite.new(@viewport2)
sp.bitmap = Bitmap.new(sprintf("~TEMP/#{basename}_%02d.png",i + 1))
sp.visible = i == 0
sp.x = @gif_infos.frame_data.offset_x
sp.y = @gif_infos.frame_data.offset_y
@sp_arr << sp
end
@update_frame_count = 0
@current_show_frame = 0
@next_frame_counts =
(@gif_infos.frame_data[0].delay_time * Graphics.frame_rate / 100)
end
end
#--------------------------------------------------------------------------
# ● 定义x=
#--------------------------------------------------------------------------
def x=(x)
if @gif_infos.nil?
@x = 0
return
end
@x = x
for i in 0...@gif_infos.total_frames
@sp_arr.x = @gif_infos.frame_data.offset_x + @x
end
end
#--------------------------------------------------------------------------
# ● 定义y=
#--------------------------------------------------------------------------
def y=(y)
if @gif_infos.nil?
@y = 0
return
end
@y = y
for i in 0...@gif_infos.total_frames
@sp_arr.y = @gif_infos.frame_data.offset_y + @y
end
end
#--------------------------------------------------------------------------
# ● 定义z=
#--------------------------------------------------------------------------
def z=(z)
if @gif_infos.nil?
@z = 0
return
end
@z = z
for i in 0...@gif_infos.total_frames
@sp_arr.z = @z
end
end
#--------------------------------------------------------------------------
# ● 更新精灵
#--------------------------------------------------------------------------
def update
if @gif_infos.nil?
return
end
@update_frame_count += 1
if @update_frame_count >= @next_frame_counts
@current_show_frame = (@current_show_frame + 1) % @gif_infos.total_frames
case @gif_infos.frame_data[@current_show_frame - 1].disposal_method
when 0 # 不处理
when 1 # 图象保留在原处
when 2 # 当前图象消失
@sp_arr[@current_show_frame - 1].visible = false
when 3 # 尚不明 = =
end
@sp_arr[@current_show_frame].visible = true
if @current_show_frame == 0
@loop_counts -= 1 if @loop_counts > 0
if @loop_counts == 0
self.dispose
return
end
for i in 0...@sp_arr.size
@sp_arr.visible = i == @current_show_frame
end
end
@update_frame_count = 0
@next_frame_counts = (@gif_infos.frame_data[@current_show_frame].\
delay_time * Graphics.frame_rate / 100)
end
end