use crate::{
color, dct, haar, payload, prng, CapacityInfo, ImageBufferRef, WatermarkConfig, WatermarkError,
WatermarkedImage,
};
pub fn embed_text(
image: ImageBufferRef<'_>,
text: &str,
config: &WatermarkConfig,
) -> Result<WatermarkedImage, WatermarkError> {
embed_payload_kind(image, text.as_bytes(), payload::TYPE_TEXT_UTF8, config)
}
pub fn embed_payload(
image: ImageBufferRef<'_>,
payload_bytes: &[u8],
config: &WatermarkConfig,
) -> Result<WatermarkedImage, WatermarkError> {
embed_payload_kind(image, payload_bytes, payload::TYPE_BINARY, config)
}
pub fn capacity(
image: ImageBufferRef<'_>,
config: &WatermarkConfig,
) -> Result<CapacityInfo, WatermarkError> {
color::validate_image(image)?;
config.validate()?;
let tile_count = tile_count(image);
let repetition_factor = config.robustness.repetitions();
let usable_bits = tile_count / repetition_factor;
let usable_bytes = usable_bits / 8;
let max_payload_bytes = usable_bytes.saturating_sub(payload::HEADER_LEN);
Ok(CapacityInfo {
width: image.width,
height: image.height,
tile_count,
repetition_factor,
usable_bits,
frame_overhead_bytes: payload::HEADER_LEN,
max_payload_bytes,
})
}
pub fn estimate_robust_capacity(
image: ImageBufferRef<'_>,
config: &WatermarkConfig,
) -> Result<CapacityInfo, WatermarkError> {
capacity(image, config)
}
pub(crate) fn tile_count(image: ImageBufferRef<'_>) -> usize {
(image.width as usize / 8) * (image.height as usize / 8)
}
fn embed_payload_kind(
image: ImageBufferRef<'_>,
payload_bytes: &[u8],
kind: u8,
config: &WatermarkConfig,
) -> Result<WatermarkedImage, WatermarkError> {
color::validate_image(image)?;
config.validate()?;
let frame = payload::build_frame(payload_bytes, kind, &config.key)?;
let repetition_factor = config.robustness.repetitions();
let required_bits = frame
.len()
.checked_mul(8)
.and_then(|bits| bits.checked_mul(repetition_factor))
.ok_or(WatermarkError::CapacityExceeded {
required_bits: usize::MAX,
available_bits: tile_count(image),
})?;
let available_bits = tile_count(image);
if required_bits > available_bits {
return Err(WatermarkError::CapacityExceeded {
required_bits,
available_bits,
});
}
let mut output = image.data.to_vec();
let schedule = prng::KeySchedule::derive(&config.key);
let positions = prng::shuffled_indices(available_bits, &schedule.placement, b"tile-order-v1");
let margin = config.strength.coefficient_margin();
for bit_index in 0..(frame.len() * 8) {
let bit = payload::byte_bit(&frame, bit_index);
let base = bit_index * repetition_factor;
for repetition in 0..repetition_factor {
let tile_index = positions[base + repetition];
embed_bit_in_tile(&mut output, image, tile_index, bit, margin);
}
}
Ok(WatermarkedImage {
data: output,
width: image.width,
height: image.height,
stride: image.stride,
pixel_format: image.pixel_format,
})
}
fn embed_bit_in_tile(
data: &mut [u8],
image: ImageBufferRef<'_>,
tile_index: usize,
bit: bool,
margin: f32,
) {
let tiles_x = image.width as usize / 8;
let tile_x = tile_index % tiles_x;
let tile_y = tile_index / tiles_x;
let luma_tile = read_luma_tile(data, image, tile_x, tile_y);
let mut haar = haar::forward_haar_8x8(&luma_tile);
let mut freq = dct::dct4(&haar.ll);
enforce_bit_margin(&mut freq, bit, margin);
haar.ll = dct::idct4(&freq);
let modified = haar::inverse_haar_8x8(&haar);
apply_luma_tile(data, image, tile_x, tile_y, &luma_tile, &modified);
}
pub(crate) fn read_bit_from_tile(
data: &[u8],
image: ImageBufferRef<'_>,
tile_index: usize,
) -> bool {
let tiles_x = image.width as usize / 8;
let tile_x = tile_index % tiles_x;
let tile_y = tile_index / tiles_x;
let luma_tile = read_luma_tile(data, image, tile_x, tile_y);
let haar = haar::forward_haar_8x8(&luma_tile);
let freq = dct::dct4(&haar.ll);
coefficient_diff(&freq) >= 0.0
}
#[allow(clippy::needless_range_loop)]
fn read_luma_tile(
data: &[u8],
image: ImageBufferRef<'_>,
tile_x: usize,
tile_y: usize,
) -> [[f32; 8]; 8] {
let mut tile = [[0.0; 8]; 8];
let channels = image.pixel_format.channels();
let base_x = tile_x * 8;
let base_y = tile_y * 8;
for y in 0..8 {
let row = (base_y + y) * image.stride;
for x in 0..8 {
let offset = row + (base_x + x) * channels;
tile[y][x] = color::luma(&data[offset..offset + channels], image.pixel_format);
}
}
tile
}
fn apply_luma_tile(
data: &mut [u8],
image: ImageBufferRef<'_>,
tile_x: usize,
tile_y: usize,
original: &[[f32; 8]; 8],
modified: &[[f32; 8]; 8],
) {
let channels = image.pixel_format.channels();
let base_x = tile_x * 8;
let base_y = tile_y * 8;
for y in 0..8 {
let row = (base_y + y) * image.stride;
for x in 0..8 {
let offset = row + (base_x + x) * channels;
let target = modified[y][x].clamp(0.0, 255.0);
let delta = target - original[y][x];
color::add_luma_delta(
&mut data[offset..offset + channels],
image.pixel_format,
delta,
);
}
}
}
fn enforce_bit_margin(freq: &mut [[f32; 4]; 4], bit: bool, margin: f32) {
let diff = coefficient_diff(freq);
if bit {
if diff < margin {
let adjustment = (margin - diff) * 0.5;
freq[1][2] += adjustment;
freq[2][1] -= adjustment;
}
} else if diff > -margin {
let adjustment = (margin + diff) * 0.5;
freq[1][2] -= adjustment;
freq[2][1] += adjustment;
}
}
fn coefficient_diff(freq: &[[f32; 4]; 4]) -> f32 {
freq[1][2] - freq[2][1]
}