We are currently solving the following issue within our repository. Here is the issue text:
--- BEGIN ISSUE ---
Decimated read result differs depending on GDAL version
```
$ rio insp tests/data/alpha_masked_values.tif
Rasterio 1.0a1 Interactive Inspector (Python 3.5.1)
Type "src.meta", "src.read(1)", or "help(src)" for more information.
>>> src.read(4, out_shape=(19, 19), masked=False)[-2:, 0:5]
array([[  0,   0,   0, 255,   0],
       [255, 255, 255, 255,   0]], dtype=uint8)
>>> rasterio.__version__
'1.0a1'
>>> rasterio.__gdal_version__
'1.11.5'
```

versus 

```
$ rio insp tests/data/alpha_masked_values.tif
Rasterio 1.0a1 Interactive Inspector (Python 3.5.1)
Type "src.meta", "src.read(1)", or "help(src)" for more information.
>>> src.read(4, out_shape=(19, 19), masked=False)[-2:, 0:5]
array([[  0,   0,  32,  64,   0],
       [255, 255, 255, 255,   0]], dtype=uint8)
>>> rasterio.__version__
'1.0a1'
>>> rasterio.__gdal_version__
'2.1.1'
```

I'll start a new branch with a similar test so we can put it through the travis build matrix.

cc @dnomadb @sgillies 


