One way to remove the tmp_buf variable is to store the temporal variables in variable output. In this way, we only need output[x][y + 1] and output[x][y + 2] to compute output[x][y], so output contains the correct values if we iterate x first and y next.
One way to remove the tmp_buf variable is to store the temporal variables in variable output. In this way, we only need output[x][y + 1] and output[x][y + 2] to compute output[x][y], so output contains the correct values if we iterate x first and y next.