IDS Peak comfortSDK, genericSDK, IPL, and AFL developer manuals are external documents. Please contact us if you need them.
Creating buffers yourself and announcing them to the IDS peak API
1.Query the PayloadSize (required size in bytes) for a buffer.
2.Query for the minimum number of buffers required.
3.Allocate the buffer yourself.
4.Announce the buffer to the system ("AnnounceBuffer").
5.Add the buffer to the "Buffer Pool" ("QueueBuffer").
comfortC
|
peak_status status = PEAK_STATUS_SUCCESS;
size_t requiredBufferSize = 0;
status = peak_Acquisition_Buffer_GetRequiredSize(hCam, &requiredBufferSize);
if (PEAK_ERROR(status)) { /* Error handling ... */ }
size_t requiredBufferCount = 0;
peak_Acquisition_Buffer_GetRequiredCount(hCam, &requiredBufferCount);
if (PEAK_ERROR(status)) { /* Error handling ... */ }
for(size_t count = 0; count < requiredBufferCount; count++)
{
uint8_t* rawBuffer = (uint8_t*) malloc(requiredBufferSize * sizeof(uint8_t);
status = peak_Acquisition_Buffer_Announce(hCam, rawBuffer, requiredBufferSize, NULL);
if (PEAK_ERROR(status)) { /* Error handling ... */ }
}
|
genericC++
|
if (dataStream)
{
int64_t payloadSize = nodemapRemoteDevice->FindNode<peak::core::nodes::IntegerNode>("PayloadSize")->Value();
// Get number of minimum required buffers
int numBuffersMinRequired = dataStream->NumBuffersAnnouncedMinRequired();
// Alloc buffers
for (size_t count = 0; count < numBuffersMinRequired ; count++)
{
uint8_t* rawBuffer = new uint8_t[payloadSize];
auto buffer = dataStream->AnnounceBuffer(rawBuffer, static_cast<size_t>(payloadSize), nullptr, nullptr);
dataStream->QueueBuffer(buffer);
}
}
|
Removing buffers from the buffer pool after the end of image acquisition and deleting them yourself
1.Revoke the buffer from the "Buffer Pool" ("RevokeBuffer").
2.Delete the raw buffer you created yourself.
comfortC
|
peak_status status = PEAK_STATUS_SUCCESS;
status = peak_Acquisition_Buffer_RevokeAll(hCam);
if (PEAK_ERROR(status)) { /* Error handling ... */ }
// you still have to free the buffers you allocated with malloc
|
genericC++
|
if (dataStream)
{
dataStream->Flush(peak::core::DataStreamFlushMode::DiscardAll);
for (const auto& buffer : m_dataStream->AnnouncedBuffers())
{
uint8_t* rawBuffer = static_cast<uint8_t*>(buffer->BasePtr());
dataStream->RevokeBuffer(buffer);
delete rawBuffer;
}
}
|
Other solution: Create the buffer yourself, announce it to the IDS peak API and pass the function to delete it.
There is another solution. You can pass the function to delete the self-allocated buffer as lambda. This is a modern construct in C++.
1.Query the PayloadSize (required size in bytes) for a buffer.
2.Query for the minimum number of buffers required.
3.Allocate the buffer yourself.
4.Announce the buffer to the system ("AnnounceBuffer").
5.Pass the delete function as "lambda".
6.Add the buffer to the "Buffer Pool" ("QueueBuffer").
genericC++
|
if (dataStream)
{
int64_t payloadSize = nodemapRemoteDevice->FindNode<peak::core::nodes::IntegerNode>("PayloadSize")->Value();
// Get number of minimum required buffers
int numBuffersMinRequired = dataStream->NumBuffersAnnouncedMinRequired();
// Alloc buffers
for (size_t count = 0; count < numBuffersMinRequired ; count++)
{
uint8_t* rawBuffer = new uint8_t[payloadSize];
auto buffer = datastream->AnnounceBuffer(rawBuffer, static_cast<size_t>(payloadSize), nullptr,
[](void* buffer, void* userPtr)
{
delete[] static_cast<uint8_t*>(buffer);
});
dataStream->QueueBuffer(buffer);
}
}
|
Procedure: Remove and release buffers from the buffer pool at the end of image acquisition
1.Revoke the buffer from the "Buffer Pool" ("RevokeBuffer")
2.The delete function specified above is automatically called by RevokeBuffer and deletes the self-created buffer.
genericC++
|
if (dataStream)
{
dataStream->Flush(peak::core::DataStreamFlushMode::DiscardAll);
for (const auto& buffer : dataStream->AnnouncedBuffers())
{
dataStream->RevokeBuffer(buffer);
}
}
|
Complete example: Before starting image acquisition, all existing buffers are deleted, released and new buffers are created
1.Specify all settings that affect the image size (Setting ROI (region of interest)).
2.Release the buffers that are already present (see above).
3.Create new buffers.
peak_status status = PEAK_STATUS_SUCCESS;
// First, set parameters that affect the buffer size e.g. setting a ROI etc.
size_t requiredBufferSize = 0;
status = peak_Acquisition_Buffer_GetRequiredSize(hCam, &requiredBufferSize);
if (PEAK_ERROR(status)) { /* Error handling ... */ }
size_t requiredBufferCount = 0;
peak_Acquisition_Buffer_GetRequiredCount(hCam, &requiredBufferCount);
if (PEAK_ERROR(status)) { /* Error handling ... */ }
// hold the pointers to the buffers to free them after use
uint8_t** rawBufferList = (uint8_t**) malloc( requiredBufferSize * sizeof(uint8_t*));
for(size_t count = 0; count < requiredBufferCount; count++)
{
uint8_t* rawBuffer = (uint8_t*) malloc(requiredBufferSize * sizeof(uint8_t));
rawBufferList[count] = rawBuffer;
status = peak_Acquisition_Buffer_Announce(hCam, rawBuffer, requiredBufferSize, NULL);
if (PEAK_ERROR(status)) { /* Error handling ... */ }
}
|
// First, set parameters that affect the buffer size e.g. setting a ROI etc.
try
{
if (dataStream)
{
// Flush queue and prepare all buffers for revoking
dataStream->Flush(DataStreamFlushMode::DiscardAll);
// Clear all old buffers
for (const auto& buffer : dataStream->AnnouncedBuffers())
{
dataStream->RevokeBuffer(buffer);
}
int64_t payloadSize = nodemapRemoteDevice->FindNode<peak::core::nodes::IntegerNode>("PayloadSize")->Value();
// Get number of minimum required buffers
int numBuffersMinRequired = dataStream->NumBuffersAnnouncedMinRequired();
// Alloc buffers
for (size_t count = 0; count < numBuffersMinRequired ; count++)
{
auto buffer = dataStream->AllocAndAnnounceBuffer(static_cast<size_t>(payloadSize), nullptr);
dataStream->QueueBuffer(buffer);
}
}
}
catch (const std::exception& e)
{
// ...
}
|
// First, set parameters that affect the buffer size e.g. setting a ROI etc..
try
{
if (dataStream != null)
{
// Flush queue and prepare all buffers for revoking
dataStream.Flush(peak.core.DataStreamFlushMode.DiscardAll);
// Clear all old buffers
foreach (var buffer in dataStream.AnnouncedBuffers())
{
dataStream.RevokeBuffer(buffer);
}
var payloadSize = nodeMapRemoteDevice.FindNode<peak.core.nodes.IntegerNode>("PayloadSize").Value();
// Get number of minimum required buffers
var numBuffersMinRequired = dataStream.NumBuffersAnnouncedMinRequired();
// Alloc buffers
for (var count = 0; count < numBuffersMinRequired; count++)
{
var buffer = dataStream.AllocAndAnnounceBuffer((uint)payloadSize, IntPtr.Zero);
dataStream.QueueBuffer(buffer);
}
}
}
catch (Exception e)
{
// ...
}
|
try:
if data_stream:
# Flush queue and prepare all buffers for revoking
data_stream.Flush(ids_peak.DataStreamFlushMode_DiscardAll)
# Clear all old buffers
for buffer in data_stream.AnnouncedBuffers():
data_stream.RevokeBuffer(buffer)
payload_size = node_map_remote_device.FindNode("PayloadSize").Value()
# Get number of minimum required buffers
num_buffers_min_required = data_stream.NumBuffersAnnouncedMinRequired()
# Alloc buffers
for count in range(num_buffers_min_required):
buffer = data_stream.AllocAndAnnounceBuffer(payload_size)
data_stream.QueueBuffer(buffer)
except Exception as e:
# ...
str_error = str(e)
|