Strumenti Utente

Strumenti Sito


magistraleinformaticanetworking:spm:skelfsem

Differenze

Queste sono le differenze tra la revisione selezionata e la versione attuale della pagina.

Link a questa pagina di confronto

Entrambe le parti precedenti la revisione Revisione precedente
magistraleinformaticanetworking:spm:skelfsem [31/03/2011 alle 08:32 (13 anni fa)]
Marco Danelutto [OCAML functional semantics for the sample skeleton set]
magistraleinformaticanetworking:spm:skelfsem [31/03/2011 alle 08:36 (13 anni fa)] (versione attuale)
Marco Danelutto [Source code]
Linea 252: Linea 252:
  
 end;; end;;
 +</code>
 +
 +<code ocaml Img.ml>
 +(** Sample usage of Skeleton functional semantics
 +    We use a dummy Image format (array of array of pixels) 
 +    and we define some functions over images to be used in 
 +    pipeline stages. 
 +    Functions are defined as map s *) 
 +
 +open Skeleton
 +
 +(** the type of black and white pixels: levels of gray *)
 +type bw_pixel = BWP of int;;
 +(** the type of black and white images: 2 dim array of pixels *)
 +type bw_image = BWImage of bw_pixel array array;;
 +
 +(** the type of color pixels: RGB *)
 +type col_pixel = CP of int * int * int;;
 +(** the type of color images: 2 dim array of pixels *)
 +type col_image = Image of col_pixel array array;;
 +
 +(** sample image definition *)
 +let p1 = CP(127,127,127);;
 +let p2 = CP(0,0,64);;
 +let p3 = CP(0,64,0);;
 +let p4 = CP(64,0,0);;
 +let a = Image [| [| p1; p1; p1; p2; p2 |] ; 
 +                 [| p2; p3; p3; p4; p1 |] ; 
 +          [| p1; p1; p1; p1; p1 |] ; 
 + [| p1; p2; p2; p2; p1 |] |];;
 +
 +(** changes color pixels to black and white *)
 +let col_to_bw x  = 
 +  match x with
 +  CP(r,g,b) -> BWP(r+g+b);;
 +
 +(** kind of smooth pixel *)
 +let average = 
 +  function
 +    CP(x,y,z) -> 
 +      let n = ((x+y+z)/3) in 
 +      CP(n,n,n);;
 +
 +(** invert colors *)
 +let invert = function
 +  CP(r,g,b) -> CP(255-r, 255-g, 255-b);;
 +
 +
 +(** kind of saturation *)
 +let sq = function
 +  CP(r,g,b) -> let msq x = ((x*x) mod 256) in 
 +    CP((msq r), (msq g), (msq b));;
 +
 +(** generic stage: takes a pixel processing function (Color to Color) and 
 +    returns the stage working on the whole array of arrays
 +    @param f the function to be applied *)
 +let stage f = 
 +  function
 +    Image x -> Image ( (map (map f)) x);;
 +
 +(** sample stage definitions *)
 +let stage_average = stage average;;
 +let stage_sq = stage sq;;
 +
 +(** color to black and white stage: this could not be defined through stage function
 +    as the type of the output image changes ... *)
 +let c_to_bw_stage = function
 +  Image x -> BWImage((map (map col_to_bw)) x);;
 +
 +(** sample main program *)
 +let main = 
 +  pipe 
 +    (pipe (stage invert) (stage average))
 +    c_to_bw_stage ;; 
 </code> </code>
magistraleinformaticanetworking/spm/skelfsem.1301560335.txt.gz · Ultima modifica: 31/03/2011 alle 08:32 (13 anni fa) da Marco Danelutto