This example demonstrates setting up the user allocated memory just before the acquisition of images. First, the size of each buffer is determined based on the data payload size. Then, depending on the the number of buffers (numBuffers) specified, the corresponding amount of memory is allocated. Finally, after setting the buffer ownership to be users, the image acquisition can commence.
It is important to note that if the user provides the memory for the buffers, the user is ultimately responsible for freeing up memory.
#include <iostream>
#include <sstream>
using namespace std;
{
INodeMap& nodeMapTLDevice = pCam->GetTLDeviceNodeMap();
INodeMap& nodeMap = pCam->GetNodeMap();
if (!IsReadable(ptrDeviceType))
{
return -1;
}
if (ptrDeviceType->GetIntValue() != DeviceType_GigEVision)
{
return 0;
}
if (enableHeartbeat)
{
cout << endl << "Resetting heartbeat..." << endl << endl;
}
else
{
cout << endl << "Disabling heartbeat..." << endl << endl;
}
CBooleanPtr ptrDeviceHeartbeat = nodeMap.GetNode(
"GevGVCPHeartbeatDisable");
if (!IsWritable(ptrDeviceHeartbeat))
{
cout << "Unable to configure heartbeat. Continuing with execution as this may be non-fatal..." << endl << endl;
}
else
{
ptrDeviceHeartbeat->SetValue(!enableHeartbeat);
if (!enableHeartbeat)
{
cout << "WARNING: Heartbeat has been disabled for the rest of this example run." << endl;
cout << " Heartbeat will be reset upon the completion of this run. If the " << endl;
cout << " example is aborted unexpectedly before the heartbeat is reset, the" << endl;
cout << " camera may need to be power cycled to reset the heartbeat." << endl << endl;
}
else
{
cout << "Heartbeat has been reset." << endl;
}
}
return 0;
}
{
}
{
}
{
int result = 0;
cout << endl << endl << "*** IMAGE ACQUISITION ***" << endl << endl;
try
{
{
cout << "Unable to get or set acquisition mode to continuous (enum retrieval). Aborting..." << endl << endl;
return -1;
}
CEnumEntryPtr ptrAcquisitionModeContinuous = ptrAcquisitionMode->GetEntryByName(
"Continuous");
{
cout << "Unable to set acquisition mode to continuous (entry retrieval). Aborting..." << endl << endl;
return -1;
}
const int64_t acquisitionModeContinuous = ptrAcquisitionModeContinuous->GetValue();
ptrAcquisitionMode->SetIntValue(acquisitionModeContinuous);
const INodeMap& sNodeMap = pCam->GetTLStreamNodeMap();
CEnumerationPtr ptrStreamBufferCountMode = sNodeMap.GetNode(
"StreamBufferCountMode");
{
cout << "Unable to get or set Buffer Count Mode (node retrieval). Aborting..." << endl << endl;
return -1;
}
CEnumEntryPtr ptrStreamBufferCountModeManual = ptrStreamBufferCountMode->GetEntryByName(
"Manual");
{
cout << "Unable to get Buffer Count Mode entry (Entry retrieval). Aborting..." << endl << endl;
return -1;
}
ptrStreamBufferCountMode->SetIntValue(ptrStreamBufferCountModeManual->GetValue());
cout << "Stream Buffer Count Mode set to manual..." << endl;
cout << "Acquisition mode set to continuous..." << endl;
CIntegerPtr ptrPayloadSize = nodeMap.GetNode(
"PayloadSize");
{
cout << "Unable to determine the payload size from the nodemap. Aborting..." << endl << endl;
return -1;
}
uint64_t bufferSize = ptrPayloadSize->GetValue();
CEnumerationPtr ptrDeviceType = pCam->GetTLDeviceNodeMap().GetNode(
"DeviceType");
if (ptrDeviceType != nullptr && ptrDeviceType->GetIntValue() == DeviceType_USB3Vision)
{
const uint64_t usbPacketSize = 1024;
bufferSize = ((bufferSize + usbPacketSize - 1) / usbPacketSize) * usbPacketSize;
}
unique_ptr<unsigned char[]> pMemBuffersContiguous;
vector<unique_ptr<unsigned char[]>> ppMemBuffersNonContiguous;
vector<void*> ppMemBuffersNonContiguousVoid;
{
pCam->SetBufferOwnership(SPINNAKER_BUFFER_OWNERSHIP_USER);
}
{
try
{
pMemBuffersContiguous =
unique_ptr<unsigned char[]>(
new unsigned char[
numBuffers *
static_cast<unsigned int>(bufferSize)]);
}
catch (bad_alloc& )
{
cout << "Unable to allocate the memory required. Aborting..." << endl << endl;
return -1;
}
pCam->SetUserBuffers(pMemBuffersContiguous.get(),
numBuffers * bufferSize);
cout << "User-allocated memory 0x" << hex << static_cast<void*>(&pMemBuffersContiguous)
<< " will be used for user buffers..." << endl;
}
else
{
try
{
{
ppMemBuffersNonContiguous.emplace_back(
unique_ptr<unsigned char[]>(new unsigned char[static_cast<unsigned int>(bufferSize)]));
}
}
catch (bad_alloc& )
{
cout << "Unable to allocate the memory required. Aborting..." << endl << endl;
return -1;
}
for (unsigned int i = 0; i < ppMemBuffersNonContiguous.size(); i++)
{
ppMemBuffersNonContiguousVoid.emplace_back(ppMemBuffersNonContiguous.at(i).get());
}
const uint64_t bufferCount = ppMemBuffersNonContiguousVoid.size();
pCam->SetUserBuffers(ppMemBuffersNonContiguousVoid.data(), bufferCount, bufferSize);
cout << "User-allocated memory:" << endl;
for (size_t i = 0; i < ppMemBuffersNonContiguousVoid.size(); i++)
{
cout << "\t0x" << hex << &ppMemBuffersNonContiguousVoid.at(i) << endl;
}
cout << "will be used for user buffers..." << endl;
}
pCam->BeginAcquisition();
CIntegerPtr ptrStreamBufferCountResult = sNodeMap.GetNode(
"StreamBufferCountResult");
{
cout << "Unable to retrieve Buffer Count result (node retrieval). Aborting..." << endl << endl;
return -1;
}
const int64_t streamBufferCountResult = ptrStreamBufferCountResult->GetValue();
cout << "Resulting stream buffer count: " << dec << streamBufferCountResult << "." << endl << endl;
cout << "Acquiring images..." << endl;
CStringPtr ptrStringSerial = nodeMapTLDevice.GetNode(
"DeviceSerialNumber");
{
deviceSerialNumber = ptrStringSerial->GetValue();
cout << "Device serial number retrieved as " << deviceSerialNumber << "..." << endl;
}
cout << endl;
for (
unsigned int imageCnt = 0; imageCnt <
k_numImages; imageCnt++)
{
try
{
ImagePtr pResultImage = pCam->GetNextImage(1000);
if (pResultImage->IsIncomplete())
{
cout << "Image incomplete: " << Image::GetImageStatusDescription(pResultImage->GetImageStatus())
<< "..." << endl
<< endl;
}
else
{
const size_t width = pResultImage->GetWidth();
const size_t height = pResultImage->GetHeight();
cout << "Grabbed image " << imageCnt << ", width = " << width << ", height = " << height << endl;
ImagePtr convertedImage = processor.
Convert(pResultImage, PixelFormat_Mono8);
ostringstream filename;
filename << "AcquisitionUserBuffer-";
if (!deviceSerialNumber.empty())
{
filename << deviceSerialNumber.c_str() << "-";
}
filename << imageCnt << ".jpg";
convertedImage->Save(filename.str().c_str());
cout << "Image saved at " << filename.str() << endl;
}
pResultImage->Release();
cout << endl;
}
{
cout <<
"Error: " << e.
what() << endl;
result = -1;
}
}
pCam->EndAcquisition();
}
{
cout <<
"Error: " << e.
what() << endl;
result = -1;
}
if (pCam->GetBufferOwnership() != SPINNAKER_BUFFER_OWNERSHIP_SYSTEM)
{
pCam->SetBufferOwnership(SPINNAKER_BUFFER_OWNERSHIP_SYSTEM);
}
return result;
}
{
cout << endl << "*** DEVICE INFORMATION ***" << endl << endl;
int result = 0;
try
{
FeatureList_t features;
const CCategoryPtr category = nodeMap.GetNode(
"DeviceInformation");
{
category->GetFeatures(features);
for (auto it = features.begin(); it != features.end(); ++it)
{
cout << pfeatureNode->GetName() << " : ";
cout << (
IsReadable(pValue) ? pValue->ToString() :
"Node not readable");
cout << endl;
}
}
else
{
cout << "Device control information not readable." << endl;
}
}
{
cout <<
"Error: " << e.
what() << endl;
result = -1;
}
return result;
}
{
int result = 0;
try
{
INodeMap& nodeMapTLDevice = pCam->GetTLDeviceNodeMap();
pCam->Init();
#ifdef _DEBUG
#else
#endif
#ifdef _DEBUG
#endif
pCam->DeInit();
}
{
cout <<
"Error: " << e.
what() << endl;
result = -1;
}
return result;
}
{
FILE* tempFile = fopen("test.txt", "w+");
if (tempFile == nullptr)
{
cout << "Failed to create file in current folder. Please check "
"permissions."
<< endl;
cout << "Press Enter to exit..." << endl;
getchar();
return -1;
}
fclose(tempFile);
remove("test.txt");
cout << "Application build date: " << __DATE__ << " " << __TIME__ << endl << endl;
const LibraryVersion spinnakerLibraryVersion = system->GetLibraryVersion();
cout <<
"Spinnaker library version: " << spinnakerLibraryVersion.
major <<
"." << spinnakerLibraryVersion.
minor
<<
"." << spinnakerLibraryVersion.
type <<
"." << spinnakerLibraryVersion.
build << endl
<< endl;
const unsigned int numCameras = camList.
GetSize();
cout << "Number of cameras detected: " << numCameras << endl << endl;
if (numCameras == 0)
{
system->ReleaseInstance();
cout << "Not enough cameras!" << endl;
cout << "Done! Press Enter to exit..." << endl;
getchar();
return -1;
}
int result = 0;
for (unsigned int i = 0; i < numCameras; i++)
{
cout << endl << "Running example for camera " << i << "..." << endl;
cout << "Camera " << i << " example complete..." << endl << endl;
}
pCam = nullptr;
system->ReleaseInstance();
cout << endl << "Done! Press Enter to exit..." << endl;
getchar();
return result;
}
int AcquireImages(CameraPtr pCam, INodeMap &nodeMap, INodeMap &nodeMapTLDevice)
Definition Acquisition.cpp:199
int main(int, char **)
Definition Acquisition.cpp:527
int ResetGVCPHeartbeat(CameraPtr pCam)
Definition Acquisition.cpp:138
int ConfigureGVCPHeartbeat(CameraPtr pCam, bool enableHeartbeat)
Definition Acquisition.cpp:68
int RunSingleCamera(CameraPtr pCam)
Definition Acquisition.cpp:479
int PrintDeviceInfo(INodeMap &nodeMap)
Definition Acquisition.cpp:441
int DisableGVCPHeartbeat(CameraPtr pCam)
Definition Acquisition.cpp:143
const unsigned int k_numImages
Definition AcquisitionMultipleCamerasWriteToFile.cpp:55
const bool isContiguous
Definition AcquisitionUserBuffer.cpp:53
constexpr int numBuffers
Definition BufferHandling.cpp:46
Used to hold a list of camera objects.
Definition CameraList.h:42
void Clear()
Clears the list of cameras and destroys their corresponding reference counted objects.
CameraPtr GetByIndex(unsigned int index) const
Returns a pointer to a camera object at the "index".
unsigned int GetSize() const
Returns the size of the camera list.
A reference tracked pointer to a camera object.
Definition CameraPtr.h:44
The Exception object represents an error that is returned from the library.
Definition Exception.h:51
virtual const char * what() const
virtual override for what().
Encapsulates a GenApi pointer dealing with the dynamic_cast automatically.
Definition Pointer.h:75
Image post processing class for converting a source image to another pixel format.
Definition ImageProcessor.h:159
void SetColorProcessing(ColorProcessingAlgorithm colorAlgorithm)
Sets the color processing algorithm used at the time of the Convert() call, therefore the most recent...
ImagePtr Convert(const ImagePtr &srcImage, PixelFormatEnums destFormat) const
Converts the source image buffer to the specified destination pixel format and returns the result in ...
A reference tracked pointer to an image object.
Definition ImagePtr.h:46
A reference tracked pointer to a system object.
Definition SystemPtr.h:44
bool IsWritable(EAccessMode AccessMode)
Tests if writable.
Definition INode.h:277
bool IsReadable(EAccessMode AccessMode)
Tests if readable.
Definition INode.h:253
interface SPINNAKER_API_ABSTRACT INodeMap
Interface to access the node map.
Definition INodeMap.h:54
@ SPINNAKER_BUFFER_OWNERSHIP_USER
Definition SpinnakerDefs.h:381
Definition Autovector.h:36
Provides easier access to the current version of Spinnaker.
Definition SpinnakerDefs.h:658
unsigned int minor
Minor version of the library.
Definition SpinnakerDefs.h:663
unsigned int major
Major version of the library.
Definition SpinnakerDefs.h:660
unsigned int type
Version type of the library.
Definition SpinnakerDefs.h:666
unsigned int build
Build number of the library.
Definition SpinnakerDefs.h:669