// File: /workspace/github_cpp_makefile_star2_O1/matthewarcus[P]mmps/circularize.cpp
// Function: main
int main(int argc, char* argv[])
{
  Image inImage;
  const char* infilename = "images/earth.ppm";
  const char* outfilename = NULL;
  Rgb background = black;
  Rgb ibackground = black;
  int width;
  int height;
  double fuzz = 2.0;

  // array of pointers. Dynamic allocation is less typing than
  // stack allocating everything.
  OptSpec *optspecs [] = {
    // General options
    new RgbSpec ("-bg", background),
    new RgbSpec ("-ibg", ibackground),
    new DoubleSpec ("-fuzz", fuzz),
    new StringSpec ("-f", infilename),
    new StringSpec ("-out", outfilename),
    NULL // Finish up
  };
  
  char *progname = argv[0];
  argc--;argv++;
  while (argc > 0) {
    int result = OptSpec::ReadOpts(argc, argv, optspecs);
    if (result != 1) {
      error(usage, progname);
    }
    argc--; argv++;
  }

  inImage.Read(infilename);

  height = inImage.Height();
  // Make sure image is big enough to fit a full circular image
  width = inImage.Height();
  double radius = height/2.0;

  Image outImage(width, height);
  //fprintf(stderr, "Size %d\n", height);

  for (int row = 0; row < inImage.Height(); row++) {
    int first = -1;
    int last = -1;
    int column;
    // First scan the row in the input image to find the bounds.
    for (column = 0; column < inImage.Width(); column++) {
      Rgb rgb;
      inImage.GetRgb(column, row, rgb);
      if (!rgb.Match(ibackground, fuzz)) {
	first = column;
	break;
      }
    }
    if (first < 0) {
      // No non-background pixel found, what to do?
      first = last = inImage.Width()/2;
    } else {
      // Now look from the right
      for (column = inImage.Width()-1; column >= 0; column--) {
	Rgb rgb;
	inImage.GetRgb(column, row, rgb);
	if (!rgb.Match(ibackground, fuzz)) {
	  last = column+1;
	  break;
	}
      }
    }
    //fprintf(stderr, "%d %d\n", first, last);
    // Work out the half width of the image on this scan line
    double hw = sqrt(square(radius) - square(radius - (row + 0.5)));
    double a = (last-first)/(2.0*hw);
    double b = first - a * (radius - hw);
    for (column = 0; column < width; column++) {
      // Find the corresponding pixel in the input image
      int p = int(floor(a * column + b));
      if (p >= first && p < last) {
	Rgb rgb;
	inImage.GetRgb(p,row,rgb);
	outImage.SetRgb(column,row,rgb);
      } else {
	outImage.SetRgb(column,row,background);
      }
    }
  }
  outImage.Write(outfilename);
  return 0;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4c8, %rsp            # imm = 0x4C8
movl %edi, 0xc(%rsp)
movq %rsi, 0x10(%rsp)
xorl %ebx, %ebx
leaq 0x40(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xaec0
leaq 0x7a11(%rip), %rax      # 0xc05e
movq %rax, 0x38(%rsp)
movq $0x0, 0x30(%rsp)
movb %bl, 0x6(%rsp)
movw %bx, 0x4(%rsp)
movb %bl, 0xa(%rsp)
movw %bx, 0x8(%rsp)
movabsq $0x4000000000000000, %rax # imm = 0x4000000000000000
movq %rax, 0x18(%rsp)
movl $0x18, %edi
callq 0x4210
movq %rax, %r14
leaq 0x79df(%rip), %rsi      # 0xc06f
movq %rax, %rdi
callq 0xa06e
leaq 0xd6e1(%rip), %rbx      # 0x11d80
addq $0x10, %rbx
movq %rbx, (%r14)
leaq 0x4(%rsp), %rax
movq %rax, 0x10(%r14)
movq %r14, 0x90(%rsp)
movl $0x18, %edi
callq 0x4210
movq %rax, %r14
leaq 0x79a8(%rip), %rsi      # 0xc073
movq %rax, %rdi
callq 0xa06e
movq %rbx, (%r14)
leaq 0x8(%rsp), %rax
movq %rax, 0x10(%r14)
movq %r14, 0x98(%rsp)
movl $0x20, %edi
callq 0x4210
movq %rax, %r14
leaq 0x797d(%rip), %rsi      # 0xc078
leaq 0x18(%rsp), %rdx
movsd 0x7900(%rip), %xmm0     # 0xc008
movq %rax, %rdi
callq 0xa1f6
movq %r14, 0xa0(%rsp)
movl $0x18, %edi
callq 0x4210
movq %rax, %r14
leaq 0x7952(%rip), %rsi      # 0xc07e
leaq 0x38(%rsp), %rdx
movq %rax, %rdi
callq 0xa0fe
movq %r14, 0xa8(%rsp)
movl $0x18, %edi
callq 0x4210
movq %rax, %r14
leaq 0x792c(%rip), %rsi      # 0xc081
leaq 0x30(%rsp), %rdx
movq %rax, %rdi
callq 0xa0fe
movq %r14, 0xb0(%rsp)
movq $0x0, 0xb8(%rsp)
movq 0x10(%rsp), %rax
movq (%rax), %rbx
movl 0xc(%rsp), %ecx
leal -0x1(%rcx), %edx
movl %edx, 0xc(%rsp)
addq $0x8, %rax
movq %rax, 0x10(%rsp)
cmpl $0x2, %ecx
jl 0x47fb
leaq 0xc(%rsp), %r14
leaq 0x10(%rsp), %r15
leaq 0x90(%rsp), %r12
leaq 0xc0(%rsp), %r13
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0xa07e
cmpl $0x1, %eax
je 0x47e5
movq 0xda05(%rip), %rdx      # 0x121d0
movl $0x400, %esi            # imm = 0x400
movq %r13, %rdi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x42a0
movq %r13, %rdi
callq 0xa018
movl 0xc(%rsp), %eax
leal -0x1(%rax), %ecx
movl %ecx, 0xc(%rsp)
addq $0x8, 0x10(%rsp)
cmpl $0x1, %eax
jg 0x47b1
movq 0x38(%rsp), %rsi
leaq 0x40(%rsp), %rdi
xorl %edx, %edx
callq 0xb09a
movq 0x48(%rsp), %rax
movq %rax, 0x20(%rsp)
movslq %eax, %rdx
leaq 0xc0(%rsp), %rdi
movq %rdx, %rsi
callq 0xaec0
cmpq $0x0, 0x48(%rsp)
jle 0x4aca
movq 0x20(%rsp), %rax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
mulsd 0x77c7(%rip), %xmm0     # 0xc010
movsd %xmm0, 0x28(%rsp)
mulsd %xmm0, %xmm0
movsd %xmm0, 0x70(%rsp)
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq %rax, 0x78(%rsp)
xorl %r13d, %r13d
leaq 0x1(%rsp), %r12
xorl %ebp, %ebp
movq 0x40(%rsp), %rcx
movl $0xffffffff, %r14d      # imm = 0xFFFFFFFF
testq %rcx, %rcx
jle 0x48e0
movl $0x2, %ebx
xorl %r15d, %r15d
movw $0x0, 0x1(%rsp)
imulq %r13, %rcx
addq 0x50(%rsp), %rcx
movb $0x0, 0x3(%rsp)
movb -0x2(%rbx,%rcx), %al
movb %al, 0x1(%rsp)
movb -0x1(%rbx,%rcx), %al
movb %al, 0x2(%rsp)
movb (%rbx,%rcx), %al
movb %al, 0x3(%rsp)
movsd 0x18(%rsp), %xmm0
movq %r12, %rdi
leaq 0x8(%rsp), %rsi
callq 0xadec
testb %al, %al
cmovel %r15d, %r14d
incq %r15
movq 0x40(%rsp), %rcx
cmpq %r15, %rcx
setg %dl
addq $0x3, %rbx
testb %dl, %al
jne 0x4885
movq 0x40(%rsp), %r15
testl %r14d, %r14d
js 0x4965
movl %r15d, %r12d
decq %r12
movl $0xffffffff, %ebx       # imm = 0xFFFFFFFF
testl %r12d, %r12d
js 0x4975
movw $0x0, 0x1(%rsp)
movb $0x0, 0x3(%rsp)
movl %r12d, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq 0x40(%rsp), %rcx
imulq %rbp, %rcx
addq %rax, %rcx
movq 0x50(%rsp), %rax
addq %rcx, %rax
addq %rcx, %rax
movb (%rax,%rcx), %dl
movb %dl, 0x1(%rsp)
movb 0x1(%rax,%rcx), %dl
movb %dl, 0x2(%rsp)
movb 0x2(%rax,%rcx), %al
movb %al, 0x3(%rsp)
movsd 0x18(%rsp), %xmm0
leaq 0x1(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0xadec
decq %r12
testb %al, %al
cmovel %r15d, %ebx
leal -0x1(%r15), %eax
movl %eax, %r15d
jne 0x48f5
jmp 0x4975
movq %r15, %r14
shrq $0x3f, %r14
addq %r15, %r14
shrq %r14
movl %r14d, %ebx
xorps %xmm0, %xmm0
cvtsi2sd %ebp, %xmm0
addsd 0x768c(%rip), %xmm0     # 0xc010
movsd 0x28(%rsp), %xmm1
subsd %xmm0, %xmm1
mulsd %xmm1, %xmm1
movsd 0x70(%rsp), %xmm0
subsd %xmm1, %xmm0
ucomisd 0x7674(%rip), %xmm0     # 0xc018
jb 0x49ac
sqrtsd %xmm0, %xmm0
jmp 0x49b1
callq 0x4190
cmpl $0x0, 0x20(%rsp)
jle 0x4ab3
movl %ebx, %eax
subl %r14d, %eax
xorps %xmm2, %xmm2
cvtsi2sd %eax, %xmm2
movsd 0x28(%rsp), %xmm1
subsd %xmm0, %xmm1
addsd %xmm0, %xmm0
divsd %xmm0, %xmm2
xorps %xmm0, %xmm0
cvtsi2sd %r14d, %xmm0
movsd %xmm2, 0x88(%rsp)
mulsd %xmm2, %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, 0x80(%rsp)
movl $0x1, %r15d
xorl %r12d, %r12d
xorps %xmm0, %xmm0
cvtsi2sd %r12d, %xmm0
mulsd 0x88(%rsp), %xmm0
addsd 0x80(%rsp), %xmm0
callq 0x42f0
cvttsd2si %xmm0, %eax
cmpl %eax, %r14d
jg 0x4a73
cmpl %eax, %ebx
jle 0x4a73
cltq
movq 0x40(%rsp), %rcx
imulq %rbp, %rcx
addq %rax, %rcx
movq 0x50(%rsp), %rax
addq %rcx, %rax
addq %rcx, %rax
movzwl (%rax,%rcx), %edx
movb 0x2(%rax,%rcx), %al
movq 0xc0(%rsp), %rcx
imulq %r13, %rcx
addq 0xd0(%rsp), %rcx
movw %dx, -0x1(%r15,%rcx)
movb %al, 0x1(%r15,%rcx)
jmp 0x4aa1
movq 0xc0(%rsp), %rax
imulq %r13, %rax
addq 0xd0(%rsp), %rax
movb 0x4(%rsp), %cl
movb %cl, -0x1(%r15,%rax)
movb 0x5(%rsp), %cl
movb %cl, (%r15,%rax)
movb 0x6(%rsp), %cl
movb %cl, 0x1(%r15,%rax)
incq %r12
addq $0x3, %r15
cmpq %r12, 0x78(%rsp)
jne 0x4a05
incq %rbp
addq $0x3, %r13
cmpq %rbp, 0x48(%rsp)
leaq 0x1(%rsp), %r12
jg 0x486d
movq 0x30(%rsp), %rsi
leaq 0xc0(%rsp), %rdi
callq 0xb2b2
leaq 0xc0(%rsp), %rdi
callq 0xaf14
leaq 0x40(%rsp), %rdi
callq 0xaf14
xorl %eax, %eax
addq $0x4c8, %rsp            # imm = 0x4C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x4b38
jmp 0x4b31
jmp 0x4b31
jmp 0x4b1d
jmp 0x4b1d
movq %rax, %rbx
movl $0x20, %esi
jmp 0x4b25
jmp 0x4b1d
movq %rax, %rbx
movl $0x18, %esi
movq %r14, %rdi
callq 0x4220
jmp 0x4b48
jmp 0x4b31
movq %rax, %rbx
jmp 0x4b48
jmp 0x4b38
movq %rax, %rbx
leaq 0xc0(%rsp), %rdi
callq 0xaf14
leaq 0x40(%rsp), %rdi
callq 0xaf14
movq %rbx, %rdi
callq 0x4310
nop