// File: misohena[P]drawstars/./image_png.h
// Function: bool writePNGFile<Image<ColorRGB<double>>>(char const*, Image<ColorRGB<double>> const&)
bool writePNGFile(const char *filename, const ImageT &image)
{
    png_bytep *rows = nullptr;

    File file(filename, "wb");
    if(!file){
        return false;
    }

    const unsigned int width = image.width();
    const unsigned int height = image.height();
    const unsigned int lineStride = 3 * width;
    std::unique_ptr<std::uint8_t[]> body(new std::uint8_t[lineStride * height]);
    for(int y = 0; y < height; ++y){
        auto dst = body.get() + y * lineStride;
        auto src = image.at(0, y);
        for(int x = 0; x < width; ++x, ++src, dst += 3){
            using VT = typename ImageT::PixelType::ValueType;
            dst[0] = static_cast<std::uint8_t>(clamp(255*src->r, VT(0), VT(255)));
            dst[1] = static_cast<std::uint8_t>(clamp(255*src->g, VT(0), VT(255)));
            dst[2] = static_cast<std::uint8_t>(clamp(255*src->b, VT(0), VT(255)));
        }
    }


    const int color_type = PNG_COLOR_TYPE_RGB; //PNG_COLOR_TYPE_RGB_ALPHA, PNG_COLOR_TYPE_GRAY

    // init png objects.
    png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
    if(!png_ptr){
        return false;
    }
    png_infop info_ptr = png_create_info_struct(png_ptr);
    if(!info_ptr){
        png_destroy_read_struct(&png_ptr, NULL, NULL);
        return false;
    }

    // error handler.
    if(setjmp(png_jmpbuf(png_ptr))){
        if(rows){
            png_free(png_ptr, rows);
        }
        png_destroy_write_struct(&png_ptr, &info_ptr);
        return false;
    }

    // setup i/o function.
    //png_set_write_fn(png_ptr, stream, &stream_write, &stream_flush);
    png_init_io(png_ptr, file);

    png_set_IHDR(
        png_ptr,
        info_ptr,
        width,
        height,
        8,
        color_type,
        PNG_INTERLACE_NONE,
        PNG_COMPRESSION_TYPE_DEFAULT,
        PNG_FILTER_TYPE_DEFAULT);

    /// @todo support palette
    //png_set_PLTE(png_ptr, info_ptr, 

    rows = (png_bytep *)png_malloc(png_ptr, sizeof(png_bytep) * height);

    for(unsigned int y = 0; y < height; ++y){
        rows[y] = const_cast<png_bytep>(body.get() + y * lineStride);
    }
    png_set_rows(png_ptr, info_ptr, rows);

    const int transform = PNG_TRANSFORM_IDENTITY; //PNG_TRANSFORM_BGR;

    png_write_png(png_ptr, info_ptr, transform, NULL);

    png_free(png_ptr, rows);
    png_destroy_write_struct(&png_ptr, &info_ptr);
    return true;
}

