<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" frameRate="15" height="575" width="550" applicationComplete="Func_Init_Application()" >
<fx:Script>
<![CDATA[
private var var_cam:Camera = Camera.getCamera();
private var var_video:Video = new Video(250,250);
private var var_picturedata:BitmapData;
private var var_rect:Rectangle = new Rectangle(0,0,var_video.width,var_video.height);
private var var_NumberOfBlackPixels:int;
private var var_NumberOfWhitePixels:int;
private var var_ByteArray:ByteArray;
private var var_NumberofSequence:int;
private var var_sequencemixed:Array;
private var var_sequence:Array;
private var var_csvfilestream:FileStream = new FileStream();
private var var_csvtargetfile:File = File.desktopDirectory.resolvePath('dataraw.csv');
private var var_formpicid:Array;
private var var_hits:int = 0;
private var var_picturedataforms:BitmapData;
private var var_bytearrayforms:ByteArray;
private var var_targetpicno:int;
private var var_actionframe:int;
private var var_timer:Timer = new Timer(2000,1); //time between two Pictures
private var var_stageheightnative:int;
private var var_stagewidthnative:int;
private var Pos:int;

private function Func_Init_Application():void {
Func_Layout();
if(var_cam != null){
var_cam.setMode(250,250,1,true);
var_stageheightnative = stage.stageHeight;
var_stagewidthnative = stage.stageWidth;
Func_TopLeft();
var_formpicid = new Array(ID_blackbox,ID_blackcircle,ID_blacktriangel);
ID_BottomRight.enabled = true;
ID_Stop_Button.enabled = false;
Func_Init_Test();
}
else{
ID_Error_Video.text = "Camera is missing\n"+ "Check if camera is connected properly\n"+
"Exit program and restart\n" ;
}
Func_Start();
}

private function Func_Init_Test():void {
var_csvfilestream.open(var_csvtargetfile, "write");
var_sequence = new Array(0,1,2);
var_sequencemixed = Func_ArrayMixer(var_sequence);
var_NumberofSequence = 0;
var_csvfilestream.writeMultiByte("*TargetPic 0=IDblackbox 1=IDblackcircle 2=IDblacktriangel 3=IDInitialization,\n","iso-8859-1");
var_csvfilestream.writeMultiByte("SeqNo,TargetPic,Black,White,IDblackbox,IDblackcircle,IDblacktriangel,\n","iso-8859-1");

}



private function Func_Layout():void{
var var_height:int = 250;
var var_width:int = 250;
addEventListener(Event.RESIZE,Func_OnResize);
ID_TopLeft.height = var_height,ID_TopRight.height = var_height,ID_BottomLeft.height = var_height,ID_BottomRight.height = var_height;
ID_TopLeft.width = var_width,ID_TopRight.width = var_width,ID_BottomLeft.width = var_width,ID_BottomRight.width = var_width;
ID_TopLeft.x = 10,ID_TopRight.x = 270,ID_BottomLeft.x = 10,ID_BottomRight.x = 270,ID_BackgroundImage.x=-445;
ID_TopLeft.y = 10,ID_TopRight.y = 10,ID_BottomLeft.y = 270,ID_BottomRight.y = 270,ID_BackgroundImage.y=-163;
}

private function Func_OnResize(e:Event):void{
ID_Main.x = ((stage.stageWidth - var_stagewidthnative)/2)+10;
ID_Main.y = ((stage.stageHeight - var_stageheightnative)/2)+10;
ID_Start.x = ((stage.stageWidth - var_stagewidthnative)/2)+10;
ID_Start.y = ((stage.stageHeight - var_stageheightnative)/2)+10;
ID_About.x = ((stage.stageWidth - var_stagewidthnative)/2)+10;
ID_About.y = ((stage.stageHeight - var_stageheightnative)/2)+10;
ID_BackgroundImage.x = ((stage.stageWidth - var_stagewidthnative)/2)-445;
ID_BackgroundImage.y = ((stage.stageHeight - var_stageheightnative)/2)-163;
}

private function Func_Stage():void{
ID_About.visible = false;
ID_Main.visible = true;
ID_Start.visible = false;
}

private function Func_About():void{
ID_About.visible = true;
ID_Main.visible = false;
ID_Start.visible = false;
}

private function Func_Start():void{
ID_About.visible = false;
ID_Main.visible = false;
ID_Start.visible = true;
}

private function Func_TopLeft():void{
var_video = new Video(var_cam.width,var_cam.height);
var_video.attachCamera(var_cam);
ID_Video.addChild(var_video);

}



private function Func_BlackWhitePic():void{
var_picturedata = new BitmapData(var_video.width,var_video.height);
var_picturedata.draw(var_video,null,null,null,var_rect);
var_NumberOfBlackPixels = 0;
var_NumberOfWhitePixels = 0;
var_ByteArray = var_picturedata.getPixels(var_rect);
var j:int = 0;
var zerocounter:int = 0;
var onecounter:int = 0;
var colorvalue:int = 0;
do
{
//var_ByteArray[j] is alphachannel == 255 no Changes
zerocounter= 0;
onecounter = 0;
if ((var_ByteArray[j+1]%2)==1){onecounter = onecounter + 1;} else {zerocounter = zerocounter + 1;}
if ((var_ByteArray[j+2]%2)==1){onecounter = onecounter + 1;} else {zerocounter = zerocounter + 1;}
if ((var_ByteArray[j+3]%2)==1){onecounter = onecounter + 1;} else {zerocounter = zerocounter + 1;}
//Majority Vote
if (onecounter > zerocounter){colorvalue = 255;var_NumberOfWhitePixels = var_NumberOfWhitePixels + 1;}
else {colorvalue = 0; var_NumberOfBlackPixels = var_NumberOfBlackPixels + 1;}


var_ByteArray[j+1] = colorvalue; //Red
var_ByteArray[j+2] = colorvalue; //Green
var_ByteArray[j+3] = colorvalue; //Blue

j = j + 4;
}while (j<var_ByteArray.length)
var var_bitmap:Bitmap = new Bitmap(var_picturedata);
var_ByteArray.position = 0;
var_picturedata.setPixels(var_rect,var_ByteArray);
ID_ImageBlackWhite.data = var_bitmap;
}








private function Func_BottomLeft(var_picNo:int):void{

ID_blackbox.visible = false;
ID_blackcircle.visible = false;
ID_blacktriangel.visible = false;
ID_Initialization.visible = false;
ID_Finished.visible = false;
ID_ImageBlackWhite.visible = true;

if(var_picNo == 0){ID_blackbox.visible = true;}
if(var_picNo == 1){ID_blackcircle.visible = true;}
if(var_picNo == 2){ID_blacktriangel.visible = true;}
if(var_picNo == 3){ID_Initialization.visible = true;
ID_ImageBlackWhite.visible = false;}
if(var_picNo == 4){ID_Finished.visible = true;
ID_ImageBlackWhite.visible = false;}


}



private function Func_CheckPics():void{
var var_String:String = "";
var var_black:int;
var var_white:int;

//Compare Random Black White Pic with the 3 different Forms

for(var k:int=0;k<var_formpicid.length;k++){
var_hits = 0;
var_picturedataforms = new BitmapData(var_video.width,var_video.height);
var_picturedataforms.draw(var_formpicid[k],null,null,null,var_rect);
var_bytearrayforms = var_picturedataforms.getPixels(var_rect);


//vergleich
var j:int = 0;
do
{
//Alpha = 255 and R=G=B=255|0 check R for Hitdecicion

if ( ((var_bytearrayforms[j+1]==255)&&(var_ByteArray[j+1]==255))||((var_bytearrayforms[j+1]<255)&&(var_ByteArray[j+1]<255)) ){
var_hits = var_hits + 1;
}

j = j + 4;


}while (j<var_bytearrayforms.length)


var_bytearrayforms.clear();
var_String = var_String + String(var_hits)+",";

}

var_csvfilestream.writeMultiByte(String(var_NumberofSequence)+","+String(var_sequencemixed[var_targetpicno])+","+String(var_NumberOfBlackPixels)+","+String(var_NumberOfWhitePixels)+","+var_String+"\n","iso-8859-1");

}

private function Func_Startprogram():void{
Func_Init_Test();
var_timer.addEventListener(TimerEvent.TIMER_COMPLETE,Func_Program);
Func_Program(new Event(""));
ID_Start_Button.enabled = false;
ID_Stop_Button.enabled = true;
ID_Back_Button.enabled = false;
ID_Mask.visible = true;
ID_fadeinMain.target = ID_Main;
ID_fadeinMain.play();
}

private function Func_Stopprogram():void{
var_timer.removeEventListener(TimerEvent.TIMER_COMPLETE,Func_Program);
ID_ImageBlackWhite.visible = false;
Func_BottomLeft(4);
ID_Start_Button.enabled = true;
ID_Stop_Button.enabled = false;
ID_Back_Button.enabled = true;
ID_Mask.visible = false;
ID_fadeoutMain.target = ID_Main;
ID_fadeoutMain.play();

//ID_fadeout_Mask.play();
}

private function Func_Program(e:Event):void{

if(var_NumberofSequence>=400){Func_Stopprogram();}
else{
Func_BlackWhitePic();
ID_text1.text = "Sequence No. " + String(var_NumberofSequence);
var_targetpicno = Math.floor(var_NumberofSequence/100);
Func_BottomLeft(var_sequencemixed[var_targetpicno]);//Select Target Picture
Func_CheckPics();
var_NumberofSequence++;
var_timer.start();
}

}

private function Func_ArrayMixer(var_array:Array):Array{
var newArray:Array = new Array();
while(var_array.length > 0)
{
Pos = Math.floor(Math.random()*var_array.length);
newArray[newArray.length]=var_array[Pos];
var_array.splice(Pos,1);

}
newArray.unshift(3);
return newArray;

}







]]>
</fx:Script>
<fx:Declarations>
<s:Fade id="ID_fadein" alphaFrom="0" alphaTo="0.7" duration="1000"/>
<s:Fade id="ID_fadeout" alphaFrom="0.7" alphaTo="0" duration="1000"/>
<s:Fade id="ID_fadeinMain" alphaFrom="0.7" alphaTo="1" duration="1000" />
<s:Fade id="ID_fadeoutMain" alphaFrom="1" alphaTo="0.7" duration="1000" />
<s:Fade id="ID_fadein_Mask" alphaFrom="0" alphaTo="1" duration="1000" target="ID_Mask"/>
<s:Fade id="ID_fadeout_Mask" alphaFrom="1" alphaTo="0" duration="1000" target="ID_Mask"/>
</fx:Declarations>
<s:BorderContainer height="100%" width="100%" borderVisible="false">
<s:backgroundFill><s:SolidColor color="#555555"/></s:backgroundFill>
</s:BorderContainer>
<mx:Image id="ID_BackgroundImage" source="pks1440gimp.jpg"/>
<s:BorderContainer id="ID_Mask" height="100%" width="100%" borderVisible="false" visible="false" showEffect="ID_fadein_Mask" hideEffect="ID_fadeout_Mask">
<s:backgroundFill><s:SolidColor color="#000000"/></s:backgroundFill>

