// File: /workspace/github_cpp_makefile_star2_O3/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 0xb0ac
leaq 0x7a21(%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 0x4200
movq %rax, %r14
leaq 0x79ef(%rip), %rsi      # 0xc06f
movq %rax, %rdi
callq 0xa266
leaq 0xd6f1(%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 0x4200
movq %rax, %r14
leaq 0x79b8(%rip), %rsi      # 0xc073
movq %rax, %rdi
callq 0xa266
movq %rbx, (%r14)
leaq 0x8(%rsp), %rax
movq %rax, 0x10(%r14)
movq %r14, 0x98(%rsp)
movl $0x20, %edi
callq 0x4200
movq %rax, %r14
leaq 0x798d(%rip), %rsi      # 0xc078
leaq 0x18(%rsp), %rdx
movsd 0x7910(%rip), %xmm0     # 0xc008
movq %rax, %rdi
callq 0xa3e0
movq %r14, 0xa0(%rsp)
movl $0x18, %edi
callq 0x4200
movq %rax, %r14
leaq 0x7962(%rip), %rsi      # 0xc07e
leaq 0x38(%rsp), %rdx
movq %rax, %rdi
callq 0xa2e8
movq %r14, 0xa8(%rsp)
movl $0x18, %edi
callq 0x4200
movq %rax, %r14
leaq 0x793c(%rip), %rsi      # 0xc081
leaq 0x30(%rsp), %rdx
movq %rax, %rdi
callq 0xa2e8
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 0x47eb
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 0xa276
cmpl $0x1, %eax
je 0x47d5
movq 0xda0d(%rip), %rdx      # 0x121c8
movl $0x400, %esi            # imm = 0x400
movq %r13, %rdi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x4290
movq %r13, %rdi
callq 0xa210
movl 0xc(%rsp), %eax
leal -0x1(%rax), %ecx
movl %ecx, 0xc(%rsp)
addq $0x8, 0x10(%rsp)
cmpl $0x1, %eax
jg 0x47a1
movq 0x38(%rsp), %rsi
leaq 0x40(%rsp), %rdi
xorl %edx, %edx
callq 0xb29a
movq 0x48(%rsp), %rax
movq %rax, 0x20(%rsp)
movslq %eax, %rdx
leaq 0xc0(%rsp), %rdi
movq %rdx, %rsi
callq 0xb0ac
cmpq $0x0, 0x48(%rsp)
jle 0x4ab2
movq 0x20(%rsp), %rax
xorps %xmm0, %xmm0
cvtsi2sd %eax, %xmm0
mulsd 0x77d7(%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), %r15
testq %r15, %r15
jle 0x48ca
movl $0x2, %r14d
xorl %ebx, %ebx
movw $0x0, 0x1(%rsp)
imulq %r13, %r15
addq 0x50(%rsp), %r15
movb $0x0, 0x3(%rsp)
movb -0x2(%r14,%r15), %al
movb %al, 0x1(%rsp)
movb -0x1(%r14,%r15), %al
movb %al, 0x2(%rsp)
movb (%r14,%r15), %al
movb %al, 0x3(%rsp)
movsd 0x18(%rsp), %xmm0
movq %r12, %rdi
leaq 0x8(%rsp), %rsi
callq 0xafd8
movq 0x40(%rsp), %r15
testb %al, %al
je 0x4a35
incq %rbx
addq $0x3, %r14
cmpq %rbx, %r15
jg 0x486f
movq 0x40(%rsp), %rax
movq %rax, %rbx
shrq $0x3f, %rbx
addq %rax, %rbx
shrq %rbx
movl %ebx, %r15d
xorps %xmm0, %xmm0
cvtsi2sd %ebp, %xmm0
addsd 0x7722(%rip), %xmm0     # 0xc010
movsd 0x28(%rsp), %xmm1
subsd %xmm0, %xmm1
mulsd %xmm1, %xmm1
movsd 0x70(%rsp), %xmm0
subsd %xmm1, %xmm0
ucomisd 0x770a(%rip), %xmm0     # 0xc018
jb 0x4916
sqrtsd %xmm0, %xmm0
jmp 0x491b
callq 0x4180
cmpl $0x0, 0x20(%rsp)
jle 0x4a1c
movl %r15d, %eax
subl %ebx, %eax
xorps %xmm2, %xmm2
cvtsi2sd %eax, %xmm2
movsd 0x28(%rsp), %xmm1
subsd %xmm0, %xmm1
addsd %xmm0, %xmm0
divsd %xmm0, %xmm2
xorps %xmm0, %xmm0
cvtsi2sd %ebx, %xmm0
movsd %xmm2, 0x88(%rsp)
mulsd %xmm2, %xmm1
subsd %xmm1, %xmm0
movsd %xmm0, 0x80(%rsp)
movl $0x1, %r14d
xorl %r12d, %r12d
xorps %xmm0, %xmm0
cvtsi2sd %r12d, %xmm0
mulsd 0x88(%rsp), %xmm0
addsd 0x80(%rsp), %xmm0
callq 0x42e0
cvttsd2si %xmm0, %eax
cmpl %eax, %ebx
jg 0x49dc
cmpl %eax, %r15d
jle 0x49dc
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(%r14,%rcx)
movb %al, 0x1(%r14,%rcx)
jmp 0x4a0a
movq 0xc0(%rsp), %rax
imulq %r13, %rax
addq 0xd0(%rsp), %rax
movb 0x4(%rsp), %cl
movb %cl, -0x1(%r14,%rax)
movb 0x5(%rsp), %cl
movb %cl, (%r14,%rax)
movb 0x6(%rsp), %cl
movb %cl, 0x1(%r14,%rax)
incq %r12
addq $0x3, %r14
cmpq %r12, 0x78(%rsp)
jne 0x496e
incq %rbp
addq $0x3, %r13
cmpq %rbp, 0x48(%rsp)
leaq 0x1(%rsp), %r12
jg 0x485d
jmp 0x4ab2
movl %r15d, %r14d
incl %r15d
decq %r14
testl %r14d, %r14d
js 0x4aa7
movw $0x0, 0x1(%rsp)
movb $0x0, 0x3(%rsp)
movl %r14d, %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
movq %r12, %rdi
leaq 0x8(%rsp), %rsi
callq 0xafd8
decl %r15d
decq %r14
testb %al, %al
jne 0x4a3e
jmp 0x48df
movl $0xffffffff, %r15d      # imm = 0xFFFFFFFF
jmp 0x48df
movq 0x30(%rsp), %rsi
leaq 0xc0(%rsp), %rdi
callq 0xb4be
leaq 0xc0(%rsp), %rdi
callq 0xb100
leaq 0x40(%rsp), %rdi
callq 0xb100
xorl %eax, %eax
addq $0x4c8, %rsp            # imm = 0x4C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x4b20
jmp 0x4b19
jmp 0x4b19
jmp 0x4b05
jmp 0x4b05
movq %rax, %rbx
movl $0x20, %esi
jmp 0x4b0d
jmp 0x4b05
movq %rax, %rbx
movl $0x18, %esi
movq %r14, %rdi
callq 0x4210
jmp 0x4b30
jmp 0x4b19
movq %rax, %rbx
jmp 0x4b30
jmp 0x4b20
movq %rax, %rbx
leaq 0xc0(%rsp), %rdi
callq 0xb100
leaq 0x40(%rsp), %rdi
callq 0xb100
movq %rbx, %rdi
callq 0x4300
nop