Files
aego/art/merge.go
T
sanspie 50be5c04bc
ci-platforms / darwin amd64 smoke (push) Skipped
GitHub / linux amd64 native GitHub-hosted native CI: success
ci-platforms / windows amd64 smoke (push) Skipped
GitHub / windows amd64 native GitHub-hosted native CI: success
GitHub / darwin arm64 native GitHub-hosted native CI: success
GitHub / darwin amd64 native GitHub-hosted native CI: success
ci / checks (push) Failing after 1h21m34s
ci / native-authoring (push) Skipped
ci-platforms / darwin arm64 smoke (push) Skipped
github-native / gitea-pending (push) Skipped
github-native / darwin amd64 native (push) Skipped
github-native / linux amd64 native (push) Skipped
github-native / windows amd64 native (push) Skipped
github-native / darwin arm64 native (push) Skipped
github-native / gitea-result (push) Skipped
ci / build (amd64, linux, ubuntu-24.04) (push) Successful in 12m4s
ci / release (push) Skipped
Stage 12.5: Art Workspace and asset authoring
Add dockable Art tools, animation and previews, guarded asset workflows,
and the Web Asset Studio built on the shared Art Core. Expand English and
Russian documentation, interactive Try scenes, examples and editor icons.

Add audio waveform navigation, source analysis, guarded previews and a
browser Worker mixer. Preserve source changes and recovery with bounded
checks, and strengthen native, browser and interoperability CI.

Prepare version 0.12.5 and manually built native artifact candidates.
Fix the production site image demo build dependency.
Reuse immutable scene indices when capturing keyframes.
Wait for real Persist save/load completion in component tests.
Stabilize browser Game View captures outside pointer hover.
Aseprite interchange and persistent Audio EDL recovery remain limited;
the artifact workflow does not publish releases or change stable.
2026-11-15 10:37:50 +03:00

139 lines
4.1 KiB
Go

package art
import (
"context"
"fmt"
"image"
)
// MergeDown bakes two adjacent normal-blend raster (or reference) siblings for
// every frame and surface channel. Their parent, frame IDs, lower layer ID and
// existing lower cel IDs survive. A repeated pair of linked images remains
// linked, while images shared with other layers are never overwritten.
//
// Backdrop-dependent blend modes are rejected instead of silently changing
// their appearance over unrelated lower layers. Indexed art is accepted only
// when the exact merged colors are representable in its current palette.
func (e *Edit) MergeDown(ctx context.Context, upper ID) (ID, error) {
if err := e.check(); err != nil {
return "", err
}
ui, ok := e.work.layer(upper)
if !ok {
return "", fmt.Errorf("art: missing layer")
}
top := e.work.Layers[ui]
li := -1
for i := ui - 1; i >= 0; i-- {
if e.work.Layers[i].Parent == top.Parent {
li = i
break
}
}
if li < 0 {
return "", fmt.Errorf("art: no layer below in the same group")
}
lower := e.work.Layers[li]
if top.Kind == Group || lower.Kind != top.Kind {
return "", fmt.Errorf("art: merge requires two raster or two reference layers")
}
if e.work.isLocked(upper) || e.work.isLocked(lower.ID) {
return "", ErrLocked
}
if !top.Visible || !lower.Visible {
return "", fmt.Errorf("art: show both layers before merging")
}
if top.Blend != BlendNormal || lower.Blend != BlendNormal {
return "", fmt.Errorf("art: merge requires Normal blend to preserve the existing backdrop")
}
before := e.work.clone()
pair := before.clone()
lower.Parent, top.Parent = "", ""
pair.Layers = []Layer{lower, top}
type source struct {
Image ID
X, Y int
Opacity uint8
}
type identity struct {
Lower, Upper source
Channel Channel
}
cache := make(map[identity]ID)
colorIndices := make(map[Color]uint32)
for i, swatch := range before.Palette {
if _, exists := colorIndices[swatch.Color]; !exists {
colorIndices[swatch.Color] = uint32(i)
}
}
for _, frame := range before.Frames {
for _, channel := range []Channel{Albedo, Normal, Height, Emission} {
if err := ctx.Err(); err != nil {
return "", err
}
key := identity{Channel: channel}
present := false
for _, cel := range before.Cels {
if cel.Frame != frame.ID || cel.Channel != channel {
continue
}
value := source{cel.Image, cel.X, cel.Y, cel.Opacity}
if cel.Layer == lower.ID {
key.Lower, present = value, true
} else if cel.Layer == upper {
key.Upper, present = value, true
}
}
if !present {
continue
}
merged := cache[key]
if merged == "" {
im := image.NewNRGBA(image.Rect(0, 0, before.Canvas.Width, before.Canvas.Height))
if err := renderRegion(pair, frame.ID, im, im.Bounds(), RenderOptions{Channel: channel, References: true}); err != nil {
return "", err
}
merged = e.doc.newID()
e.work.Images[merged] = newPixels(im.Bounds().Dx(), im.Bounds().Dy())
for y := 0; y < im.Bounds().Dy(); y++ {
if err := ctx.Err(); err != nil {
return "", err
}
for x := 0; x < im.Bounds().Dx(); x++ {
p := im.NRGBAAt(x, y)
color := Color{p.R, p.G, p.B, p.A}
var pixel uint32
if channel == Albedo && before.Canvas.Mode == Indexed {
if color.A != 0 {
var exact bool
pixel, exact = colorIndices[color]
if !exact {
return "", fmt.Errorf("art: merged color is outside the indexed palette; convert to RGBA explicitly")
}
}
} else {
pixel = encode(before.forChannel(channel), color)
}
if err := e.raw(merged, x, y, pixel); err != nil {
return "", err
}
}
}
cache[key] = merged
}
if _, err := e.EnsureCel(lower.ID, frame.ID, channel); err != nil {
return "", err
}
ci, _ := e.work.findCel(lower.ID, frame.ID, channel)
e.work.Cels[ci].Image = merged
e.work.Cels[ci].X, e.work.Cels[ci].Y, e.work.Cels[ci].Opacity = 0, 0, 255
}
}
e.work.Layers[li].Opacity = 255
e.touch(image.Rect(0, 0, before.Canvas.Width, before.Canvas.Height))
if err := e.DeleteLayer(upper); err != nil {
return "", err
}
return lower.ID, nil
}