// File: /workspace/github_cpp_makefile_star2_O2/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:
pushq %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x20, %rsp
movq %rsi, %r14
leaq 0x4234(%rip), %rsi      # 0xc538
callq 0x4360
movq %rax, 0x18(%rsp)
testq %rax, %rax
je 0x8465
movl (%r14), %ebx
movl 0x4(%r14), %eax
movl %eax, %r15d
movl %r15d, %eax
imull %ebx, %eax
leal (%rax,%rax,2), %eax
movl %eax, %edi
callq 0x4030
movq %rax, 0x10(%rsp)
movq 0x8(%r14), %rcx
movl %ebx, %edx
leal (%rdx,%rdx,2), %edx
xorl %esi, %esi
movl %esi, %r9d
movsd 0x3d1c(%rip), %xmm0     # 0xc068
xorpd %xmm1, %xmm1
movl %esi, %edi
movl %esi, %r8d
cmpq %r15, %r9
je 0x83fb
movl %r8d, %r10d
addq %rax, %r10
movl %edi, %r11d
imulq $0x18, %r11, %r11
addq %rcx, %r11
movl %esi, %r14d
movl %ebx, %ebp
subl $0x1, %ebp
jb 0x83ee
movsd (%r11,%r14,8), %xmm2
mulsd %xmm0, %xmm2
movapd %xmm0, %xmm3
minsd %xmm2, %xmm3
cmpltsd %xmm1, %xmm2
andnpd %xmm3, %xmm2
cvttsd2si %xmm2, %r12d
movb %r12b, (%r10,%r14)
movsd 0x8(%r11,%r14,8), %xmm2
mulsd %xmm0, %xmm2
movapd %xmm0, %xmm3
minsd %xmm2, %xmm3
cmpltsd %xmm1, %xmm2
andnpd %xmm3, %xmm2
cvttsd2si %xmm2, %r12d
movb %r12b, 0x1(%r10,%r14)
movsd 0x10(%r11,%r14,8), %xmm2
mulsd %xmm0, %xmm2
movapd %xmm0, %xmm3
minsd %xmm2, %xmm3
cmpltsd %xmm1, %xmm2
andnpd %xmm3, %xmm2
cvttsd2si %xmm2, %r12d
movb %r12b, 0x2(%r10,%r14)
addq $0x3, %r14
jmp 0x8373
incq %r9
addl %edx, %r8d
addl %ebx, %edi
jmp 0x8355
leaq 0x4139(%rip), %rdi      # 0xc53b
xorl %eax, %eax
movl %eax, %ecx
movq %rcx, %rsi
movq %rcx, %rdx
callq 0x40e0
movq %rax, (%rsp)
testq %rax, %rax
je 0x845c
movq %rax, %r14
movq %rax, %rdi
callq 0x4510
movq %rax, 0x8(%rsp)
testq %rax, %rax
je 0x846e
movq 0x9b6a(%rip), %rsi      # 0x11fa0
movl $0xc8, %edx
movq %r14, %rdi
callq 0x4340
movq %rax, %rdi
callq 0x4410
testl %eax, %eax
je 0x8484
movq %rsp, %rdi
leaq 0x8(%rsp), %rsi
callq 0x4240
xorl %eax, %eax
movl %eax, %ebx
jmp 0x8530
xorl %eax, %eax
movl %eax, %ebx
jmp 0x853a
xorl %eax, %eax
movl %eax, %ebx
movl %eax, %edx
movq %rsp, %rdi
movq %rdx, %rsi
callq 0x41d0
jmp 0x8530
movq (%rsp), %rdi
movq 0x18(%rsp), %rsi
callq 0x4560
movl %r15d, %ecx
movq (%rsp), %rdi
movq 0x8(%rsp), %rsi
subq $0x8, %rsp
pushq $0x8
popq %r8
pushq $0x2
popq %r9
movl %ebx, %edx
pushq $0x0
pushq $0x0
pushq $0x0
callq 0x44b0
addq $0x20, %rsp
movq (%rsp), %rdi
leaq (,%r15,8), %rsi
callq 0x44c0
movq %rax, %r14
movq 0x10(%rsp), %rax
movl %ebx, %ecx
leal (%rcx,%rcx,2), %ecx
xorl %edx, %edx
movl %edx, %esi
cmpq %rsi, %r15
je 0x84f2
movl %edx, %edi
addq %rax, %rdi
movq %rdi, (%r14,%rsi,8)
incq %rsi
addl %ecx, %edx
jmp 0x84dd
movq (%rsp), %rdi
movq 0x8(%rsp), %rsi
movq %r14, %rdx
callq 0x4470
movq (%rsp), %rdi
movq 0x8(%rsp), %rsi
xorl %edx, %edx
movl %edx, %ecx
callq 0x40f0
movq (%rsp), %rdi
movq %r14, %rsi
callq 0x41f0
movb $0x1, %bl
movq %rsp, %rdi
leaq 0x8(%rsp), %rsi
callq 0x4240
leaq 0x10(%rsp), %rdi
callq 0x94b2
leaq 0x18(%rsp), %rdi
callq 0x94cc
movl %ebx, %eax
addq $0x20, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
jmp 0x855c
jmp 0x855c
movq %rax, %rbx
jmp 0x8569
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x94b2
leaq 0x18(%rsp), %rdi
callq 0x94cc
movq %rbx, %rdi
callq 0x4520