濃度変換
require "cglib.rb" # image file img_file = "img.bmp" # convert function def f(n) n = 1 * n # to fixnum if n % 8 == 0 then return n else return 255 - n end end # create objects form = Forms::Form.new form.width = 500 form.height = 500 pictures = [Forms::PictureBox.new, Forms::PictureBox.new] pictures[0].image = Drawing::Image::FromFile(img_file) pictures[1].left = pictures[0].image.width + 10 pictures.map {|pic| pic.width = pictures[0].image.width pic.height = pictures[0].image.height } button = Forms::Button.new button.top = pictures[0].image.height + 10 graph = Forms::PictureBox.new graph.width = 256 graph.height = 256 graph.top = pictures[0].image.height + 10 graph.left = form.width - 265 graph.backColor = Drawing::Color.black # convert button.click {|sender, args| # draw convert picture pictures[1].image = Drawing::Bitmap.new(pictures[0].width, pictures[1].height) pictures[1].Refresh pictures[0].image.width.times {|x| pictures[0].image.height.times {|y| pixel = pictures[0].image.getPixel(x, y) color = Drawing::Color::fromArgb(f(pixel.r), f(pixel.g), f(pixel.b)) pictures[1].image.setPixel(x, y, color) } } pictures[1].Refresh # draw graph graphic = graph.createGraphics graph.Refresh pre_p = [0, f(0)] 256.times {|i| graphic.drawLine(Drawing::Pens.red, pre_p[0], pre_p[1], i, 255 - f(i)) pre_p = [i, 255 - f(i)] } graphic.Dispose } # add controls pictures.concat([button, graph]).map {|cntrl| form.controls.add(cntrl) } # show form form.showDialog()
cglib.rbってのは
require 'mscorlib' require 'System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' require 'System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' Forms = System::Windows::Forms Drawing = System::Drawing Drawing2D = System::Drawing::Drawing2D
とか書いてあるだけ。