show 関数

ダウンロード

require "zlib"
def show(image)
 def chunk(type, data)
   [data.bytesize, type, data, Zlib.crc32(type + data)].pack("NA4A*N")
 end
 def make_png(raw_data)
   height = raw_data.size
   width = raw_data[0].size
   s = "\x89PNG\r\n\x1a\n".force_encoding("ascii-8bit")
   s1 = chunk("IHDR", [width, height, 8, 2, 0, 0, 0].pack("NNCCCCC"))
   s2 = chunk("IDAT", Zlib::Deflate.deflate(raw_data.map {|line|
                        ([0] + line.flatten).pack("C*") }.join))
   s3 = chunk("IEND", "")
   s + s1 + s2 + s3
 end
 def to_8bit(c)
   if c.is_a?(Array)
     if c[0].is_a?(Numeric)
       c.map{|e| (e * 255).to_i }
     else
       raise "show(): bad array element " + c.to_s
     end
   else
     [(c * 255).to_i] * 3
   end
 end
 if !image.is_a?(Array)
   puts "show(): not an array" ; false
 else
   if !image[0].is_a?(Array)
     image = [image]
   end
   h = image.size
   s = h < 10 ? 20 : h < 60 ? 10 : h < 400 ? 400 / h : 1
   data = image.flat_map{|r| [r.flat_map{|c| [to_8bit(c)] * s }] * s}
   IRuby.display make_png(data), mime: "image/png" ; true
 end
end

参考文献: Ruby で png 画像を自力で生成する



    一覧 単語検索   最終更新のRSS

RubyShowMethod