SunImagePlugin.py 4.2 KB

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