// File: /workspace/github_cpp_makefile_star2_O3/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 $0x38, %rsp
movq %r9, %rbx
movl %r8d, 0xc(%rsp)
movl %edx, 0x4(%rsp)
movl %esi, %ebp
movq %rdi, %r14
shll $0x3, %ecx
movl %ecx, 0x8(%rsp)
xorl %edi, %edi
callq 0x2130
movq %rax, 0x30(%rsp)
movl $0x3d0, %edx            # imm = 0x3D0
movq %r14, %rdi
xorl %esi, %esi
callq 0x20b0
testq %rbx, %rbx
je 0x4921
leaq 0x11c(%r14), %rdi
movl $0x200, %edx            # imm = 0x200
movq %rbx, %rsi
callq 0x20a0
leaq 0x31c(%r14), %rax
movq %rax, 0x10(%rsp)
movups 0x6b46(%rip), %xmm0     # 0xb47a
movups %xmm0, 0x34c(%r14)
movups 0x6b27(%rip), %xmm0     # 0xb46a
movups %xmm0, 0x33c(%r14)
movups 0x6b08(%rip), %xmm0     # 0xb45a
movups %xmm0, 0x32c(%r14)
movups 0x6ae9(%rip), %xmm0     # 0xb44a
movups %xmm0, 0x31c(%r14)
leaq 0x35c(%r14), %rax
movq %rax, 0x18(%rsp)
movups 0x6b0f(%rip), %xmm0     # 0xb48b
movups %xmm0, 0x35c(%r14)
movups 0x6b10(%rip), %xmm0     # 0xb49b
movups %xmm0, 0x36c(%r14)
movups 0x6b11(%rip), %xmm0     # 0xb4ab
movups %xmm0, 0x37c(%r14)
movups 0x6b12(%rip), %xmm0     # 0xb4bb
movups %xmm0, 0x38c(%r14)
leaq 0x39c(%r14), %rax
movq %rax, 0x20(%rsp)
movups 0x6a2b(%rip), %xmm0     # 0xb3ef
movups %xmm0, 0x39c(%r14)
movl $0x32646c, 0x3ab(%r14) # imm = 0x32646C
leaq 0x30(%rsp), %rdi
callq 0x2110
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 0x69f7(%rip), %rsi      # 0xb402
movq %rdi, 0x28(%rsp)
xorl %eax, %eax
pushq %r10
pushq %r11
callq 0x2060
addq $0x10, %rsp
leaq 0xc(%r14), %r13
movq %rbx, %r9
movzwl 0xa(%r14), %ebx
leal 0x1(%rbx), %ecx
leal 0x2(%rbx), %eax
movl $0xffff, %r12d          # imm = 0xFFFF
movabsq $0xa002a4949, %rdx      # imm = 0xA002A4949
movq %rdx, (%r14)
movw %cx, 0xa(%r14)
movl %ecx, %edx
andl %r12d, %edx
leal 0x3(%rbx), %esi
andl %r12d, %eax
leal 0x4(%rbx), %edi
andl %r12d, %esi
leal 0x5(%rbx), %ecx
andl %r12d, %edi
leal 0x6(%rbx), %r15d
andl %r12d, %ecx
leaq (%rbx,%rbx,2), %r8
movl $0x400fe, 0xc(%r14,%r8,4) # imm = 0x400FE
movq $0x1, 0x10(%r14,%r8,4)
leaq (%rdx,%rdx,2), %rdx
movabsq $0x100040100, %r8       # imm = 0x100040100
movq %r8, 0xc(%r14,%rdx,4)
movl %ebp, 0x14(%r14,%rdx,4)
leaq (%rax,%rax,2), %rax
movabsq $0x100040101, %rdx      # imm = 0x100040101
movq %rdx, 0xc(%r14,%rax,4)
movl 0x4(%rsp), %edx
movl %edx, 0x14(%r14,%rax,4)
leaq (%rsi,%rsi,2), %rax
movabsq $0x100030102, %rdx      # imm = 0x100030102
movq %rdx, 0xc(%r14,%rax,4)
movl 0x8(%rsp), %edx
movw %dx, 0x14(%r14,%rax,4)
movd %edx, %xmm0
pshuflw $0x0, %xmm0, %xmm0      # xmm0 = xmm0[0,0,0,0,4,5,6,7]
movq %xmm0, 0x114(%r14)
leaq (%rdi,%rdi,2), %rax
movabsq $0x100030103, %rdx      # imm = 0x100030103
movq %rdx, 0xc(%r14,%rax,4)
movw $0x1, %dx
movw %dx, 0x14(%r14,%rax,4)
movw %r15w, 0xa(%r14)
leaq (%rcx,%rcx,2), %rax
movabsq $0x100030106, %rcx      # imm = 0x100030106
movq %rcx, 0xc(%r14,%rax,4)
movw %dx, 0x14(%r14,%rax,4)
testq %r9, %r9
je 0x4b58
leaq 0x11c(%r14), %rdi
callq 0x2080
incl %eax
addl $0x7, %ebx
movw %bx, 0xa(%r14)
movzwl %r15w, %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
movl %ebx, %r15d
movq 0x10(%rsp), %rdi
callq 0x2080
incl %eax
leal 0x1(%r15), %ebx
movw %bx, 0xa(%r14)
movzwl %r15w, %ecx
leaq (%rcx,%rcx,2), %rcx
movl $0x2010f, (%r13,%rcx,4) # imm = 0x2010F
movl %eax, 0x4(%r13,%rcx,4)
movl $0x31c, 0x8(%r13,%rcx,4) # imm = 0x31C
movq 0x18(%rsp), %rdi
callq 0x2080
incl %eax
leal 0x2(%r15), %ecx
andl %r12d, %ebx
leaq (%rbx,%rbx,2), %rdx
movl $0x20110, (%r13,%rdx,4) # imm = 0x20110
movl %eax, 0x4(%r13,%rdx,4)
movl $0x35c, 0x8(%r13,%rdx,4) # imm = 0x35C
leal 0x3(%r15), %eax
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(%r15), %ecx
andl %r12d, %eax
leaq (%rax,%rax,2), %rax
movabsq $0x100030115, %rdx      # imm = 0x100030115
movq %rdx, (%r13,%rax,4)
movw $0x1, %si
movw %si, 0x8(%r13,%rax,4)
leal 0x5(%r15), %eax
andl %r12d, %ecx
leaq (%rcx,%rcx,2), %rcx
movabsq $0x100040116, %rdx      # imm = 0x100040116
movq %rdx, (%r13,%rcx,4)
movl 0x4(%rsp), %edx
movl %edx, 0x8(%r13,%rcx,4)
imull %edx, %ebp
imull 0x8(%rsp), %ebp
sarl $0x3, %ebp
leal 0x6(%r15), %ecx
andl %r12d, %eax
leaq (%rax,%rax,2), %rax
movabsq $0x100040117, %rdx      # imm = 0x100040117
movq %rdx, (%r13,%rax,4)
movl %ebp, 0x8(%r13,%rax,4)
leal 0x7(%r15), %ebx
movw %bx, 0xa(%r14)
andl %r12d, %ecx
leaq (%rcx,%rcx,2), %rax
movabsq $0x10003011c, %rcx      # imm = 0x10003011C
movq %rcx, (%r13,%rax,4)
movw %si, 0x8(%r13,%rax,4)
movq 0x20(%rsp), %rdi
callq 0x2080
incl %eax
leal 0x8(%r15), %ebp
movw %bp, 0xa(%r14)
andl %r12d, %ebx
leaq (%rbx,%rbx,2), %rcx
movl $0x20131, (%r13,%rcx,4) # imm = 0x20131
movl %eax, 0x4(%r13,%rcx,4)
movl $0x39c, 0x8(%r13,%rcx,4) # imm = 0x39C
movq 0x28(%rsp), %rdi
callq 0x2080
incl %eax
leal 0x9(%r15), %ecx
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(%r15), %eax
movw %ax, 0xa(%r14)
andl %r12d, %ecx
leaq (%rcx,%rcx,2), %rax
movabsq $0x100030153, %rcx      # imm = 0x100030153
movq %rcx, (%r13,%rax,4)
movzbl 0xc(%rsp), %ecx
leal 0x1(,%rcx,2), %ecx
movw %cx, 0x8(%r13,%rax,4)
addq $0x38, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop