Creating the SingleImage (Color) Project
These are the steps to create a program that captures one frame of compressed data from a GbE-2 (color model), performs Bayer conversion (debayering) and white balance adjustment to decode it in color, and saves it as a file in the PPM format (Portable Pixmap), which can be viewed with standard image viewing software.
-
Write the source code
Open the main file (for example
SingleImageColor.cpp) and replace its contents with the following code.#include <stdio.h>#include <stdlib.h>#include <PUCLIB.h> // Include the PUCLIB headerint main() {printf("Starting INFINICAM GbE-2 SingleImage (color) capture...\n");// 1. Initialize the SDKPUCRESULT ret = PUC_Initialize();if (ret != PUC_SUCCEEDED) {printf("Error: failed to initialize the SDK. ErrorCode: %d\n", ret);return EXIT_FAILURE;}// 2. Search for GbE-2 devices on the networkPUC_DETECT_FILTER filter = { 0 };filter.nDeviceTypeFlags = PUC_DETECT_TARGET_GBE2; // Search for GbE-2PUC_DETECT_INFO detectInfo = { 0 };ret = PUC_DetectDevice2(&filter, &detectInfo);if (ret != PUC_SUCCEEDED || detectInfo.nDeviceCount == 0) {printf("Error: no GbE-2 camera was found. ErrorCode: %d\n", ret);return EXIT_FAILURE;}// 3. Open the first device foundPUC_HANDLE hDevice = NULL;ret = PUC_OpenDevice2(0, &hDevice);if (ret != PUC_SUCCEEDED) {printf("Error: failed to open the device. ErrorCode: %d\n", ret);return EXIT_FAILURE;}// 4. Check for the color model and set the white balance gainPUC_COLOR_TYPE colorType = PUC_COLOR_MONO;PUC_GetColorType(hDevice, &colorType);if (colorType != PUC_COLOR_COLOR) {printf("Error: the connected camera is a monochrome model.\n");printf("For the monochrome model, use the SingleImage sample.\n");PUC_CloseDevice(hDevice);return EXIT_FAILURE;}// Set the white balance gain for the R, G, and B channels (1.0 = no change)PUC_SetWhiteBalanceGain(1.0, 1.0, 1.0);// 5. Get the resolutionUINT32 width = 0, height = 0;ret = PUC_GetResolution(hDevice, &width, &height);if (ret != PUC_SUCCEEDED) {printf("Error: failed to get the resolution. ErrorCode: %d\n", ret);PUC_CloseDevice(hDevice);return EXIT_FAILURE;}printf("Resolution: %u x %u pixels (color)\n", width, height);// 6. Get the whole quantization table (used for decoding)unsigned short qVals[PUC_Q_COUNT] = { 0 };ret = PUC_GetQuantization2(hDevice, qVals);if (ret != PUC_SUCCEEDED) {printf("Error: failed to get the quantization table. ErrorCode: %d\n", ret);PUC_CloseDevice(hDevice);return EXIT_FAILURE;}// 7. Get the transfer data size and allocate buffersUINT32 xferDataSize = 0;ret = PUC_GetXferDataSize(hDevice, &xferDataSize);if (ret != PUC_SUCCEEDED) {printf("Error: failed to get the transfer data size. ErrorCode: %d\n", ret);PUC_CloseDevice(hDevice);return EXIT_FAILURE;}// Buffer for the compressed dataunsigned char* pCompressedBuffer = (unsigned char*)malloc(xferDataSize);// Buffer for the color-decoded data (RGB 24-bit = 3 bytes per pixel)// Align the line width in bytes (width * 3) to a 4-byte boundaryUINT32 lineBytes = ((width * 3) + 3) & ~3;unsigned long decodedBufferSize = (unsigned long)lineBytes * height;unsigned char* pDecodedBuffer = (unsigned char*)malloc(decodedBufferSize);if (pCompressedBuffer == NULL || pDecodedBuffer == NULL) {printf("Error: failed to allocate memory.\n");free(pCompressedBuffer);free(pDecodedBuffer);PUC_CloseDevice(hDevice);return EXIT_FAILURE;}// 8. Get one frame of dataPUC_XFER_DATA_INFO xferDataInfo = { 0 };xferDataInfo.pData = pCompressedBuffer;xferDataInfo.nDataSize = xferDataSize;printf("Capturing one frame...\n");ret = PUC_GetSingleXferData(hDevice, &xferDataInfo);if (ret == PUC_SUCCEEDED) {// 9. Decode the compressed data in color// The SDK automatically applies Bayer conversion (debayering) and white balance,// and expands the result as RGB data (1 pixel = 3 bytes).ret = PUC_DecodeData(pDecodedBuffer, 0, 0, width, height, lineBytes, pCompressedBuffer, qVals);if (ret == PUC_SUCCEEDED) {// 10. Save the decoded color image in PPM (P6) formatconst char* filename = "captured_color_image.ppm";FILE* fp = NULL;if (fopen_s(&fp, filename, "wb") == 0 && fp != NULL) {// Write the PPM header (P6 / width / height / maximum value 255)fprintf(fp, "P6\n%u %u\n255\n", width, height);// Write the valid color image data (RGB, 3 bytes/px) one line at a timefor (UINT32 y = 0; y < height; y++) {fwrite(pDecodedBuffer + (y * lineBytes), sizeof(unsigned char), width * 3, fp);}fclose(fp);printf("SUCCESS: saved the PPM color image file (%s)\n", filename);} else {printf("Error: failed to save the file.\n");}} else {printf("Error: failed to decode the image data. ErrorCode: %d\n", ret);}} else {printf("Error: failed to get the image data. ErrorCode: %d\n", ret);}// 11. Free memory and close the devicefree(pCompressedBuffer);free(pDecodedBuffer);PUC_CloseDevice(hDevice);return EXIT_SUCCESS;} -
Build and run
Connect a color model camera and press
Ctrl+F5. When the build finishes, the program runs automatically. If you see the following, it worked.Starting INFINICAM GbE-2 SingleImage (color) capture...Resolution: 800 x 600 pixels (color)Capturing one frame...SUCCESS: saved the PPM color image file (captured_color_image.ppm)The image file
captured_color_image.ppmis saved in the project folder (for exampleC:\infinicam_projects\SingleImageColor\). -
Open the saved image
PPM files cannot be opened with the “Photos” app that comes with Windows. Open them with a free image program such as ImageJ, IrfanView, or GIMP.
What is the PPM format?
Section titled “What is the PPM format?”PPM (Portable Pixmap Format) is an uncompressed full-color image format standardized by the Netpbm project. It consists of a text header and binary pixel data with 8 bits each for R, G, and B (24-bit color in total).
Basic file structure (P6 format)
Section titled “Basic file structure (P6 format)”P6800 600255[Binary RGB pixel data (width × height × 3 bytes)]- Magic number (
P6): indicates a binary full-color (RGB 24-bit) image. - Image width and height: the resolution in pixels, such as
800 600. - Maximum value (
255): the maximum level of each color channel (R, G, B). - Pixel data: right after the line break, the brightness data follows as it is, in R, G, B order (1 pixel = 3 bytes).
