Advertisement
blizzboz

Untitled

Jun 10th, 2011
645
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 2.56 KB | None | 0 0
  1. module HQ
  2.  
  3. open System
  4. open System.Drawing
  5. open System.Drawing.Imaging
  6.  
  7. type Pixel(red : double, green : double, blue : double, alpha : double) =
  8.  
  9.     let toInt(value : double) =
  10.         value
  11.             |> Math.Round
  12.             |> Convert.ToInt32
  13.             |> fun i -> if i < 0 then 0 elif i > 255 then 255 else i
  14.  
  15.     member p.Red = red
  16.     member p.Green = green
  17.     member p.Blue = blue
  18.     member p.Alpha = alpha
  19.  
  20.     static member FromColor(color : Color) =
  21.         new Pixel(color.R |> Convert.ToDouble,
  22.             color.G |> Convert.ToDouble,
  23.             color.B |> Convert.ToDouble,
  24.             color.A |> Convert.ToDouble)
  25.  
  26.     member p.ToColor() = Color.FromArgb(alpha |> toInt, red |> toInt, green |> toInt, blue |> toInt)
  27.  
  28. type Pixelmap(width : int, height : int, pixels : Pixel[,]) =
  29.  
  30.     member m.Pixels = pixels
  31.     member m.Width = width
  32.     member m.Height = height
  33.  
  34.     member m.EachPixel(action) : unit =
  35.         m.Pixels |> Array2D.iteri (fun x y pixel -> action(x, y, pixel))
  36.  
  37.     static member FromBitmap(bitmap : Bitmap) =
  38.         new Pixelmap(bitmap.Width, bitmap.Height,
  39.             fun x y -> Pixel.FromColor(bitmap.GetPixel(x, y))
  40.             |> Array2D.init bitmap.Width bitmap.Height)  
  41.  
  42.     member m.ToBitmap() : Bitmap =
  43.         let bitmap = new Bitmap(width, height)
  44.         fun(x, y, pixel:Pixel) -> bitmap.SetPixel(x, y, pixel.ToColor())
  45.         |> m.EachPixel
  46.         bitmap
  47.  
  48. type Adjust(pixelmap : Pixelmap) =
  49.  
  50.     let ToGray(pixel:Pixel) = (pixel.Red + pixel.Green + pixel.Blue) / 3.0
  51.  
  52.     member a.Apply filter =      
  53.         fun(x, y, pixel : Pixel) -> pixelmap.Pixels.[x, y] <- filter pixel
  54.         |> pixelmap.EachPixel
  55.  
  56.     member a.Saturation(value : double) : unit =
  57.         if value <> 0.0 then
  58.             let sat = value + 256.0
  59.             fun (pixel : Pixel) ->
  60.                 let gray = pixel |> ToGray
  61.                 new Pixel(gray + (pixel.Red - gray) * sat / 255.0,
  62.                     gray + (pixel.Green - gray) * sat / 255.0,
  63.                     gray + (pixel.Blue - gray) * sat / 255.0, pixel.Alpha)
  64.             |> a.Apply
  65.  
  66.     member a.Brightness(value : double) : unit =
  67.         if value <> 0.0 then
  68.             fun (pixel : Pixel) ->
  69.                 new Pixel(pixel.Red + value,
  70.                     pixel.Green + value,
  71.                     pixel.Blue + value, pixel.Alpha)
  72.             |> a.Apply
  73.  
  74.     member a.Grayscale() : unit =
  75.         fun (pixel : Pixel) ->
  76.             let gray = pixel |> ToGray
  77.             new Pixel(gray, gray, gray, pixel.Alpha)
  78.         |> a.Apply
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement