소스 검색

Fix watermarks overlapping animation frames in some cases

DarthSim 1 년 전
부모
커밋
97217949a7
2개의 변경된 파일33개의 추가작업 그리고 1개의 파일을 삭제
  1. 1 0
      CHANGELOG.md
  2. 32 1
      processing/watermark.go

+ 1 - 0
CHANGELOG.md

@@ -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

+ 32 - 1
processing/watermark.go

@@ -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++ {