Example #1
0
    def decode(self, data: bytes, index: int = 0) -> int:
        """Decode a byte buffer.

		Args:
			data (bytes): data buffer to decode
			index (int, optional): index within the buffer to start at. Defaults to 0.

		Raises:
			RuntimeError: "unknown brush version"
			RuntimeError: "File format error.  Magic value mismatch"

		Returns:
			int: offset]
		"""
        ioBuf = IO(data, index)
        headerSize = ioBuf.u32
        self.version = ioBuf.u32
        if self.version != 2:
            raise RuntimeError(f"ERR: unknown brush version {self.version}")
        self.width = ioBuf.u32
        self.height = ioBuf.u32
        self.bpp = ioBuf.u32  # only allows grayscale or RGB
        self.mode = self.COLOR_MODES[self.bpp]
        magic = ioBuf.getBytes(4)
        if magic.decode("ascii") != "GIMP":
            raise RuntimeError('"' + magic.decode("ascii") + '" ' +
                               str(index) +
                               " File format error.  Magic value mismatch.")
        self.spacing = ioBuf.u32
        nameLen = headerSize - ioBuf.index
        self.name = ioBuf.getBytes(nameLen).decode("UTF-8")
        self.rawImage = ioBuf.getBytes(self.width * self.height * self.bpp)
        return ioBuf.index
Example #2
0
	def decode(self, data: bytes, index: int = 0) -> int:
		"""Decode a byte buffer

		:param data: data buffer to decode
		:param index: index within the buffer to start at
		"""
		ioBuf = IO(data, index)
		self.name = ioBuf.sz754
		self.flags = ioBuf.u32
		dataLength = ioBuf.u32
		self.data = ioBuf.getBytes(dataLength)
		return ioBuf.index
Example #3
0
	def _propertiesDecode(self, ioBuf: IO):
		"""Decode a list of properties."""
		while True:
			try:
				propertyType = ioBuf.u32
				dataLength = ioBuf.u32
			except struct.error:  # end of data, so that's that.
				break
			if propertyType == 0:
				break
			self._propertyDecode(propertyType, ioBuf.getBytes(dataLength))
		return ioBuf.index
    def decode(self, data: bytes, index: int = 0) -> int:
        """Decode a byte buffer.

		Steps:
		Create a new IO buffer (array of binary values)
		Check that the file is a valid gimp xcf
		Grab the file version
		Grab other attributes as outlined in the spec
		Get precision data using the class and ioBuf buffer
		List of properties
		Get the layers and add the pointers to them
		Get the channels and add the pointers to them
		Return the offset

		Args:
			data (bytes): data buffer to decode
			index (int, optional): index within the buffer to start at]. Defaults to 0.

		Raises:
			RuntimeError: "Not a valid GIMP file"

		Returns:
			int: offset
		"""
        # Create a new IO buffer (array of binary values)
        ioBuf = IO(data, index)
        # Check that the file is a valid gimp xcf
        if ioBuf.getBytes(9) != b"gimp xcf ":
            raise RuntimeError("Not a valid GIMP file")
        # Grab the file version
        version = ioBuf.cString
        if version == "file":
            self.version = 0
        else:
            self.version = int(version[1:])
        # Grab other attributes as outlined in the spec
        self.width = ioBuf.u32
        self.height = ioBuf.u32
        self.baseColorMode = ioBuf.u32
        # Get precision data using the class and ioBuf buffer
        self.precision = Precision()
        self.precision.decode(self.version, ioBuf)
        # List of properties
        self._propertiesDecode(ioBuf)
        self._layerPtr = []
        self._layers = []
        # Get the layers and add the pointers to them
        while True:
            ptr = self._pointerDecode(ioBuf)
            if ptr == 0:
                break
            self._layerPtr.append(ptr)
            layer = GimpLayer(self)
            layer.decode(ioBuf.data, ptr)
            self._layers.append(layer)
        # Get the channels and add the pointers to them
        self._channelPtr = []
        self._channels = []
        while True:
            ptr = self._pointerDecode(ioBuf)
            if ptr == 0:
                break
            self._channelPtr.append(ptr)
            chnl = GimpChannel(self)
            chnl.decode(ioBuf.data, ptr)
            self._channels.append(chnl)
        # Return the offset
        return ioBuf.index