// File: tomfunkhouser[P]gaps/pkgs/R2Shapes/R2Grid.cpp
// Function: gaps::R2Grid::Convolve(double const (*) [3])
void R2Grid::
Convolve(const RNScalar filter[3][3]) 
{
  // Make temporary copy of grid
  R2Grid copy(*this);

  // Mark boundaries unknown
  for (int i = 0; i < XResolution(); i++) { 
    SetGridValue(i, 0, R2_GRID_UNKNOWN_VALUE);
    SetGridValue(i, YResolution()-1, R2_GRID_UNKNOWN_VALUE);
  }
  for (int j = 0; j < YResolution(); j++) { 
    SetGridValue(0, j, R2_GRID_UNKNOWN_VALUE);
    SetGridValue(XResolution()-1, j, R2_GRID_UNKNOWN_VALUE);
  }

  // Convolve grid with 3x3 filter
  for (int j = 1; j < YResolution()-1; j++) { 
    for (int i = 1; i < XResolution()-1; i++) { 
      RNScalar value = copy.GridValue(i, j);
      if (value != R2_GRID_UNKNOWN_VALUE) {
        RNScalar sum = 0;
        RNBoolean unknown = FALSE;
        for (int dj = -1; dj <= 1; dj++) {
          for (int di = -1; di <= 1; di++) {
            value = copy.GridValue(i + di, j + dj);
            if (value == R2_GRID_UNKNOWN_VALUE) { unknown = TRUE; break; }
            else sum += filter[dj+1][di+1] * value;
          }
          if (unknown) break; 
        }
        if (unknown) SetGridValue(i, j, R2_GRID_UNKNOWN_VALUE);
        else SetGridValue(i, j, sum);
      }
    }
  }
}

