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
int AverageColour_c(unsigned char *generalArray, void **args, unsigned int argsCount, unsigned int *argSize)
{
// total number of pixels pass onto us
unsigned int arraySize = *(unsigned int*)args[0];
int i = 0, red = 0, green = 0, blue = 0;
if( generalArray == 0)
return -1;
// iterate through colour values
for(i = 54; i < arraySize;)
{
red += generalArray[i++];
green += generalArray[i++];
blue += generalArray[i++];
}
// extract how many pixels were there in total
int total = (arraySize - 54) / 3;
// compute average value for each colour
red /= total;
green /= total;
blue /= total;
// convert into final Color value
total = (blue << 16) | (green << 8) | red;
return total;
}
The total running time of the aforementioned RLP call is 214ms. The exact same code, encapsulated in a NETMF function call runs for a total of 16335ms. In other words, approximately 76 times faster. For a very simple piece of code.
The NET side of the code is pretty simple in itself:
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 47 48 49 50 51 52 53
RLP.Procedure AverageColour;
// This method is run when the mainboard is powered up or reset.
void ProgramStarted()
{
// Use Debug.Print to show messages in Visual Studio's "Output" window during debugging.
Debug.Print( "Program Started" );
InitializeRLP();
// setup Gadgeteer modules
button.ButtonReleased += new Button.ButtonEventHandler( button_ButtonReleased );
camera.PictureCaptured += new Camera.PictureCapturedEventHandler( camera_PictureCaptured );
camera.DebugPrintEnabled = false;
camera.CurrentPictureResolution = Camera.PictureResolution.Resolution320x240;
}
void InitializeRLP()
{
// personal unlock code
RLP.Unlock( "...", new byte[] { ... } );
// fetch, load and initialize our RLP
byte[] elf_file = Resources.GetBytes( Resources.BinaryResources.RLP_test );
RLP.LoadELF( elf_file );
RLP.InitializeBSSRegion( elf_file );
// extract the procedures
AverageColour = RLP.GetProcedure( elf_file, "AverageColour_c" );
// dispose of the loaded binary data we no longer need
elf_file = null;
Debug.GC( true );
}
void camera_PictureCaptured( Camera sender, GT.Picture picture )
{
const int pixels = 320 * 240;
int colour = AverageColour.InvokeEx( picture.PictureData, 54 + pixels * 3 );
Color average = (Color)colour;
Debug.Print( "Average colour: Red - " +
ColorUtility.GetRValue( average ) +
" Green - " +
ColorUtility.GetGValue( average ) +
" Blue - " +
ColorUtility.GetBValue( average ) );
}
void button_ButtonReleased( Button sender, Button.ButtonState state )
{
camera.TakePicture();
}
No comments:
Post a Comment