;; Blend effect - combines two video frames
;; Streaming-compatible: frame is background, overlay is second frame
;; Usage: (blend background overlay :opacity 0.5 :mode "alpha")
;;
;; Params:
;;   mode - blend mode (add, multiply, screen, overlay, difference, lighten, darken, alpha)
;;   opacity - blend amount (0-1)

(require-primitives "image" "blending" "core")

(define-effect blend
  :params (
    (overlay :type frame :default nil)
    (mode :type string :default "alpha")
    (opacity :type float :default 0.5)
  )
  (if (core:is-nil overlay)
    frame
    (let [a frame
          b overlay
          a-h (image:height a)
          a-w (image:width a)
          b-h (image:height b)
          b-w (image:width b)
          ;; Resize b to match a if needed
          b-sized (if (and (= a-w b-w) (= a-h b-h))
                    b
                    (image:resize b a-w a-h "linear"))]
      (if (= mode "alpha")
        (blending:blend-images a b-sized opacity)
        (blending:blend-images a (blending:blend-mode a b-sized mode) opacity)))))
