Skip to content

Commit

Permalink
fix(updateloop): face rect algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
FlandiaYingman committed May 1, 2024
1 parent b81336e commit 7de039c
Showing 1 changed file with 37 additions and 3 deletions.
40 changes: 37 additions & 3 deletions internal/pkg/arkprocessor/processor_character.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,43 @@ func mergeCharacterFace(body image.Image, face image.Image, faceRect image.Recta
// Because faceRect is based on body as if it is a square, we need to adjust it.
dx := body.Bounds().Dx()
dy := body.Bounds().Dy()
xOffset := (dx - max(dx, dy)) / 2
yOffset := (dy - max(dx, dy)) / 2
faceRect = faceRect.Add(image.Pt(xOffset, yOffset))

if dx == dy && dx < 1024 {
// If the body is square and the side is less than 1024px,
// it should be resized into 1024px.

// Example: avg_npc_457_1
// size of body = 512px, but the face parameters are as if the size of the body is 1024px (scale)

factor := float64(min(max(dx, dy), 1024.0)) / 1024.0
faceRect = image.Rect(
int(float64(faceRect.Min.X)*factor),
int(float64(faceRect.Min.Y)*factor),
int(float64(faceRect.Max.X)*factor),
int(float64(faceRect.Max.Y)*factor),
)
}

if dx != dy && max(dx, dy) < 1024 {
// If the body is not a square and both sides are less than 1024px,
// add paddings (top, left, right, but not bottom) to make it a square with both sides 1024px.

// Example: avgnew_147_shining_1:
// both sides of body < 1024px,

xOffset := (dx - 1024) / 2 // left + right
yOffset := dy - 1024 // top only
faceRect = faceRect.Add(image.Pt(xOffset, yOffset))
} else {
// If the body is not a square and either side is greater than (or equal to) 1024px,
// add paddings (left, right) to make it a square with both sides the longer side.

// Example: avg_npc_1303_1:
// one side of body > 1024px
xOffset := (dx - max(dx, dy)) / 2
yOffset := (dy - max(dx, dy)) / 2
faceRect = faceRect.Add(image.Pt(xOffset, yOffset))
}

if !faceRect.In(body.Bounds()) {
return nil, errors.Errorf("merge character face: face rectangle %v is not in the body's bounds %v", faceRect, body.Bounds())
Expand Down

0 comments on commit 7de039c

Please sign in to comment.