Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions invokeai/backend/image_util/imwatermark/vendor.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@
# `opencv-contrib-python`. It's easier to copy the code over than complicate the installation process by
# requiring an extra post-install step of removing `opencv-python` and installing `opencv-contrib-python`.

import base64
import struct
import uuid
import base64

import cv2
import numpy as np
import pywt
Expand Down Expand Up @@ -266,12 +267,17 @@ def diffuse_dct_matrix(self, block, wmBit, scale):
return block

def infer_dct_matrix(self, block, scale):
pos = np.argmax(abs(block.flatten()[1:])) + 1
i, j = pos // self._block, pos % self._block

val = block[i][j]
# Inline routine for fast max-abs with index (excluding DC)
flat = block.ravel()
v = flat[1:]
abs_v = np.abs(v)
pos_in_v = np.argmax(abs_v)
pos = pos_in_v + 1 # because we skipped DC at index 0

i, j = divmod(pos, self._block)
val = block[i, j]
if val < 0:
val = abs(val)
val = -val # abs(val), but avoids Python function call

if (val % scale) > 0.5 * scale:
return 1
Expand Down