// File: /workspace/github_cpp_makefile_star2_O1/devernay[P]vlpovutils/vlpov_motionfield2.cpp
// Function: tiff_head(tiff_hdr*, int, int, int, int, bool, char const*)
static void tiff_head (struct tiff_hdr *th, int width, int height, int colors, int bytes_per_sample, bool isfloat, const char *desc)
{
    int c, psize=0;
    struct tm *t;
    int output_bps = bytes_per_sample*8;
    time_t timestamp = time(NULL);
    const int x = 1;

    memset (th, 0, sizeof *th);
    if (desc) {
        strncpy (th->desc, desc, 512);
    }
    strncpy (th->make, "INRIA", 64);
    strncpy (th->model, "vlpovutils", 64);
    strcpy (th->soft, "vlpov_motionfield2");
    t = gmtime (&timestamp);
    sprintf (th->date, "%04d:%02d:%02d %02d:%02d:%02d",
             t->tm_year+1900,t->tm_mon+1,t->tm_mday,t->tm_hour,t->tm_min,t->tm_sec);
    //strncpy (th->artist, artist, 64);

    th->order = ((unsigned char*)&x)[0] ? 0x4949 : 0x4d4d; //(uint16_t)(htonl(0x4d4d4949) >> 16);
    th->magic = 42;
    th->ifd = 10;
    tiff_set (&th->ntag, 254, 4, 1, 0); /* SUBFILETYPE=0 */
    tiff_set (&th->ntag, 256, 4, 1, width); /* IMAGEWIDTH */
    tiff_set (&th->ntag, 257, 4, 1, height);  /* IMAGELENGTH */
    tiff_set (&th->ntag, 258, 3, colors, output_bps);  /* BITSPERSAMPLE */
    if (colors > 2) {
        th->tag[th->ntag-1].val.i0 = TOFF(th->bps);
    }
    for (c=0; c < 4; c++) {
        th->bps[c] = output_bps;
    }
    tiff_set (&th->ntag, 259, 3, 1, 1); /* COMPRESSION=NONE */
    tiff_set (&th->ntag, 262, 3, 1, 1 + (colors > 1)); /* PHOTOMETRIC= MINISBLACK or RGB (CFA is 32803, LINEAR RAW is 34892) */ 
    if (desc) {
      tiff_set (&th->ntag, 270, 2, strlen(th->desc)+1, TOFF(th->desc));  /* IMAGEDESCRIPTION */
    }
    tiff_set (&th->ntag, 271, 2, strlen(th->make)+1, TOFF(th->make));  /* MAKE */
    tiff_set (&th->ntag, 272, 2, strlen(th->model)+1, TOFF(th->model));  /* MODEL */
    //if (full) {
    //if (oprof) psize = ntohl(oprof[0]);
    tiff_set (&th->ntag, 273, 4, 1, sizeof *th + psize);  /* STRIPOFFSETS */
    tiff_set (&th->ntag, 277, 3, 1, colors);  /* SAMPLESPERPIXEL */
    tiff_set (&th->ntag, 278, 4, 1, height);  /* ROWSPERSTRIP */
    tiff_set (&th->ntag, 279, 4, 1, height*width*colors*output_bps/8);  /* STRIPBYTECOUNTS */
    //} else
    //  tiff_set (&th->ntag, 274, 3, 1, 1);  /* ORIENTATION=TOPLEFT */
    //tiff_set (&th->ntag, 282, 5, 1, TOFF(th->rat[6]));  /* XRESOLUTION */
    //tiff_set (&th->ntag, 283, 5, 1, TOFF(th->rat[8]));  /* YRESOLUTION */
    tiff_set (&th->ntag, 284, 3, 1, 1);   /* PLANARCONFIG=CONTIG */
    //tiff_set (&th->ntag, 296, 3, 1, 2);   /* RESOLUTIONUNIT=INCH */
    tiff_set (&th->ntag, 305, 2, strlen(th->soft)+1, TOFF(th->soft));  /* SOFTWARE */
    tiff_set (&th->ntag, 306, 2, strlen(th->date)+1, TOFF(th->date));  /* DATETIME */
    //tiff_set (&th->ntag, 315, 2, strlen(th->artist)+1, TOFF(th->artist));  /* ARTIST */
    tiff_set (&th->ntag, 339, 3, 1, isfloat ? 3 : 1);  /* SAMPLEFORMAT (3: float 1: uint) */
    //tiff_set (&th->ntag, 34665, 4, 1, TOFF(th->nexif));  /* EXIFIFD */
    //if (psize) tiff_set (&th->ntag, 34675, 7, psize, sizeof *th); /* ICCPROFILE */
    //tiff_set (&th->nexif, 33434, 5, 1, TOFF(th->rat[0]));
    //tiff_set (&th->nexif, 33437, 5, 1, TOFF(th->rat[2]));
    //tiff_set (&th->nexif, 34855, 3, 1, iso_speed);
    //tiff_set (&th->nexif, 37386, 5, 1, TOFF(th->rat[4]));
    //for (c=0; c < 6; c++) th->rat[c] = 1000000;
    //th->rat[0] *= shutter;
    //th->rat[2] *= aperture;
    //th->rat[4] *= focal_len;
    //th->rat[6] = th->rat[8] = 300;
    //th->rat[7] = th->rat[9] = 1;
    
    /* Adobe DNG support:
     - for RAW data with PHOTOMETRIC=CFA
     - for linear data with PHOTOMETRIC=LINEARRAW)
     References:
     http://www.adobe.com/products/dng/pdfs/dng_spec.pdf
     http://www.cybercom.net/~dcoffin/dcraw/elphel_dng.c
     http://www.cybercom.net/~dcoffin/dcraw/libtiff.patch
     */
    //tiff_set (&th->ntag, 33421, 3, 2, TOFF(th->cfarepeatpatterndim) );  /* CFAREPEATPATTERNDIM=2,2 */
    //th->cfarepeatpatterndim[0] = 2;
    //th->cfarepeatpatterndim[1] = 2;
    //tiff_set (&th->ntag, 33422, 1, 4, TOFF(th->cfapattern)); /* CFAPATTERN */
    //th->cfapattern[0] = 1;                /* G */
    //th->cfapattern[1] = 0;                /* R */
    //th->cfapattern[2] = 2;                /* B */
    //th->cfapattern[3] = 1;                /* G */  
    //tiff_set (&th->ntag, 50706, 1, 4,  TOFF(th->dngversion));  /* DNGVERSION=1,1,0,0 */
    //th->dngversion[0] = 1;
    //th->dngversion[1] = 1;
    //th->dngversion[2] = 0;
    //th->dngversion[3] = 0;
    //tiff_set (&th->ntag, 50708, 2, 128, TOFF(th->cameramodel));  /* TODO: use Blinky vendor+model */
    //tiff_set (&th->ntag, 50717, 3, 1, (int16_t)(0xffff<<(16-buffer->header.bpp)));  /* WHITELEVEL=max image level */
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
movq %r9, %rbx
movl %r8d, 0x14(%rsp)
movl %ecx, %r15d
movl %edx, 0xc(%rsp)
movl %esi, %ebp
movq %rdi, %r14
shll $0x3, %r15d
xorl %edi, %edi
callq 0x2140
movq %rax, 0x40(%rsp)
movl $0x3d0, %edx            # imm = 0x3D0
movq %r14, %rdi
xorl %esi, %esi
callq 0x20b0
testq %rbx, %rbx
je 0x4956
leaq 0x11c(%r14), %rdi
movl $0x200, %edx            # imm = 0x200
movq %rbx, %rsi
callq 0x20a0
movq %rbx, 0x28(%rsp)
leaq 0x31c(%r14), %rax
movq %rax, 0x18(%rsp)
movups 0x6b0c(%rip), %xmm0     # 0xb47a
movups %xmm0, 0x34c(%r14)
movups 0x6aed(%rip), %xmm0     # 0xb46a
movups %xmm0, 0x33c(%r14)
movups 0x6ace(%rip), %xmm0     # 0xb45a
movups %xmm0, 0x32c(%r14)
movups 0x6aaf(%rip), %xmm0     # 0xb44a
movups %xmm0, 0x31c(%r14)
leaq 0x35c(%r14), %rax
movq %rax, 0x20(%rsp)
movups 0x6ad5(%rip), %xmm0     # 0xb48b
movups %xmm0, 0x35c(%r14)
movups 0x6ad6(%rip), %xmm0     # 0xb49b
movups %xmm0, 0x36c(%r14)
movups 0x6ad7(%rip), %xmm0     # 0xb4ab
movups %xmm0, 0x37c(%r14)
movups 0x6ad8(%rip), %xmm0     # 0xb4bb
movups %xmm0, 0x38c(%r14)
leaq 0x39c(%r14), %rax
movq %rax, 0x30(%rsp)
movups 0x69f1(%rip), %xmm0     # 0xb3ef
movups %xmm0, 0x39c(%r14)
movl $0x32646c, 0x3ab(%r14) # imm = 0x32646C
leaq 0x40(%rsp), %rdi
callq 0x2120
leaq 0x3bc(%r14), %rdi
movl $0x76c, %edx            # imm = 0x76C
addl 0x14(%rax), %edx
movl 0x10(%rax), %ecx
incl %ecx
movl 0xc(%rax), %r8d
movl 0x8(%rax), %r9d
movl (%rax), %r10d
movl 0x4(%rax), %r11d
leaq 0x69bd(%rip), %rsi      # 0xb402
xorl %ebx, %ebx
movq %rdi, 0x38(%rsp)
xorl %eax, %eax
pushq %r10
pushq %r11
callq 0x2060
addq $0x10, %rsp
movabsq $0xa002a4949, %rax      # imm = 0xA002A4949
movq %rax, (%r14)
leaq 0xc(%r14), %r13
movzwl 0xa(%r14), %r12d
leal 0x1(%r12), %eax
movw %ax, 0xa(%r14)
leaq (%r12,%r12,2), %rcx
movl $0x400fe, 0xc(%r14,%rcx,4) # imm = 0x400FE
movq $0x1, 0x10(%r14,%rcx,4)
leal 0x2(%r12), %ecx
movw %cx, 0xa(%r14)
movl $0xffff, %edx           # imm = 0xFFFF
andl %edx, %eax
leaq (%rax,%rax,2), %rax
movabsq $0x100040100, %rsi      # imm = 0x100040100
movq %rsi, 0xc(%r14,%rax,4)
movl %ebp, 0x14(%r14,%rax,4)
leal 0x3(%r12), %esi
movw %si, 0xa(%r14)
andl %edx, %ecx
leaq (%rcx,%rcx,2), %rax
movabsq $0x100040101, %rcx      # imm = 0x100040101
movq %rcx, 0xc(%r14,%rax,4)
movl 0xc(%rsp), %ecx
movl %ecx, 0x14(%r14,%rax,4)
leal 0x4(%r12), %eax
movw %ax, 0xa(%r14)
andl %edx, %esi
leaq (%rsi,%rsi,2), %rcx
movabsq $0x100030102, %rdx      # imm = 0x100030102
movq %rdx, 0xc(%r14,%rcx,4)
movw %r15w, 0x14(%r14,%rcx,4)
movw %r15w, 0x114(%r14,%rbx,2)
incq %rbx
cmpq $0x4, %rbx
jne 0x4b07
movl %ebp, 0x10(%rsp)
leal 0x5(%r12), %ecx
leal 0x6(%r12), %ebx
movw %cx, 0xa(%r14)
movzwl %ax, %eax
leaq (%rax,%rax,2), %rax
movabsq $0x100030103, %rdx      # imm = 0x100030103
movq %rdx, (%r13,%rax,4)
movw $0x1, %dx
movw %dx, 0x8(%r13,%rax,4)
movw %bx, 0xa(%r14)
movzwl %cx, %eax
leaq (%rax,%rax,2), %rax
movabsq $0x100030106, %rcx      # imm = 0x100030106
movq %rcx, (%r13,%rax,4)
movw %dx, 0x8(%r13,%rax,4)
cmpq $0x0, 0x28(%rsp)
je 0x4bab
leaq 0x11c(%r14), %rdi
callq 0x2080
incl %eax
leal 0x7(%r12), %ecx
movw %cx, 0xa(%r14)
movzwl %bx, %ecx
leaq (%rcx,%rcx,2), %rcx
movl $0x2010e, (%r13,%rcx,4) # imm = 0x2010E
movl %eax, 0x4(%r13,%rcx,4)
movl $0x11c, 0x8(%r13,%rcx,4) # imm = 0x11C
movq 0x18(%rsp), %rdi
callq 0x2080
incl %eax
movzwl 0xa(%r14), %ebx
leal 0x1(%rbx), %ebp
movw %bp, 0xa(%r14)
leaq (%rbx,%rbx,2), %rcx
movl $0x2010f, (%r13,%rcx,4) # imm = 0x2010F
movl %eax, 0x4(%r13,%rcx,4)
movl $0x31c, 0x8(%r13,%rcx,4) # imm = 0x31C
movq 0x20(%rsp), %rdi
callq 0x2080
incl %eax
leal 0x2(%rbx), %ecx
movw %cx, 0xa(%r14)
movl $0xffff, %r12d          # imm = 0xFFFF
andl %r12d, %ebp
leaq (,%rbp,2), %rdx
addq %rbp, %rdx
movl $0x20110, (%r13,%rdx,4) # imm = 0x20110
movl %eax, 0x4(%r13,%rdx,4)
movl $0x35c, 0x8(%r13,%rdx,4) # imm = 0x35C
leal 0x3(%rbx), %eax
movw %ax, 0xa(%r14)
andl %r12d, %ecx
leaq (%rcx,%rcx,2), %rcx
movl $0x40111, (%r13,%rcx,4) # imm = 0x40111
movabsq $0x3d000000001, %rdx    # imm = 0x3D000000001
movq %rdx, 0x4(%r13,%rcx,4)
leal 0x4(%rbx), %ecx
movw %cx, 0xa(%r14)
andl %r12d, %eax
leaq (%rax,%rax,2), %rax
movabsq $0x100030115, %rdx      # imm = 0x100030115
movq %rdx, (%r13,%rax,4)
movw $0x1, %di
movw %di, 0x8(%r13,%rax,4)
leal 0x5(%rbx), %eax
movw %ax, 0xa(%r14)
andl %r12d, %ecx
leaq (%rcx,%rcx,2), %rcx
movabsq $0x100040116, %rdx      # imm = 0x100040116
movq %rdx, (%r13,%rcx,4)
movl 0xc(%rsp), %edx
movl %edx, 0x8(%r13,%rcx,4)
movl 0x10(%rsp), %esi
imull %edx, %esi
imull %r15d, %esi
sarl $0x3, %esi
leal 0x6(%rbx), %ecx
movw %cx, 0xa(%r14)
andl %r12d, %eax
leaq (%rax,%rax,2), %rax
movabsq $0x100040117, %rdx      # imm = 0x100040117
movq %rdx, (%r13,%rax,4)
movl %esi, 0x8(%r13,%rax,4)
leal 0x7(%rbx), %r15d
movw %r15w, 0xa(%r14)
andl %r12d, %ecx
leaq (%rcx,%rcx,2), %rax
movabsq $0x10003011c, %rcx      # imm = 0x10003011C
movq %rcx, (%r13,%rax,4)
movw %di, 0x8(%r13,%rax,4)
movq 0x30(%rsp), %rdi
callq 0x2080
incl %eax
leal 0x8(%rbx), %ebp
movw %bp, 0xa(%r14)
andl %r12d, %r15d
leaq (%r15,%r15,2), %rcx
movl $0x20131, (%r13,%rcx,4) # imm = 0x20131
movl %eax, 0x4(%r13,%rcx,4)
movl $0x39c, 0x8(%r13,%rcx,4) # imm = 0x39C
movq 0x38(%rsp), %rdi
callq 0x2080
incl %eax
leal 0x9(%rbx), %ecx
movw %cx, 0xa(%r14)
andl %r12d, %ebp
leaq (,%rbp,2), %rdx
addq %rbp, %rdx
movl $0x20132, (%r13,%rdx,4) # imm = 0x20132
movl %eax, 0x4(%r13,%rdx,4)
movl $0x3bc, 0x8(%r13,%rdx,4) # imm = 0x3BC
leal 0xa(%rbx), %eax
movw %ax, 0xa(%r14)
andl %r12d, %ecx
leaq (%rcx,%rcx,2), %rax
movabsq $0x100030153, %rcx      # imm = 0x100030153
movq %rcx, (%r13,%rax,4)
movzbl 0x14(%rsp), %ecx
leal 0x1(,%rcx,2), %ecx
movw %cx, 0x8(%r13,%rax,4)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq