#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;
 
 
    const char* dir_path = (const char*)user_data;
    char img_path[256];
    FILE* fd;
    TiffHeader header = {
        .id=0x4949, 
        .version = 42, 
        .offset_first_ifd = 
sizeof(header) + (int32_t)frame->
data_size     };
    TiffIFDEntry img_lenght = {.tag=257, .type=3, .N=1, .value=frame->
height};      
    TiffIFDEntry img_width = {.tag=256, .type=3, .N=1, .value=frame->
width};        
    TiffIFDEntry compression = {.tag=259, .type=3, .N=1, .value=1};                 
    TiffIFDEntry bits_per_sample = {.tag=258, .type=3, .N=1, .value=8};             
    TiffIFDEntry strip_offsets = {.tag=273, .type=3, .N=1, .value=8};               
    TiffIFDEntry samples_per_pixels = {.tag=277, .type=3, .N=1, .value=1};          
    TiffIFDEntry photometric_interpretation = {.tag=262, .type=3, .N=1, .value=1};  
    int16_t number_of_ifd_entries = 7;
    int16_t next_ifd_offset = 0;  
 
    sprintf(img_path, 
"%s/eye_image_%" PRId64 
".tif", dir_path, frame->
device_time_stamp); 
    printf("Received image %s\n", img_path);
 
    fd = fopen(img_path, "wb");
 
    fwrite(&header, sizeof(header), 1, fd);        
 
    
    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);
}
 
    
 
    
    sleep_ms(2000);
 
    
}