2 people like it.

Bitmap Primitives Helpers

Bitmap primitives helper functions for manipulating bitmap in memory as an array of tuples (x,y,color).

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
43: 
44: 
45: 
46: 
(*
 * Bitmap Primitives Helpers
 * Partly inspired by http://fssnip.net/si.
*)
open System.Drawing

// deserialize a bitmap file
let fromFile (name : string) = new Bitmap(name)

// serialize a bitmap as png
let toFile (name : string) (bmp: Bitmap) =
    bmp.Save(name, Imaging.ImageFormat.Png) |> ignore
    bmp

// load a bitmap in array of tuples (x,y,Color)
let toRgbArray (bmp : Bitmap) =
    [| for y in 0..bmp.Height-1 do
       for x in 0..bmp.Width-1 -> x,y,bmp.GetPixel(x,y) |]   

// builds a bitmap instance from an array of tuples
let toBitmap a =
    let height = (a |> Array.Parallel.map (fun (x,_,_) -> x) |> Array.max) + 1
    let width = (a |> Array.Parallel.map (fun (_,y,_) -> y) |> Array.max) + 1
    let bmp = new Bitmap(width, height)
    a |> Array.Parallel.iter (fun (x,y,c) -> bmp.SetPixel(x,y,c))
    bmp

// converts an image to gray scale
let toGrayScale a =
    a |> Array.Parallel.map (
        fun (x,y,c : System.Drawing.Color) -> 
            let gscale = int((float c.R * 0.3) + (float c.G * 0.59) + (float c.B * 0.11))
            in  x,y,Color.FromArgb(int c.A, gscale, gscale, gscale))

(*
 * Usage example:
*)
let convertImage =
    use newBitmap =
        fromFile @"C:/Temp/with-color.jpg"
        |> toRgbArray
        |> toGrayScale // or custom in memory manipulation function
        |> toBitmap
    use savedBitmap =
        toFile @"C:/Temp/gray-scale.png" newBitmap
    ()
namespace System
namespace System.Drawing
val fromFile : name:string -> Bitmap

Full name: Script.fromFile
val name : string
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = System.String

Full name: Microsoft.FSharp.Core.string
Multiple items
type Bitmap =
  inherit Image
  new : filename:string -> Bitmap + 11 overloads
  member Clone : rect:Rectangle * format:PixelFormat -> Bitmap + 1 overload
  member GetHbitmap : unit -> nativeint + 1 overload
  member GetHicon : unit -> nativeint
  member GetPixel : x:int * y:int -> Color
  member LockBits : rect:Rectangle * flags:ImageLockMode * format:PixelFormat -> BitmapData + 1 overload
  member MakeTransparent : unit -> unit + 1 overload
  member SetPixel : x:int * y:int * color:Color -> unit
  member SetResolution : xDpi:float32 * yDpi:float32 -> unit
  member UnlockBits : bitmapdata:BitmapData -> unit
  ...

Full name: System.Drawing.Bitmap

--------------------
Bitmap(filename: string) : unit
   (+0 other overloads)
Bitmap(stream: System.IO.Stream) : unit
   (+0 other overloads)
Bitmap(original: Image) : unit
   (+0 other overloads)
Bitmap(filename: string, useIcm: bool) : unit
   (+0 other overloads)
Bitmap(type: System.Type, resource: string) : unit
   (+0 other overloads)
Bitmap(stream: System.IO.Stream, useIcm: bool) : unit
   (+0 other overloads)
Bitmap(width: int, height: int) : unit
   (+0 other overloads)
Bitmap(original: Image, newSize: Size) : unit
   (+0 other overloads)
Bitmap(width: int, height: int, format: Imaging.PixelFormat) : unit
   (+0 other overloads)
Bitmap(width: int, height: int, g: Graphics) : unit
   (+0 other overloads)
val toFile : name:string -> bmp:Bitmap -> Bitmap

