This example covers all of the following: the preparation of a camera to acquire compressed images (or compressed chunk images), image retrieval, image saving, loading compressed images from disk, reconstructing compressed images, and converting compressed images.
#include <iostream>
#include <sstream>
#include <fstream>
using namespace std;
{
size_t imageSize,
size_t width,
size_t height,
size_t xOffset,
size_t yOffset,
{
}
};
{
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");
{
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;
}
{
}
{
}
INodeMap& nodeMap,
INodeMap& nodeMapTLDevice,
vector<CompressedImageInfo>& compressedImageInfos)
{
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 get acquisition mode to continuous (entry retrieval). Aborting..." << endl << endl;
return -1;
}
const int64_t acquisitionModeContinuous = ptrAcquisitionModeContinuous->GetValue();
ptrAcquisitionMode->SetIntValue(acquisitionModeContinuous);
cout << "Acquisition mode set to continuous..." << endl;
pCam->BeginAcquisition();
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();
const bool isCompressed = pResultImage->IsCompressed();
cout << "Grabbed image " << imageCnt << ", width = " << width << ", height = " << height
<< ", IsCompressed = " << (isCompressed ? "true" : "false");
{
ChunkData chunkData = pResultImage->GetChunkData();
const int64_t chunkImageCRC = chunkData.
GetCRC();
cout << ", compression ratio = " << compressionRatio << ", CRC = " << chunkImageCRC;
}
cout << endl;
if (pResultImage->HasCRC())
{
if (!pResultImage->CheckCRC())
{
cout << "WARNING: CRC mismatch could lead to image decompression failures" << endl;
}
}
ostringstream filename;
filename << "Compression-";
if (!deviceSerialNumber.empty())
{
filename << deviceSerialNumber.c_str() << "-";
}
filename << imageCnt;
cout << "Image saved at " << filename.str() << ".raw" << endl;
filename.str(),
pResultImage->GetImageSize(),
width,
height,
pResultImage->GetXOffset(),
pResultImage->GetYOffset(),
pResultImage->GetPixelFormat());
compressedImageInfos.push_back(imageInfo);
}
pResultImage->Release();
cout << endl;
}
{
cout <<
"Error: " << e.
what() << endl;
result = -1;
}
}
pCam->EndAcquisition();
}
{
cout <<
"Error: " << e.
what() << endl;
return -1;
}
return result;
}
{
int result = 0;
cout << endl << "*** DEVICE INFORMATION ***" << endl << endl;
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;
}
{
try
{
cout << endl << "Configuring camera settings to enable image chunk data..." << endl;
bool chunkEnabled = false;
CBooleanPtr ptrChunkModeActive = nodeMap.GetNode(
"ChunkModeActive");
{
ptrChunkModeActive->SetValue(true);
{
CEnumEntryPtr ptrCompressionRatioEntry = ptrChunkSelector->GetEntryByName(
"CompressionRatio");
{
ptrChunkSelector->SetIntValue(ptrCompressionRatioEntry->GetValue());
CBooleanPtr ptrChunkEnable = nodeMap.GetNode(
"ChunkEnable");
{
ptrChunkEnable->SetValue(true);
chunkEnabled = true;
}
}
}
}
cout << "CompressionRatio chunk data " << (chunkEnabled ? "enabled" : "not enabled") << endl;
}
{
cout <<
"Unexpected error while configuring image chunk data: " << e.
what() << endl;
return false;
}
return true;
}
{
try
{
cout << endl << "Disabling image chunk data..." << endl;
{
CEnumEntryPtr ptrCompressionRatioEntry = ptrChunkSelector->GetEntryByName(
"CompressionRatio");
{
ptrChunkSelector->SetIntValue(ptrCompressionRatioEntry->GetValue());
CBooleanPtr ptrChunkEnable = nodeMap.GetNode(
"ChunkEnable");
{
ptrChunkEnable->SetValue(false);
}
}
}
CBooleanPtr ptrChunkModeActive = nodeMap.GetNode(
"ChunkModeActive");
{
ptrChunkModeActive->SetValue(false);
}
cout << "Disabled image chunk data..." << endl;
}
{
cout <<
"Unexpected error while disabling image chunk data: " << e.
what() << endl;
return false;
}
return true;
}
{
try
{
cout << endl << "Configuring camera settings to enable image compression..." << endl;
{
cout << "Unable to get or set pixel format. Aborting..." << endl << endl;
return false;
}
CBooleanPtr ptrIspEnable = nodeMap.GetNode(
"IspEnable");
{
ptrIspEnable->SetValue(false);
cout << "IspEnable set to false..." << endl;
}
CEnumEntryPtr ptrPixelFormatBayerRG8 = ptrPixelFormat->GetEntryByName(
"BayerRG8");
{
CEnumEntryPtr ptrPixelFormatMono8 = ptrPixelFormat->GetEntryByName(
"Mono8");
{
cout << "Unable to get pixel format to BayerRG8 or Mono8. Aborting..." << endl << endl;
return false;
}
ptrPixelFormat->SetIntValue(ptrPixelFormatMono8->GetValue());
cout << "Pixel format set to " << ptrPixelFormatMono8->GetSymbolic() << "..." << endl;
}
else
{
ptrPixelFormat->SetIntValue(ptrPixelFormatBayerRG8->GetValue());
cout << "Pixel format set to " << ptrPixelFormatBayerRG8->GetSymbolic() << "..." << endl;
}
CEnumerationPtr ptrCompressionMode = nodeMap.GetNode(
"ImageCompressionMode");
{
cout << "Unable to set image compression mode to Lossless (enum retrieval). Aborting..." << endl << endl;
return false;
}
CEnumEntryPtr ptrCompressionModeLossless = ptrCompressionMode->GetEntryByName(
"Lossless");
{
cout << "Unable to get image compression mode to Lossless (entry retrieval). Aborting..." << endl << endl;
return false;
}
ptrCompressionMode->SetIntValue(ptrCompressionModeLossless->GetValue());
cout << "Compression mode set to " << ptrCompressionModeLossless->GetSymbolic() << "..." << endl;
cout << endl << "*** COMPRESSION SETTINGS ***" << endl << endl;
cout << "Compression Mode: "
<< (
IsReadable(ptrCompressionMode) ? ptrCompressionMode->ToString() :
"Node not readable") << endl;
CIntegerPtr ptrCompressionBlockSize = nodeMap.GetNode(
"LosslessCompressionBlockSize");
cout << "Compression Block Size: "
<< (
IsReadable(ptrCompressionBlockSize) ? ptrCompressionBlockSize->ToString() :
"Node not readable")
<< endl;
CFloatPtr ptrCompressionRatio = nodeMap.GetNode(
"CompressionRatio");
cout << "Compression Ratio: "
<< (
IsReadable(ptrCompressionRatio) ? ptrCompressionRatio->ToString() :
"Node not readable") << endl;
string compressionSaturationPriority = "Node not readable";
CEnumerationPtr ptrCompressionSaturationPriority = nodeMap.GetNode(
"CompressionSaturationPriority");
{
CEnumEntryPtr ptrCompressionSaturationPriorityEntry = ptrCompressionSaturationPriority->GetCurrentEntry();
if (
IsReadable(ptrCompressionSaturationPriorityEntry))
{
compressionSaturationPriority = ptrCompressionSaturationPriorityEntry->GetSymbolic();
}
}
cout << "Compression Saturation Priority: " << compressionSaturationPriority << endl;
}
{
cout <<
"Unexpected error while configuring image compression: " << e.
what() << endl;
return false;
}
return true;
}
{
try
{
cout << endl << "Disabling image compression..." << endl;
CEnumerationPtr ptrCompressionMode = nodeMap.GetNode(
"ImageCompressionMode");
{
cout << "Unable to set image compression mode to Off (enum retrieval). Aborting..." << endl << endl;
return false;
}
CEnumEntryPtr ptrCompressionModeOff = ptrCompressionMode->GetEntryByName(
"Off");
{
cout << "Unable to get image compression mode to Off (entry retrieval). Aborting..." << endl << endl;
return false;
}
ptrCompressionMode->SetIntValue(ptrCompressionModeOff->GetValue());
cout << "Compression mode set to " << ptrCompressionModeOff->GetSymbolic() << "..." << endl;
}
{
cout <<
"Unexpected error while disabling image compression: " << e.
what() << endl;
return false;
}
return true;
}
{
int result = 0;
const unsigned int kNumDecompressionThreads = 4;
try
{
cout << "Number of decompression threads set to " << kNumDecompressionThreads << endl << endl;
}
{
cout << "Unexpected error when setting the number of decompression threads to " << kNumDecompressionThreads
<< endl;
cout <<
"Error: " << se.
what() << endl;
result = -1;
}
for (auto imageInfo = compressedImageInfos.begin(); imageInfo != compressedImageInfos.end(); ++imageInfo)
{
ifstream file(imageInfo->fileName + ".raw", ios::binary | ios::in);
if (!file)
{
cout << "Failed to load image " << imageInfo->fileName << endl;
result = -1;
continue;
}
cout << "Loading compressed image from '" << imageInfo->fileName << ".raw'" << endl;
std::shared_ptr<char> imageBuffer(new char[imageInfo->compressedImageSize], std::default_delete<char[]>());
file.read(imageBuffer.get(), imageInfo->compressedImageSize);
file.close();
try
{
ImagePtr loadedCompressedImage = Image::Create(
imageInfo->imageWidth,
imageInfo->imageHeight,
imageInfo->imageXOffset,
imageInfo->imageYOffset,
imageInfo->imagePixelFormat,
imageBuffer.get(),
SPINNAKER_TLPAYLOAD_TYPE_LOSSLESS_COMPRESSED,
imageInfo->compressedImageSize);
ImagePtr convertedImage = processor.
Convert(loadedCompressedImage, PixelFormat_RGB8);
if (convertedImage->IsIncomplete())
{
cout << "Decompressed / Converted image is incomplete : "
<< Image::GetImageStatusDescription(convertedImage->GetImageStatus()) << endl;
}
convertedImage->Save(imageInfo->fileName.c_str(), SPINNAKER_IMAGE_FILE_FORMAT_JPEG);
cout << "Image saved at " << imageInfo->fileName << ".jpg" << endl << endl;
}
{
cout << "Unexpected error when processing " << imageInfo->fileName << endl;
cout <<
"Error: " << se.
what() << endl;
result = -1;
continue;
}
}
return result;
}
{
int result = 0;
try
{
INodeMap& nodeMapTLDevice = pCam->GetTLDeviceNodeMap();
pCam->Init();
#ifdef _DEBUG
#else
#endif
{
cout << "Failed to enable image chunk data. Please check if image chunk data is supported on this camera"
<< endl;
return -1;
}
{
cout << "Failed to enable image compression. Please check if image compression is supported on this camera"
<< endl;
return -1;
}
vector<CompressedImageInfo> compressedImageInfos;
result = result |
AcquireImages(pCam, nodeMap, nodeMapTLDevice, compressedImageInfos);
{
cout << "Failed to disable image compression." << endl;
result = -1;
}
{
cout << "Failed to disable image chunk data." << endl;
result = -1;
}
#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
bool DisableImageCompression(INodeMap &nodeMap)
Definition Compression.cpp:598
int ProcessCompressedImagesFromFile(const vector< CompressedImageInfo > &compressedImageInfos)
Definition Compression.cpp:637
const bool enableChunkData
Definition Compression.cpp:49
bool EnableImageCompression(INodeMap &nodeMap)
Definition Compression.cpp:482
bool DisableImageChunkData(INodeMap &nodeMap)
Definition Compression.cpp:437
bool EnableImageChunkData(INodeMap &nodeMap)
Definition Compression.cpp:379
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 chunk data which contains additional information about an image.
Definition ChunkData.h:42
float64_t GetCompressionRatio() const
Description: Visibility:
int64_t GetCRC() const
Description: Returns the CRC of the image payload.
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().
SmartPointer for IFloat interface pointer.
Definition Pointer.h:421
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 SetNumDecompressionThreads(unsigned int numThreads)
Sets the default number of threads used for image decompression during Convert().
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
PixelFormatEnums
Definition CameraDefs.h:730
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_IMAGE_FILE_FORMAT_RAW
Raw data.
Definition SpinnakerDefs.h:208
Definition Autovector.h:36
Definition Compression.cpp:52
size_t imageHeight
Definition Compression.cpp:56
size_t imageYOffset
Definition Compression.cpp:58
PixelFormatEnums imagePixelFormat
Definition Compression.cpp:59
size_t imageXOffset
Definition Compression.cpp:57
string fileName
Definition Compression.cpp:53
size_t imageWidth
Definition Compression.cpp:55
size_t compressedImageSize
Definition Compression.cpp:54
Provides easier access to the current version of Spinnaker.
Definition SpinnakerDefs.h:657
unsigned int minor
Minor version of the library.
Definition SpinnakerDefs.h:662
unsigned int major
Major version of the library.
Definition SpinnakerDefs.h:659
unsigned int type
Version type of the library.
Definition SpinnakerDefs.h:665
unsigned int build
Build number of the library.
Definition SpinnakerDefs.h:668