// Assembly:
subq $0xe8, %rsp
movq %rdi, 0xd8(%rsp)
movq %rsi, 0xd0(%rsp)
movq $0x0, 0xc8(%rsp)
movq 0xd8(%rsp), %rsi
leaq 0xc0(%rsp), %rdi
leaq 0x105e4(%rip), %rdx     # 0x1e4ae
callq 0x11bb0
leaq 0xc0(%rsp), %rdi
callq 0x11bf0
cmpq $0x0, %rax
jne 0xdefa
movb $0x0, 0xe7(%rsp)
movl $0x1, 0xbc(%rsp)
jmp 0xe3b5
movq 0xd0(%rsp), %rdi
callq 0x11c00
movl %eax, 0xb8(%rsp)
movq 0xd0(%rsp), %rdi
callq 0x11c10
movl %eax, 0xb4(%rsp)
movl 0xb8(%rsp), %ecx
movl %ecx, %eax
leal (%rax,%rax,2), %eax
movl %eax, 0xb0(%rsp)
movl 0xb0(%rsp), %eax
movl 0xb4(%rsp), %ecx
imull %ecx, %eax
movl %eax, %edi
callq 0x4030
movq %rax, 0x60(%rsp)
jmp 0xdf54
movq 0x60(%rsp), %rsi
leaq 0xa8(%rsp), %rdi
callq 0x11c20
movl $0x0, 0x98(%rsp)
movl 0x98(%rsp), %eax
cmpl 0xb4(%rsp), %eax
jae 0xe13e
leaq 0xa8(%rsp), %rdi
callq 0x11c50
movl 0x98(%rsp), %ecx
imull 0xb0(%rsp), %ecx
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, 0x90(%rsp)
movq 0xd0(%rsp), %rdi
movl 0x98(%rsp), %edx
xorl %esi, %esi
callq 0x11c80
movq %rax, 0x88(%rsp)
movl $0x0, 0x84(%rsp)
movl 0x84(%rsp), %eax
cmpl 0xb8(%rsp), %eax
jae 0xe126
movq 0x88(%rsp), %rax
movsd (%rax), %xmm0
movsd 0x10061(%rip), %xmm2    # 0x1e060
mulsd %xmm2, %xmm0
xorps %xmm1, %xmm1
callq 0xe9a0
movsd %xmm0, 0x58(%rsp)
jmp 0xe013
movsd 0x58(%rsp), %xmm0
cvttsd2si %xmm0, %eax
movb %al, %cl
movq 0x90(%rsp), %rax
movb %cl, (%rax)
movq 0x88(%rsp), %rax
movsd 0x8(%rax), %xmm0
movsd 0x10022(%rip), %xmm2    # 0x1e060
mulsd %xmm2, %xmm0
xorps %xmm1, %xmm1
callq 0xe9a0
movsd %xmm0, 0x50(%rsp)
jmp 0xe052
movsd 0x50(%rsp), %xmm0
cvttsd2si %xmm0, %eax
movb %al, %cl
movq 0x90(%rsp), %rax
movb %cl, 0x1(%rax)
movq 0x88(%rsp), %rax
movsd 0x10(%rax), %xmm0
movsd 0xffe2(%rip), %xmm2     # 0x1e060
mulsd %xmm2, %xmm0
xorps %xmm1, %xmm1
callq 0xe9a0
movsd %xmm0, 0x48(%rsp)
jmp 0xe092
movsd 0x48(%rsp), %xmm0
cvttsd2si %xmm0, %eax
movb %al, %cl
movq 0x90(%rsp), %rax
movb %cl, 0x2(%rax)
movl 0x84(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x84(%rsp)
movq 0x88(%rsp), %rax
addq $0x18, %rax
movq %rax, 0x88(%rsp)
movq 0x90(%rsp), %rax
addq $0x3, %rax
movq %rax, 0x90(%rsp)
jmp 0xdfd7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xa0(%rsp)
movl %eax, 0x9c(%rsp)
jmp 0xe3d3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0xa0(%rsp)
movl %eax, 0x9c(%rsp)
leaq 0xa8(%rsp), %rdi
callq 0x11cc0
jmp 0xe3d3
jmp 0xe128
movl 0x98(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x98(%rsp)
jmp 0xdf71
movl $0x2, 0x80(%rsp)
leaq 0x10361(%rip), %rdi     # 0x1e4b1
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rsi
movq %rcx, %rdx
callq 0x40f0
movq %rax, 0x40(%rsp)
jmp 0xe166
movq 0x40(%rsp), %rax
movq %rax, 0x78(%rsp)
cmpq $0x0, 0x78(%rsp)
jne 0xe190
movb $0x0, 0xe7(%rsp)
movl $0x1, 0xbc(%rsp)
jmp 0xe3a8
movq 0x78(%rsp), %rdi
callq 0x45e0
movq %rax, 0x38(%rsp)
jmp 0xe1a1
movq 0x38(%rsp), %rax
movq %rax, 0x70(%rsp)
cmpq $0x0, 0x70(%rsp)
jne 0xe1de
xorl %eax, %eax
movl %eax, %edx
leaq 0x78(%rsp), %rdi
movq %rdx, %rsi
callq 0x4210
jmp 0xe1c6
movb $0x0, 0xe7(%rsp)
movl $0x1, 0xbc(%rsp)
jmp 0xe3a8
movq 0x78(%rsp), %rdi
movq 0x1bdae(%rip), %rsi     # 0x29f98
movl $0xc8, %edx
callq 0x43f0
movq %rax, 0x30(%rsp)
jmp 0xe1fb
movq 0x30(%rsp), %rdi
callq 0x44d0
cmpl $0x0, %eax
je 0xe254
cmpq $0x0, 0xc8(%rsp)
je 0xe22b
movq 0x78(%rsp), %rdi
movq 0xc8(%rsp), %rsi
callq 0x4250
jmp 0xe229
jmp 0xe22b
leaq 0x78(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x42b0
jmp 0xe23c
movb $0x0, 0xe7(%rsp)
movl $0x1, 0xbc(%rsp)
jmp 0xe3a8
movq 0x78(%rsp), %rax
movq %rax, 0x28(%rsp)
leaq 0xc0(%rsp), %rdi
callq 0x11bf0
movq 0x28(%rsp), %rdi
movq %rax, %rsi
callq 0x4640
jmp 0xe27a
movq 0x78(%rsp), %rdi
movq 0x70(%rsp), %rsi
movl 0xb8(%rsp), %edx
movl 0xb4(%rsp), %ecx
movq %rsp, %rax
movl $0x0, 0x10(%rax)
movl $0x0, 0x8(%rax)
movl $0x0, (%rax)
movl $0x8, %r8d
movl $0x2, %r9d
callq 0x4580
jmp 0xe2bc
movq 0x78(%rsp), %rdi
movl 0xb4(%rsp), %eax
movl %eax, %esi
shlq $0x3, %rsi
callq 0x4590
movq %rax, 0x20(%rsp)
jmp 0xe2da
movq 0x20(%rsp), %rax
movq %rax, 0xc8(%rsp)
movl $0x0, 0x6c(%rsp)
movl 0x6c(%rsp), %eax
cmpl 0xb4(%rsp), %eax
jae 0xe33a
leaq 0xa8(%rsp), %rdi
callq 0x11c50
movq %rax, %rdx
movl 0x6c(%rsp), %eax
imull 0xb0(%rsp), %eax
movl %eax, %eax
addq %rax, %rdx
movq 0xc8(%rsp), %rax
movl 0x6c(%rsp), %ecx
movq %rdx, (%rax,%rcx,8)
movl 0x6c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x6c(%rsp)
jmp 0xe2ef
movq 0x78(%rsp), %rdi
movq 0x70(%rsp), %rsi
movq 0xc8(%rsp), %rdx
callq 0x4540
jmp 0xe353
movl $0x0, 0x68(%rsp)
movq 0x78(%rsp), %rdi
movq 0x70(%rsp), %rsi
xorl %edx, %edx
movl %edx, %ecx
callq 0x4100
jmp 0xe370
movq 0x78(%rsp), %rdi
movq 0xc8(%rsp), %rsi
callq 0x4250
jmp 0xe384
leaq 0x78(%rsp), %rdi
leaq 0x70(%rsp), %rsi
callq 0x42b0
jmp 0xe395
movb $0x1, 0xe7(%rsp)
movl $0x1, 0xbc(%rsp)
leaq 0xa8(%rsp), %rdi
callq 0x11cc0
leaq 0xc0(%rsp), %rdi
callq 0x11d20
movb 0xe7(%rsp), %al
andb $0x1, %al
addq $0xe8, %rsp
retq
leaq 0xc0(%rsp), %rdi
callq 0x11d20
movq 0xa0(%rsp), %rdi
callq 0x45f0
nopl (%rax)