Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
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.

QoiImagePlugin.py 3.5KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. #
  2. # The Python Imaging Library.
  3. #
  4. # QOI support for PIL
  5. #
  6. # See the README file for information on usage and redistribution.
  7. #
  8. import os
  9. from . import Image, ImageFile
  10. from ._binary import i32be as i32
  11. from ._binary import o8
  12. def _accept(prefix):
  13. return prefix[:4] == b"qoif"
  14. class QoiImageFile(ImageFile.ImageFile):
  15. format = "QOI"
  16. format_description = "Quite OK Image"
  17. def _open(self):
  18. if not _accept(self.fp.read(4)):
  19. msg = "not a QOI file"
  20. raise SyntaxError(msg)
  21. self._size = tuple(i32(self.fp.read(4)) for i in range(2))
  22. channels = self.fp.read(1)[0]
  23. self.mode = "RGB" if channels == 3 else "RGBA"
  24. self.fp.seek(1, os.SEEK_CUR) # colorspace
  25. self.tile = [("qoi", (0, 0) + self._size, self.fp.tell(), None)]
  26. class QoiDecoder(ImageFile.PyDecoder):
  27. _pulls_fd = True
  28. def _add_to_previous_pixels(self, value):
  29. self._previous_pixel = value
  30. r, g, b, a = value
  31. hash_value = (r * 3 + g * 5 + b * 7 + a * 11) % 64
  32. self._previously_seen_pixels[hash_value] = value
  33. def decode(self, buffer):
  34. self._previously_seen_pixels = {}
  35. self._previous_pixel = None
  36. self._add_to_previous_pixels(b"".join(o8(i) for i in (0, 0, 0, 255)))
  37. data = bytearray()
  38. bands = Image.getmodebands(self.mode)
  39. while len(data) < self.state.xsize * self.state.ysize * bands:
  40. byte = self.fd.read(1)[0]
  41. if byte == 0b11111110: # QOI_OP_RGB
  42. value = self.fd.read(3) + o8(255)
  43. elif byte == 0b11111111: # QOI_OP_RGBA
  44. value = self.fd.read(4)
  45. else:
  46. op = byte >> 6
  47. if op == 0: # QOI_OP_INDEX
  48. op_index = byte & 0b00111111
  49. value = self._previously_seen_pixels.get(op_index, (0, 0, 0, 0))
  50. elif op == 1: # QOI_OP_DIFF
  51. value = (
  52. (self._previous_pixel[0] + ((byte & 0b00110000) >> 4) - 2)
  53. % 256,
  54. (self._previous_pixel[1] + ((byte & 0b00001100) >> 2) - 2)
  55. % 256,
  56. (self._previous_pixel[2] + (byte & 0b00000011) - 2) % 256,
  57. )
  58. value += (self._previous_pixel[3],)
  59. elif op == 2: # QOI_OP_LUMA
  60. second_byte = self.fd.read(1)[0]
  61. diff_green = (byte & 0b00111111) - 32
  62. diff_red = ((second_byte & 0b11110000) >> 4) - 8
  63. diff_blue = (second_byte & 0b00001111) - 8
  64. value = tuple(
  65. (self._previous_pixel[i] + diff_green + diff) % 256
  66. for i, diff in enumerate((diff_red, 0, diff_blue))
  67. )
  68. value += (self._previous_pixel[3],)
  69. elif op == 3: # QOI_OP_RUN
  70. run_length = (byte & 0b00111111) + 1
  71. value = self._previous_pixel
  72. if bands == 3:
  73. value = value[:3]
  74. data += value * run_length
  75. continue
  76. value = b"".join(o8(i) for i in value)
  77. self._add_to_previous_pixels(value)
  78. if bands == 3:
  79. value = value[:3]
  80. data += value
  81. self.set_as_raw(bytes(data))
  82. return -1, 0
  83. Image.register_open(QoiImageFile.format, QoiImageFile, _accept)
  84. Image.register_decoder("qoi", QoiDecoder)
  85. Image.register_extension(QoiImageFile.format, ".qoi")