f8g

フィルタ

3*3とか配列を決めてやって、中央のピクセルが周囲のやつで決まるやつ。

# ぼかす
f = [
	[0.1, 0.1, 0.1],
	[0.1, 0.2, 0.1],
	[0.1, 0.1, 0.1]
]

# newPixel = pixel[-1][-1] * f[-1][-1] + pixel[0][-1] * f[0][-1] + pixel[1][-1] * f[1][-1] +
#            pixel[-1][ 0] * f[-1][ 0] + pixel[0][ 0] * f[0][ 0] + pixel[1][ 0] * f[1][ 0] +
#            pixel[-1][ 1] * f[-1][ 1] + pixel[0][ 1] * f[0][ 1] + pixel[1][ 1] * f[1][ 1]

フィルタ行列を色々変えて、シャープにしたり、エッジを出したり、エンボスにしたりする。

class System::Drawing::Bitmap
	def filter(f)
		newImage = Drawing::Bitmap.new(self.width, self.height)

		(self.width - 2).times {|x|
			(self.height - 2).times {|y|
				rgb = [0.0, 0.0, 0.0]

				pixels = [
					[self.GetPixel(x, y  ), self.GetPixel(x+1, y  ), self.GetPixel(x+2, y  )],
					[self.GetPixel(x, y+1), self.GetPixel(x+1, y+1), self.GetPixel(x+2, y+1)],
					[self.GetPixel(x, y+2), self.GetPixel(x+1, y+2), self.GetPixel(x+2, y+2)]
				]

				i = 0
				while i < 3
					j = 0
					while j < 3
						rgb[0] += f[i][j] * (1 * pixels[i][j].r)
						rgb[1] += f[i][j] * (1 * pixels[i][j].g)
						rgb[2] += f[i][j] * (1 * pixels[i][j].b)
						j += 1
					end
					i += 1
				end

				rgb[0] = (rgb[0] < 0)? 0:
				         (rgb[0] > 255)? 255 : rgb[0]
				rgb[1] = (rgb[1] < 0)? 0:
				         (rgb[1] > 255)? 255 : rgb[1]
				rgb[2] = (rgb[2] < 0)? 0:
				         (rgb[2] > 255)? 255 : rgb[2]

				newPixel = Drawing::Color::FromArgb(rgb[0], rgb[1], rgb[2])
				newImage.SetPixel(x+1, y+1, newPixel)
			}
		}

		newImage
	end
end

前のできた画像にフィルタかける。

f = [
	[-0.7, -1  , -0.7],
	[-1  ,  7.8, -1  ],
	[-0.7, -1  , -0.7]
]



IronRubyも1ヶ月前よりかなり使えるようになってた。けど、ブロック使うと遅いので、ループの中では使うときは気をつけたほうがいいかも。