SunImagePlugin.py 4.3 KB

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