Skip to content

Commit

Permalink
Fix watermarks overlapping animation frames in some cases
Browse files Browse the repository at this point in the history
  • Loading branch information
DarthSim committed Apr 24, 2024
1 parent aeb2c08 commit 9721794
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -15,6 +15,7 @@
### Fix
- Fix parsing some TIFFs.
- Fix over-shrinking during scale-on-load.
- Fix watermarks overlapping animation frames in some cases.
- (pro) Fix false-positive video detections.

## [3.23.0] - 2024-03-11
Expand Down
33 changes: 32 additions & 1 deletion processing/watermark.go
Expand Up @@ -120,9 +120,40 @@ func applyWatermark(img *vips.Image, wmData *imagedata.ImageData, opts *options.
}

left, top := 0, 0
wmWidth := wm.Width()
wmHeight := wm.Height()

if !opts.ShouldReplicate() {
left, top = calcPosition(width, frameHeight, wm.Width(), wm.Height(), &opts.Gravity, offsetScale, true)
left, top = calcPosition(width, frameHeight, wmWidth, wmHeight, &opts.Gravity, offsetScale, true)
}

if left >= width || top >= height || -left >= wmWidth || -top >= wmHeight {
// Watermark is completely outside the image
return nil
}

// if watermark is partially outside the image, it may partially be visible
// on the next frame. We need to crop it vertically.
// We don't care about horizontal overlap, as frames are stacked vertically
if framesCount > 1 {
cropTop := 0
cropHeight := wmHeight

if top < 0 {
cropTop = -top
cropHeight -= cropTop
top = 0
}

if top+cropHeight > frameHeight {
cropHeight = frameHeight - top
}

if cropTop > 0 || cropHeight < wmHeight {
if err := wm.Crop(0, cropTop, wmWidth, cropHeight); err != nil {
return err
}
}
}

for i := 0; i < framesCount; i++ {
Expand Down

0 comments on commit 9721794

Please sign in to comment.