SunImagePlugin.py 4.5 KB

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