#!ruby -Ku # -*- mode: ruby; coding: utf-8 -*- # Last updated: <2017/04/08 19:36:49 +0900> # # pixel scalking , Scale2x, Scale3x module ScaleNx # pixel scaling, Scale2x # @param src [Array] pixel data array # @return [Array] 2x pixel data array def scale2x(src) width, height = src[0].size, src.size nwidth, nheight = width * 2, height * 2 dst = Array.new(nheight).map { Array.new(nwidth).map { Array.new(4, 0) } } height.times do |y| width.times do |x| e = src[y][x] d = (x - 1 >= 0)? src[y][x - 1] : e f = (x + 1 < width)? src[y][x + 1] : e if y - 1 >= 0 b = src[y - 1][x] a = (x - 1 >= 0)? src[y - 1][x - 1] : b c = (x + 1 < width)? src[y - 1][x + 1] : b else a = d b = e c = f end if y + 1 < height h = src[y + 1][x] g = (x - 1 >= 0)? src[y + 1][x - 1] : h i = (x + 1 < width)? src[y + 1][x + 1] : h else g = d h = e i = f end if b != h && d != f e0 = (d == b)? d : e e1 = (b == f)? f : e e2 = (d == h)? d : e e3 = (h == f)? f : e else e0 = e e1 = e e2 = e e3 = e end dx = x * 2 dy = y * 2 dst[dy][dx] = e0 dst[dy][dx + 1] = e1 dst[dy + 1][dx] = e2 dst[dy + 1][dx + 1] = e3 end end return dst end # pixel scaling, Scale3x # @param src [Array] pixel data array # @return [Array] 3x pixel data array def scale3x(src) width, height = src[0].size, src.size nwidth, nheight = width * 3, height * 3 dst = Array.new(nheight).map { Array.new(nwidth).map { Array.new(4, 0) } } height.times do |y| width.times do |x| e = src[y][x] d = (x - 1 >= 0)? src[y][x - 1] : e f = (x + 1 < width)? src[y][x + 1] : e if y - 1 >= 0 b = src[y - 1][x] a = (x - 1 >= 0)? src[y - 1][x - 1] : b c = (x + 1 < width)? src[y - 1][x + 1] : b else a = d b = e c = f end if y + 1 < height h = src[y + 1][x] g = (x - 1 >= 0)? src[y + 1][x - 1] : h i = (x + 1 < width)? src[y + 1][x + 1] : h else g = d h = e i = f end if b != h and d != f e0 = (d == b)? d : e e1 = ((d == b and e != c) or (b == f and e != a))? b : e e2 = (b == f)? f : e e3 = ((d == b and e != g) or (d == h and e != a))? d : e e4 = e e5 = ((b == f and e != i) or (h == f and e != c))? f : e e6 = (d == h)? d : e e7 = ((d == h and e != i) or (h == f and e != g))? h : e e8 = (h == f)? f : e else e0 = e e1 = e e2 = e e3 = e e4 = e e5 = e e6 = e e7 = e e8 = e end dx = x * 3 dy = y * 3 dst[dy][dx] = e0 dst[dy][dx + 1] = e1 dst[dy][dx + 2] = e2 dst[dy + 1][dx] = e3 dst[dy + 1][dx + 1] = e4 dst[dy + 1][dx + 2] = e5 dst[dy + 2][dx] = e6 dst[dy + 2][dx + 1] = e7 dst[dy + 2][dx + 2] = e8 end end return dst end end if $0 == __FILE__ # ---------------------------------------- require 'dxruby' include ScaleNx # convert DXRuby Image to array def conv_image2array(img) w, h = img.width, img.height data = [] h.times do |y| dt = [] w.times do |x| a, r, g, b = img[x, y] dt.push([r, g, b, a]) end data.push(dt) end return data end # convert array to DXRuby Image def conv_array2image(src) w, h = src[0].size, src.size img = Image.new(w, h, [0, 0, 0, 0]) h.times do |y| src[y].each_with_index do |v, x| r, g, b, a = v img[x, y] = [a, r, g, b] end end return img end srcimg = Image.load("hq3x_original.png") src = conv_image2array(srcimg) dst = scale2x(src) dstimg2x = conv_array2image(dst) dst = scale3x(src) dstimg3x = conv_array2image(dst) Window.resize(540, 360) Window.minFilter = TEXF_POINT Window.magFilter = TEXF_POINT Window.scale = 2 Window.loop do break if Input.keyPush?(K_ESCAPE) x, y = 0, 0 Window.draw(0, 0, srcimg) x += srcimg.width + 8 Window.draw(x, y, dstimg2x) y += dstimg2x.height + 8 Window.draw(x, y, dstimg3x) end end