@contentauth/c2pa-node 0.10.1 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Cargo.toml +1 -1
- package/README.md +71 -55
- package/dist/Builder.js +6 -32
- package/dist/Builder.spec.js +74 -71
- package/dist/IdentityAssertion.spec.js +82 -4
- package/dist/Reader.js +9 -13
- package/dist/Reader.spec.js +4 -27
- package/dist/Settings.js +0 -39
- package/dist/Settings.spec.js +1 -79
- package/dist/Signer.spec.js +1 -1
- package/dist/Trustmark.js +14 -0
- package/dist/Trustmark.spec.js +247 -9
- package/dist/index.node +2 -2
- package/dist/types/Builder.d.ts +4 -20
- package/dist/types/Reader.d.ts +6 -10
- package/dist/types/Settings.d.ts +0 -21
- package/dist/types/Trustmark.d.ts +12 -0
- package/dist/types/types.d.ts +22 -30
- package/package.json +3 -3
- package/src/lib.rs +1 -0
- package/src/neon_identity_assertion_signer.rs +12 -3
- package/src/neon_trustmark.rs +126 -8
|
@@ -5,4 +5,16 @@ export declare class Trustmark implements TrustmarkInterface {
|
|
|
5
5
|
static newTrustmark(config: TrustmarkConfig): Promise<Trustmark>;
|
|
6
6
|
encode(image: Buffer, strength: number, watermark?: string): Promise<Buffer>;
|
|
7
7
|
decode(image: Buffer): Promise<string>;
|
|
8
|
+
/**
|
|
9
|
+
* Remove a P/Q watermark from an 8-bit JPEG or PNG, returning an encoded PNG
|
|
10
|
+
* with the same dimensions and alpha. Requires `loadRemover: true`.
|
|
11
|
+
* Removal is lossy and does not guarantee restoration of the original pixels.
|
|
12
|
+
* Input is limited to 64 MiB, 16,384 pixels per dimension, and 32 million
|
|
13
|
+
* pixels, with a best-effort 128 MiB decoder allocation budget. Limits are
|
|
14
|
+
* per call, not a cap on total process memory.
|
|
15
|
+
* Image metadata (EXIF, XMP, ICC profiles, and C2PA manifests) is not copied.
|
|
16
|
+
* Existing credentials do not remain valid for the modified pixels; sign a
|
|
17
|
+
* new manifest to attach valid Content Credentials to the output.
|
|
18
|
+
*/
|
|
19
|
+
remove(image: Buffer): Promise<Buffer>;
|
|
8
20
|
}
|
package/dist/types/types.d.ts
CHANGED
|
@@ -194,12 +194,6 @@ export interface HashedUri {
|
|
|
194
194
|
alg?: string;
|
|
195
195
|
}
|
|
196
196
|
|
|
197
|
-
/**
|
|
198
|
-
* Optional settings that can be provided when creating a Reader or Builder.
|
|
199
|
-
* Can be a JSON string or a settings object.
|
|
200
|
-
*/
|
|
201
|
-
export type C2paSettings = string | object;
|
|
202
|
-
|
|
203
197
|
export interface BuilderInterface {
|
|
204
198
|
/**
|
|
205
199
|
* An intent lets the API know what kind of manifest to create.
|
|
@@ -529,34 +523,32 @@ export interface TrustmarkInterface {
|
|
|
529
523
|
* @param image image to extract the watermark from (must be in a supported image format like JPEG, PNG, etc.)
|
|
530
524
|
*/
|
|
531
525
|
decode(image: Buffer): Promise<string>;
|
|
526
|
+
|
|
527
|
+
/**
|
|
528
|
+
* Remove a P/Q watermark using the separately loaded removal model.
|
|
529
|
+
* Requires `loadRemover: true`; rejects unsupported input and removal failures.
|
|
530
|
+
* Removal is lossy, may change unwatermarked images, and does not guarantee
|
|
531
|
+
* restoration of original pixels or removal of arbitrary watermarks.
|
|
532
|
+
* Rejects inputs exceeding 64 MiB, 16,384 pixels per dimension, or 32 million
|
|
533
|
+
* pixels. Decoding has a best-effort 128 MiB allocation budget; limits are
|
|
534
|
+
* per call, not a cap on total process memory.
|
|
535
|
+
* Image metadata (EXIF, XMP, ICC profiles, and C2PA manifests) is not copied.
|
|
536
|
+
* Existing credentials do not remain valid for the modified pixels; sign a
|
|
537
|
+
* new manifest to attach valid Content Credentials to the output.
|
|
538
|
+
* @param image An 8-bit JPEG or PNG (RGB, RGBA, or grayscale)
|
|
539
|
+
* @returns Encoded PNG preserving dimensions and alpha, not raw pixel data
|
|
540
|
+
*/
|
|
541
|
+
remove(image: Buffer): Promise<Buffer>;
|
|
532
542
|
}
|
|
533
543
|
|
|
534
544
|
export interface TrustmarkConfig {
|
|
535
545
|
variant: TrustmarkVariant;
|
|
536
546
|
version: TrustmarkVersion;
|
|
537
547
|
modelPath?: string;
|
|
548
|
+
/**
|
|
549
|
+
* Load/download the separate `remover_P.onnx` or `remover_Q.onnx` model.
|
|
550
|
+
* Only supported for P/Q. Defaults to false to avoid additional downloads,
|
|
551
|
+
* startup time, and memory usage. Uses the same `modelPath` as encode/decode.
|
|
552
|
+
*/
|
|
553
|
+
loadRemover?: boolean;
|
|
538
554
|
}
|
|
539
|
-
|
|
540
|
-
import type {
|
|
541
|
-
Settings as _Settings,
|
|
542
|
-
TrustSettings as _TrustSettings,
|
|
543
|
-
VerifySettings as _VerifySettings
|
|
544
|
-
} from "@contentauth/c2pa-utilities";
|
|
545
|
-
|
|
546
|
-
/**
|
|
547
|
-
* @deprecated Use `TrustSettings` instead, which encapsulates both normal trust and CAWG trust settings.
|
|
548
|
-
* Kept as an alias for backwards compatibility.
|
|
549
|
-
*/
|
|
550
|
-
export type TrustConfig = _TrustSettings;
|
|
551
|
-
|
|
552
|
-
/**
|
|
553
|
-
* @deprecated Use `VerifySettings` instead.
|
|
554
|
-
* Kept as an alias for backwards compatibility.
|
|
555
|
-
*/
|
|
556
|
-
export type VerifyConfig = _VerifySettings;
|
|
557
|
-
|
|
558
|
-
/**
|
|
559
|
-
* @deprecated Use `Settings` instead.
|
|
560
|
-
* Kept as an alias for backwards compatibility.
|
|
561
|
-
*/
|
|
562
|
-
export type SettingsContext = _Settings;
|
package/package.json
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
"url": "git+https://github.com/contentauth/c2pa-js.git",
|
|
6
6
|
"directory": "packages/c2pa-node"
|
|
7
7
|
},
|
|
8
|
-
"version": "0.
|
|
8
|
+
"version": "0.12.0",
|
|
9
9
|
"description": "Node.js bindings for C2PA",
|
|
10
10
|
"main": "dist/index.js",
|
|
11
11
|
"type": "module",
|
|
@@ -64,8 +64,8 @@
|
|
|
64
64
|
"pkg-dir": "^8.0.0",
|
|
65
65
|
"pretty-bytes": "^6.1.1",
|
|
66
66
|
"unzipper": "^0.10.14",
|
|
67
|
-
"@contentauth/c2pa-types": "0.
|
|
68
|
-
"@contentauth/c2pa-utilities": "0.5.
|
|
67
|
+
"@contentauth/c2pa-types": "0.8.0",
|
|
68
|
+
"@contentauth/c2pa-utilities": "0.5.1"
|
|
69
69
|
},
|
|
70
70
|
"nx": {
|
|
71
71
|
"tags": [
|
package/src/lib.rs
CHANGED
|
@@ -214,6 +214,7 @@ fn main(mut cx: ModuleContext) -> NeonResult<()> {
|
|
|
214
214
|
)?;
|
|
215
215
|
cx.export_function("trustmarkEncode", neon_trustmark::NeonTrustmark::encode)?;
|
|
216
216
|
cx.export_function("trustmarkDecode", neon_trustmark::NeonTrustmark::decode)?;
|
|
217
|
+
cx.export_function("trustmarkRemove", neon_trustmark::NeonTrustmark::remove)?;
|
|
217
218
|
|
|
218
219
|
Ok(())
|
|
219
220
|
}
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
// each license.
|
|
13
13
|
|
|
14
14
|
use std::ops::Deref;
|
|
15
|
-
use std::sync::RwLock;
|
|
15
|
+
use std::sync::{PoisonError, RwLock};
|
|
16
16
|
|
|
17
17
|
use async_trait::async_trait;
|
|
18
18
|
use c2pa::{
|
|
@@ -135,9 +135,18 @@ impl AsyncSigner for NeonIdentityAssertionSigner {
|
|
|
135
135
|
c2pa::AsyncSigner::timestamp_request_body(&*self.signer.read().unwrap(), message)
|
|
136
136
|
}
|
|
137
137
|
|
|
138
|
-
async fn send_timestamp_request(
|
|
138
|
+
async fn send_timestamp_request(
|
|
139
|
+
&self,
|
|
140
|
+
message: &[u8],
|
|
141
|
+
context: &c2pa::Context,
|
|
142
|
+
) -> Option<Result<Vec<u8>, c2pa::Error>> {
|
|
139
143
|
let signer = self.signer.read().unwrap().clone();
|
|
140
|
-
c2pa::AsyncSigner::send_timestamp_request(&signer, message).await
|
|
144
|
+
c2pa::AsyncSigner::send_timestamp_request(&signer, message, context).await
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
fn direct_cose_handling(&self) -> bool {
|
|
148
|
+
let signer = self.signer.read().unwrap_or_else(PoisonError::into_inner);
|
|
149
|
+
c2pa::AsyncSigner::direct_cose_handling(&*signer)
|
|
141
150
|
}
|
|
142
151
|
|
|
143
152
|
fn dynamic_assertions(&self) -> Vec<Box<dyn AsyncDynamicAssertion>> {
|
package/src/neon_trustmark.rs
CHANGED
|
@@ -13,18 +13,19 @@
|
|
|
13
13
|
|
|
14
14
|
use std::{
|
|
15
15
|
fs::OpenOptions,
|
|
16
|
-
io::Write,
|
|
16
|
+
io::{Cursor, Write},
|
|
17
17
|
path::PathBuf,
|
|
18
18
|
sync::{Arc, Mutex},
|
|
19
19
|
time::Duration,
|
|
20
20
|
};
|
|
21
21
|
|
|
22
|
+
use image::ImageDecoder as _;
|
|
22
23
|
use neon::prelude::*;
|
|
23
24
|
use neon::result::{JsResult, NeonResult};
|
|
24
25
|
use neon::types::{buffer::TypedArray, Finalize, JsObject};
|
|
25
26
|
use rand::{distributions::Standard, prelude::Distribution as _};
|
|
26
27
|
use reqwest::Client;
|
|
27
|
-
use trustmark::{Trustmark, Variant, Version};
|
|
28
|
+
use trustmark::{Trustmark, Variant, Version, WatermarkRemover};
|
|
28
29
|
|
|
29
30
|
use crate::error::{as_js_error, as_js_error_fn, Error, Result};
|
|
30
31
|
use crate::runtime::runtime;
|
|
@@ -34,12 +35,14 @@ pub struct WatermarkConfig {
|
|
|
34
35
|
variant: Variant,
|
|
35
36
|
version: Version,
|
|
36
37
|
model_path: PathBuf,
|
|
38
|
+
load_remover: bool,
|
|
37
39
|
}
|
|
38
40
|
|
|
39
41
|
pub struct NeonTrustmark {
|
|
40
42
|
// All Trustmark calls are synchronous, so the standard Mutex works.
|
|
41
43
|
trustmark: Arc<Mutex<Trustmark>>,
|
|
42
44
|
version: Version,
|
|
45
|
+
remover: Option<Arc<WatermarkRemover>>,
|
|
43
46
|
}
|
|
44
47
|
|
|
45
48
|
impl Finalize for NeonTrustmark {}
|
|
@@ -79,10 +82,19 @@ pub fn parse_watermark_config(
|
|
|
79
82
|
.map(|val| std::path::PathBuf::from(val.value(cx)))
|
|
80
83
|
.unwrap_or_else(|| PathBuf::from("./models"));
|
|
81
84
|
|
|
85
|
+
let load_remover = obj
|
|
86
|
+
.get_opt::<JsBoolean, _, _>(cx, "loadRemover")?
|
|
87
|
+
.is_some_and(|value| value.value(cx));
|
|
88
|
+
|
|
89
|
+
if load_remover && !matches!(variant, Variant::P | Variant::Q) {
|
|
90
|
+
return cx.throw_error("Watermark configuration error: removal supports only P/Q variants");
|
|
91
|
+
}
|
|
92
|
+
|
|
82
93
|
Ok(WatermarkConfig {
|
|
83
94
|
variant,
|
|
84
95
|
version,
|
|
85
96
|
model_path,
|
|
97
|
+
load_remover,
|
|
86
98
|
})
|
|
87
99
|
}
|
|
88
100
|
|
|
@@ -110,9 +122,25 @@ impl NeonTrustmark {
|
|
|
110
122
|
Err(err) => return as_js_error_fn(&mut cx, err.into()).and_then(|err| cx.throw(err)),
|
|
111
123
|
};
|
|
112
124
|
|
|
125
|
+
let remover = if config.load_remover {
|
|
126
|
+
let result = fetch_models(
|
|
127
|
+
&config.model_path,
|
|
128
|
+
&[format!("remover_{}.onnx", config.variant)],
|
|
129
|
+
)
|
|
130
|
+
.and_then(|path| WatermarkRemover::new(path, config.variant).map_err(Error::Watermark));
|
|
131
|
+
|
|
132
|
+
match result {
|
|
133
|
+
Ok(remover) => Some(Arc::new(remover)),
|
|
134
|
+
Err(err) => return as_js_error_fn(&mut cx, err).and_then(|err| cx.throw(err)),
|
|
135
|
+
}
|
|
136
|
+
} else {
|
|
137
|
+
None
|
|
138
|
+
};
|
|
139
|
+
|
|
113
140
|
Ok(cx.boxed(Self {
|
|
114
141
|
trustmark: Arc::new(Mutex::new(trustmark)),
|
|
115
142
|
version: config.version,
|
|
143
|
+
remover,
|
|
116
144
|
}))
|
|
117
145
|
}
|
|
118
146
|
|
|
@@ -156,6 +184,31 @@ impl NeonTrustmark {
|
|
|
156
184
|
Ok(promise)
|
|
157
185
|
}
|
|
158
186
|
|
|
187
|
+
pub fn remove(mut cx: FunctionContext) -> JsResult<JsPromise> {
|
|
188
|
+
let this = cx.this::<JsBox<Self>>()?;
|
|
189
|
+
let Some(remover) = this.remover.clone() else {
|
|
190
|
+
return cx.throw_error(
|
|
191
|
+
"Watermark remover not loaded; initialize Trustmark with loadRemover: true",
|
|
192
|
+
);
|
|
193
|
+
};
|
|
194
|
+
let buffer = cx.argument::<JsBuffer>(0)?;
|
|
195
|
+
let image_bytes = buffer.as_slice(&cx);
|
|
196
|
+
if image_bytes.len() > MAX_REMOVAL_INPUT_BYTES {
|
|
197
|
+
let error = Error::WatermarkConfiguration("removal input exceeds 64 MiB".to_string());
|
|
198
|
+
return as_js_error_fn(&mut cx, error).and_then(|err| cx.throw(err));
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
let image_bytes = image_bytes.to_vec();
|
|
202
|
+
let promise = cx
|
|
203
|
+
.task(move || remove_image(&image_bytes, &remover))
|
|
204
|
+
.promise(move |mut cx, result: Result<Vec<u8>>| match result {
|
|
205
|
+
Ok(png) => JsBuffer::from_slice(&mut cx, &png),
|
|
206
|
+
Err(err) => as_js_error(&mut cx, err).and_then(|err| cx.throw(err)),
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
Ok(promise)
|
|
210
|
+
}
|
|
211
|
+
|
|
159
212
|
pub fn decode(mut cx: FunctionContext) -> JsResult<JsPromise> {
|
|
160
213
|
let this = cx.this::<JsBox<Self>>()?;
|
|
161
214
|
let trustmark = this.trustmark.clone();
|
|
@@ -177,6 +230,65 @@ impl NeonTrustmark {
|
|
|
177
230
|
}
|
|
178
231
|
}
|
|
179
232
|
|
|
233
|
+
// Bound the input copy, decoding, and the full-resolution buffers allocated by
|
|
234
|
+
// removal. These are per-call limits, not a process-wide concurrency budget.
|
|
235
|
+
const MAX_REMOVAL_INPUT_BYTES: usize = 64 * 1024 * 1024;
|
|
236
|
+
const MAX_REMOVAL_DIMENSION: u32 = 16_384;
|
|
237
|
+
const MAX_REMOVAL_PIXELS: u64 = 32_000_000;
|
|
238
|
+
const MAX_REMOVAL_DECODE_BYTES: u64 = 128 * 1024 * 1024;
|
|
239
|
+
|
|
240
|
+
fn remove_image(bytes: &[u8], remover: &WatermarkRemover) -> Result<Vec<u8>> {
|
|
241
|
+
let format = image::guess_format(bytes)?;
|
|
242
|
+
if !matches!(format, image::ImageFormat::Jpeg | image::ImageFormat::Png) {
|
|
243
|
+
return Err(Error::WatermarkConfiguration(
|
|
244
|
+
"removal supports only 8-bit JPEG/PNG images".to_string(),
|
|
245
|
+
));
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
let mut limits = image::Limits::default();
|
|
249
|
+
limits.max_image_width = Some(MAX_REMOVAL_DIMENSION);
|
|
250
|
+
limits.max_image_height = Some(MAX_REMOVAL_DIMENSION);
|
|
251
|
+
limits.max_alloc = Some(MAX_REMOVAL_DECODE_BYTES);
|
|
252
|
+
let mut reader = image::ImageReader::with_format(Cursor::new(bytes), format);
|
|
253
|
+
reader.limits(limits.clone());
|
|
254
|
+
let mut decoder = reader.into_decoder()?;
|
|
255
|
+
let (width, height) = decoder.dimensions();
|
|
256
|
+
if u64::from(width) * u64::from(height) > MAX_REMOVAL_PIXELS {
|
|
257
|
+
return Err(Error::WatermarkConfiguration(
|
|
258
|
+
"removal image exceeds 32000000 pixels".to_string(),
|
|
259
|
+
));
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
if !matches!(
|
|
263
|
+
decoder.color_type(),
|
|
264
|
+
image::ColorType::L8
|
|
265
|
+
| image::ColorType::La8
|
|
266
|
+
| image::ColorType::Rgb8
|
|
267
|
+
| image::ColorType::Rgba8
|
|
268
|
+
) {
|
|
269
|
+
return Err(Error::WatermarkConfiguration(
|
|
270
|
+
"removal supports only 8-bit JPEG/PNG images".to_string(),
|
|
271
|
+
));
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// Include the decoded image buffer in the allocation budget, as
|
|
275
|
+
// ImageReader::decode does. from_decoder does not reserve it itself.
|
|
276
|
+
limits.reserve(decoder.total_bytes())?;
|
|
277
|
+
decoder.set_limits(limits)?;
|
|
278
|
+
let image = image::DynamicImage::from_decoder(decoder)?;
|
|
279
|
+
let has_alpha = image.color().has_alpha();
|
|
280
|
+
let recovered = remover.remove(image).map_err(Error::Watermark)?;
|
|
281
|
+
let output = if has_alpha {
|
|
282
|
+
image::DynamicImage::ImageRgba8(recovered.to_rgba8())
|
|
283
|
+
} else {
|
|
284
|
+
image::DynamicImage::ImageRgb8(recovered.to_rgb8())
|
|
285
|
+
};
|
|
286
|
+
let mut png = Cursor::new(Vec::new());
|
|
287
|
+
output.write_to(&mut png, image::ImageFormat::Png)?;
|
|
288
|
+
|
|
289
|
+
Ok(png.into_inner())
|
|
290
|
+
}
|
|
291
|
+
|
|
180
292
|
fn check_strength(strength: f32) -> Result<()> {
|
|
181
293
|
if !(0.0..=1.0).contains(&strength) {
|
|
182
294
|
return Err(Error::WatermarkConfiguration(
|
|
@@ -195,6 +307,16 @@ const MAX_RETRIES: u32 = 3;
|
|
|
195
307
|
// Taken from Trustmark xtask.
|
|
196
308
|
// Will not overwrite models if they already exist.
|
|
197
309
|
pub fn fetch_model(variant: Variant, dir_path: &std::path::Path) -> Result<PathBuf> {
|
|
310
|
+
fetch_models(
|
|
311
|
+
dir_path,
|
|
312
|
+
&[
|
|
313
|
+
format!("encoder_{variant}.onnx"),
|
|
314
|
+
format!("decoder_{variant}.onnx"),
|
|
315
|
+
],
|
|
316
|
+
)
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
fn fetch_models(dir_path: &std::path::Path, filenames: &[String]) -> Result<PathBuf> {
|
|
198
320
|
let rt = runtime();
|
|
199
321
|
let client = Client::builder()
|
|
200
322
|
.timeout(Duration::from_secs(120))
|
|
@@ -208,12 +330,8 @@ pub fn fetch_model(variant: Variant, dir_path: &std::path::Path) -> Result<PathB
|
|
|
208
330
|
|
|
209
331
|
let root = "https://cai-watermark.adobe.net/watermarking/trustmark-models";
|
|
210
332
|
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
let decoder_filename = format!("decoder_{variant}.onnx");
|
|
214
|
-
|
|
215
|
-
for filename in [encoder_filename, decoder_filename] {
|
|
216
|
-
let model_path = dir_path.join(&filename);
|
|
333
|
+
for filename in filenames {
|
|
334
|
+
let model_path = dir_path.join(filename);
|
|
217
335
|
if model_path.exists() {
|
|
218
336
|
continue;
|
|
219
337
|
}
|