</s:BorderContainer>

<s:BorderContainer id="ID_Start" borderVisible="false" visible="false" showEffect="ID_fadein" hideEffect="ID_fadeout" alpha="0.7" >
<s:backgroundFill><s:SolidColor alpha="0"/></s:backgroundFill>
<s:Button x="50" y="50" height="70" width="70" label="Start" color="green" fontWeight="bold" click="Func_Stage()" />
<s:Button x="400" y="400" height="60" width="60" label="About" click="Func_About()" />
</s:BorderContainer>

<s:BorderContainer id="ID_About" borderVisible="false" alpha="0.7" visible="false" showEffect="ID_fadein" hideEffect="ID_fadeout" >
<s:backgroundFill><s:SolidColor alpha="0"/></s:backgroundFill>

<s:TextArea id="ID_AboutTextArea" x="10" y="10" height="400" width="400" editable="false">
<s:span fontWeight="bold">PKS</s:span><s:br/>
Version: 1.0<s:br/>
Email: vavadeve@outlook.com<s:br/>
Author: Vali Van de Ven<s:br/>
2013-2014<s:br/>
<s:br/>
PKS is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
<s:br/>
<s:br/>
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
<s:br/>
<s:span fontWeight="bold">http://www.gnu.org/licenses/</s:span>
<s:br/>
<s:br/>
<s:span fontWeight="bold">Function of the program (short)</s:span> <s:br/>
The program, PKS, generates a black/white image from a webcam stream and compares it with another black/white image. The program doesn't collect and upload any data. Connection to the internet is not necessary.<s:br/>
<s:br/>
<s:span fontWeight="bold">Function of the program</s:span><s:br/>
If you start PKS a camera should already connected to your computer.<s:br/>
1. PKS creates an csv on your desktop. This csv is used for data storing during the running of the programm. Note: If you restart PKS or the test sequence this csv is overwrite.<s:br/>
2. If you press start you get to the main screen. On top left you see the webcam stream which is scaled to 250*250 Pixel.<s:br/>
3. Press start in bottom right square.<s:br/>
4. From now every 2 sec a b/w image is created (approx. 2 sec speed depends on your hardware). and data is stored in the csv. For the first 100he image is not visible, this is the initialization. After the first 100 images the b/w image appears top right and the target image appears bottom left. There are 3 target images, a square, a triangle and a circle. Ever image lasts for 100 times. The order of appearance of the images is pseudo random. After 400 images, about 13 minutes, the run is over.<s:br/>
5. Close the program and check the csv for evidence.<s:br/>
6. Note: If you restart the program the the csv is overwrite without any notification. <s:br/>
<s:br/>
<s:span fontWeight="bold">About the black/white images:</s:span> The webcam stream is scaled to 250*250. Every pixel is check by its red, green and blue value (from 0 to 255) if 2 or more values are even the pixel is black if 2 or more values are odd the pixel is white.<s:br/>
<s:br/>
<s:span fontWeight="bold">About the target images:</s:span> All target images consists of 46875 (75%) white pixels.<s:br/>
<s:br/>
<s:span fontWeight="bold">Disclaimer of Warranty:</s:span> PKS is provided without warranties of any kind. <s:br/>