Full name: Script.toFile
val bmp : Bitmap
Image.Save(filename: string) : unit
Image.Save(stream: System.IO.Stream, format: Imaging.ImageFormat) : unit
Image.Save(filename: string, format: Imaging.ImageFormat) : unit
Image.Save(stream: System.IO.Stream, encoder: Imaging.ImageCodecInfo, encoderParams: Imaging.EncoderParameters) : unit
Image.Save(filename: string, encoder: Imaging.ImageCodecInfo, encoderParams: Imaging.EncoderParameters) : unit
namespace System.Drawing.Imaging
Multiple items
type ImageFormat =
  new : guid:Guid -> ImageFormat
  member Equals : o:obj -> bool
  member GetHashCode : unit -> int
  member Guid : Guid
  member ToString : unit -> string
  static member Bmp : ImageFormat
  static member Emf : ImageFormat
  static member Exif : ImageFormat
  static member Gif : ImageFormat
  static member Icon : ImageFormat
  ...

Full name: System.Drawing.Imaging.ImageFormat

--------------------
Imaging.ImageFormat(guid: System.Guid) : unit
property Imaging.ImageFormat.Png: Imaging.ImageFormat
val ignore : value:'T -> unit

Full name: Microsoft.FSharp.Core.Operators.ignore
val toRgbArray : bmp:Bitmap -> (int * int * Color) []

Full name: Script.toRgbArray
val y : int
property Image.Height: int
val x : int
property Image.Width: int
Bitmap.GetPixel(x: int, y: int) : Color
val toBitmap : a:(int * int * Color) [] -> Bitmap

Full name: Script.toBitmap
val a : (int * int * Color) []
val height : int
module Array

from Microsoft.FSharp.Collections
module Parallel

from Microsoft.FSharp.Collections.ArrayModule
val map : mapping:('T -> 'U) -> array:'T [] -> 'U []

Full name: Microsoft.FSharp.Collections.ArrayModule.Parallel.map
val max : array:'T [] -> 'T (requires comparison)

Full name: Microsoft.FSharp.Collections.Array.max
val width : int
val iter : action:('T -> unit) -> array:'T [] -> unit

Full name: Microsoft.FSharp.Collections.ArrayModule.Parallel.iter
val c : Color
Bitmap.SetPixel(x: int, y: int, color: Color) : unit
val toGrayScale : a:('a * 'b * Color) [] -> ('a * 'b * Color) []

Full name: Script.toGrayScale
val a : ('a * 'b * Color) []
val x : 'a
val y : 'b
type Color =
  struct
    member A : byte
    member B : byte
    member Equals : obj:obj -> bool
    member G : byte
    member GetBrightness : unit -> float32
    member GetHashCode : unit -> int
    member GetHue : unit -> float32
    member GetSaturation : unit -> float32
    member IsEmpty : bool
    member IsKnownColor : bool
    ...
  end

Full name: System.Drawing.Color
val gscale : int
Multiple items
val int : value:'T -> int (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.int

--------------------
type int = int32

Full name: Microsoft.FSharp.Core.int

--------------------
type int<'Measure> = int

Full name: Microsoft.FSharp.Core.int<_>
Multiple items
val float : value:'T -> float (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.float

--------------------
type float = System.Double

Full name: Microsoft.FSharp.Core.float

--------------------
type float<'Measure> = float

Full name: Microsoft.FSharp.Core.float<_>
property Color.R: byte
property Color.G: byte
property Color.B: byte
Color.FromArgb(argb: int) : Color
Color.FromArgb(alpha: int, baseColor: Color) : Color
Color.FromArgb(red: int, green: int, blue: int) : Color
Color.FromArgb(alpha: int, red: int, green: int, blue: int) : Color
property Color.A: byte
val convertImage : unit

Full name: Script.convertImage
val newBitmap : Bitmap
val savedBitmap : Bitmap

More information

Link:http://fssnip.net/sn
Posted:9 years ago
Author:Giacomo Stelluti Scala
Tags: graphics