Skip to content
⚡ Developer documentation for INFINICAM, the real-time high-speed camera (UC-1.1 / GbE-2)

Creating the SingleImage Project

These are the steps to create a program that captures and decodes one frame of compressed data from a GbE-2, and saves it as a file in the PGM format (Portable Graymap), which can be viewed with standard image viewing software.

  1. Write the source code

    Open the main file (for example SingleImage.cpp) and replace its contents with the following code.

    #include <stdio.h>
    #include <stdlib.h>
    #include <PUCLIB.h> // Include the PUCLIB header
    int main() {
    printf("Starting INFINICAM GbE-2 SingleImage capture...\n");
    // 1. Initialize the SDK
    PUCRESULT 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 network
    PUC_DETECT_FILTER filter = { 0 };
    filter.nDeviceTypeFlags = PUC_DETECT_TARGET_GBE2; // Search for GbE-2
    PUC_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 found
    PUC_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. Get the resolution
    UINT32 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\n", width, height);
    // 5. 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;
    }
    // 6. Get the transfer data size and allocate buffers
    UINT32 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 data
    unsigned char* pCompressedBuffer = (unsigned char*)malloc(xferDataSize);
    // Buffer for the decoded data (line width aligned to a multiple of 4)
    UINT32 lineBytes = (width + 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;
    }
    // 7. Get one frame of data
    PUC_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) {
    // 8. Decode the compressed data
    // For the GbE-2, the SDK automatically corrects the 180-degree rotation
    ret = PUC_DecodeData(pDecodedBuffer, 0, 0, width, height, lineBytes, pCompressedBuffer, qVals);
    if (ret == PUC_SUCCEEDED) {
    // 9. Save the decoded image in PGM (P5) format
    const char* filename = "captured_image.pgm";
    FILE* fp = NULL;
    if (fopen_s(&fp, filename, "wb") == 0 && fp != NULL) {
    // Write the PGM header (P5 / width / height / maximum value 255)
    fprintf(fp, "P5\n%u %u\n255\n", width, height);
    // Write the valid image data one line at a time
    for (UINT32 y = 0; y < height; y++) {
    fwrite(pDecodedBuffer + (y * lineBytes), sizeof(unsigned char), width, fp);
    }
    fclose(fp);
    printf("SUCCESS: saved the PGM 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);
    }
    // 10. Free memory and close the device
    free(pCompressedBuffer);
    free(pDecodedBuffer);
    PUC_CloseDevice(hDevice);
    return EXIT_SUCCESS;
    }
  2. Build and run

    Connect the 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 capture...
    Resolution: 800 x 600 pixels
    Capturing one frame...
    SUCCESS: saved the PGM image file (captured_image.pgm)

    The image file captured_image.pgm is saved in the project folder (for example C:\infinicam_projects\SingleImage\).

  3. Open the saved image

    PGM files cannot be opened with the “Photos” app that comes with Windows. Open them with a free image program such as:

    • ImageJ (image analysis software for research; you can open files by drag and drop)
    • IrfanView (a lightweight image viewer)
    • GIMP (image editing software)

PGM (Portable Graymap Format) is a grayscale image format with a very simple structure: a text header followed by binary pixel data.

P5
800 600
255
[Binary pixel data (width × height bytes)]
  1. Magic number (P5): indicates a binary grayscale image.
  2. Image width and height: the resolution in pixels, such as 800 600.
  3. Maximum value (255): the maximum gray level of each pixel (255 for 8-bit monochrome).
  4. Pixel data: right after the line break, the brightness values (1 pixel = 1 byte) follow as they are.