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. Returns: int: offset """ ioBuf = IO(data, index, boolSize=32) self.name = ioBuf.sz754 self.uniqueId = ioBuf.u32 self.visible = ioBuf.boolean self.linked = ioBuf.boolean numParasites = ioBuf.u32 numStrokes = ioBuf.u32 for _ in range(numParasites): parasite = GimpParasite() ioBuf.index = parasite.decode(ioBuf.data, ioBuf.index) self.parasites.append(parasite) for _ in range(numStrokes): gimpstroke = GimpStroke(self) ioBuf.index = gimpstroke.decode(ioBuf.data, ioBuf.index) self.strokes.append(gimpstroke) return ioBuf.index
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. Returns: int: offset """ ioBuf = IO(data, index) self.name = ioBuf.textLine secondLine = ioBuf.textLine.split(" ") self.params = {} numBrushes = int(secondLine[0]) # everything that's left is a gimp-image-pipe-parameters parasite for i in range(1, len(secondLine)): param = secondLine[i].split(":", 1) self.params[param[0].strip()] = param[1].strip() self.brushes = [] for _ in range(numBrushes): brush = GimpGbrBrush() ioBuf.index = brush.decode( ioBuf.data, ioBuf.index ) # TODO: broken. For some reason there is extra data between brushes! self.brushes.append(brush) return ioBuf.index
def encode(self): """Encode to a byte array. Steps: Create a new IO buffer (array of binary values) The file is a valid gimp xcf Set the file version Set other attributes as outlined in the spec Set precision data using the class and ioBuf buffer List of properties Set the layers and add the pointers to them Set the channels and add the pointers to them Return the data """ # Create a new IO buffer (array of binary values) ioBuf = IO() # The file is a valid gimp xcf ioBuf.addBytes("gimp xcf ") # Set the file version ioBuf.addBytes(f"v{self.version:03d}\0") # Set other attributes as outlined in the spec ioBuf.u32 = self.width ioBuf.u32 = self.height ioBuf.u32 = self.baseColorMode # Set precision data using the class and ioBuf buffer if self.precision is None: self.precision = Precision() self.precision.encode(self.version, ioBuf) # List of properties ioBuf.addBytes(self._propertiesEncode()) dataAreaIdx = ioBuf.index + self.pointerSize * (len(self.layers) + len(self._channels)) dataAreaIO = IO() # Set the layers and add the pointers to them for layer in self.layers: ioBuf.index = dataAreaIdx + dataAreaIO.index dataAreaIO.addBytes(layer.encode()) # Set the channels and add the pointers to them for channel in self._channels: ioBuf.index = dataAreaIdx + dataAreaIO.index dataAreaIO.addBytes(channel.encode()) ioBuf.addBytes(dataAreaIO) # Return the data return ioBuf.data
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. Returns: int: offset """ ioBuf = IO(data, index, boolSize=32) self.strokeType = ioBuf.u32 self.closedShape = ioBuf.boolean numFloatsPerPoint = ioBuf.u32 numPoints = ioBuf.u32 for _ in range(numPoints): gimpPoint = GimpPoint(self) ioBuf.index = gimpPoint.decode(ioBuf.data, ioBuf.index, numFloatsPerPoint) self.points.append(gimpPoint) return ioBuf.index