Tobii Pro SDK C API
eye_images.c
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include <stdint.h>
#if _WIN32 || _WIN64
#include <windows.h>
static void sleep_ms(int time) {
Sleep(time);
}
#else
#include <unistd.h>
static void sleep_ms(int time) {
usleep(time * 1000);
}
#endif
typedef struct {
int16_t id;
int16_t version;
int32_t offset_first_ifd;
} TiffHeader;
typedef struct {
int16_t tag;
int16_t type;
int32_t N;
int32_t value;
} TiffIFDEntry;
void eye_image_callback(TobiiResearchEyeImage* frame, void* user_data) {
const char* dir_path = (const char*)user_data;
char img_path[256];
FILE* fd;
TiffHeader header = {
.id=0x4949, // II little endian
.version = 42, // Image version
.offset_first_ifd = sizeof(header) + (int32_t)frame->data_size // Offset to first IFD from start of file
};
TiffIFDEntry img_lenght = {.tag=257, .type=3, .N=1, .value=frame->height}; // Image length (height)
TiffIFDEntry img_width = {.tag=256, .type=3, .N=1, .value=frame->width}; // Image width
TiffIFDEntry compression = {.tag=259, .type=3, .N=1, .value=1}; // Compression 1 = no compression
TiffIFDEntry bits_per_sample = {.tag=258, .type=3, .N=1, .value=8}; // BitsPerSample
TiffIFDEntry strip_offsets = {.tag=273, .type=3, .N=1, .value=8}; // StripOffsets
TiffIFDEntry samples_per_pixels = {.tag=277, .type=3, .N=1, .value=1}; // SamplesPerPixel
TiffIFDEntry photometric_interpretation = {.tag=262, .type=3, .N=1, .value=1}; // PhotometricInterpretation
int16_t number_of_ifd_entries = 7;
int16_t next_ifd_offset = 0; // 0, hence no more IFD in this file
sprintf(img_path, "%s/eye_image_%" PRId64 ".tif", dir_path, frame->device_time_stamp); // Create image name
printf("Received image %s\n", img_path);
fd = fopen(img_path, "wb");
fwrite(&header, sizeof(header), 1, fd); // Writing header
fwrite(frame->data, frame->data_size, 1, fd); // Writing image data to file
// Writing IFD entries, starts with number of entries
fwrite(&number_of_ifd_entries, 1, sizeof(number_of_ifd_entries), fd);
fwrite(&img_lenght, 1, sizeof(img_lenght), fd);
fwrite(&img_width, 1, sizeof(img_width), fd);
fwrite(&compression, 1, sizeof(compression), fd);
fwrite(&bits_per_sample, 1, sizeof(bits_per_sample), fd);
fwrite(&strip_offsets, 1, sizeof(strip_offsets), fd);
fwrite(&samples_per_pixels, 1, sizeof(samples_per_pixels), fd);
fwrite(&photometric_interpretation, 1, sizeof(photometric_interpretation), fd);
fwrite(&next_ifd_offset, 1, sizeof(next_ifd_offset), fd);
fclose(fd);
}
void eye_images_example(TobiiResearchEyeTracker* eyetracker, const char* dir_path) {
/* Subscribe to eye images */
tobii_research_subscribe_to_eye_image(eyetracker, eye_image_callback, (void*)dir_path);
/* Wait for eye images. */
sleep_ms(2000);
/* Unsubscribe to eye images */
tobii_research_unsubscribe_from_eye_image(eyetracker, eye_image_callback);
}
//! [Example]