// File: /workspace/github_cpp_makefile_star2_O2/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 0x2130
movq %rax, 0x40(%rsp)
movl $0x3d0, %edx            # imm = 0x3D0
movq %r14, %rdi
xorl %esi, %esi
callq 0x20c0
testq %rbx, %rbx
je 0x3414
leaq 0x11c(%r14), %rdi
movl $0x200, %edx            # imm = 0x200
movq %rbx, %rsi
callq 0x20b0
movq %rbx, 0x20(%rsp)
leaq 0x31c(%r14), %rax
movq %rax, 0x18(%rsp)
movups 0x515c(%rip), %xmm0     # 0x8588
movups %xmm0, 0x34c(%r14)
movups 0x513d(%rip), %xmm0     # 0x8578
movups %xmm0, 0x33c(%r14)
movups 0x511e(%rip), %xmm0     # 0x8568
movups %xmm0, 0x32c(%r14)
movups 0x50ff(%rip), %xmm0     # 0x8558
movups %xmm0, 0x31c(%r14)
leaq 0x35c(%r14), %rax
movq %rax, 0x28(%rsp)
movups 0x5125(%rip), %xmm0     # 0x8599
movups %xmm0, 0x35c(%r14)
movups 0x5126(%rip), %xmm0     # 0x85a9
movups %xmm0, 0x36c(%r14)
movups 0x5127(%rip), %xmm0     # 0x85b9
movups %xmm0, 0x37c(%r14)
movups 0x5128(%rip), %xmm0     # 0x85c9
movups %xmm0, 0x38c(%r14)
leaq 0x39c(%r14), %rax
movq %rax, 0x30(%rsp)
movups 0x5021(%rip), %xmm0     # 0x84dd
movups %xmm0, 0x39c(%r14)
movl $0x32646c, 0x3ab(%r14) # imm = 0x32646C
leaq 0x40(%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 0x4fed(%rip), %rsi      # 0x84f0
xorl %r12d, %r12d
movq %rdi, 0x38(%rsp)
xorl %eax, %eax
pushq %r10
pushq %r11
callq 0x2050
popq %rcx
popq %rdx
movabsq $0xa002a4949, %rax      # imm = 0xA002A4949
movq %rax, (%r14)
leaq 0xc(%r14), %r13
movzwl 0xa(%r14), %ebx
leal 0x1(%rbx), %eax
movw %ax, 0xa(%r14)
imulq $0xc, %rbx, %rcx
movl $0x400fe, 0xc(%r14,%rcx) # imm = 0x400FE
movq $0x1, 0x10(%r14,%rcx)
leal 0x2(%rbx), %ecx
movl $0xffff, %edx           # imm = 0xFFFF
andl %edx, %eax
imulq $0xc, %rax, %rax
movabsq $0x100040100, %rsi      # imm = 0x100040100
movq %rsi, 0xc(%r14,%rax)
movl %ebp, 0x14(%r14,%rax)
leal 0x3(%rbx), %esi
andl %edx, %ecx
imulq $0xc, %rcx, %rax
movabsq $0x100040101, %rcx      # imm = 0x100040101
movq %rcx, 0xc(%r14,%rax)
movl 0xc(%rsp), %ecx
movl %ecx, 0x14(%r14,%rax)
leal 0x4(%rbx), %eax
andl %edx, %esi
imulq $0xc, %rsi, %rcx
movabsq $0x100030102, %rdx      # imm = 0x100030102
movq %rdx, 0xc(%r14,%rcx)
movw %r15w, 0x14(%r14,%rcx)
cmpq $0x4, %r12
je 0x35c1
movw %r15w, 0x114(%r14,%r12,2)
incq %r12
jmp 0x35ad
leal 0x5(%rbx), %ecx
movzwl %ax, %eax
imulq $0xc, %rax, %rax
leal 0x6(%rbx), %r12d
movzwl %cx, %ecx
imulq $0xc, %rcx, %rcx
movabsq $0x100030103, %rdx      # imm = 0x100030103
movq %rdx, (%r13,%rax)
movw $0x1, %dx
movw %dx, 0x8(%r13,%rax)
movw %r12w, 0xa(%r14)
movabsq $0x100030106, %rax      # imm = 0x100030106
movq %rax, (%r13,%rcx)
movw %dx, 0x8(%r13,%rcx)
cmpq $0x0, 0x20(%rsp)
movl %ebp, 0x10(%rsp)
je 0x364d
leaq 0x11c(%r14), %rdi
callq 0x2070
incl %eax
movzwl %r12w, %ecx
leal 0x7(%rbx), %edx
movw %dx, 0xa(%r14)
imulq $0xc, %rcx, %rcx
movl $0x2010e, (%r13,%rcx)   # imm = 0x2010E
movl %eax, 0x4(%r13,%rcx)
movl $0x11c, 0x8(%r13,%rcx)  # imm = 0x11C
movl %edx, %r12d
movq 0x18(%rsp), %rdi
callq 0x2070
incl %eax
leal 0x1(%r12), %ebp
movw %bp, 0xa(%r14)
movzwl %r12w, %ecx
imulq $0xc, %rcx, %rcx
movl $0x2010f, (%r13,%rcx)   # imm = 0x2010F
movl %eax, 0x4(%r13,%rcx)
movl $0x31c, 0x8(%r13,%rcx)  # imm = 0x31C
movq 0x28(%rsp), %rdi
callq 0x2070
incl %eax
leal 0x2(%r12), %ecx
movl $0xffff, %ebx           # imm = 0xFFFF
andl %ebx, %ebp
imulq $0xc, %rbp, %rdx
movl $0x20110, (%r13,%rdx)   # imm = 0x20110
movl %eax, 0x4(%r13,%rdx)
movl $0x35c, 0x8(%r13,%rdx)  # imm = 0x35C
leal 0x3(%r12), %eax
andl %ebx, %ecx
imulq $0xc, %rcx, %rcx
movl $0x40111, (%r13,%rcx)   # imm = 0x40111
movabsq $0x3d000000001, %rdx    # imm = 0x3D000000001
movq %rdx, 0x4(%r13,%rcx)
leal 0x4(%r12), %ecx
andl %ebx, %eax
imulq $0xc, %rax, %rax
movabsq $0x100030115, %rdx      # imm = 0x100030115
movq %rdx, (%r13,%rax)
movw $0x1, %di
movw %di, 0x8(%r13,%rax)
leal 0x5(%r12), %eax
andl %ebx, %ecx
imulq $0xc, %rcx, %rcx
movabsq $0x100040116, %rdx      # imm = 0x100040116
movq %rdx, (%r13,%rcx)
movl 0xc(%rsp), %edx
movl %edx, 0x8(%r13,%rcx)
movl 0x10(%rsp), %esi
imull %edx, %esi
imull %r15d, %esi
sarl $0x3, %esi
leal 0x6(%r12), %ecx
andl %ebx, %eax
imulq $0xc, %rax, %rax
movabsq $0x100040117, %rdx      # imm = 0x100040117
movq %rdx, (%r13,%rax)
movl %esi, 0x8(%r13,%rax)
leal 0x7(%r12), %r15d
movw %r15w, 0xa(%r14)
andl %ebx, %ecx
imulq $0xc, %rcx, %rax
movabsq $0x10003011c, %rcx      # imm = 0x10003011C
movq %rcx, (%r13,%rax)
movw %di, 0x8(%r13,%rax)
movq 0x30(%rsp), %rdi
callq 0x2070
incl %eax
leal 0x8(%r12), %ebp
movw %bp, 0xa(%r14)
andl %ebx, %r15d
imulq $0xc, %r15, %rcx
movl $0x20131, (%r13,%rcx)   # imm = 0x20131
movl %eax, 0x4(%r13,%rcx)
movl $0x39c, 0x8(%r13,%rcx)  # imm = 0x39C
movq 0x38(%rsp), %rdi
callq 0x2070
incl %eax
leal 0x9(%r12), %ecx
andl %ebx, %ebp
imulq $0xc, %rbp, %rdx
movl $0x20132, (%r13,%rdx)   # imm = 0x20132
movl %eax, 0x4(%r13,%rdx)
movl $0x3bc, 0x8(%r13,%rdx)  # imm = 0x3BC
leal 0xa(%r12), %eax
movw %ax, 0xa(%r14)
andl %ebx, %ecx
imulq $0xc, %rcx, %rax
movabsq $0x100030153, %rcx      # imm = 0x100030153
movq %rcx, (%r13,%rax)
movzbl 0x14(%rsp), %ecx
leal 0x1(,%rcx,2), %ecx
movw %cx, 0x8(%r13,%rax)
addq $0x48, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop