SunImagePlugin.py 4.3 KB

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