Friday, March 7, 2014
PPMimageEditor using combination of C# and F# functional language
description of the assignment
this is one of the many ways of how to implement high order functional program within GUI interface.
here is a code for F# part
module PPMImageLibrary
#light
//
// <<Plaimanus Lueondee>>
// U. of Illinois, Chicago
// CS341, Spring 2014
// Homework 6
//
//
// DebugOutput:
//
// Outputs to console, which appears in the "Output" window pane of
// Visual Studio when you run with debugging (F5).
//
let rec private OutputImage(image:int list list) =
match image with
| [ ] -> printfn "**END**"
| _ -> printfn "%A" image.Head
OutputImage(image.Tail)
let DebugOutput(width:int, height:int, depth:int, image:int list list) =
printfn "**HEADER**"
printfn "W=%A, H=%A, D=%A" width height depth
printfn "**IMAGE**"
OutputImage(image)
//
// TransformFirstRowWhite:
//
// An example transformation: replaces the first row of the given image
// with a row of all white pixels.
//
let TransformFirstRowWhite(depth:int, image:int list list) =
let numCols = image.Head.Length // number of columns in first row
let AllWhite = [ for i in 1 .. numCols -> depth ] // white is RGB = depth depth depth
AllWhite::image.Tail // first row all white :: followed by rest of original image
//helper function for grayScale function
let rec modifyGray (image:int list) =
match image with
|[] ->[]
|_->
let R = image.Head
let G = image.Tail.Head
let B = image.Tail.Tail.Head
let gray =(R+G+B)/3
[gray;gray;gray]@modifyGray(image.Tail.Tail.Tail)
//F# -Base PPM image Library for GrayScale button
let rec grayScale(image:int list list)=
match image with
|[]->[]
|_->
let gray = modifyGray(image.Head)
gray::grayScale(image.Tail)
//run a new list with an invert order
//F#base PPM image Library for Vertical Flip Button
let flip(image:int list list) =
let r = List.rev image
r
//helper function for horizontal flip
let rec re(image:int list) =
match image with
|[]->[]
|_->
let R = image.Head
let G = image.Tail.Head
let B = image.Tail.Tail.Head
let newList = List.rev [R;G;B]//reverser red green blue in each pixel
newList@re(image.Tail.Tail.Tail)//concatenate wtih all pixel
//F# -base PPM image Library for HorizontalFlip Button
let rec horizontalFlip(image:int list list) =
match image with
|[]->[]
|_->
let reverse = re(image.Head)
let reverse = List.rev reverse//reverser the whoile thing after reverse the pixel
reverse::horizontalFlip(image.Tail)
//helper function for invert image
let rec inverter(depth:int , image:int list) =
match image with
|[]->[]
|_->
let R = image.Head
let G = image.Tail.Head
let B = image.Tail.Tail.Head
let subtract = [depth-R;depth-G;depth-B]//depth value - the amount of red gree blue in each phixel
subtract@inverter(depth,image.Tail.Tail.Tail)
//F# -base PPM image Library for Invert Button
let rec invert(depth:int , image:int list list)=
match image with
|[]-> []
|_->
let inv = inverter(depth,image.Head)//send to a helper function
inv::invert(depth,image.Tail)//recursively call function
//Posterization function helper
let rec myster(image:int list ) =
match image with
|[]->[]
|_->
let R = image.Head
let G = image.Tail.Head
let B = image.Tail.Tail.Head
let gray =(R+G+B)/3
let r =List.rev [R;G;B]
if gray < 60 then
[R*0;G*0;B*0]@myster(image.Tail.Tail.Tail)
elif gray < 75 then
[255;255;255]@myster(image.Tail.Tail.Tail)
elif gray <128 then
[255;0;0]@myster(image.Tail.Tail.Tail)
elif gray < 255 then
[0;255;0]@myster(image.Tail.Tail.Tail)
else
[0;0;255]@myster(image.Tail.Tail.Tail)
//F# -base PPM image Library for Mysterious button
let rec mysterious(image:int list list) =
match image with
|[]->[]
|_->
let m = myster(image.Head)
m::mysterious(image.Tail)
//helper function for WriteP3Image
let rec concat(image:int list list) =
match image with
|[] -> []
|_->
image.Head @ concat(image.Tail)
// WriteP3Image:
//
// Writes the given image out to a text file, in "P3" format. Returns true if successful,
// false if not.
//
let WriteP3Image(filepath:string, width:int, height:int, depth:int, image:int list list) =
let firstList = [width; height; depth] @ concat(image)
let convertString = firstList |> List.map string
let All= ["P3"] @ convertString
System.IO.File.WriteAllLines(filepath, All)
true // success
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment