#!ruby -Ku # -*- mode: ruby; coding: utf-8 -*- # Last updated: <2017/04/15 10:58:24 +0900> # # Arrayのベンチマーク # # [y][x][r, g, b, a] の配列からDXRubyのImageに変換する処理をいくつか試す require 'dxruby' require 'benchmark' # 元画像データの生成 def make_src_array(w, h) src = Array.new(h) { Array.new(w) } h.times do |y| w.times do |x| r = 255 * x / (w - 1) g = 255 * y / (h - 1) b = 255 * (w - 1 - x) / (w - 1) a = 255 src[y][x] = [r, g, b, a] end end return src end # Imageに1ドットずつRGBを書き込んでいくやり方 def convert_to_image(ary) w, h = ary[0].size, ary.size img = Image.new(w, h, [0, 0, 0, 0]) h.times do |y| w.times do |x| r, g, b, a = ary[y][x] img[x, y] = a, r, g, b end end return img end # Imageに1ドットずつRGBを書き込んでいくやり方その2 def convert_to_image2(ary) w, h = ary[0].size, ary.size img = Image.new(w, h, [0, 0, 0, 0]) ary.each_with_index do |line, y| line.each_with_index do |rgba, x| # img[x, y] = rgba[3], rgba[0], rgba[1], rgba[2] r, g, b, a = rgba img[x, y] = a, r, g, b end end return img end # 配列を平坦化してRGBAをARGBに並び替え、Image.createFromArrayを使う def convert_to_image3(ary) w, h = ary[0].size, ary.size d = ary.flatten i = 0 while i < d.size # d[i], d[i+1], d[i+2], d[i+3] = d[i+3], d[i], d[i+1], d[i+2] # d[i, 4] = d.values_at(i + 3, i, i + 1, i + 2) d[i, 4] = d[i+3], d[i], d[i+1], d[i+2] i += 4 end return Image.createFromArray(w, h, d) end # ループを回してARGB並びの配列を作成後、Image.createFromArrayを使う def convert_to_image4(ary) w, h = ary[0].size, ary.size d = [] ary.each do |line| line.each do |r, g, b, a| # d.push(a) ; d.push(r) ; d.push(g) ; d.push(b) d << a << r << g << b end end return Image.createFromArray(w, h, d) end # ---------------------------------------- src = make_src_array(1600, 140) # 元画像データ作成 imgs = [] # 配列をDXRubyのImageに変換 Benchmark.bm(12) do |x| x.report("Image[] ") { imgs.push(convert_to_image(src)) } x.report("Image[]2 ") { imgs.push(convert_to_image2(src)) } x.report("flatten ") { imgs.push(convert_to_image3(src)) } x.report("loop ") { imgs.push(convert_to_image4(src)) } end Window.resize(800, 600) Window.loop do break if Input.keyPush?(K_ESCAPE) x, y = 0, 0 imgs.each do |img| Window.draw(x, y, img) y += img.height + 4 end end