// File: /workspace/github_cpp_makefile_star2_O2/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 $0xf8, %rsp
movl %edi, 0x14(%rsp)
movq %rsi, 0x28(%rsp)
xorl %ebx, %ebx
leaq 0x48(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xa2e6
andq $0x0, 0x38(%rsp)
leaq 0x7b5b(%rip), %rax      # 0xc05e
andw $0x0, 0x10(%rsp)
movq %rax, 0x40(%rsp)
andw $0x0, 0xc(%rsp)
movb %bl, 0x12(%rsp)
movb %bl, 0xe(%rsp)
movabsq $0x4000000000000000, %rax # imm = 0x4000000000000000
movq %rax, 0x20(%rsp)
pushq $0x18
popq %rdi
callq 0x4210
movq %rax, %r14
leaq 0x7b32(%rip), %rsi      # 0xc06f
leaq 0x10(%rsp), %rdx
movq %rax, %rdi
callq 0x4980
movq %r14, 0xc0(%rsp)
pushq $0x18
popq %rdi
callq 0x4210
movq %rax, %r14
leaq 0x7b0f(%rip), %rsi      # 0xc073
leaq 0xc(%rsp), %rdx
movq %rax, %rdi
callq 0x4980
movq %r14, 0xc8(%rsp)
pushq $0x20
popq %rdi
callq 0x4210
movq %rax, %r14
leaq 0x7aed(%rip), %rsi      # 0xc078
leaq 0x20(%rsp), %rdx
movsd 0x7a70(%rip), %xmm0     # 0xc008
movq %rax, %rdi
callq 0x971a
movq %r14, 0xd0(%rsp)
pushq $0x18
popq %rdi
callq 0x4210
movq %rax, %r14
leaq 0x7ac4(%rip), %rsi      # 0xc07e
leaq 0x40(%rsp), %rdx
movq %rax, %rdi
callq 0x960a
movq %r14, 0xd8(%rsp)
pushq $0x18
popq %rdi
callq 0x4210
movq %rax, %r14
leaq 0x7aa0(%rip), %rsi      # 0xc081
leaq 0x38(%rsp), %rdx
movq %rax, %rdi
callq 0x960a
leaq 0xc0(%rsp), %rbx
movq %r14, 0x20(%rbx)
andq $0x0, 0x28(%rbx)
leaq 0x28(%rsp), %r14
movq (%r14), %rcx
movq (%rcx), %r15
leaq 0x14(%rsp), %r12
movl (%r12), %eax
decl %eax
movl %eax, (%r12)
addq $0x8, %rcx
movq %rcx, 0x28(%rsp)
testl %eax, %eax
jle 0x4659
movq %r12, %rdi
movq %r14, %rsi
movq %rbx, %rdx
callq 0x959a
cmpl $0x1, %eax
je 0x4648
movq 0xdba0(%rip), %rdi      # 0x121e0
movq %r15, %rsi
callq 0x49a9
movl 0x14(%rsp), %eax
decl %eax
movl %eax, 0x14(%rsp)
movq 0x28(%rsp), %rcx
jmp 0x4619
movq 0x40(%rsp), %rsi
leaq 0x48(%rsp), %rdi
xorl %edx, %edx
callq 0xa46a
movq 0x50(%rsp), %r12
movslq %r12d, %rdx
leaq 0x90(%rsp), %rdi
movq %rdx, %rsi
callq 0xa2e6
cvtsi2sd %r12d, %xmm0
mulsd 0x7981(%rip), %xmm0     # 0xc010
movsd %xmm0, 0x30(%rsp)
mulsd %xmm0, %xmm0
movsd %xmm0, 0x78(%rsp)
xorl %r13d, %r13d
testl %r12d, %r12d
cmovlel %r13d, %r12d
leaq 0x9(%rsp), %rbp
leaq 0xc(%rsp), %r15
xorl %eax, %eax
cmpq %rax, 0x50(%rsp)
jle 0x48f5
movq %rax, 0x18(%rsp)
pushq $0x2
popq %r14
xorl %ebx, %ebx
movq 0x48(%rsp), %rax
cmpq %rbx, %rax
jle 0x4721
andw $0x0, 0x9(%rsp)
imulq %r13, %rax
addq 0x58(%rsp), %rax
movb $0x0, 0xb(%rsp)
movb -0x2(%r14,%rax), %cl
movb %cl, 0x9(%rsp)
movb -0x1(%r14,%rax), %cl
movb %cl, 0xa(%rsp)
movb (%r14,%rax), %al
movb %al, 0xb(%rsp)
movsd 0x20(%rsp), %xmm0
movq %rbp, %rdi
movq %r15, %rsi
callq 0xa206
testb %al, %al
je 0x4736
incq %rbx
addq $0x3, %r14
jmp 0x46cb
movq 0x48(%rsp), %rax
cqto
pushq $0x2
popq %rcx
idivq %rcx
movq %rax, %rbx
movl %ebx, %r15d
jmp 0x47b1
movq 0x48(%rsp), %r15
movl %r15d, %r14d
incl %r15d
decq %r14
testl %r14d, %r14d
js 0x47ab
andw $0x0, 0x9(%rsp)
movb $0x0, 0xb(%rsp)
movl %r14d, %eax
andl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
movq 0x48(%rsp), %rcx
imulq 0x18(%rsp), %rcx
addq %rax, %rcx
movq 0x58(%rsp), %rax
addq %rcx, %rax
addq %rcx, %rax
movb (%rax,%rcx), %dl
movb %dl, 0x9(%rsp)
movb 0x1(%rax,%rcx), %dl
movb %dl, 0xa(%rsp)
movb 0x2(%rax,%rcx), %al
movb %al, 0xb(%rsp)
movsd 0x20(%rsp), %xmm0
movq %rbp, %rdi
leaq 0xc(%rsp), %rsi
callq 0xa206
decl %r15d
decq %r14
testb %al, %al
jne 0x4744
jmp 0x47b1
pushq $-0x1
popq %rax
movl %eax, %r15d
cvtsi2sdl 0x18(%rsp), %xmm0
addsd 0x7851(%rip), %xmm0     # 0xc010
movsd 0x30(%rsp), %xmm1
subsd %xmm0, %xmm1
mulsd %xmm1, %xmm1
movsd 0x78(%rsp), %xmm0
subsd %xmm1, %xmm0
ucomisd 0x7839(%rip), %xmm0     # 0xc018
jb 0x47e7
sqrtsd %xmm0, %xmm0
jmp 0x47ec
callq 0x4190
movl %r15d, %eax
subl %ebx, %eax
cvtsi2sd %eax, %xmm2
movsd 0x30(%rsp), %xmm1
subsd %xmm0, %xmm1
addsd %xmm0, %xmm0
divsd %xmm0, %xmm2
cvtsi2sd %ebx, %xmm0
movsd %xmm2, 0x88(%rsp)
mulsd %xmm2, %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, 0x80(%rsp)
pushq $0x1
popq %r14
xorl %ebp, %ebp
cmpq %rbp, %r12
je 0x48da
cvtsi2sd %ebp, %xmm0
mulsd 0x88(%rsp), %xmm0
addsd 0x80(%rsp), %xmm0
callq 0x4310
cvttsd2si %xmm0, %eax
cmpl %eax, %ebx
jg 0x48a0
cmpl %eax, %r15d
jle 0x48a0
cltq
movq 0x48(%rsp), %rcx
imulq 0x18(%rsp), %rcx
addq %rax, %rcx
movq 0x58(%rsp), %rax
addq %rcx, %rax
addq %rcx, %rax
movzwl (%rax,%rcx), %edx
movb 0x2(%rax,%rcx), %al
movq 0x90(%rsp), %rcx
imulq %r13, %rcx
addq 0xa0(%rsp), %rcx
movw %dx, -0x1(%r14,%rcx)
movb %al, 0x1(%r14,%rcx)
jmp 0x48ce
movq 0x90(%rsp), %rax
imulq %r13, %rax
addq 0xa0(%rsp), %rax
movb 0x10(%rsp), %cl
movb %cl, -0x1(%r14,%rax)
movb 0x11(%rsp), %cl
movb %cl, (%r14,%rax)
movb 0x12(%rsp), %cl
movb %cl, 0x1(%r14,%rax)
incq %rbp
addq $0x3, %r14
jmp 0x482b
movq 0x18(%rsp), %rax
incq %rax
addq $0x3, %r13
leaq 0x9(%rsp), %rbp
leaq 0xc(%rsp), %r15
jmp 0x46b5
movq 0x38(%rsp), %rsi
leaq 0x90(%rsp), %rdi
callq 0xa672
leaq 0x90(%rsp), %rdi
callq 0xa31e
leaq 0x48(%rsp), %rdi
callq 0xa31e
xorl %eax, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x495e
jmp 0x4957
jmp 0x4957
jmp 0x4945
jmp 0x4945
movq %rax, %rbx
pushq $0x20
jmp 0x494a
jmp 0x4945
movq %rax, %rbx
pushq $0x18
popq %rsi
movq %r14, %rdi
callq 0x4220
jmp 0x496e
jmp 0x4957
movq %rax, %rbx
jmp 0x496e
jmp 0x495e
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
callq 0xa31e
leaq 0x48(%rsp), %rdi
callq 0xa31e
movq %rbx, %rdi
callq 0x4330