// File: /workspace/github_cpp_makefile_star2_O1/matthewarcus[P]mmps/combine.cpp
// Function: main
int main(int argc, char* argv[])
{
  Image image1;
  Image image2;
  const char* file1;
  const char* file2;
  const char* outfilename = NULL;
  Rgb background = black;
  int width;
  int height;
  double fuzz = 2.0;

  char *progname = argv[0];
  argc--;argv++;
  OptSpec *optspecs [] = {
    // General options
    new RgbSpec ("-bg", background),
    new DoubleSpec ("-fuzz", fuzz),
    new StringSpec ("-out", outfilename),
    NULL // Finish up
  };

  while (argc > 0) {
    int result = OptSpec::ReadOpts(argc, argv, optspecs);
    if (result != 1) {
      break;
    }
    argc--; argv++;
  }
  
  if (argc < 2) {
    error(usage, progname);
  }
  file1 = argv[0];
  file2 = argv[1];
  image1.Read(file1);
  image2.Read(file2);

  width = image1.Width();
  height = image1.Height();
  if (image2.Width() != width || image2.Height() != height) {
    error("Images must have same dimensions");
  }
  Image outImage(width, height);
  for (int row = 0; row < height; row++) {
    for (int col = 0; col < width; col++) {
      Rgb rgb1;
      Rgb rgb2;
      Rgb out;
      image1.GetRgb(col,row,rgb1);
      image2.GetRgb(col,row,rgb2);
      if (rgb1.Match(background,fuzz)) {
	out = rgb2;
      } else if (rgb2.Match(background, fuzz)) {
	out = rgb1;
      } else {
#if 0
	// Average the rgb values, this produces alarming discontinuities
	out.r = (rgb1.r + rgb2.r)/2;
	out.g = (rgb1.g + rgb2.g)/2;
	out.b = (rgb1.b + rgb2.b)/2;
#endif
	// But taking the maximum of the rgb values is smooth
	out.r = max(rgb1.r,rgb2.r);
	out.g = max(rgb1.g,rgb2.g);
	out.b = max(rgb1.b,rgb2.b);

      }
      outImage.SetRgb(col,row,out);
    }
 }
  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, 0x8(%rsp)
movq %rsi, 0x10(%rsp)
leaq 0x70(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xad7c
leaq 0x40(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xad7c
movq $0x0, 0x28(%rsp)
movb $0x0, 0xe(%rsp)
movw $0x0, 0xc(%rsp)
movabsq $0x4000000000000000, %rax # imm = 0x4000000000000000
movq %rax, 0x18(%rsp)
movq 0x10(%rsp), %rax
movq (%rax), %rbx
decl 0x8(%rsp)
addq $0x8, %rax
movq %rax, 0x10(%rsp)
movl $0x18, %edi
callq 0x4210
movq %rax, %r14
leaq 0x7987(%rip), %rsi      # 0xc026
movq %rax, %rdi
callq 0x9f2a
leaq 0xd6d2(%rip), %rax      # 0x11d80
addq $0x10, %rax
movq %rax, (%r14)
leaq 0xc(%rsp), %rax
movq %rax, 0x10(%r14)
movq %r14, 0xa0(%rsp)
movl $0x20, %edi
callq 0x4210
movq %rax, %r14
leaq 0x7950(%rip), %rsi      # 0xc02a
leaq 0x18(%rsp), %rdx
movsd 0x7921(%rip), %xmm0     # 0xc008
movq %rax, %rdi
callq 0xa0b2
movq %r14, 0xa8(%rsp)
movl $0x18, %edi
callq 0x4210
movq %rax, %r14
leaq 0x7925(%rip), %rsi      # 0xc030
leaq 0x28(%rsp), %rdx
movq %rax, %rdi
callq 0x9fba
movq %r14, 0xb0(%rsp)
movq $0x0, 0xb8(%rsp)
cmpl $0x0, 0x8(%rsp)
jle 0x476e
leaq 0x8(%rsp), %r14
leaq 0x10(%rsp), %r15
leaq 0xa0(%rsp), %r12
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x9f3a
cmpl $0x1, %eax
jne 0x4762
decl 0x8(%rsp)
addq $0x8, 0x10(%rsp)
cmpl $0x1, %eax
jne 0x476e
cmpl $0x0, 0x8(%rsp)
jg 0x4745
cmpl $0x1, 0x8(%rsp)
jg 0x479e
movq 0xda54(%rip), %rdx      # 0x121d0
leaq 0xc0(%rsp), %r14
movl $0x400, %esi            # imm = 0x400
movq %r14, %rdi
movq %rbx, %rcx
xorl %eax, %eax
callq 0x42a0
movq %r14, %rdi
callq 0x9ed4
movq 0x10(%rsp), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rbx
leaq 0x70(%rsp), %rdi
xorl %edx, %edx
callq 0xaf56
leaq 0x40(%rsp), %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0xaf56
movq 0x70(%rsp), %rax
movq 0x78(%rsp), %r15
movq %rax, 0x20(%rsp)
movslq %eax, %rbx
cmpq %rbx, 0x40(%rsp)
jne 0x47e8
movslq %r15d, %rax
cmpq %rax, 0x48(%rsp)
je 0x47f4
leaq 0x7846(%rip), %rdi      # 0xc035
callq 0x9ed4
movslq %r15d, %rdx
leaq 0xc0(%rsp), %rdi
movq %rbx, %rsi
callq 0xad7c
testl %r15d, %r15d
jle 0x4975
movl $0x7fffffff, %eax       # imm = 0x7FFFFFFF
andq %rax, %r15
movq 0x20(%rsp), %rbp
andq %rax, %rbp
xorl %r12d, %r12d
leaq 0xc(%rsp), %r14
xorl %r13d, %r13d
xorl %eax, %eax
movq %r15, 0x30(%rsp)
movq %rax, 0x38(%rsp)
cmpl $0x0, 0x20(%rsp)
jle 0x495b
movl $0x1, %r15d
xorl %ebx, %ebx
movw %r12w, 0x5(%rsp)
movb %r12b, 0x7(%rsp)
movw %r12w, 0x2(%rsp)
movb %r12b, 0x4(%rsp)
movq 0x70(%rsp), %rax
imulq %r13, %rax
addq 0x80(%rsp), %rax
movb -0x1(%r15,%rax), %cl
movb %cl, 0x5(%rsp)
movb (%r15,%rax), %cl
movb %cl, 0x6(%rsp)
movb 0x1(%r15,%rax), %al
movq 0x40(%rsp), %rcx
imulq %r13, %rcx
addq 0x50(%rsp), %rcx
movb %al, 0x7(%rsp)
movb -0x1(%r15,%rcx), %al
movb %al, 0x2(%rsp)
movb (%r15,%rcx), %al
movb %al, 0x3(%rsp)
movb 0x1(%r15,%rcx), %al
movb %al, 0x4(%rsp)
movsd 0x18(%rsp), %xmm0
leaq 0x5(%rsp), %rdi
movq %r14, %rsi
callq 0xaca8
testb %al, %al
je 0x48d8
movb 0x2(%rsp), %cl
movb 0x3(%rsp), %al
movb 0x4(%rsp), %dl
jmp 0x4929
movsd 0x18(%rsp), %xmm0
leaq 0x2(%rsp), %rdi
movq %r14, %rsi
callq 0xaca8
movb 0x5(%rsp), %cl
testb %al, %al
je 0x48fd
movb 0x6(%rsp), %al
movb 0x7(%rsp), %dl
jmp 0x4929
movzbl %cl, %eax
movzbl 0x2(%rsp), %ecx
cmpb %cl, %al
cmoval %eax, %ecx
movzbl 0x6(%rsp), %edx
movzbl 0x3(%rsp), %eax
cmpb %al, %dl
cmoval %edx, %eax
movzbl 0x7(%rsp), %esi
movzbl 0x4(%rsp), %edx
cmpb %dl, %sil
cmoval %esi, %edx
movq 0xc0(%rsp), %rsi
imulq %r13, %rsi
addq 0xd0(%rsp), %rsi
movb %cl, -0x1(%r15,%rsi)
movb %al, (%r15,%rsi)
movb %dl, 0x1(%r15,%rsi)
incq %rbx
addq $0x3, %r15
cmpq %rbx, %rbp
jne 0x484a
movq 0x38(%rsp), %rax
incq %rax
addq $0x3, %r13
movq 0x30(%rsp), %r15
cmpq %r15, %rax
jne 0x4832
movq 0x28(%rsp), %rsi
leaq 0xc0(%rsp), %rdi
callq 0xb16e
leaq 0xc0(%rsp), %rdi
callq 0xadd0
leaq 0x40(%rsp), %rdi
callq 0xadd0
leaq 0x70(%rsp), %rdi
callq 0xadd0
xorl %eax, %eax
addq $0x4c8, %rsp            # imm = 0x4C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x49ea
jmp 0x49e5
jmp 0x49cc
movq %rax, %rbx
movl $0x20, %esi
jmp 0x49d4
movq %rax, %rbx
movl $0x18, %esi
movq %r14, %rdi
callq 0x4220
jmp 0x49fa
movq %rax, %rbx
jmp 0x4a04
jmp 0x49e5
movq %rax, %rbx
jmp 0x49fa
movq %rax, %rbx
leaq 0xc0(%rsp), %rdi
callq 0xadd0
leaq 0x40(%rsp), %rdi
callq 0xadd0
leaq 0x70(%rsp), %rdi
callq 0xadd0
movq %rbx, %rdi
callq 0x4310
nop