McIdasImagePlugin.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. import struct
  19. from . import Image, ImageFile
  20. # __version__ is deprecated and will be removed in a future version. Use
  21. # PIL.__version__ instead.
  22. __version__ = "0.2"
  23. def _accept(s):
  24. return s[:8] == b"\x00\x00\x00\x00\x00\x00\x00\x04"
  25. ##
  26. # Image plugin for McIdas area images.
  27. class McIdasImageFile(ImageFile.ImageFile):
  28. format = "MCIDAS"
  29. format_description = "McIdas area file"
  30. def _open(self):
  31. # parse area file directory
  32. s = self.fp.read(256)
  33. if not _accept(s) or len(s) != 256:
  34. raise SyntaxError("not an McIdas area file")
  35. self.area_descriptor_raw = s
  36. self.area_descriptor = w = [0] + list(struct.unpack("!64i", s))
  37. # get mode
  38. if w[11] == 1:
  39. mode = rawmode = "L"
  40. elif w[11] == 2:
  41. # FIXME: add memory map support
  42. mode = "I"
  43. rawmode = "I;16B"
  44. elif w[11] == 4:
  45. # FIXME: add memory map support
  46. mode = "I"
  47. rawmode = "I;32B"
  48. else:
  49. raise SyntaxError("unsupported McIdas format")
  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 = [("raw", (0, 0) + self.size, offset, (rawmode, stride, 1))]
  55. # --------------------------------------------------------------------
  56. # registry
  57. Image.register_open(McIdasImageFile.format, McIdasImageFile, _accept)
  58. # no default extension