</s:TextArea>
<s:Button x="10" y="430" label="Back" enabled="true" click="Func_Start()"/>
</s:BorderContainer>


<s:BorderContainer id="ID_Main" borderVisible="false" visible="false" alpha="0.7" x="10" y="10" showEffect="ID_fadein" hideEffect="ID_fadeout" >
<s:backgroundFill><s:SolidColor alpha="0"/></s:backgroundFill>
<s:BorderContainer id="ID_TopLeft" borderVisible="false" >
<s:VideoDisplay id="ID_Video"/>
<mx:Text id="ID_Error_Video" x="20" y="20" color="#FF0000" fontWeight="bold"></mx:Text>
</s:BorderContainer>
<s:BorderContainer id="ID_TopRight" borderVisible="false" >
<mx:Image id="ID_ImageBlackWhite">
</mx:Image>
</s:BorderContainer>
<s:BorderContainer id="ID_BottomLeft" borderVisible="false" >
<mx:Image id="ID_blackbox" source="blackbox.png" visible="false"/>
<mx:Image id="ID_blackcircle" source="blackcircle.png" visible="false"/>
<mx:Image id="ID_blacktriangel" source="blacktriangel.png" visible="false"/>
<mx:Image id="ID_Initialization" source="Initialization.png" visible="false"/>
<mx:Image id="ID_Finished" source="Finished.png" visible="false"/>
</s:BorderContainer>
<s:BorderContainer id="ID_BottomRight" borderVisible="false" enabled="false">
<s:backgroundFill><s:SolidColor color="#0000FF"/></s:backgroundFill>
<s:Button id="ID_Start_Button" label="Start" height="50" width="50" enabled="true" click="Func_Startprogram()" y="20" x="20" />
<s:Button id="ID_Stop_Button" label="Stop" height="50" width="50" enabled="true" click="Func_Stopprogram()" y="20" x="180"/>
<s:Button id="ID_Back_Button" label="Back" height="50" width="50" enabled="true" click="Func_Start()" y="180" x="180" />
<mx:Text id="ID_text1" y="125" x="20" color="white" fontWeight="bold" fontSize="15"/>
</s:BorderContainer>
</s:BorderContainer>

</s:WindowedApplication>