Grab#

#include <stdio.h>
#include <stdlib.h>
#include "ItalaApiC/ItalaC.h"

#define ACQUIRE_COUNT 50

ItalaError ErrorManager(ItalaError e) {
  size_t sizeMessage = 255;
  char* message = (char*)malloc(sizeof(char) * sizeMessage);
  ERR_GetLastErrorMessage(message, &sizeMessage);
  printf("\nITALA ERROR (%d):\t%s, %zu char", ERR_GetLastErrorCode(), message, sizeMessage);
  free(message);
  return e;
}

int main(int argc, char** argv) {
  printf("***** Grab example started. *****\n");
  ItalaError error = ItalaErrorSuccess;

  error = SYS_Initialize();
  if (error) return ErrorManager(error);

  size_t devicesInfoSize = 0;
  error = SYS_EnumerateDevices(700);
  if (error) return ErrorManager(error);
  error = SYS_GetDeviceCount(&devicesInfoSize);
  if (error) return ErrorManager(error);
  if (devicesInfoSize == 0) {
    printf("No devices found. Example canceled.\n");
    return ItalaErrorError;
  }
  DeviceInfo deviceInfo;
  error = SYS_GetDeviceByIndex(0, &deviceInfo);
  if (error) return ErrorManager(error);
  if (deviceInfo.AccessStatus != AvailableReadWrite) {
    printf("Target device is unaccessible in RW mode. Example canceled.\n");
    return ItalaErrorError;
  }

  H_DEVICE hDevice = NULL;
  error = SYS_CreateDevice(deviceInfo, &hDevice);
  if (error) return ErrorManager(error);
  printf("First device found is initialized.\n");

  H_NODEMAP hNodeMap = NULL;
  error = DEV_GetNodeMap(hDevice, &hNodeMap);
  if (error) return ErrorManager(error);

  // Start a default continuous acquisition. Internally, the runtime allocates a
  // queue of 16 image buffers thus a maximum of 16 images can be grabbed simultaneously
  // without being released. An additional overload function allows to set the
  // buffer and frame count.
  error = DEV_StartAcquisition(hDevice);
  if (error) return ErrorManager(error);
  printf("Acquisition started.\n\n");

  uint64_t frameId = 0;
  uint64_t timestamp = 0;
  H_IMAGE hImage = NULL;
  bool isIncomplete = false;
  // Perform a continuous acquisition by looping ACQUIRE_COUNT times and
  // and grab a new image at every loop.
  for (size_t i = 0; i < ACQUIRE_COUNT; i++) {
    // Grab the first available image. If the specified
    // 1000ms timeout expires, a TimeoutException occurs. Don't forget to
    // dispose the image when it's no longer needed.
    error = DEV_GetNextImage(hDevice, 1000, &hImage);

    // Check if the image data is incomplete. This may happen due to packets
    // lost during transmission. Packets could get lost for many of reasons
    // resulting from an incorrect network setup, bad camera configuration,
    // poor hardware quality or inadequate conditions of the environment.
    error = IMG_IsIncomplete(hImage, &isIncomplete);
    if (error) return ErrorManager(error);
    if (isIncomplete) {
      // Report some info if the image is partially filled.
      printf("Incomplete image.\n");
      size_t filled = 0;
      size_t payloadSize = 0;
      error = IMG_GetBytesFilled(hImage, &filled);
      if (error) return ErrorManager(error);
      error = IMG_GetPayloadSize(hImage, &payloadSize);
      if (error) return ErrorManager(error);
      printf("\tBytes filled: %zu/%zu", filled, payloadSize);
    } else {
      printf("Image grabbed.\n");
    }

    error = IMG_GetFrameID(hImage, &frameId);
    if (error) return ErrorManager(error);
    error = IMG_GetTimestamp(hImage, &timestamp);
    if (error) return ErrorManager(error);
    printf("\tFrameID: %jd\n", frameId);
    printf("\tTimestamp: %jd\n\n", timestamp);

    // DO SOMETHING WITH THE IMAGE..

    // When the image is no longer required (e.g. when the current loop is over)
    // it MUST be disposed by calling the dedicated function. When an image is
    // disposed, its memory is released and it can be reused for a new grab.
    error = IMG_Dispose(hImage);
    hImage = NULL;
    if (error) return ErrorManager(error);
  }

  error = DEV_StopAcquisition(hDevice);
  if (error) return ErrorManager(error);

  error = DEV_Dispose(hDevice);
  hDevice = NULL;
  if (error) return ErrorManager(error);
  printf("Device instance disposed.\n");

  error = SYS_Dispose();
  if (error) return ErrorManager(error);
  printf("System instance disposed.\n");
  return 0;
}