// File: /workspace/github_cpp_makefile_star2_O3/tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Image.cpp
// Function: gaps::R2Image::WritePPMFile(char const*, int) const
int R2Image::
WritePPMFile(const char *filename, int ascii) const
{
  // Check ncomponents
  assert(ncomponents == 3);

  // Check pgm/ppm
  int pgm = (strstr(filename, ".pgm")) ? 1 : 0;

  // Check type
  if (ascii) {
    // Open file
    FILE *fp = fopen(filename, "w");
    if (!fp) {
      RNFail("Unable to open image file: %s", filename);
      return 0;
    }

    // Print PPM image file 
    // First ppm pixel is top-left, so write in opposite scan-line order
    if (pgm) fprintf(fp, "P2\n");
    else fprintf(fp, "P3\n");
    fprintf(fp, "%d %d\n", width, height);
    fprintf(fp, "255\n");
    for (int j = height-1; j >= 0 ; j--) {
      for (int i = 0; i < width; i++) {
        const unsigned char *p = Pixel(i, j);
        int r = *(p++) & 0xFF;
        int g = *(p++) & 0xFF;
        int b = *(p++) & 0xFF;
        if (pgm) fprintf(fp, "%-3d  ", (int) (0.30*r + 0.59*g + 0.11*b));
        else fprintf(fp, "%-3d %-3d %-3d  ", r, g, b);
        if (((i+1) % 4) == 0) fprintf(fp, "\n");
      }
      if ((width % 4) != 0) fprintf(fp, "\n");
    }
    fprintf(fp, "\n");

    // Close file
    fclose(fp);
  }
  else {
    // Open file
    FILE *fp = fopen(filename, "wb");
    if (!fp) {
      RNFail("Unable to open image file: %s", filename);
      return 0;
    }
    
    // Print PPM image file 
    // First ppm pixel is top-left, so write in opposite scan-line order
    if (pgm) fprintf(fp, "P5\n");
    else fprintf(fp, "P6\n");
    fprintf(fp, "%d %d\n", width, height);
    fprintf(fp, "255\n");
    for (int j = height-1; j >= 0 ; j--) {
      for (int i = 0; i < width; i++) {
        const unsigned char *p = Pixel(i, j);
        int r = *(p++) & 0xFF;
        int g = *(p++) & 0xFF;
        int b = *(p++) & 0xFF;
        if (pgm) fprintf(fp, "%c", (int) (0.30*r + 0.59*g + 0.11*b));
        else fprintf(fp, "%c%c%c", r, g, b);
      }
    }
    
    // Close file
    fclose(fp);
  }

  // Return number of bytes written
  return width * height * ncomponents;  
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
pushq %rax
movl %edx, %ebp
movq %rsi, %r12
movq %rdi, %rbx
leaq 0x3ed5e(%rip), %rsi     # 0xa9bc6
movq %r12, %rdi
callq 0xe100
movq %rax, %r14
testl %ebp, %ebp
je 0x6afe5
leaq 0x3c937(%rip), %rsi     # 0xa77b9
movq %r12, %rdi
callq 0xe530
testq %rax, %rax
je 0x6b125
movq %rax, %r15
testq %r14, %r14
leaq 0x3ee80(%rip), %rax     # 0xa9d20
leaq 0x3ee4a(%rip), %rdi     # 0xa9cf1
cmoveq %rax, %rdi
movl $0x3, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0xe8b0
movl (%rbx), %edx
movl 0x4(%rbx), %ecx
leaq 0x37b6e(%rip), %rsi     # 0xa2a37
movq %r15, %rdi
xorl %eax, %eax
callq 0xe670
leaq 0x3eea8(%rip), %rdi     # 0xa9d82
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0xe8b0
movl 0x4(%rbx), %eax
testl %eax, %eax
jle 0x6afd3
leaq 0x3ee93(%rip), %r13     # 0xa9d91
movq %rax, (%rsp)
leal -0x1(%rax), %r12d
movl (%rbx), %eax
testl %eax, %eax
jle 0x6afb5
xorl %ebp, %ebp
movl 0xc(%rbx), %eax
imull %r12d, %eax
movl 0x8(%rbx), %ecx
imull %ebp, %ecx
addl %eax, %ecx
movq 0x10(%rbx), %rax
movslq %ecx, %rsi
movzbl (%rax,%rsi), %edx
movzbl 0x1(%rax,%rsi), %ecx
movzbl 0x2(%rax,%rsi), %r8d
testq %r14, %r14
je 0x6af85
xorps %xmm0, %xmm0
cvtsi2sd %edx, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %ecx, %xmm1
mulsd 0x3e39e(%rip), %xmm1    # 0xa92f0
mulsd 0x3e39e(%rip), %xmm0    # 0xa92f8
addsd %xmm1, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %r8d, %xmm1
mulsd 0x3e392(%rip), %xmm1    # 0xa9300
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %edx
movq %r15, %rdi
movq %r13, %rsi
xorl %eax, %eax
callq 0xe670
jmp 0x6af96
movq %r15, %rdi
leaq 0x3edf8(%rip), %rsi     # 0xa9d87
xorl %eax, %eax
callq 0xe670
incl %ebp
testb $0x3, %bpl
jne 0x6afab
movl $0xa, %edi
movq %r15, %rsi
callq 0xe5a0
movl (%rbx), %eax
cmpl %eax, %ebp
jl 0x6af12
testb $0x3, %al
je 0x6afc6
movl $0xa, %edi
movq %r15, %rsi
callq 0xe5a0
cmpl $0x1, (%rsp)
movl %r12d, %eax
jg 0x6aefe
movl $0xa, %edi
movq %r15, %rsi
callq 0xe5a0
jmp 0x6b111
leaq 0x37a95(%rip), %rsi     # 0xa2a81
movq %r12, %rdi
callq 0xe530
testq %rax, %rax
je 0x6b125
movq %rax, %r15
testq %r14, %r14
leaq 0x3ed49(%rip), %rax     # 0xa9d53
leaq 0x3ed3e(%rip), %rdi     # 0xa9d4f
cmoveq %rax, %rdi
movl $0x3, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0xe8b0
movl (%rbx), %edx
movl 0x4(%rbx), %ecx
leaq 0x37a04(%rip), %rsi     # 0xa2a37
movq %r15, %rdi
xorl %eax, %eax
callq 0xe670
leaq 0x3ed3e(%rip), %rdi     # 0xa9d82
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0xe8b0
movl 0x4(%rbx), %ebp
testl %ebp, %ebp
jle 0x6b111
movl (%rbx), %eax
leaq 0x3ed2e(%rip), %r12     # 0xa9d98
movl %ebp, %edx
movq %rdx, (%rsp)
leal -0x1(%rdx), %ebp
testl %eax, %eax
jle 0x6b107
xorl %r13d, %r13d
movl 0xc(%rbx), %eax
imull %ebp, %eax
movl 0x8(%rbx), %ecx
imull %r13d, %ecx
addl %eax, %ecx
movq 0x10(%rbx), %rax
movslq %ecx, %rsi
movzbl (%rax,%rsi), %edx
movzbl 0x1(%rax,%rsi), %ecx
movzbl 0x2(%rax,%rsi), %r8d
testq %r14, %r14
je 0x6b0ec
xorps %xmm0, %xmm0
cvtsi2sd %edx, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %ecx, %xmm1
mulsd 0x3e232(%rip), %xmm1    # 0xa92f0
mulsd 0x3e232(%rip), %xmm0    # 0xa92f8
addsd %xmm1, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %r8d, %xmm1
mulsd 0x3e226(%rip), %xmm1    # 0xa9300
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %edi
movq %r15, %rsi
callq 0xe5a0
jmp 0x6b0f9
movq %r15, %rdi
movq %r12, %rsi
xorl %eax, %eax
callq 0xe670
incl %r13d
movl (%rbx), %eax
cmpl %eax, %r13d
jl 0x6b07e
cmpl $0x2, (%rsp)
jge 0x6b06a
movq %r15, %rdi
callq 0xe430
movl 0x4(%rbx), %ebp
imull (%rbx), %ebp
imull 0x8(%rbx), %ebp
jmp 0x6b138
leaq 0x3eaf9(%rip), %rdi     # 0xa9c25
xorl %ebp, %ebp
movq %r12, %rsi
xorl %eax, %eax
callq 0x719d2
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop