SunImagePlugin.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #
  2. # The Python Imaging Library.
  3. # $Id$
  4. #
  5. # Sun image file handling
  6. #
  7. # History:
  8. # 1995-09-10 fl Created
  9. # 1996-05-28 fl Fixed 32-bit alignment
  10. # 1998-12-29 fl Import ImagePalette module
  11. # 2001-12-18 fl Fixed palette loading (from Jean-Claude Rimbault)
  12. #
  13. # Copyright (c) 1997-2001 by Secret Labs AB
  14. # Copyright (c) 1995-1996 by Fredrik Lundh
  15. #
  16. # See the README file for information on usage and redistribution.
  17. #
  18. from __future__ import annotations
  19. __lazy_modules__ = {"PIL._binary"}
  20. from . import Image, ImageFile, ImagePalette
  21. from ._binary import i32be as i32
  22. def _accept(prefix: bytes) -> bool:
  23. return len(prefix) >= 4 and i32(prefix) == 0x59A66A95
  24. ##
  25. # Image plugin for Sun raster files.
  26. class SunImageFile(ImageFile.ImageFile):
  27. format = "SUN"
  28. format_description = "Sun Raster File"
  29. def _open(self) -> None:
  30. # The Sun Raster file header is 32 bytes in length
  31. # and has the following format:
  32. # typedef struct _SunRaster
  33. # {
  34. # DWORD MagicNumber; /* Magic (identification) number */
  35. # DWORD Width; /* Width of image in pixels */
  36. # DWORD Height; /* Height of image in pixels */
  37. # DWORD Depth; /* Number of bits per pixel */
  38. # DWORD Length; /* Size of image data in bytes */
  39. # DWORD Type; /* Type of raster file */
  40. # DWORD ColorMapType; /* Type of color map */
  41. # DWORD ColorMapLength; /* Size of the color map in bytes */
  42. # } SUNRASTER;
  43. assert self.fp is not None
  44. # HEAD
  45. s = self.fp.read(32)
  46. if not _accept(s):
  47. msg = "not an SUN raster file"
  48. raise SyntaxError(msg)
  49. offset = 32
  50. self._size = i32(s, 4), i32(s, 8)
  51. depth = i32(s, 12)
  52. # data_length = i32(s, 16) # unreliable, ignore.
  53. file_type = i32(s, 20)
  54. palette_type = i32(s, 24) # 0: None, 1: RGB, 2: Raw/arbitrary
  55. palette_length = i32(s, 28)
  56. if depth == 1:
  57. self._mode, rawmode = "1", "1;I"
  58. elif depth == 4:
  59. self._mode, rawmode = "L", "L;4"
  60. elif depth == 8:
  61. self._mode = rawmode = "L"
  62. elif depth == 24:
  63. if file_type == 3:
  64. self._mode, rawmode = "RGB", "RGB"
  65. else:
  66. self._mode, rawmode = "RGB", "BGR"
  67. elif depth == 32:
  68. if file_type == 3:
  69. self._mode, rawmode = "RGB", "RGBX"
  70. else:
  71. self._mode, rawmode = "RGB", "BGRX"
  72. else:
  73. msg = "Unsupported Mode/Bit Depth"
  74. raise SyntaxError(msg)
  75. if palette_length:
  76. if palette_length > 1024:
  77. msg = "Unsupported Color Palette Length"
  78. raise SyntaxError(msg)
  79. if palette_type != 1:
  80. msg = "Unsupported Palette Type"
  81. raise SyntaxError(msg)
  82. offset = offset + palette_length
  83. self.palette = ImagePalette.raw("RGB;L", self.fp.read(palette_length))
  84. if self.mode == "L":
  85. self._mode = "P"
  86. rawmode = rawmode.replace("L", "P")
  87. # 16 bit boundaries on stride
  88. stride = ((self.size[0] * depth + 15) // 16) * 2
  89. # file type: Type is the version (or flavor) of the bitmap
  90. # file. The following values are typically found in the Type
  91. # field:
  92. # 0000h Old
  93. # 0001h Standard
  94. # 0002h Byte-encoded
  95. # 0003h RGB format
  96. # 0004h TIFF format
  97. # 0005h IFF format
  98. # FFFFh Experimental
  99. # Old and standard are the same, except for the length tag.
  100. # byte-encoded is run-length-encoded
  101. # RGB looks similar to standard, but RGB byte order
  102. # TIFF and IFF mean that they were converted from T/IFF
  103. # Experimental means that it's something else.
  104. # (https://www.fileformat.info/format/sunraster/egff.htm)
  105. if file_type in (0, 1, 3, 4, 5):
  106. self.tile = [
  107. ImageFile._Tile("raw", (0, 0, *self.size), offset, (rawmode, stride))
  108. ]
  109. elif file_type == 2:
  110. self.tile = [
  111. ImageFile._Tile("sun_rle", (0, 0, *self.size), offset, rawmode)
  112. ]
  113. else:
  114. msg = "Unsupported Sun Raster file type"
  115. raise SyntaxError(msg)
  116. #
  117. # registry
  118. Image.register_open(SunImageFile.format, SunImageFile, _accept)
  119. Image.register_extension(SunImageFile.format, ".ras")