McIdasImagePlugin.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #
  2. # The Python Imaging Library.
  3. # $Id$
  4. #
  5. # Basic McIdas support for PIL
  6. #
  7. # History:
  8. # 1997-05-05 fl Created (8-bit images only)
  9. # 2009-03-08 fl Added 16/32-bit support.
  10. #
  11. # Thanks to Richard Jones and Craig Swank for specs and samples.
  12. #
  13. # Copyright (c) Secret Labs AB 1997.
  14. # Copyright (c) Fredrik Lundh 1997.
  15. #
  16. # See the README file for information on usage and redistribution.
  17. #
  18. from __future__ import annotations
  19. __lazy_modules__ = {"struct"}
  20. import struct
  21. from . import Image, ImageFile
  22. def _accept(prefix: bytes) -> bool:
  23. return prefix.startswith(b"\x00\x00\x00\x00\x00\x00\x00\x04")
  24. ##
  25. # Image plugin for McIdas area images.
  26. class McIdasImageFile(ImageFile.ImageFile):
  27. format = "MCIDAS"
  28. format_description = "McIdas area file"
  29. def _open(self) -> None:
  30. # parse area file directory
  31. assert self.fp is not None
  32. s = self.fp.read(256)
  33. if not _accept(s) or len(s) != 256:
  34. msg = "not an McIdas area file"
  35. raise SyntaxError(msg)
  36. self.area_descriptor_raw = s
  37. self.area_descriptor = w = [0, *struct.unpack("!64i", s)]
  38. # get mode
  39. if w[11] == 1:
  40. mode = rawmode = "L"
  41. elif w[11] == 2:
  42. mode = rawmode = "I;16B"
  43. elif w[11] == 4:
  44. # FIXME: add memory map support
  45. mode = "I"
  46. rawmode = "I;32B"
  47. else:
  48. msg = "unsupported McIdas format"
  49. raise SyntaxError(msg)
  50. self._mode = mode
  51. self._size = w[10], w[9]
  52. offset = w[34] + w[15]
  53. stride = w[15] + w[10] * w[11] * w[14]
  54. self.tile = [
  55. ImageFile._Tile("raw", (0, 0, *self.size), offset, (rawmode, stride, 1))
  56. ]
  57. # --------------------------------------------------------------------
  58. # registry
  59. Image.register_open(McIdasImageFile.format, McIdasImageFile, _accept)
  60. # no default extension