// File: 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
movq %rsp, %rbp
subq $0x80, %rsp
movb %r9b, %al
movq 0x10(%rbp), %r9
movq %rdi, -0x8(%rbp)
movl %esi, -0xc(%rbp)
movl %edx, -0x10(%rbp)
movl %ecx, -0x14(%rbp)
movl %r8d, -0x18(%rbp)
andb $0x1, %al
movb %al, -0x19(%rbp)
movl $0x0, -0x24(%rbp)
movl -0x18(%rbp), %eax
shll $0x3, %eax
movl %eax, -0x34(%rbp)
xorl %eax, %eax
movl %eax, %edi
callq 0x31d0
movq %rax, -0x40(%rbp)
movl $0x1, -0x44(%rbp)
movq -0x8(%rbp), %rdi
xorl %esi, %esi
movl $0x3d0, %edx            # imm = 0x3D0
callq 0x3100
cmpq $0x0, 0x10(%rbp)
je 0x56fc
movq -0x8(%rbp), %rdi
addq $0x11c, %rdi            # imm = 0x11C
movq 0x10(%rbp), %rsi
movl $0x200, %edx            # imm = 0x200
callq 0x30f0
movq -0x8(%rbp), %rdi
addq $0x31c, %rdi            # imm = 0x31C
leaq 0xcdbf(%rip), %rsi      # 0x124cd
movl $0x40, %edx
callq 0x30f0
movq -0x8(%rbp), %rdi
addq $0x35c, %rdi            # imm = 0x35C
leaq 0xcda9(%rip), %rsi      # 0x124d3
movl $0x40, %edx
callq 0x30f0
movq -0x8(%rbp), %rdi
addq $0x39c, %rdi            # imm = 0x39C
leaq 0xcd98(%rip), %rsi      # 0x124de
callq 0x31e0
leaq -0x40(%rbp), %rdi
callq 0x31b0
movq %rax, -0x30(%rbp)
movq -0x8(%rbp), %rdi
addq $0x3bc, %rdi            # imm = 0x3BC
movq -0x30(%rbp), %rax
movl 0x14(%rax), %edx
addl $0x76c, %edx            # imm = 0x76C
movq -0x30(%rbp), %rax
movl 0x10(%rax), %ecx
addl $0x1, %ecx
movq -0x30(%rbp), %rax
movl 0xc(%rax), %r8d
movq -0x30(%rbp), %rax
movl 0x8(%rax), %r9d
movq -0x30(%rbp), %rax
movl 0x4(%rax), %r10d
movq -0x30(%rbp), %rax
movl (%rax), %eax
leaq 0xcd52(%rip), %rsi      # 0x124f1
movl %r10d, (%rsp)
movl %eax, 0x8(%rsp)
movb $0x0, %al
callq 0x3070
movb -0x44(%rbp), %dl
movl $0x4d4d, %eax           # imm = 0x4D4D
movl $0x4949, %ecx           # imm = 0x4949
cmpb $0x0, %dl
cmovnel %ecx, %eax
movw %ax, %cx
movq -0x8(%rbp), %rax
movw %cx, (%rax)
movq -0x8(%rbp), %rax
movw $0x2a, 0x2(%rax)
movq -0x8(%rbp), %rax
movl $0xa, 0x4(%rax)
movq -0x8(%rbp), %rdi
addq $0xa, %rdi
movl $0xfe, %esi
movl $0x4, %edx
movl $0x1, %ecx
xorl %r8d, %r8d
callq 0x5b80
movq -0x8(%rbp), %rdi
addq $0xa, %rdi
movl -0xc(%rbp), %r8d
movl $0x100, %esi            # imm = 0x100
movl $0x4, %edx
movl $0x1, %ecx
callq 0x5b80
movq -0x8(%rbp), %rdi
addq $0xa, %rdi
movl -0x10(%rbp), %r8d
movl $0x101, %esi            # imm = 0x101
movl $0x4, %edx
movl $0x1, %ecx
callq 0x5b80
movq -0x8(%rbp), %rdi
addq $0xa, %rdi
movl -0x14(%rbp), %ecx
movl -0x34(%rbp), %r8d
movl $0x102, %esi            # imm = 0x102
movl $0x3, %edx
callq 0x5b80
cmpl $0x2, -0x14(%rbp)
jle 0x5896
movq -0x8(%rbp), %rax
addq $0x114, %rax            # imm = 0x114
movq -0x8(%rbp), %rcx
subq %rcx, %rax
movl %eax, %ecx
movq -0x8(%rbp), %rax
addq $0xc, %rax
movq -0x8(%rbp), %rdx
movzwl 0xa(%rdx), %edx
subl $0x1, %edx
movslq %edx, %rdx
imulq $0xc, %rdx, %rdx
addq %rdx, %rax
movl %ecx, 0x8(%rax)
movl $0x0, -0x20(%rbp)
cmpl $0x4, -0x20(%rbp)
jge 0x58c4
movl -0x34(%rbp), %eax
movw %ax, %dx
movq -0x8(%rbp), %rax
movslq -0x20(%rbp), %rcx
movw %dx, 0x114(%rax,%rcx,2)
movl -0x20(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x20(%rbp)
jmp 0x589d
movq -0x8(%rbp), %rdi
addq $0xa, %rdi
movl $0x103, %esi            # imm = 0x103
movl $0x3, %edx
movl $0x1, %r8d
movl %r8d, %ecx
callq 0x5b80
movq -0x8(%rbp), %rdi
addq $0xa, %rdi
cmpl $0x1, -0x14(%rbp)
setg %al
andb $0x1, %al
movzbl %al, %r8d
addl $0x1, %r8d
movl $0x106, %esi            # imm = 0x106
movl $0x3, %edx
movl $0x1, %ecx
callq 0x5b80
cmpq $0x0, 0x10(%rbp)
je 0x5961
movq -0x8(%rbp), %rax
addq $0xa, %rax
movq %rax, -0x50(%rbp)
movq -0x8(%rbp), %rdi
addq $0x11c, %rdi            # imm = 0x11C
callq 0x30d0
movq -0x50(%rbp), %rdi
addq $0x1, %rax
movl %eax, %ecx
movq -0x8(%rbp), %rax
addq $0x11c, %rax            # imm = 0x11C
movq -0x8(%rbp), %rdx
subq %rdx, %rax
movl %eax, %r8d
movl $0x10e, %esi            # imm = 0x10E
movl $0x2, %edx
callq 0x5b80
movq -0x8(%rbp), %rax
addq $0xa, %rax
movq %rax, -0x70(%rbp)
movq -0x8(%rbp), %rdi
addq $0x31c, %rdi            # imm = 0x31C
callq 0x30d0
movq -0x70(%rbp), %rdi
addq $0x1, %rax
movl %eax, %ecx
movq -0x8(%rbp), %rax
addq $0x31c, %rax            # imm = 0x31C
movq -0x8(%rbp), %rdx
subq %rdx, %rax
movl %eax, %r8d
movl $0x10f, %esi            # imm = 0x10F
movl $0x2, %edx
callq 0x5b80
movq -0x8(%rbp), %rax
addq $0xa, %rax
movq %rax, -0x68(%rbp)
movq -0x8(%rbp), %rdi
addq $0x35c, %rdi            # imm = 0x35C
callq 0x30d0
movq -0x68(%rbp), %rdi
addq $0x1, %rax
movl %eax, %ecx
movq -0x8(%rbp), %rax
addq $0x35c, %rax            # imm = 0x35C
movq -0x8(%rbp), %rdx
subq %rdx, %rax
movl %eax, %r8d
movl $0x110, %esi            # imm = 0x110
movl $0x2, %edx
callq 0x5b80
movq -0x8(%rbp), %rdi
addq $0xa, %rdi
movslq -0x24(%rbp), %rax
addq $0x3d0, %rax            # imm = 0x3D0
movl %eax, %r8d
movl $0x111, %esi            # imm = 0x111
movl $0x4, %edx
movl $0x1, %ecx
callq 0x5b80
movq -0x8(%rbp), %rdi
addq $0xa, %rdi
movl -0x14(%rbp), %r8d
movl $0x115, %esi            # imm = 0x115
movl $0x3, %edx
movl $0x1, %ecx
callq 0x5b80
movq -0x8(%rbp), %rdi
addq $0xa, %rdi
movl -0x10(%rbp), %r8d
movl $0x116, %esi            # imm = 0x116
movl $0x4, %edx
movl $0x1, %ecx
callq 0x5b80
movq -0x8(%rbp), %rdi
addq $0xa, %rdi
movl -0x10(%rbp), %eax
imull -0xc(%rbp), %eax
imull -0x14(%rbp), %eax
imull -0x34(%rbp), %eax
movl $0x8, %ecx
cltd
idivl %ecx
movl %eax, %r8d
movl $0x117, %esi            # imm = 0x117
movl $0x4, %edx
movl $0x1, %ecx
callq 0x5b80
movq -0x8(%rbp), %rdi
addq $0xa, %rdi
movl $0x11c, %esi            # imm = 0x11C
movl $0x3, %edx
movl $0x1, %r8d
movl %r8d, %ecx
callq 0x5b80
movq -0x8(%rbp), %rax
addq $0xa, %rax
movq %rax, -0x60(%rbp)
movq -0x8(%rbp), %rdi
addq $0x39c, %rdi            # imm = 0x39C
callq 0x30d0
movq -0x60(%rbp), %rdi
addq $0x1, %rax
movl %eax, %ecx
movq -0x8(%rbp), %rax
addq $0x39c, %rax            # imm = 0x39C
movq -0x8(%rbp), %rdx
subq %rdx, %rax
movl %eax, %r8d
movl $0x131, %esi            # imm = 0x131
movl $0x2, %edx
callq 0x5b80
movq -0x8(%rbp), %rax
addq $0xa, %rax
movq %rax, -0x58(%rbp)
movq -0x8(%rbp), %rdi
addq $0x3bc, %rdi            # imm = 0x3BC
callq 0x30d0
movq -0x58(%rbp), %rdi
addq $0x1, %rax
movl %eax, %ecx
movq -0x8(%rbp), %rax
addq $0x3bc, %rax            # imm = 0x3BC
movq -0x8(%rbp), %rdx
subq %rdx, %rax
movl %eax, %r8d
movl $0x132, %esi            # imm = 0x132
movl $0x2, %edx
callq 0x5b80
movq -0x8(%rbp), %rdi
addq $0xa, %rdi
movb -0x19(%rbp), %cl
movl $0x1, %r8d
movl $0x3, %eax
testb $0x1, %cl
cmovnel %eax, %r8d
movl $0x153, %esi            # imm = 0x153
movl $0x3, %edx
movl $0x1, %ecx
callq 0x5b80
addq $0x80, %rsp
popq %rbp
retq
nop