ぼかし
30分プログラム、その47。普通のぼかし(blur)。普通じゃないぼかしは、ガウスぼかしとか。
教材は、フルスクラッチによるグラフィックスプログラミング入門。
require "sdl" def map_surface!(surface,&f) surface.lock (0...surface.h).each do|y| (0...surface.w).each do |x| surface[x,y] = f.call x,y,surface[x,y] end end surface.unlock surface end def max(x,y) x > y ? x : y end def min(x,y) x < y ? x : y end SIZE=2 def blur(surface) map_surface!(surface){|x,y,i| sum = [0,0,0] count = 0 (max(0,x-SIZE)...min(surface.w,x+SIZE)).each{|x1| (max(0,y-SIZE)...min(surface.h,y+SIZE)).each{|y1| (r,g,b) = surface.get_rgb surface[x1,y1] sum[0] += r sum[1] += g sum[2] += b count += 1 } } sum.map{|x| x / count } } end SDL.init(SDL::INIT_EVERYTHING) image = SDL::Surface.load_bmp 'ChiPet.bmp' screen = SDL.set_video_mode(image.w,image.h,16,SDL::SWSURFACE) screen.put image,0,0 blur(screen) screen.update_rect 0,0,0,0 loop{ while event=SDL::Event2.poll do case event when SDL::Event2::Quit exit end end }
- 画像処理は関数型できれいに書ける気がしない
- image[x,y] = image[x,y]とやるとなぜか色が落ちる。なぜだ
- Enumerable#max,Enumerable#minの存在は知ってるよ
- めっさ遅い