(* These are Mathematica procedures for an inclass                    *)
(* demonstration of image manipulation techniques in the context of   *) 
(* of linear algebra                                                  *)
(* Oliver Knill, Math21b, Harvard, Fall, 2004, October 2004           *)

ApplyFilter[F_,A_]:=Graphics[Raster[F[A[[1,1]]],A[[1,2]],A[[1,3]],A[[1,4]]],A[[2]],A[[3]],A[[4]]];
A=Import["zermatt.jpg"]; 

Dim[x_]:=Round[x/2]; B1=ApplyFilter[Dim,A];
Brighten[x_]:=Round[120+2x/3]; B2=ApplyFilter[Brighten,A];
Display["dimmed.jpg",B1,"JPEG"]; 
Display["brightend.jpg",B2,"JPEG"]; 1

ApplyFilter[F_,A_]:=Graphics[Raster[F[A[[1,1]]],A[[1,2]],A[[1,3]],A[[1,4]]],A[[2]],A[[3]],A[[4]]];
A=Import["zermatt.jpg"]; 

red[{r_,g_,b_}]:={r,0,0}; Rred[S_]:=Table[red[S[[i,j]]],{i,Length[S]},{j,Length[S[[1]]]}];
green[{r_,g_,b_}]:={0,g,0}; Ggreen[S_]:=Table[green[S[[i,j]]],{i,Length[S]},{j,Length[S[[1]]]}];
blue[{r_,g_,b_}]:={0,0,b}; Bblue[S_]:=Table[blue[S[[i,j]]],{i,Length[S]},{j,Length[S[[1]]]}];

B1=ApplyFilter[Rred,A];
B2=ApplyFilter[Ggreen,A];
B3=ApplyFilter[Bblue,A];
Display["red.jpg",B1,"JPEG"];
Display["green.jpg",B2,"JPEG"];
Display["blue.jpg",B3,"JPEG"];

ApplyFilter[F_,A_]:=Graphics[Raster[F[A[[1,1]]],A[[1,2]],A[[1,3]],A[[1,4]]],A[[2]],A[[3]],A[[4]]];
A=Import["zermatt.jpg"]; 

rotate[{r_,g_,b_}]:={b,r,g}; Rotate[S_]:=Table[rotate[S[[i,j]]],{i,Length[S]},{j,Length[S[[1]]]}]; 
reflect[{r_,g_,b_}]:={255-r,255-g,255-b}; Reflect[S_]:=Table[reflect[S[[i,j]]],{i,Length[S]},{j,Length[S[[1]]]}]; 
B4=ApplyFilter[Rotate,A]; 
B5=ApplyFilter[Reflect,A]; 
Display["rotate.jpg",B4,"JPEG"]; 
Display["reflect.jpg",B5,"JPEG"]; 

Combine[F_,U_,V_]:=Graphics[Raster[F[U[[1,1]],V[[1,1]]],U[[1,2]],U[[1,3]],U[[1,4]]],U[[2]],U[[3]],U[[4]]];
A1=Import["zermatt.jpg"]; A2=Import["oliver.jpg"]; 

LinearCombination[a_,b_]:=Function[{x,y},a*x+b*y]; 
B=Combine[LinearCombination[1/3,2/3],A1,A2]; 
Display["mixture.jpg",B,"JPEG"]; 

PictureName[n_]:=FromCharacterCode[Join[IntegerDigits[n]+48,ToCharacterCode[".tif"]]];
Do[Display[PictureName[100+k],Combine[LinearCombination[k/10,1-k/10],A1,A2],"TIFF"],{k,10}]

A1=Import["zermatt.jpg"]; A2=Import["oliver.jpg"];
Combine[F_,U_,V_]:=Graphics[Raster[F[U[[1,1]],V[[1,1]]],U[[1,2]],U[[1,3]],U[[1,4]]],U[[2]],U[[3]],U[[4]]];

A1=Import["zermatt.jpg"]; A2=Import["oliver.jpg"];
F1[a_,b_]:=Function[{x,y},
  Module[{},
    m=Length[x[[1]]];  (* width  *)
    n=Length[x];       (* height *)
    z=x; 
    Do[
      If[j<a*m, K=0]; 
      If[j>a*m && j<b*m, K=(j-a*m)/(b*m-a*m)];
      If[j>b*m, K=1]; 
      z[[i,j]]=Round[K*x[[i,j]]+(1-K)*y[[i,j]]],
      {i,n},{j,m} 
    ]; z ]
]
B=Combine[F1[1/3,2/3],A1,A2];
Display["leftrighttransition.jpg",B,"JPEG"];



PictureName[n_]:=FromCharacterCode[Join[IntegerDigits[n]+48,ToCharacterCode[".tif"]]];
(* Do[Display[PictureName[100+k],Combine[F1[0,k/10],A1,A2],"TIFF"],{k,10}] *)
(* Do[Display[PictureName[110+k],Combine[F1[k/10,1],A1,A2],"TIFF"],{k,1,10}] *)


A1=Import["zermatt.jpg"]; A2=Import["oliver.jpg"];
Combine[F_,U_,V_]:=Graphics[Raster[F[U[[1,1]],V[[1,1]]],U[[1,2]],U[[1,3]],U[[1,4]]],U[[2]],U[[3]],U[[4]]];
F2[r1_,r2_]:=Function[{x,y},
  Module[{},
    m=Length[x[[1]]];  mc=m/2;
    n=Length[x];       nc=n/2;
    rr1=(Min[m,n]*r1)^2; 
    rr2=(Min[m,n]*r2)^2; 
    z=x;
    Do[
      rr=(i-nc)^2+(j-mc)^2;
      If[rr<r1,  K=0];    
      If[rr1<=rr<=rr2, K=(rr-rr1)/(rr2-rr1)];
      If[rr>rr2, K=1];
      z[[i,j]]=Round[K*x[[i,j]]+(1-K)*y[[i,j]]],
      {i,n},{j,m}
    ]; z ]
]
B=Combine[F2[1/10,1/2],A1,A2];
Display["circle.jpg",B,"JPEG"];


