Load image data based on tile list
(self)
| 289 | self.fp = None |
| 290 | |
| 291 | def load(self) -> Image.core.PixelAccess | None: |
| 292 | """Load image data based on tile list""" |
| 293 | |
| 294 | if not self.tile and self._im is None: |
| 295 | msg = "cannot load this image" |
| 296 | raise OSError(msg) |
| 297 | |
| 298 | pixel = Image.Image.load(self) |
| 299 | if not self.tile: |
| 300 | return pixel |
| 301 | |
| 302 | self.map: mmap.mmap | None = None |
| 303 | use_mmap = self.filename and len(self.tile) == 1 |
| 304 | |
| 305 | assert self.fp is not None |
| 306 | readonly = 0 |
| 307 | |
| 308 | # look for read/seek overrides |
| 309 | if hasattr(self, "load_read"): |
| 310 | read = self.load_read |
| 311 | # don't use mmap if there are custom read/seek functions |
| 312 | use_mmap = False |
| 313 | else: |
| 314 | read = self.fp.read |
| 315 | |
| 316 | if hasattr(self, "load_seek"): |
| 317 | seek = self.load_seek |
| 318 | use_mmap = False |
| 319 | else: |
| 320 | seek = self.fp.seek |
| 321 | |
| 322 | if use_mmap: |
| 323 | # try memory mapping |
| 324 | decoder_name, extents, offset, args = self.tile[0] |
| 325 | if isinstance(args, str): |
| 326 | args = (args, 0, 1) |
| 327 | if ( |
| 328 | decoder_name == "raw" |
| 329 | and isinstance(args, tuple) |
| 330 | and len(args) >= 3 |
| 331 | and args[0] == self.mode |
| 332 | and args[0] in Image._MAPMODES |
| 333 | ): |
| 334 | if offset < 0: |
| 335 | msg = "Tile offset cannot be negative" |
| 336 | raise ValueError(msg) |
| 337 | try: |
| 338 | # use mmap, if possible |
| 339 | import mmap |
| 340 | |
| 341 | with open(self.filename) as fp: |
| 342 | self.map = mmap.mmap(fp.fileno(), 0, access=mmap.ACCESS_READ) |
| 343 | if offset + self.size[1] * args[1] > self.map.size(): |
| 344 | msg = "buffer is not large enough" |
| 345 | raise OSError(msg) |
| 346 | self.im = Image.core.map_buffer( |
| 347 | self.map, self.size, decoder_name, offset, args |
| 348 | ) |
no test coverage detected