Skip to content

Commit 3348f76

Browse files
committed
optimise layering function
1 parent abb99e9 commit 3348f76

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+165
-131
lines changed

documentation/reference/gimpformats/gimpXcfDocument.md

Lines changed: 53 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,13 @@
2929
- [GimpDocument().save](#gimpdocument()save)
3030
- [GimpDocument().saveNew](#gimpdocument()savenew)
3131
- [GimpDocument().setLayer](#gimpdocument()setlayer)
32+
- [applyMask](#applymask)
3233
- [blendModeLookup](#blendmodelookup)
3334
- [blendModeLookup](#blendmodelookup-1)
35+
- [blendWithFlattened](#blendwithflattened)
3436
- [flattenAll](#flattenall)
3537
- [flattenLayerOrGroup](#flattenlayerorgroup)
3638
- [renderLayerOrGroup](#renderlayerorgroup)
37-
- [renderMaskWOffset](#rendermaskwoffset)
3839

3940
## BlendType
4041

@@ -461,6 +462,26 @@ def setLayer(self, index, layer) -> None: ...
461462

462463

463464

465+
## applyMask
466+
467+
[Show source in gimpXcfDocument.py:587](../../../gimpformats/gimpXcfDocument.py#L587)
468+
469+
Applies a mask efficiently.
470+
471+
#### Signature
472+
473+
```python
474+
def applyMask(
475+
image: Image.Image,
476+
mask: Image.Image,
477+
xOffset: int,
478+
yOffset: int,
479+
size: tuple[int, int],
480+
) -> Image.Image: ...
481+
```
482+
483+
484+
464485
## blendModeLookup
465486

466487
[Show source in gimpXcfDocument.py:434](../../../gimpformats/gimpXcfDocument.py#L434)
@@ -485,7 +506,9 @@ def blendModeLookup(
485506

486507
## blendModeLookup
487508

488-
[Show source in gimpXcfDocument.py:617](../../../gimpformats/gimpXcfDocument.py#L617)
509+
[Show source in gimpXcfDocument.py:607](../../../gimpformats/gimpXcfDocument.py#L607)
510+
511+
Looks up the blend mode from the lookup table.
489512

490513
#### Signature
491514

@@ -495,9 +518,31 @@ def blendModeLookup(blend_mode, blendLookup): ...
495518

496519

497520

521+
## blendWithFlattened
522+
523+
[Show source in gimpXcfDocument.py:596](../../../gimpformats/gimpXcfDocument.py#L596)
524+
525+
Optimized function to blend layers with existing flattened image.
526+
527+
#### Signature
528+
529+
```python
530+
def blendWithFlattened(
531+
flattened: Image.Image | None, foreground: Image.Image, layer: GimpLayer
532+
) -> Image.Image: ...
533+
```
534+
535+
#### See also
536+
537+
- [GimpLayer](./GimpLayer.md#gimplayer)
538+
539+
540+
498541
## flattenAll
499542

500-
[Show source in gimpXcfDocument.py:589](../../../gimpformats/gimpXcfDocument.py#L589)
543+
[Show source in gimpXcfDocument.py:567](../../../gimpformats/gimpXcfDocument.py#L567)
544+
545+
Optimized flattenAll to avoid excessive recursion.
501546

502547
#### Signature
503548

@@ -517,6 +562,8 @@ def flattenAll(
517562

518563
[Show source in gimpXcfDocument.py:520](../../../gimpformats/gimpXcfDocument.py#L520)
519564

565+
Optimized function to flatten a layer or group with reduced redundant operations.
566+
520567
#### Signature
521568

522569
```python
@@ -532,26 +579,14 @@ def flattenLayerOrGroup(
532579

533580
## renderLayerOrGroup
534581

535-
[Show source in gimpXcfDocument.py:600](../../../gimpformats/gimpXcfDocument.py#L600)
536-
537-
#### Signature
538-
539-
```python
540-
def renderLayerOrGroup(
541-
image: Image.Image, size: tuple[int, int], offsets: tuple[int, int] = (0, 0)
542-
) -> Image.Image: ...
543-
```
544-
545-
582+
[Show source in gimpXcfDocument.py:577](../../../gimpformats/gimpXcfDocument.py#L577)
546583

547-
## renderMaskWOffset
548-
549-
[Show source in gimpXcfDocument.py:608](../../../gimpformats/gimpXcfDocument.py#L608)
584+
Optimized function to render a layer or group with reduced conversions.
550585

551586
#### Signature
552587

553588
```python
554-
def renderMaskWOffset(
589+
def renderLayerOrGroup(
555590
image: Image.Image, size: tuple[int, int], offsets: tuple[int, int] = (0, 0)
556591
) -> Image.Image: ...
557592
```

gimpformats/GimpChannel.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def __init__(
3535
self.image = image
3636
self._data = None
3737

38-
def decode(self, data: bytes, index: int = 0) -> int:
38+
def decode(self, data: bytes | bytearray, index: int = 0) -> int:
3939
"""Decode a byte buffer.
4040
4141
Args:

gimpformats/GimpLayer.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class GimpLayer(GimpIOBase):
2525
PIL_MODE_TO_LAYER_MODE = {"L": 2, "LA": 3, "RGB": 0, "RGBA": 1}
2626

2727
def __init__(self, parent, name: str | None = None, image: Image | None = None) -> None:
28-
"""Represents a single layer in a gimp image.
28+
"""Represent a single layer in a gimp image.
2929
3030
Args:
3131
----
@@ -47,11 +47,11 @@ def __init__(self, parent, name: str | None = None, image: Image | None = None)
4747
if image is not None:
4848
self.image = image # done last as it resets some of the above defaults
4949

50-
def decode(self, data: bytes, index: int = 0) -> int:
50+
def decode(self, data: bytes | bytearray, index: int = 0) -> int:
5151
"""Decode a byte buffer.
5252
5353
Steps:
54-
Create a new IO buffer (array of binary values)
54+
Create a new IO buffer
5555
Grab attributes as outlined in the spec
5656
List of properties
5757
Get the image hierarchy and mask pointers
@@ -67,7 +67,7 @@ def decode(self, data: bytes, index: int = 0) -> int:
6767
int: offset
6868
6969
"""
70-
# Create a new IO buffer (array of binary values)
70+
# Create a new IO buffer
7171
ioBuf = IO(data, index)
7272
# Grab attributes as outlined in the spec
7373
self.width = ioBuf.u32
@@ -88,14 +88,14 @@ def encode(self) -> bytearray:
8888
"""Encode to byte array.
8989
9090
Steps:
91-
Create a new IO buffer (array of binary values)
91+
Create a new IO buffer
9292
Set attributes as outlined in the spec
9393
List of properties
9494
Set the image hierarchy and mask pointers
9595
Return the data
9696
9797
"""
98-
# Create a new IO buffer (array of binary values)
98+
# Create a new IO buffer
9999
dataAreaIO = IO()
100100
ioBuf = IO()
101101
# Set attributes as outlined in the spec

gimpformats/binaryiotools.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#!/usr/bin/env python3
21
"""
32
Base binary I/O helper.
43

0 commit comments

Comments
 (0)