--- END ISSUE ---

Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
--- BEGIN FILES ---
Path: `rasterio/env.py`
Content:
```
1 """Rasterio's GDAL/AWS environment"""
2 
3 import logging
4 
5 from rasterio._drivers import (
6     GDALEnv, del_gdal_config, get_gdal_config, set_gdal_config)
7 from rasterio.dtypes import check_dtype
8 from rasterio.errors import EnvError
9 from rasterio.compat import string_types
10 from rasterio.transform import guard_transform
11 from rasterio.vfs import parse_path, vsi_path
12 
13 
14 # The currently active GDAL/AWS environment is a private attribute.
15 _env = None
16 
17 log = logging.getLogger(__name__)
18 
19 # Rasterio defaults
20 default_options = {
21     'CHECK_WITH_INVERT_PROJ': True
22 }
23 
24 class Env(object):
25     """Abstraction for GDAL and AWS configuration
26 
27     The GDAL library is stateful: it has a registry of format drivers,
28     an error stack, and dozens of configuration options.
29 
30     Rasterio's approach to working with GDAL is to wrap all the state
31     up using a Python context manager (see PEP 343,
32     https://www.python.org/dev/peps/pep-0343/). When the context is
33     entered GDAL drivers are registered, error handlers are
34     configured, and configuration options are set. When the context
35     is exited, drivers are removed from the registry and other
36     configurations are removed.
37 
38     Example:
39 
40         with rasterio.Env(GDAL_CACHEMAX=512) as env:
41             # All drivers are registered, GDAL's raster block cache
42             # size is set to 512MB.
43             # Commence processing...
44             ...
45             # End of processing.
46 
47         # At this point, configuration options are set to their
48         # previous (possible unset) values.
49 
50     A boto3 session or boto3 session constructor arguments
51     `aws_access_key_id`, `aws_secret_access_key`, `aws_session_token`
52     may be passed to Env's constructor. In the latter case, a session
53     will be created as soon as needed. AWS credentials are configured
54     for GDAL as needed.
55     """
56 
57     def __init__(self, aws_session=None, aws_access_key_id=None,
58                  aws_secret_access_key=None, aws_session_token=None,
59                  region_name=None, profile_name=None, **options):
60         """Create a new GDAL/AWS environment.
61 
62         Note: this class is a context manager. GDAL isn't configured
63         until the context is entered via `with rasterio.Env():`
64 
65         Parameters
66         ----------
67         aws_session: object, optional
68             A boto3 session.
69         aws_access_key_id: string, optional
70             An access key id, as per boto3.
71         aws_secret_access_key: string, optional
72             A secret access key, as per boto3.
73         aws_session_token: string, optional
74             A session token, as per boto3.
75         region_name: string, optional
76             A region name, as per boto3.
77         profile_name: string, optional
78             A shared credentials profile name, as per boto3.
79         **options: optional
80             A mapping of GDAL configuration options, e.g.,
81             `CPL_DEBUG=True, CHECK_WITH_INVERT_PROJ=False`.
82 
83         Returns
84         -------
85         A new instance of Env.
86 
87         Note: We raise EnvError if the GDAL config options
88         AWS_ACCESS_KEY_ID or AWS_SECRET_ACCESS_KEY are given. AWS
89         credentials are handled exclusively by boto3.
90         """
91         if ('AWS_ACCESS_KEY_ID' in options or
92                 'AWS_SECRET_ACCESS_KEY' in options):
93             raise EnvError(
94                 "GDAL's AWS config options can not be directly set. "
95                 "AWS credentials are handled exclusively by boto3.")
96         self.aws_access_key_id = aws_access_key_id
97         self.aws_secret_access_key = aws_secret_access_key
98         self.aws_session_token = aws_session_token
99         self.region_name = region_name
100         self.profile_name = profile_name
101         self.aws_session = aws_session
102         self._creds = (
103             self.aws_session._session.get_credentials()
104             if self.aws_session else None)
105         self.options = options.copy()
106         self.previous_options = {}
107         defenv()
108 
109     def get_aws_credentials(self):
110         """Get credentials and configure GDAL."""
111         import boto3
112         options = {}
113         if not self.aws_session:
114             self.aws_session = boto3.Session(
115                 aws_access_key_id=self.aws_access_key_id,
116                 aws_secret_access_key=self.aws_secret_access_key,
117                 aws_session_token=self.aws_session_token,
118                 region_name=self.region_name,
119                 profile_name=self.profile_name)
120         self._creds = self.aws_session._session.get_credentials()
121 
122         # Pass these credentials to the GDAL environment.
123         if self._creds.access_key:  # pragma: no branch
124             options.update(aws_access_key_id=self._creds.access_key)
125         if self._creds.secret_key:  # pragma: no branch
126             options.update(aws_secret_access_key=self._creds.secret_key)
127         if self._creds.token:
128             options.update(aws_session_token=self._creds.token)
129         if self.aws_session.region_name:
130             options.update(aws_region=self.aws_session.region_name)
131 
132         # Pass these credentials to the GDAL environment.
133         defenv()
134         global _env
135         _env.update_config_options(**options)
136 
137     def drivers(self):
138         """Return a mapping of registered drivers."""
139         global _env
140         return _env.drivers()
141 
142     def __enter__(self):
143         self.previous_options = getenv()
144         setenv(**self.options)
145         log.debug("Entering env %r context", self)
146         return self
147 
148     def __exit__(self, exc_type=None, exc_val=None, exc_tb=None):
149         delenv()
150         setenv(**self.previous_options)
151         log.debug("Exiting env %r context", self)
152 
153 
154 def defenv():
155     """Create a default environment if necessary."""
156     global _env
157     if _env:
158         log.debug("Environment %r exists", _env)
159     else:
160         _env = GDALEnv()
161         _env.update_config_options(**default_options)
162         log.debug(
163             "New GDAL environment %r created", _env)
164 
165 
166 def getenv():
167     """Get a mapping of current options."""
168     global _env
169     if not _env:
170         raise EnvError("No environment exists")
171     else:
172         log.debug("Got a copy of environment %r options", _env)
173         return _env.options.copy()
174 
175 
176 def setenv(**options):
177     """Set options in the existing environment."""
178     global _env
179     if not _env:
180         raise EnvError("No environment exists")
181     else:
182         _env.update_config_options(**options)
183         log.debug("Updated existing %r with options %r", _env, options)
184 
185 
186 def delenv():
187     """Delete options in the existing environment."""
188     global _env
189     if not _env:
190         raise EnvError("No environment exists")
191     else:
192         _env.clear_config_options()
193         log.debug("Cleared existing %r options", _env)
194 
195 
196 def ensure_env(f):
197     """A decorator that ensures an env exists before a function
198     calls any GDAL C functions."""
199     defenv()
200     return f
201
```
--- END FILES ---

Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.

Here is an example:

