1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Copyright 2026 Mahmoud Harmouch.
//
// Licensed under the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
//! # Layer Pixel: Pixel-Domain Watermark Scrubbing
//!
//! This module implements a decode-then-re-encode pipeline that neutralises
//! pixel-domain AI watermarks such as **SynthID-Image**, **StegaStamp**,
//! **Tree-Ring**, and **StableSignature**.
//!
//! ## How It Works
//!
//! Invisible pixel watermarks work by perturbing individual pixel values by an
//! amount that is imperceptible to the human eye but detectable by a matched
//! neural detector. The key insight is that the perturbation signal is encoded
//! in the *frequency* or *spatial* domain of the specific output compression
//! stream. Re-encoding the image from raw pixels-without the original
//! compression state-destroys the signal while preserving the visible content:
//!
//! ```text
//! Input bytes โ decode to raw RGBA u8 pixels โ encode as lossless PNG โ Output bytes
//! ```
//!
//! The new file contains only the visible pixel values; the perturbation that required
//! access to the model's internal state cannot survive the round-trip.
//!
//! ## Supported Input Formats
//!
//! | Format | Decoder | Notes |
//! |--------|---------|-------|
//! | PNG | `image::io::Reader` | Lossless; pixel values preserved exactly |
//! | JPEG | `image::io::Reader` | Lossy decode; re-encoded as PNG (lossless) |
//! | WebP | `image::io::Reader` | Lossless & lossy variants both handled |
//!
//! ## Output Format
//!
//! Output is always **PNG** regardless of input format. PNG is chosen because
//! it is lossless-the re-encoded pixels are identical to the decoded values,
//! ensuring no additional quality loss beyond the original decode step.
//!
//! ## Limitations
//!
//! - Does **not** handle steganographic watermarks that survive a lossy
//! JPEG-style quantisation step (those require a separate frequency-domain
//! filter).
//! - Not available on `wasm32` targets (no file-system I/O and binary size
//! constraints make the `image` crate unsuitable for WASM).
//!
//! ## Performance
//!
//! Decoding and re-encoding are both O(w ร h) in the number of pixels.
//! Memory usage is also O(w ร h) for the intermediate RGBA buffer.
//!
//! ## Example
//!
//! ```no_run
//! use cum_rs::pixel_scrub::scrub_pixels;
//!
//! let png_bytes = std::fs::read("input.png").unwrap();
//! let clean = scrub_pixels(&png_bytes).unwrap();
//! std::fs::write("output.png", &clean).unwrap();
//! ```
use ImageFormat;
use ImageReader;
use Error;
use fmt;
use Cursor;
/// Error type for pixel-scrubbing operations.
///
/// Wraps lower-level decoding and encoding errors from the `image` crate with
/// a human-readable context string.
/// Decodes an image from `bytes`, converts it to raw RGBA pixels, and
/// re-encodes it as a lossless PNG.
///
/// The format is auto-detected from the byte stream magic bytes. Supported
/// input formats are PNG, JPEG, and WebP. The output is always PNG.
///
/// This operation strips any pixel-domain watermark that depends on the
/// original compression context (SynthID-Image, StegaStamp, Tree-Ring,
/// StableSignature) by forcing a fresh compression pass over raw pixel data.
///
/// # Arguments
///
/// * `bytes` - Raw bytes of the source image (PNG, JPEG, or WebP).
///
/// # Returns
///
/// `Ok(Vec<u8>)` containing the re-encoded PNG bytes on success, or a
/// [`ScrubError`] if the input cannot be decoded.
///
/// # Errors
///
/// Returns [`ScrubError`] when:
/// - The input bytes are empty.
/// - The format is not recognised or not supported.
/// - The image decoder reports a malformed stream.
/// - PNG encoding fails (should not occur for valid pixel buffers).
///
/// # Examples
///
/// ```no_run
/// use cum_rs::pixel_scrub::scrub_pixels;
///
/// let png = std::fs::read("photo.png").unwrap();
/// let scrubbed = scrub_pixels(&png).unwrap();
/// assert!(scrubbed.starts_with(b"\x89PNG"));
/// ```
///
/// # Time Complexity
///
/// O(w ร h) where w and h are the pixel dimensions of the image.
///
/// # Space Complexity
///
/// O(w ร h) for the intermediate RGBA buffer and the output PNG bytes.
/// Returns `true` when the byte slice begins with the PNG magic signature
/// (`\x89PNG\r\n\x1a\n`).
///
/// Used in tests and by callers to confirm that [`scrub_pixels`] produced a
/// valid PNG stream.
///
/// # Arguments
///
/// * `bytes` - Bytes to inspect.
///
/// # Returns
///
/// `true` if `bytes` starts with the PNG magic bytes.
///
/// # Time Complexity
///
/// O(1).
///
/// # Space Complexity
///
/// O(1).
// Copyright 2026 Mahmoud Harmouch.
//
// Licensed under the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.