You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
4.3 KiB

4 years ago
  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__ = "0.3"
  21. def _accept(prefix):
  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):
  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. # HEAD
  43. s = self.fp.read(32)
  44. if i32(s) != 0x59a66a95:
  45. raise SyntaxError("not an SUN raster file")
  46. offset = 32
  47. self._size = i32(s[4:8]), i32(s[8:12])
  48. depth = i32(s[12:16])
  49. # data_length = i32(s[16:20]) # unreliable, ignore.
  50. file_type = i32(s[20:24])
  51. palette_type = i32(s[24:28]) # 0: None, 1: RGB, 2: Raw/arbitrary
  52. palette_length = i32(s[28:32])
  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. raise SyntaxError("Unsupported Mode/Bit Depth")
  71. if palette_length:
  72. if palette_length > 1024:
  73. raise SyntaxError("Unsupported Color Palette Length")
  74. if palette_type != 1:
  75. raise SyntaxError("Unsupported Palette Type")
  76. offset = offset + palette_length
  77. self.palette = ImagePalette.raw("RGB;L",
  78. self.fp.read(palette_length))
  79. if self.mode == "L":
  80. self.mode = "P"
  81. rawmode = rawmode.replace('L', 'P')
  82. # 16 bit boundaries on stride
  83. stride = ((self.size[0] * depth + 15) // 16) * 2
  84. # file type: Type is the version (or flavor) of the bitmap
  85. # file. The following values are typically found in the Type
  86. # field:
  87. # 0000h Old
  88. # 0001h Standard
  89. # 0002h Byte-encoded
  90. # 0003h RGB format
  91. # 0004h TIFF format
  92. # 0005h IFF format
  93. # FFFFh Experimental
  94. # Old and standard are the same, except for the length tag.
  95. # byte-encoded is run-length-encoded
  96. # RGB looks similar to standard, but RGB byte order
  97. # TIFF and IFF mean that they were converted from T/IFF
  98. # Experimental means that it's something else.
  99. # (https://www.fileformat.info/format/sunraster/egff.htm)
  100. if file_type in (0, 1, 3, 4, 5):
  101. self.tile = [("raw", (0, 0)+self.size, offset, (rawmode, stride))]
  102. elif file_type == 2:
  103. self.tile = [("sun_rle", (0, 0)+self.size, offset, rawmode)]
  104. else:
  105. raise SyntaxError('Unsupported Sun Raster file type')
  106. #
  107. # registry
  108. Image.register_open(SunImageFile.format, SunImageFile, _accept)
  109. Image.register_extension(SunImageFile.format, ".ras")