int main(int argc, char** argv)
{        
    fc2Error error;
    fc2Context context;
    fc2PGRGuid guid;
    unsigned int numCameras = 0;
    fc2CameraInfo camInfo[10];
    unsigned int numCamInfo = 10;
    FILE* tempFile;
    unsigned int i;

    PrintBuildInfo();

    // Since this application saves images in the current folder
    // we must ensure that we have permission to write to this folder.
    // If we do not have permission, fail right away.
    tempFile = fopen("test.txt", "w+");
    if (tempFile == NULL)
    {
        printf("Failed to create file in current folder.  Please check permissions.\n");
        return -1;
    }
    fclose(tempFile);
    remove("test.txt");    

    error = fc2CreateGigEContext( &context );
    if ( error != FC2_ERROR_OK )
    {
        printf( "Error in fc2CreateContext: %s\n", fc2ErrorToDescription(error) );
        return -1;
    }        

    error = fc2DiscoverGigECameras( context, camInfo, &numCamInfo);
    if ( error != FC2_ERROR_OK )
    {
        printf( "Error in fc2DiscoverGigECameras: %s\n", fc2ErrorToDescription(error) );
        return -1;
    }

    printf( "Number of cameras discovered: %u\n", numCamInfo );

    for ( i=0; i < numCamInfo; i++)
    {
        PrintCameraInfo( &camInfo[i] );
    }

    error = fc2GetNumOfCameras( context, &numCameras );
    if ( error != FC2_ERROR_OK )
    {
        printf( "Error in fc2GetNumOfCameras: %s\n", fc2ErrorToDescription(error) );
        return 0;
    }

    if ( numCameras == 0 )
    {
        // No cameras detected
        printf( "No cameras detected.\n");
        return 0;
    }

    for (i=0; i < numCameras; i++)
    {
        fc2InterfaceType interfaceType;

        error = fc2GetCameraFromIndex( context, i, &guid);
        if ( error != FC2_ERROR_OK )
        {
            printf( "Error in fc2GetCameraFromIndex: %s\n", fc2ErrorToDescription(error) );
            return -1;
        }    

        error = fc2GetInterfaceTypeFromGuid( context, &guid, &interfaceType );
        if ( error != FC2_ERROR_OK )
        {
            printf( "Error in fc2GetInterfaceTypFromGuid: %s\n", fc2ErrorToDescription(error) );
            return -1;
        }    

        if ( interfaceType == FC2_INTERFACE_GIGE )
        {
            RunSingleCamera( context, guid);
        }
    }

	error = fc2DestroyContext( context );
	if ( error != FC2_ERROR_OK )
	{
		printf( "Error in fc2DestroyContext: %d\n", error );
		return 0;
	}

    printf( "Done! Press Enter to exit...\n" );
    getchar();

	return 0;
}
void showFC2Error(fc2Error error) {
  if (error != FC2_ERROR_OK) {
    SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "FlyCapture2 Error",
                             fc2ErrorToDescription(error), NULL);
  }
}
int RunSingleCamera( fc2Context context, fc2PGRGuid guid)
{
    const int k_numImages = 1000;

    fc2Error error;
    fc2CameraInfo camInfo;
    unsigned int numStreamChannels = 0;
    fc2GigEImageSettingsInfo imageSettingsInfo;
    fc2Image rawImage, convertedImage;    
    fc2GigEImageSettings imageSettings;
    int imageCnt;
    unsigned int i;
    char filename[512];

    printf( "Connecting to camera...\n" );

    // Connect to a camera
    error = fc2Connect( context, &guid );
    if ( error != FC2_ERROR_OK )
    {
        printf( "Error in fc2Connect: %s\n", fc2ErrorToDescription(error) );
        return -1;
    }

    // Get the camera information
    error = fc2GetCameraInfo( context, &camInfo);
    if ( error != FC2_ERROR_OK )
    {
        printf( "Error in fc2GetCameraInfo: %s\n", fc2ErrorToDescription(error) );
        return -1;
    }

    PrintCameraInfo(&camInfo);        

    error = fc2GetNumStreamChannels( context, &numStreamChannels );
    if ( error != FC2_ERROR_OK )
    {
        printf( "Error in fc2GetNumStreamChannels: %s\n", fc2ErrorToDescription(error) );
        return -1;
    }

    for ( i = 0; i < numStreamChannels; i++)
    {
        fc2GigEStreamChannel streamChannel;
        error = fc2GetGigEStreamChannelInfo( context, i, &streamChannel );
        if ( error != FC2_ERROR_OK )
        {
            printf( "Error in fc2GetGigEStreamChannelInfo: %s\n", fc2ErrorToDescription(error) );
            return -1;
        }

        printf( "\nPrinting stream channel information for channel %u:\n", i );
        PrintStreamChannelInfo( &streamChannel );
    }    

    printf( "Querying GigE image setting information...\n" );

    error = fc2GetGigEImageSettingsInfo( context, &imageSettingsInfo );
    if ( error != FC2_ERROR_OK )
    {
        printf( "Error in fc2GetGigEImageSettingsInfo: %s\n", fc2ErrorToDescription(error) );
        return -1;
    }

    imageSettings.offsetX = 0;
    imageSettings.offsetY = 0;
    imageSettings.height = imageSettingsInfo.maxHeight;
    imageSettings.width = imageSettingsInfo.maxWidth;
    imageSettings.pixelFormat = FC2_PIXEL_FORMAT_MONO8;

    printf( "Setting GigE image settings...\n" );

    error = fc2SetGigEImageSettings( context, &imageSettings );
    if ( error != FC2_ERROR_OK )
    {
        printf( "Error in fc2SetGigEImageSettings: %s\n", fc2ErrorToDescription(error) );
        return -1;
    }

    printf( "Starting image capture...\n" );

    // Start capturing images
    error = fc2StartCapture( context);
    if ( error != FC2_ERROR_OK )
    {
        printf( "Error in fc2StartCapture: %s\n", fc2ErrorToDescription(error) );
        return -1;
    }

    // Prepare images
    error = fc2CreateImage( &rawImage );
    if ( error != FC2_ERROR_OK )
    {
        printf( "Error in fc2CreateImage: %s\n", fc2ErrorToDescription(error) );
        return -1;
    }

    error = fc2CreateImage( &convertedImage );
    if ( error != FC2_ERROR_OK )
    {
        printf( "Error in fc2CreateImage: %s\n", fc2ErrorToDescription(error) );
        return -1;
    }

    for ( imageCnt=0; imageCnt < k_numImages; imageCnt++ )
    {              
        // Retrieve an image
        error = fc2RetrieveBuffer( context, &rawImage );
        if ( error != FC2_ERROR_OK )
        {
            printf( "Error in fc2RetrieveBuffer: %s\n", fc2ErrorToDescription(error) );
            return -1;
        }

        printf( "Grabbed image %d\n", imageCnt );
        
        // Convert the raw image
        error = fc2ConvertImageTo( FC2_PIXEL_FORMAT_MONO8, 
                                &rawImage,
                                &convertedImage );
        if ( error != FC2_ERROR_OK )
        {
            printf( "Error in fc2ConvertImage: %s\n", fc2ErrorToDescription(error) );
            return -1;
        }

        // Create a unique filename
        sprintf( filename, "GigEGrabEx-%u-%d.pgm", camInfo.serialNumber, imageCnt );

        /*
        // Save the image. If a file format is not passed in, then the file
        // extension is parsed to attempt to determine the file format.
        error = fc2SaveImage( &convertedImage, filename, FC2_PGM );
        if ( error != FC2_ERROR_OK )
        {
            printf( "Error in fc2SaveImage: %s\n", fc2ErrorToDescription(error) );
            return -1;
        }
        */
    }         

    printf( "Stopping capture...\n" );

    // Stop capturing images
    error = fc2StopCapture( context);
    if ( error != FC2_ERROR_OK )
    {
        printf( "Error in fc2StopCapture: %s\n", fc2ErrorToDescription(error) );
    }

    // Disconnect the camera
    error = fc2Disconnect( context);
    if ( error != FC2_ERROR_OK )
    {
        printf( "Error in fc2Disconnect: %s\n", fc2ErrorToDescription(error) );
    }

    error = fc2DestroyImage( &rawImage );
    error = fc2DestroyImage( &convertedImage );

    return 0;
}