```diff
diff --git a/examples/server_async.py b/examples/server_async.py
--- a/examples/server_async.py
+++ b/examples/server_async.py
@@ -313,4 +313,4 @@
 
 
 if __name__ == "__main__":
-    asyncio.run(run_async_server("."), debug=True)
+    asyncio.run(run_async_server(), debug=True)
diff --git a/examples/server_sync.py b/examples/server_sync.py
--- a/examples/server_sync.py
+++ b/examples/server_sync.py
@@ -313,5 +313,5 @@
 
 
 if __name__ == "__main__":
-    server = run_sync_server(".")
+    server = run_sync_server()
     server.shutdown()

```

diff --git a/rasterio/env.py b/rasterio/env.py
--- a/rasterio/env.py
+++ b/rasterio/env.py
@@ -18,7 +18,8 @@
 
 # Rasterio defaults
 default_options = {
-    'CHECK_WITH_INVERT_PROJ': True
+    'CHECK_WITH_INVERT_PROJ': True,
+    'GTIFF_IMPLICIT_JPEG_OVR': False
 }
 
 class Env(object):

{"golden_diff": "diff --git a/rasterio/env.py b/rasterio/env.py\n--- a/rasterio/env.py\n+++ b/rasterio/env.py\n@@ -18,7 +18,8 @@\n \n # Rasterio defaults\n default_options = {\n-    'CHECK_WITH_INVERT_PROJ': True\n+    'CHECK_WITH_INVERT_PROJ': True,\n+    'GTIFF_IMPLICIT_JPEG_OVR': False\n }\n \n class Env(object):\n", "issue": "Decimated read result differs depending on GDAL version\n```\n$ rio insp tests/data/alpha_masked_values.tif\nRasterio 1.0a1 Interactive Inspector (Python 3.5.1)\nType \"src.meta\", \"src.read(1)\", or \"help(src)\" for more information.\n>>> src.read(4, out_shape=(19, 19), masked=False)[-2:, 0:5]\narray([[  0,   0,   0, 255,   0],\n       [255, 255, 255, 255,   0]], dtype=uint8)\n>>> rasterio.__version__\n'1.0a1'\n>>> rasterio.__gdal_version__\n'1.11.5'\n```\n\nversus \n\n```\n$ rio insp tests/data/alpha_masked_values.tif\nRasterio 1.0a1 Interactive Inspector (Python 3.5.1)\nType \"src.meta\", \"src.read(1)\", or \"help(src)\" for more information.\n>>> src.read(4, out_shape=(19, 19), masked=False)[-2:, 0:5]\narray([[  0,   0,  32,  64,   0],\n       [255, 255, 255, 255,   0]], dtype=uint8)\n>>> rasterio.__version__\n'1.0a1'\n>>> rasterio.__gdal_version__\n'2.1.1'\n```\n\nI'll start a new branch with a similar test so we can put it through the travis build matrix.\n\ncc @dnomadb @sgillies \n\n", "before_files": [{"content": "\"\"\"Rasterio's GDAL/AWS environment\"\"\"\n\nimport logging\n\nfrom rasterio._drivers import (\n    GDALEnv, del_gdal_config, get_gdal_config, set_gdal_config)\nfrom rasterio.dtypes import check_dtype\nfrom rasterio.errors import EnvError\nfrom rasterio.compat import string_types\nfrom rasterio.transform import guard_transform\nfrom rasterio.vfs import parse_path, vsi_path\n\n\n# The currently active GDAL/AWS environment is a private attribute.\n_env = None\n\nlog = logging.getLogger(__name__)\n\n# Rasterio defaults\ndefault_options = {\n    'CHECK_WITH_INVERT_PROJ': True\n}\n\nclass Env(object):\n    \"\"\"Abstraction for GDAL and AWS configuration\n\n    The GDAL library is stateful: it has a registry of format drivers,\n    an error stack, and dozens of configuration options.\n\n    Rasterio's approach to working with GDAL is to wrap all the state\n    up using a Python context manager (see PEP 343,\n    https://www.python.org/dev/peps/pep-0343/). When the context is\n    entered GDAL drivers are registered, error handlers are\n    configured, and configuration options are set. When the context\n    is exited, drivers are removed from the registry and other\n    configurations are removed.\n\n    Example:\n\n        with rasterio.Env(GDAL_CACHEMAX=512) as env:\n            # All drivers are registered, GDAL's raster block cache\n            # size is set to 512MB.\n            # Commence processing...\n            ...\n            # End of processing.\n\n        # At this point, configuration options are set to their\n        # previous (possible unset) values.\n\n    A boto3 session or boto3 session constructor arguments\n    `aws_access_key_id`, `aws_secret_access_key`, `aws_session_token`\n    may be passed to Env's constructor. In the latter case, a session\n    will be created as soon as needed. AWS credentials are configured\n    for GDAL as needed.\n    \"\"\"\n\n    def __init__(self, aws_session=None, aws_access_key_id=None,\n                 aws_secret_access_key=None, aws_session_token=None,\n                 region_name=None, profile_name=None, **options):\n        \"\"\"Create a new GDAL/AWS environment.\n\n        Note: this class is a context manager. GDAL isn't configured\n        until the context is entered via `with rasterio.Env():`\n\n        Parameters\n        ----------\n        aws_session: object, optional\n            A boto3 session.\n        aws_access_key_id: string, optional\n            An access key id, as per boto3.\n        aws_secret_access_key: string, optional\n            A secret access key, as per boto3.\n        aws_session_token: string, optional\n            A session token, as per boto3.\n        region_name: string, optional\n            A region name, as per boto3.\n        profile_name: string, optional\n            A shared credentials profile name, as per boto3.\n        **options: optional\n            A mapping of GDAL configuration options, e.g.,\n            `CPL_DEBUG=True, CHECK_WITH_INVERT_PROJ=False`.\n\n        Returns\n        -------\n        A new instance of Env.\n\n        Note: We raise EnvError if the GDAL config options\n        AWS_ACCESS_KEY_ID or AWS_SECRET_ACCESS_KEY are given. AWS\n        credentials are handled exclusively by boto3.\n        \"\"\"\n        if ('AWS_ACCESS_KEY_ID' in options or\n                'AWS_SECRET_ACCESS_KEY' in options):\n            raise EnvError(\n                \"GDAL's AWS config options can not be directly set. \"\n                \"AWS credentials are handled exclusively by boto3.\")\n        self.aws_access_key_id = aws_access_key_id\n        self.aws_secret_access_key = aws_secret_access_key\n        self.aws_session_token = aws_session_token\n        self.region_name = region_name\n        self.profile_name = profile_name\n        self.aws_session = aws_session\n        self._creds = (\n            self.aws_session._session.get_credentials()\n            if self.aws_session else None)\n        self.options = options.copy()\n        self.previous_options = {}\n        defenv()\n\n    def get_aws_credentials(self):\n        \"\"\"Get credentials and configure GDAL.\"\"\"\n        import boto3\n        options = {}\n        if not self.aws_session:\n            self.aws_session = boto3.Session(\n                aws_access_key_id=self.aws_access_key_id,\n                aws_secret_access_key=self.aws_secret_access_key,\n                aws_session_token=self.aws_session_token,\n                region_name=self.region_name,\n                profile_name=self.profile_name)\n        self._creds = self.aws_session._session.get_credentials()\n\n        # Pass these credentials to the GDAL environment.\n        if self._creds.access_key:  # pragma: no branch\n            options.update(aws_access_key_id=self._creds.access_key)\n        if self._creds.secret_key:  # pragma: no branch\n            options.update(aws_secret_access_key=self._creds.secret_key)\n        if self._creds.token:\n            options.update(aws_session_token=self._creds.token)\n        if self.aws_session.region_name:\n            options.update(aws_region=self.aws_session.region_name)\n\n        # Pass these credentials to the GDAL environment.\n        defenv()\n        global _env\n        _env.update_config_options(**options)\n\n    def drivers(self):\n        \"\"\"Return a mapping of registered drivers.\"\"\"\n        global _env\n        return _env.drivers()\n\n    def __enter__(self):\n        self.previous_options = getenv()\n        setenv(**self.options)\n        log.debug(\"Entering env %r context\", self)\n        return self\n\n    def __exit__(self, exc_type=None, exc_val=None, exc_tb=None):\n        delenv()\n        setenv(**self.previous_options)\n        log.debug(\"Exiting env %r context\", self)\n\n\ndef defenv():\n    \"\"\"Create a default environment if necessary.\"\"\"\n    global _env\n    if _env:\n        log.debug(\"Environment %r exists\", _env)\n    else:\n        _env = GDALEnv()\n        _env.update_config_options(**default_options)\n        log.debug(\n            \"New GDAL environment %r created\", _env)\n\n\ndef getenv():\n    \"\"\"Get a mapping of current options.\"\"\"\n    global _env\n    if not _env:\n        raise EnvError(\"No environment exists\")\n    else:\n        log.debug(\"Got a copy of environment %r options\", _env)\n        return _env.options.copy()\n\n\ndef setenv(**options):\n    \"\"\"Set options in the existing environment.\"\"\"\n    global _env\n    if not _env:\n        raise EnvError(\"No environment exists\")\n    else:\n        _env.update_config_options(**options)\n        log.debug(\"Updated existing %r with options %r\", _env, options)\n\n\ndef delenv():\n    \"\"\"Delete options in the existing environment.\"\"\"\n    global _env\n    if not _env:\n        raise EnvError(\"No environment exists\")\n    else:\n        _env.clear_config_options()\n        log.debug(\"Cleared existing %r options\", _env)\n\n\ndef ensure_env(f):\n    \"\"\"A decorator that ensures an env exists before a function\n    calls any GDAL C functions.\"\"\"\n    defenv()\n    return f\n", "path": "rasterio/env.py"}], "after_files": [{"content": "\"\"\"Rasterio's GDAL/AWS environment\"\"\"\n\nimport logging\n\nfrom rasterio._drivers import (\n    GDALEnv, del_gdal_config, get_gdal_config, set_gdal_config)\nfrom rasterio.dtypes import check_dtype\nfrom rasterio.errors import EnvError\nfrom rasterio.compat import string_types\nfrom rasterio.transform import guard_transform\nfrom rasterio.vfs import parse_path, vsi_path\n\n\n# The currently active GDAL/AWS environment is a private attribute.\n_env = None\n\nlog = logging.getLogger(__name__)\n\n# Rasterio defaults\ndefault_options = {\n    'CHECK_WITH_INVERT_PROJ': True,\n    'GTIFF_IMPLICIT_JPEG_OVR': False\n}\n\nclass Env(object):\n    \"\"\"Abstraction for GDAL and AWS configuration\n\n    The GDAL library is stateful: it has a registry of format drivers,\n    an error stack, and dozens of configuration options.\n\n    Rasterio's approach to working with GDAL is to wrap all the state\n    up using a Python context manager (see PEP 343,\n    https://www.python.org/dev/peps/pep-0343/). When the context is\n    entered GDAL drivers are registered, error handlers are\n    configured, and configuration options are set. When the context\n    is exited, drivers are removed from the registry and other\n    configurations are removed.\n\n    Example:\n\n        with rasterio.Env(GDAL_CACHEMAX=512) as env:\n            # All drivers are registered, GDAL's raster block cache\n            # size is set to 512MB.\n            # Commence processing...\n            ...\n            # End of processing.\n\n        # At this point, configuration options are set to their\n        # previous (possible unset) values.\n\n    A boto3 session or boto3 session constructor arguments\n    `aws_access_key_id`, `aws_secret_access_key`, `aws_session_token`\n    may be passed to Env's constructor. In the latter case, a session\n    will be created as soon as needed. AWS credentials are configured\n    for GDAL as needed.\n    \"\"\"\n\n    def __init__(self, aws_session=None, aws_access_key_id=None,\n                 aws_secret_access_key=None, aws_session_token=None,\n                 region_name=None, profile_name=None, **options):\n        \"\"\"Create a new GDAL/AWS environment.\n\n        Note: this class is a context manager. GDAL isn't configured\n        until the context is entered via `with rasterio.Env():`\n\n        Parameters\n        ----------\n        aws_session: object, optional\n            A boto3 session.\n        aws_access_key_id: string, optional\n            An access key id, as per boto3.\n        aws_secret_access_key: string, optional\n            A secret access key, as per boto3.\n        aws_session_token: string, optional\n            A session token, as per boto3.\n        region_name: string, optional\n            A region name, as per boto3.\n        profile_name: string, optional\n            A shared credentials profile name, as per boto3.\n        **options: optional\n            A mapping of GDAL configuration options, e.g.,\n            `CPL_DEBUG=True, CHECK_WITH_INVERT_PROJ=False`.\n\n        Returns\n        -------\n        A new instance of Env.\n\n        Note: We raise EnvError if the GDAL config options\n        AWS_ACCESS_KEY_ID or AWS_SECRET_ACCESS_KEY are given. AWS\n        credentials are handled exclusively by boto3.\n        \"\"\"\n        if ('AWS_ACCESS_KEY_ID' in options or\n                'AWS_SECRET_ACCESS_KEY' in options):\n            raise EnvError(\n                \"GDAL's AWS config options can not be directly set. \"\n                \"AWS credentials are handled exclusively by boto3.\")\n        self.aws_access_key_id = aws_access_key_id\n        self.aws_secret_access_key = aws_secret_access_key\n        self.aws_session_token = aws_session_token\n        self.region_name = region_name\n        self.profile_name = profile_name\n        self.aws_session = aws_session\n        self._creds = (\n            self.aws_session._session.get_credentials()\n            if self.aws_session else None)\n        self.options = options.copy()\n        self.previous_options = {}\n        defenv()\n\n    def get_aws_credentials(self):\n        \"\"\"Get credentials and configure GDAL.\"\"\"\n        import boto3\n        options = {}\n        if not self.aws_session:\n            self.aws_session = boto3.Session(\n                aws_access_key_id=self.aws_access_key_id,\n                aws_secret_access_key=self.aws_secret_access_key,\n                aws_session_token=self.aws_session_token,\n                region_name=self.region_name,\n                profile_name=self.profile_name)\n        self._creds = self.aws_session._session.get_credentials()\n\n        # Pass these credentials to the GDAL environment.\n        if self._creds.access_key:  # pragma: no branch\n            options.update(aws_access_key_id=self._creds.access_key)\n        if self._creds.secret_key:  # pragma: no branch\n            options.update(aws_secret_access_key=self._creds.secret_key)\n        if self._creds.token:\n            options.update(aws_session_token=self._creds.token)\n        if self.aws_session.region_name:\n            options.update(aws_region=self.aws_session.region_name)\n\n        # Pass these credentials to the GDAL environment.\n        defenv()\n        global _env\n        _env.update_config_options(**options)\n\n    def drivers(self):\n        \"\"\"Return a mapping of registered drivers.\"\"\"\n        global _env\n        return _env.drivers()\n\n    def __enter__(self):\n        self.previous_options = getenv()\n        setenv(**self.options)\n        log.debug(\"Entering env %r context\", self)\n        return self\n\n    def __exit__(self, exc_type=None, exc_val=None, exc_tb=None):\n        delenv()\n        setenv(**self.previous_options)\n        log.debug(\"Exiting env %r context\", self)\n\n\ndef defenv():\n    \"\"\"Create a default environment if necessary.\"\"\"\n    global _env\n    if _env:\n        log.debug(\"Environment %r exists\", _env)\n    else:\n        _env = GDALEnv()\n        _env.update_config_options(**default_options)\n        log.debug(\n            \"New GDAL environment %r created\", _env)\n\n\ndef getenv():\n    \"\"\"Get a mapping of current options.\"\"\"\n    global _env\n    if not _env:\n        raise EnvError(\"No environment exists\")\n    else:\n        log.debug(\"Got a copy of environment %r options\", _env)\n        return _env.options.copy()\n\n\ndef setenv(**options):\n    \"\"\"Set options in the existing environment.\"\"\"\n    global _env\n    if not _env:\n        raise EnvError(\"No environment exists\")\n    else:\n        _env.update_config_options(**options)\n        log.debug(\"Updated existing %r with options %r\", _env, options)\n\n\ndef delenv():\n    \"\"\"Delete options in the existing environment.\"\"\"\n    global _env\n    if not _env:\n        raise EnvError(\"No environment exists\")\n    else:\n        _env.clear_config_options()\n        log.debug(\"Cleared existing %r options\", _env)\n\n\ndef ensure_env(f):\n    \"\"\"A decorator that ensures an env exists before a function\n    calls any GDAL C functions.\"\"\"\n    defenv()\n    return f\n", "path": "rasterio/env.py"}]}