// File: /workspace/github_cpp_makefile_star2_O1/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 0x3df96(%rip), %rsi     # 0xa9ba6
movq %r12, %rdi
callq 0xe100
movq %rax, %r14
testl %ebp, %ebp
je 0x6bd8d
leaq 0x3bb93(%rip), %rsi     # 0xa77bd
movq %r12, %rdi
callq 0xe530
movq %rax, %r15
testq %rax, %rax
je 0x6bec0
testq %r14, %r14
leaq 0x3e0b8(%rip), %rax     # 0xa9d00
leaq 0x3e082(%rip), %rdi     # 0xa9cd1
cmoveq %rax, %rdi
movl $0x3, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0xe8b0
movl (%rbx), %edx
movl 0x4(%rbx), %ecx
leaq 0x36dc9(%rip), %rsi     # 0xa2a3a
movq %r15, %rdi
xorl %eax, %eax
callq 0xe670
leaq 0x3e0e0(%rip), %rdi     # 0xa9d62
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0xe8b0
movl 0x4(%rbx), %eax
testl %eax, %eax
jle 0x6bd7b
leaq 0x3e0cb(%rip), %r13     # 0xa9d71
movq %rax, (%rsp)
leal -0x1(%rax), %r12d
movl (%rbx), %eax
testl %eax, %eax
jle 0x6bd5d
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 0x6bd2d
xorps %xmm0, %xmm0
cvtsi2sd %edx, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %ecx, %xmm1
mulsd 0x3d5f6(%rip), %xmm1    # 0xa92f0
mulsd 0x3d5f6(%rip), %xmm0    # 0xa92f8
addsd %xmm1, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %r8d, %xmm1
mulsd 0x3d5ea(%rip), %xmm1    # 0xa9300
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %edx
movq %r15, %rdi
movq %r13, %rsi
xorl %eax, %eax
callq 0xe670
jmp 0x6bd3e
movq %r15, %rdi
leaq 0x3e030(%rip), %rsi     # 0xa9d67
xorl %eax, %eax
callq 0xe670
incl %ebp
testb $0x3, %bpl
jne 0x6bd53
movl $0xa, %edi
movq %r15, %rsi
callq 0xe5a0
movl (%rbx), %eax
cmpl %eax, %ebp
jl 0x6bcba
testb $0x3, %al
je 0x6bd6e
movl $0xa, %edi
movq %r15, %rsi
callq 0xe5a0
cmpl $0x1, (%rsp)
movl %r12d, %eax
jg 0x6bca6
movl $0xa, %edi
movq %r15, %rsi
callq 0xe5a0
jmp 0x6beb6
leaq 0x36cf0(%rip), %rsi     # 0xa2a84
movq %r12, %rdi
callq 0xe530
movq %rax, %r15
testq %rax, %rax
je 0x6bec0
testq %r14, %r14
leaq 0x3df81(%rip), %rax     # 0xa9d33
leaq 0x3df76(%rip), %rdi     # 0xa9d2f
cmoveq %rax, %rdi
movl $0x3, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0xe8b0
movl (%rbx), %edx
movl 0x4(%rbx), %ecx
leaq 0x36c5f(%rip), %rsi     # 0xa2a3a
movq %r15, %rdi
xorl %eax, %eax
callq 0xe670
leaq 0x3df76(%rip), %rdi     # 0xa9d62
movl $0x4, %esi
movl $0x1, %edx
movq %r15, %rcx
callq 0xe8b0
movl 0x4(%rbx), %ebp
testl %ebp, %ebp
jle 0x6beb6
leaq 0x3df68(%rip), %r12     # 0xa9d78
movl %ebp, %ecx
movq %rcx, (%rsp)
leal -0x1(%rcx), %ebp
cmpl $0x0, (%rbx)
jle 0x6beac
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 0x6be93
xorps %xmm0, %xmm0
cvtsi2sd %edx, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %ecx, %xmm1
mulsd 0x3d48b(%rip), %xmm1    # 0xa92f0
mulsd 0x3d48b(%rip), %xmm0    # 0xa92f8
addsd %xmm1, %xmm0
xorps %xmm1, %xmm1
cvtsi2sd %r8d, %xmm1
mulsd 0x3d47f(%rip), %xmm1    # 0xa9300
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %edi
movq %r15, %rsi
callq 0xe5a0
jmp 0x6bea0
movq %r15, %rdi
movq %r12, %rsi
xorl %eax, %eax
callq 0xe670
incl %r13d
cmpl (%rbx), %r13d
jl 0x6be25
cmpl $0x2, (%rsp)
jge 0x6be10
movq %r15, %rdi
callq 0xe430
jmp 0x6bed1
leaq 0x3dd3e(%rip), %rdi     # 0xa9c05
movq %r12, %rsi
xorl %eax, %eax
callq 0x72a1e
testq %r15, %r15
je 0x6bee2
movl 0x4(%rbx), %eax
imull (%rbx), %eax
imull 0x8(%rbx), %eax
jmp 0x6bee4
xorl %eax, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
nop