// File: /workspace/github_cpp_makefile_star2_O2/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 0x38abc(%rip), %rsi     # 0x9ab46
movq %r12, %rdi
callq 0xe100
movq %rax, %r14
testl %ebp, %ebp
je 0x621fe
leaq 0x366c5(%rip), %rsi     # 0x98769
movq %r12, %rdi
callq 0xe550
testq %rax, %rax
je 0x622fe
movq %rax, %r15
testq %r14, %r14
leaq 0x38bde(%rip), %rax     # 0x9aca0
leaq 0x38ba8(%rip), %rdi     # 0x9ac71
cmoveq %rax, %rdi
pushq $0x3
popq %rsi
pushq $0x1
popq %r12
movq %r12, %rdx
movq %r15, %rcx
callq 0xe8e0
movl (%rbx), %edx
movl 0x4(%rbx), %ecx
leaq 0x318fc(%rip), %rsi     # 0x939e7
movq %r15, %rdi
xorl %eax, %eax
callq 0xe6a0
leaq 0x38c06(%rip), %rdi     # 0x9ad02
pushq $0x4
popq %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0xe8e0
movl 0x4(%rbx), %r12d
leaq 0x38bfc(%rip), %r13     # 0x9ad11
testl %r12d, %r12d
jle 0x621dc
decl %r12d
xorl %ebp, %ebp
movl (%rbx), %eax
cmpl %eax, %ebp
jge 0x621c4
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 0x62197
cvtsi2sd %edx, %xmm0
cvtsi2sd %ecx, %xmm1
mulsd 0x38139(%rip), %xmm1    # 0x9a2a0
mulsd 0x38139(%rip), %xmm0    # 0x9a2a8
addsd %xmm1, %xmm0
cvtsi2sd %r8d, %xmm1
mulsd 0x38130(%rip), %xmm1    # 0x9a2b0
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %edx
movq %r15, %rdi
movq %r13, %rsi
xorl %eax, %eax
callq 0xe6a0
jmp 0x621a8
movq %r15, %rdi
leaq 0x38b66(%rip), %rsi     # 0x9ad07
xorl %eax, %eax
callq 0xe6a0
incl %ebp
testb $0x3, %bpl
jne 0x62123
pushq $0xa
popq %rdi
movq %r15, %rsi
callq 0xe5c0
jmp 0x62123
testb $0x3, %al
je 0x62115
pushq $0xa
popq %rdi
movq %r15, %rsi
callq 0xe5c0
jmp 0x62115
pushq $0xa
popq %rdi
movq %r15, %rsi
callq 0xe5c0
movq %r15, %rdi
callq 0xe440
movl 0x4(%rbx), %ebp
imull (%rbx), %ebp
imull 0x8(%rbx), %ebp
jmp 0x62311
leaq 0x3182c(%rip), %rsi     # 0x93a31
movq %r12, %rdi
callq 0xe550
testq %rax, %rax
je 0x622fe
movq %rax, %r15
testq %r14, %r14
leaq 0x38ab0(%rip), %rax     # 0x9acd3
leaq 0x38aa5(%rip), %rdi     # 0x9accf
cmoveq %rax, %rdi
pushq $0x3
popq %rsi
pushq $0x1
popq %r12
movq %r12, %rdx
movq %r15, %rcx
callq 0xe8e0
movl (%rbx), %edx
movl 0x4(%rbx), %ecx
leaq 0x3179b(%rip), %rsi     # 0x939e7
movq %r15, %rdi
xorl %eax, %eax
callq 0xe6a0
leaq 0x38aa5(%rip), %rdi     # 0x9ad02
pushq $0x4
popq %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0xe8e0
movl 0x4(%rbx), %ebp
leaq 0x38aa3(%rip), %r12     # 0x9ad18
testl %ebp, %ebp
jle 0x621e7
decl %ebp
xorl %r13d, %r13d
cmpl (%rbx), %r13d
jge 0x62275
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 0x622ec
cvtsi2sd %edx, %xmm0
cvtsi2sd %ecx, %xmm1
mulsd 0x37fdf(%rip), %xmm1    # 0x9a2a0
mulsd 0x37fdf(%rip), %xmm0    # 0x9a2a8
addsd %xmm1, %xmm0
cvtsi2sd %r8d, %xmm1
mulsd 0x37fd6(%rip), %xmm1    # 0x9a2b0
addsd %xmm0, %xmm1
cvttsd2si %xmm1, %edi
movq %r15, %rsi
callq 0xe5c0
jmp 0x622f9
movq %r15, %rdi
movq %r12, %rsi
xorl %eax, %eax
callq 0xe6a0
incl %r13d
jmp 0x62282
leaq 0x388a0(%rip), %rdi     # 0x9aba5
xorl %ebp, %ebp
movq %r12, %rsi
xorl %eax, %eax
callq 0x681ca
movl %ebp, %eax
addq $0x8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq