// File: /workspace/github_cpp_makefile_star2_O2/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 $0xf8, %rsp
movq %rsi, %r14
movl %edi, %ebp
movl %edi, 0x10(%rsp)
movq %rsi, 0x18(%rsp)
leaq 0x70(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xa1da
leaq 0x40(%rsp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0xa1da
andq $0x0, 0x28(%rsp)
andw $0x0, 0x14(%rsp)
movb $0x0, 0x16(%rsp)
movabsq $0x4000000000000000, %rax # imm = 0x4000000000000000
movq %rax, 0x20(%rsp)
movq (%r14), %rbx
decl %ebp
movl %ebp, 0x10(%rsp)
addq $0x8, %r14
movq %r14, 0x18(%rsp)
pushq $0x18
popq %rdi
callq 0x4210
movq %rax, %r14
leaq 0x6adb(%rip), %rsi      # 0xb026
leaq 0x14(%rsp), %rdx
movq %rax, %rdi
callq 0x4876
movq %r14, 0xa0(%rsp)
pushq $0x20
popq %rdi
callq 0x4210
movq %rax, %r14
leaq 0x6ab8(%rip), %rsi      # 0xb02a
leaq 0x20(%rsp), %rdx
movsd 0x6a89(%rip), %xmm0     # 0xb008
movq %rax, %rdi
callq 0x960e
movq %r14, 0xa8(%rsp)
pushq $0x18
popq %rdi
callq 0x4210
movq %rax, %r14
leaq 0x6a8f(%rip), %rsi      # 0xb030
leaq 0x28(%rsp), %rdx
movq %rax, %rdi
callq 0x94fe
movq %r14, 0xb0(%rsp)
andq $0x0, 0xb8(%rsp)
cmpl $0x0, 0x10(%rsp)
jle 0x460a
leaq 0x10(%rsp), %r14
leaq 0x18(%rsp), %r15
leaq 0xa0(%rsp), %r12
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x948e
movl 0x10(%rsp), %ecx
cmpl $0x1, %eax
jne 0x4603
leal -0x1(%rcx), %eax
movl %eax, 0x10(%rsp)
addq $0x8, 0x18(%rsp)
cmpl $0x1, %ecx
jg 0x45d8
jmp 0x4605
movl %ecx, %eax
cmpl $0x1, %eax
jg 0x4619
movq 0xcbcf(%rip), %rdi      # 0x111e0
movq %rbx, %rsi
callq 0x489f
movq 0x18(%rsp), %rax
movq (%rax), %rsi
movq 0x8(%rax), %rbx
leaq 0x70(%rsp), %rdi
xorl %edx, %edx
callq 0xa35e
leaq 0x40(%rsp), %rdi
movq %rbx, %rsi
xorl %edx, %edx
callq 0xa35e
movq 0x70(%rsp), %r12
movq 0x78(%rsp), %r14
movslq %r12d, %rbx
cmpq %rbx, 0x40(%rsp)
jne 0x465e
movslq %r14d, %rdx
cmpq %rdx, 0x48(%rsp)
je 0x466d
leaq 0x69d0(%rip), %rdi      # 0xb035
callq 0x9430
movslq %r14d, %rdx
leaq 0xc8(%rsp), %rdi
movq %rbx, %rsi
callq 0xa1da
xorl %ebp, %ebp
testl %r12d, %r12d
cmovlel %ebp, %r12d
testl %r14d, %r14d
cmovlel %ebp, %r14d
movq %r14, 0x30(%rsp)
leaq 0x14(%rsp), %r14
xorl %r13d, %r13d
xorl %eax, %eax
cmpq 0x30(%rsp), %rax
je 0x47d9
movq %rax, 0x38(%rsp)
pushq $0x1
popq %r15
xorl %ebx, %ebx
cmpq %rbx, %r12
je 0x47c8
andw $0x0, 0xd(%rsp)
andw $0x0, 0xa(%rsp)
movb %bpl, 0xf(%rsp)
movb %bpl, 0xc(%rsp)
movq 0x70(%rsp), %rax
imulq %r13, %rax
addq 0x80(%rsp), %rax
movb -0x1(%r15,%rax), %cl
movb %cl, 0xd(%rsp)
movb (%r15,%rax), %cl
movb %cl, 0xe(%rsp)
movb 0x1(%r15,%rax), %al
movq 0x40(%rsp), %rcx
imulq %r13, %rcx
addq 0x50(%rsp), %rcx
movb %al, 0xf(%rsp)
movb -0x1(%r15,%rcx), %al
movb %al, 0xa(%rsp)
movb (%r15,%rcx), %al
movb %al, 0xb(%rsp)
movb 0x1(%r15,%rcx), %al
movb %al, 0xc(%rsp)
movsd 0x20(%rsp), %xmm0
leaq 0xd(%rsp), %rdi
movq %r14, %rsi
callq 0xa0fa
testb %al, %al
je 0x4749
movb 0xa(%rsp), %cl
movb 0xb(%rsp), %al
movb 0xc(%rsp), %dl
jmp 0x479a
movsd 0x20(%rsp), %xmm0
leaq 0xa(%rsp), %rdi
movq %r14, %rsi
callq 0xa0fa
movb 0xd(%rsp), %cl
testb %al, %al
je 0x476e
movb 0xe(%rsp), %al
movb 0xf(%rsp), %dl
jmp 0x479a
movzbl %cl, %eax
movzbl 0xa(%rsp), %ecx
cmpb %cl, %al
cmoval %eax, %ecx
movzbl 0xe(%rsp), %edx
movzbl 0xb(%rsp), %eax
cmpb %al, %dl
cmoval %edx, %eax
movzbl 0xf(%rsp), %esi
movzbl 0xc(%rsp), %edx
cmpb %dl, %sil
cmoval %esi, %edx
movq 0xc8(%rsp), %rsi
imulq %r13, %rsi
addq 0xd8(%rsp), %rsi
movb %cl, -0x1(%r15,%rsi)
movb %al, (%r15,%rsi)
movb %dl, 0x1(%r15,%rsi)
incq %rbx
addq $0x3, %r15
jmp 0x46b2
movq 0x38(%rsp), %rax
incq %rax
addq $0x3, %r13
jmp 0x469c
movq 0x28(%rsp), %rsi
leaq 0xc8(%rsp), %rdi
callq 0xa566
leaq 0xc8(%rsp), %rdi
callq 0xa212
leaq 0x40(%rsp), %rdi
callq 0xa212
leaq 0x70(%rsp), %rdi
callq 0xa212
xorl %eax, %eax
addq $0xf8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x4849
jmp 0x4844
jmp 0x482d
movq %rax, %rbx
pushq $0x20
jmp 0x4832
movq %rax, %rbx
pushq $0x18
popq %rsi
movq %r14, %rdi
callq 0x4220
jmp 0x4859
movq %rax, %rbx
jmp 0x4863
jmp 0x4844
movq %rax, %rbx
jmp 0x4859
movq %rax, %rbx
leaq 0xc8(%rsp), %rdi
callq 0xa212
leaq 0x40(%rsp), %rdi
callq 0xa212
leaq 0x70(%rsp), %rdi
callq 0xa212
movq %rbx, %rdi
callq 0x4330
nop