Grab#

/***********************************************************************************
 *
 * Itala API - Copyright (C) 2022 Opto Engineering
 *
 * THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY SUFFERED BY LICENSE AS
 * A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 *
 ***********************************************************************************/

/**
 * @example Grab.cpp
 *
 * @brief "Grab" example shows how to acquire images in a basic fashion after
 * a simple camera configuration is performed (exposure time, pixel format and
 * image size). Images are acquired in loops where a wait for a new available
 * image is performed and some data is printed if the image is successfully grabbed.
 * When a single loop is over the current image is disposed, i.e. returned to
 * Itala so that its memory can be released and made available for newer
 * acquisitions. The grabbed image can be displayed by third party libraries
 * by accessing its width, height, format and data informations.
 *
 * @see GrabTrigger.cpp
 * @see GrabChunks.cpp
 * @see DeviceConfiguration.cpp
 */

// Include ItalaApi
#include "ItalaApi/Itala.h"

// Include the GenICam library (for strings and exceptions)
#include "GenICam.h"

#define INDENT "\t"
#define ACQUIRE_COUNT 50

void Grab_Sample() {
  std::cout << "***** Grab example started. *****" << std::endl << std::endl;

  Itala::ISystem* pSystem = Itala::CreateSystem();
  Itala::DeviceInfoList deviceInfos = pSystem->EnumerateDevices(700);

  if (deviceInfos.size() == 0) throw GENERIC_EXCEPTION("No devices found. Example canceled.");

  if (deviceInfos[0].AccessStatus() != Itala::DeviceAccessStatus::AvailableReadWrite)
    throw GENERIC_EXCEPTION("Target device is unaccessible in RW mode. Example canceled.");

  Itala::IDevice* pDevice = pSystem->CreateDevice(deviceInfos[0]);

  std::cout << "First device initialized." << std::endl;

  // 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.
  pDevice->StartAcquisition();

  std::cout << "Acquisition started." << std::endl << std::endl;

  Itala::IImage* pImage = 0;

  // 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.
    pImage = pDevice->GetNextImage(1000);

    // 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.
    if (pImage->IsIncomplete()) {
      // Report some info if the image is partially filled.
      std::cout << "Incomplete image." << std::endl;
      std::cout << INDENT << "Bytes filled: " << pImage->GetBytesFilled() << "/"
                << pImage->GetPayloadSize() << std::endl;
    } else {
      std::cout << "Image grabbed." << std::endl;
    }

    std::cout << INDENT << "FrameID:" << pImage->GetFrameID() << std::endl;
    std::cout << INDENT << "Timestamp: " << pImage->GetTimestamp() << std::endl;
    std::cout << std::endl;

    // 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.
    pImage->Dispose();
    pImage = nullptr;
  }

  pDevice->StopAcquisition();
  std::cout << "Acquisition stopped." << std::endl << std::endl;

  pDevice->Dispose();
  pDevice = nullptr;
  std::cout << "Device instance disposed." << std::endl;

  pSystem->Dispose();
  pSystem = nullptr;
  std::cout << "System instance disposed." << std::endl;
}

int main(int /*argc*/, char** /*argv*/) {
  try {
    Grab_Sample();
  } catch (GenICam::GenericException& e) {
    std::cout << e.what() << std::endl;
  }
}