// Assembly:
subq $0x168, %rsp            # imm = 0x168
movq %rdi, 0x160(%rsp)
movq %rsi, 0x158(%rsp)
movq 0x160(%rsp), %rsi
movq %rsi, 0x40(%rsp)
leaq 0x88(%rsp), %rdi
callq 0xa73a0
movl $0x0, 0x84(%rsp)
movq 0x40(%rsp), %rdi
movl 0x84(%rsp), %eax
movl %eax, 0x38(%rsp)
callq 0xb9260
movl %eax, 0x3c(%rsp)
jmp 0xad187
movl 0x38(%rsp), %eax
movl 0x3c(%rsp), %ecx
cmpl %ecx, %eax
jge 0xad225
movq 0x40(%rsp), %rdi
movl 0x84(%rsp), %esi
xorl %edx, %edx
movsd 0x86d03(%rip), %xmm0    # 0x133eb0
callq 0x6bc00
jmp 0xad1b4
movq 0x40(%rsp), %rdi
movl 0x84(%rsp), %eax
movl %eax, 0x30(%rsp)
callq 0xb9280
movl %eax, 0x34(%rsp)
jmp 0xad1cf
movl 0x30(%rsp), %esi
movq 0x40(%rsp), %rdi
movl 0x34(%rsp), %edx
decl %edx
movsd 0x86cca(%rip), %xmm0    # 0x133eb0
callq 0x6bc00
jmp 0xad1ed
jmp 0xad1ef
movl 0x84(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x84(%rsp)
jmp 0xad16c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, 0x78(%rsp)
movl %eax, 0x74(%rsp)
leaq 0x88(%rsp), %rdi
callq 0xa7690
jmp 0xad4c7
movl $0x0, 0x70(%rsp)
movq 0x40(%rsp), %rdi
movl 0x70(%rsp), %eax
movl %eax, 0x28(%rsp)
callq 0xb9280
movl %eax, 0x2c(%rsp)
jmp 0xad245
movl 0x28(%rsp), %eax
movl 0x2c(%rsp), %ecx
cmpl %ecx, %eax
jge 0xad2a8
movq 0x40(%rsp), %rdi
movl 0x70(%rsp), %edx
xorl %esi, %esi
movsd 0x86c4c(%rip), %xmm0    # 0x133eb0
callq 0x6bc00
jmp 0xad26b
movq 0x40(%rsp), %rdi
callq 0xb9260
movl %eax, 0x24(%rsp)
jmp 0xad27b
movq 0x40(%rsp), %rdi
movl 0x24(%rsp), %esi
decl %esi
movl 0x70(%rsp), %edx
movsd 0x86c1e(%rip), %xmm0    # 0x133eb0
callq 0x6bc00
jmp 0xad299
jmp 0xad29b
movl 0x70(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x70(%rsp)
jmp 0xad22d
movl $0x1, 0x6c(%rsp)
movq 0x40(%rsp), %rdi
movl 0x6c(%rsp), %eax
movl %eax, 0x1c(%rsp)
callq 0xb9280
movl %eax, 0x20(%rsp)
jmp 0xad2c8
movl 0x1c(%rsp), %eax
movl 0x20(%rsp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0xad4b2
movl $0x1, 0x68(%rsp)
movq 0x40(%rsp), %rdi
movl 0x68(%rsp), %eax
movl %eax, 0x14(%rsp)
callq 0xb9260
movl %eax, 0x18(%rsp)
jmp 0xad2fb
movl 0x14(%rsp), %eax
movl 0x18(%rsp), %ecx
subl $0x1, %ecx
cmpl %ecx, %eax
jge 0xad4a0
movl 0x68(%rsp), %esi
movl 0x6c(%rsp), %edx
leaq 0x88(%rsp), %rdi
callq 0xb92a0
movsd %xmm0, 0x8(%rsp)
jmp 0xad32b
movsd 0x8(%rsp), %xmm0
movsd %xmm0, 0x60(%rsp)
movsd 0x60(%rsp), %xmm0
movsd 0x86b6b(%rip), %xmm1    # 0x133eb0
ucomisd %xmm1, %xmm0
jne 0xad352
jp 0xad352
jmp 0xad48e
xorps %xmm0, %xmm0
movsd %xmm0, 0x58(%rsp)
movl $0x0, 0x54(%rsp)
movl $0xffffffff, 0x50(%rsp) # imm = 0xFFFFFFFF
cmpl $0x1, 0x50(%rsp)
jg 0xad44b
movl $0xffffffff, 0x4c(%rsp) # imm = 0xFFFFFFFF
cmpl $0x1, 0x4c(%rsp)
jg 0xad430
movl 0x68(%rsp), %esi
movl 0x4c(%rsp), %eax
addl %eax, %esi
movl 0x6c(%rsp), %edx
movl 0x50(%rsp), %eax
addl %eax, %edx
leaq 0x88(%rsp), %rdi
callq 0xb92a0
movsd %xmm0, (%rsp)
jmp 0xad3b1
movsd (%rsp), %xmm0
movsd %xmm0, 0x60(%rsp)
movsd 0x60(%rsp), %xmm0
movsd 0x86ae6(%rip), %xmm1    # 0x133eb0
ucomisd %xmm1, %xmm0
jne 0xad3dc
jp 0xad3dc
movl $0x1, 0x54(%rsp)
jmp 0xad430
movq 0x158(%rsp), %rax
movl 0x50(%rsp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
imulq $0x18, %rcx, %rcx
addq %rcx, %rax
movl 0x4c(%rsp), %ecx
addl $0x1, %ecx
movslq %ecx, %rcx
movsd (%rax,%rcx,8), %xmm0
movsd 0x60(%rsp), %xmm2
movsd 0x58(%rsp), %xmm1
mulsd %xmm2, %xmm0
addsd %xmm1, %xmm0
movsd %xmm0, 0x58(%rsp)
jmp 0xad420
movl 0x4c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x4c(%rsp)
jmp 0xad37e
cmpl $0x0, 0x54(%rsp)
je 0xad439
jmp 0xad44b
jmp 0xad43b
movl 0x50(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x50(%rsp)
jmp 0xad36b
cmpl $0x0, 0x54(%rsp)
je 0xad470
movq 0x40(%rsp), %rdi
movl 0x68(%rsp), %esi
movl 0x6c(%rsp), %edx
movsd 0x86a49(%rip), %xmm0    # 0x133eb0
callq 0x6bc00
jmp 0xad46e
jmp 0xad48c
movq 0x40(%rsp), %rdi
movl 0x68(%rsp), %esi
movl 0x6c(%rsp), %edx
movsd 0x58(%rsp), %xmm0
callq 0x6bc00
jmp 0xad48a
jmp 0xad48c
jmp 0xad48e
jmp 0xad490
movl 0x68(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x68(%rsp)
jmp 0xad2e3
jmp 0xad4a2
movl 0x6c(%rsp), %eax
addl $0x1, %eax
movl %eax, 0x6c(%rsp)
jmp 0xad2b0
leaq 0x88(%rsp), %rdi
callq 0xa7690
addq $0x168, %rsp            # imm = 0x168
retq
movq 0x78(%rsp), %rdi
callq 0xf890
nopw %cs:(%rax,%rax)