chromaframe-sdk 0.1.1

Deterministic, privacy-preserving color measurement and ranking SDK
Documentation
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
use crate::color::srgb_channel_to_linear;
use crate::score::clamp01;
use crate::types::{CaptureQualityReport, MeasurementMode, QualityCheck};
use image::{DynamicImage, GenericImageView, ImageReader};
use std::fmt;
use std::io::Cursor;
use thiserror::Error;

#[derive(Debug, Error)]
pub enum ImageError {
    #[error("image decode failed: {0}")]
    DecodeFailed(String),
    #[error("image exceeds decode pixel limit")]
    DecodeLimitExceeded,
    #[error("image has no usable opaque pixels")]
    NoUsablePixels,
    #[error("color profile transform failed")]
    ColorProfileTransformFailed,
}

const MAX_DECODE_PIXELS: u64 = 24_000_000;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum IccProfileState {
    NotEmbedded,
    EmbeddedUnsupported,
    TransformSucceeded,
    TransformFailed,
}

#[derive(Clone)]
pub struct NormalizedImage {
    pub width: u32,
    pub height: u32,
    pub pixels: Vec<LinearPixel>,
    pub measurement_mode: MeasurementMode,
    pub orientation_applied: bool,
    pub icc_status: String,
}

impl fmt::Debug for NormalizedImage {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("NormalizedImage")
            .field("dimensions", &(self.width, self.height))
            .field("pixel_count", &self.pixels.len())
            .field("measurement_mode", &self.measurement_mode)
            .field("orientation_applied", &self.orientation_applied)
            .field("icc_status", &self.icc_status)
            .field("metadata_retained", &false)
            .finish()
    }
}

#[derive(Clone, Copy)]
pub struct LinearPixel {
    pub r: f32,
    pub g: f32,
    pub b: f32,
    pub y: f32,
    pub opaque: bool,
}

impl fmt::Debug for LinearPixel {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("LinearPixel")
            .field("channels", &"[REDACTED]")
            .field("opaque", &self.opaque)
            .finish()
    }
}

pub fn decode_image(
    bytes: &[u8],
    allow_apparent_color_fallback: bool,
) -> Result<NormalizedImage, ImageError> {
    if bytes.is_empty() {
        return Err(ImageError::NoUsablePixels);
    }
    let icc_profile_state = detect_encoded_icc_profile_state(bytes, allow_apparent_color_fallback);
    if let Some((width, height)) = png_header_dimensions(bytes) {
        reject_oversized_dimensions(width, height)?;
    }
    let reader = ImageReader::new(Cursor::new(bytes))
        .with_guessed_format()
        .map_err(|err| ImageError::DecodeFailed(err.to_string()))?;
    let (width, height) = reader
        .into_dimensions()
        .map_err(|err| ImageError::DecodeFailed(err.to_string()))?;
    reject_oversized_dimensions(width, height)?;
    let reader = ImageReader::new(Cursor::new(bytes))
        .with_guessed_format()
        .map_err(|err| ImageError::DecodeFailed(err.to_string()))?;
    let image = reader
        .decode()
        .map_err(|err| ImageError::DecodeFailed(err.to_string()))?;
    normalize_dynamic_image_with_profile_state(
        &image,
        icc_profile_state,
        allow_apparent_color_fallback,
    )
}

fn png_header_dimensions(bytes: &[u8]) -> Option<(u32, u32)> {
    if bytes.len() < 24 {
        return None;
    }
    if &bytes[0..8] != b"\x89PNG\r\n\x1a\n" || &bytes[12..16] != b"IHDR" {
        return None;
    }
    let width = u32::from_be_bytes(bytes[16..20].try_into().ok()?);
    let height = u32::from_be_bytes(bytes[20..24].try_into().ok()?);
    Some((width, height))
}

pub fn normalize_dynamic_image(
    image: &DynamicImage,
    allow_apparent_color_fallback: bool,
) -> Result<NormalizedImage, ImageError> {
    normalize_dynamic_image_with_profile_state(
        image,
        IccProfileState::NotEmbedded,
        allow_apparent_color_fallback,
    )
}

pub fn normalize_dynamic_image_with_profile_state(
    image: &DynamicImage,
    icc_profile_state: IccProfileState,
    allow_apparent_color_fallback: bool,
) -> Result<NormalizedImage, ImageError> {
    let (width, height) = image.dimensions();
    reject_oversized_dimensions(width, height)?;
    let (measurement_mode, icc_status) =
        resolve_measurement_mode(icc_profile_state, allow_apparent_color_fallback)?;
    let rgba = image.to_rgba8();
    let mut pixels = Vec::with_capacity((width * height) as usize);
    let mut opaque_count = 0usize;
    for pixel in rgba.pixels() {
        let [r8, g8, b8, a8] = pixel.0;
        let opaque = a8 > 0;
        if opaque {
            opaque_count += 1;
        }
        let r = f32::from(r8) / 255.0;
        let g = f32::from(g8) / 255.0;
        let b = f32::from(b8) / 255.0;
        let y = linear_luminance(r, g, b);
        pixels.push(LinearPixel { r, g, b, y, opaque });
    }
    if opaque_count == 0 {
        return Err(ImageError::NoUsablePixels);
    }
    Ok(NormalizedImage {
        width,
        height,
        pixels,
        measurement_mode,
        orientation_applied: false,
        icc_status: icc_status.to_string(),
    })
}

pub fn resolve_measurement_mode(
    icc_profile_state: IccProfileState,
    allow_apparent_color_fallback: bool,
) -> Result<(MeasurementMode, &'static str), ImageError> {
    match icc_profile_state {
        IccProfileState::NotEmbedded => Ok((MeasurementMode::SrgbAssumed, "srgb_assumed")),
        IccProfileState::EmbeddedUnsupported => Ok((
            MeasurementMode::ApparentColorProfileUnsupported,
            "embedded_icc_profile_unsupported",
        )),
        IccProfileState::TransformSucceeded => {
            #[cfg(feature = "icc-lcms2")]
            {
                Ok((MeasurementMode::IccNormalized, "icc_normalized"))
            }
            #[cfg(not(feature = "icc-lcms2"))]
            {
                Ok((
                    MeasurementMode::ApparentColorProfileUnsupported,
                    "embedded_icc_profile_unsupported",
                ))
            }
        }
        IccProfileState::TransformFailed if allow_apparent_color_fallback => Ok((
            MeasurementMode::AllowedApparentFallback,
            "icc_transform_failed_apparent_fallback",
        )),
        IccProfileState::TransformFailed => Err(ImageError::ColorProfileTransformFailed),
    }
}

pub fn detect_encoded_icc_profile_state(
    bytes: &[u8],
    allow_apparent_color_fallback: bool,
) -> IccProfileState {
    if !has_encoded_icc_profile(bytes) {
        return IccProfileState::NotEmbedded;
    }
    if allow_apparent_color_fallback {
        return IccProfileState::TransformFailed;
    }
    IccProfileState::EmbeddedUnsupported
}

fn has_encoded_icc_profile(bytes: &[u8]) -> bool {
    has_png_iccp_chunk(bytes) || has_jpeg_icc_profile_segment(bytes)
}

fn has_png_iccp_chunk(bytes: &[u8]) -> bool {
    if bytes.len() < 16 || &bytes[0..8] != b"\x89PNG\r\n\x1a\n" {
        return false;
    }
    let mut offset = 8usize;
    while offset + 12 <= bytes.len() {
        let length = u32::from_be_bytes(match bytes[offset..offset + 4].try_into() {
            Ok(value) => value,
            Err(_) => return false,
        }) as usize;
        let chunk_type = &bytes[offset + 4..offset + 8];
        if chunk_type == b"iCCP" {
            return true;
        }
        let Some(next_offset) = offset
            .checked_add(12)
            .and_then(|base| base.checked_add(length))
        else {
            return false;
        };
        if next_offset > bytes.len() {
            return false;
        }
        offset = next_offset;
    }
    false
}

fn has_jpeg_icc_profile_segment(bytes: &[u8]) -> bool {
    if bytes.len() < 4 || bytes[0] != 0xff || bytes[1] != 0xd8 {
        return false;
    }
    let mut offset = 2usize;
    while offset + 4 <= bytes.len() {
        if bytes[offset] != 0xff {
            return false;
        }
        let marker = bytes[offset + 1];
        if marker == 0xda || marker == 0xd9 {
            return false;
        }
        let segment_length = u16::from_be_bytes([bytes[offset + 2], bytes[offset + 3]]) as usize;
        if segment_length < 2 || offset + 2 + segment_length > bytes.len() {
            return false;
        }
        let segment_data = &bytes[offset + 4..offset + 2 + segment_length];
        if marker == 0xe2 && segment_data.starts_with(b"ICC_PROFILE\0") {
            return true;
        }
        offset += 2 + segment_length;
    }
    false
}

fn reject_oversized_dimensions(width: u32, height: u32) -> Result<(), ImageError> {
    let pixel_count = u64::from(width) * u64::from(height);
    if pixel_count <= MAX_DECODE_PIXELS {
        return Ok(());
    }
    Err(ImageError::DecodeLimitExceeded)
}

#[must_use]
pub fn linear_luminance(r: f32, g: f32, b: f32) -> f32 {
    0.2126 * srgb_channel_to_linear(r)
        + 0.7152 * srgb_channel_to_linear(g)
        + 0.0722 * srgb_channel_to_linear(b)
}

pub fn capture_quality_report(image: &NormalizedImage) -> Result<CaptureQualityReport, ImageError> {
    let valid_pixels: Vec<_> = image
        .pixels
        .iter()
        .copied()
        .filter(|pixel| pixel.opaque)
        .collect();
    if valid_pixels.is_empty() {
        return Err(ImageError::NoUsablePixels);
    }
    let valid_count = valid_pixels.len() as f32;
    let over_clip_fraction = valid_pixels
        .iter()
        .filter(|pixel| pixel.r.max(pixel.g).max(pixel.b) >= 250.0 / 255.0 || pixel.y >= 0.98)
        .count() as f32
        / valid_count;
    let under_clip_fraction =
        valid_pixels.iter().filter(|pixel| pixel.y <= 0.02).count() as f32 / valid_count;
    let white_balance = white_balance_check(&valid_pixels);
    let blur = blur_check(image);
    let shadow = shadow_check(image, &valid_pixels);
    Ok(CaptureQualityReport {
        decode_status: "decoded".to_string(),
        orientation_applied: image.orientation_applied,
        dimensions: (image.width, image.height),
        icc_status: image.icc_status.clone(),
        metadata_retained: false,
        over_clip_fraction,
        under_clip_fraction,
        white_balance,
        blur,
        shadow,
        face_angle: QualityCheck::NotMeasured {
            reason: "deferred".to_string(),
            deduction: 0.0,
        },
        filters_or_makeup: QualityCheck::NotMeasured {
            reason: "deferred".to_string(),
            deduction: 0.0,
        },
        occlusion: QualityCheck::NotMeasured {
            reason: "deferred".to_string(),
            deduction: 0.0,
        },
        calibration_card: QualityCheck::NotMeasured {
            reason: "deferred".to_string(),
            deduction: 0.0,
        },
    })
}

fn white_balance_check(valid_pixels: &[LinearPixel]) -> QualityCheck<f32> {
    let pixels: Vec<_> = valid_pixels
        .iter()
        .filter(|pixel| pixel.r.max(pixel.g).max(pixel.b) < 250.0 / 255.0 && pixel.y > 0.02)
        .collect();
    if pixels.len() < 64 {
        return QualityCheck::NotMeasured {
            reason: "insufficient_non_clipped_pixels".to_string(),
            deduction: 0.0,
        };
    }
    let (sum_r, sum_g, sum_b) = pixels.iter().fold((0.0, 0.0, 0.0), |acc, pixel| {
        (acc.0 + pixel.r, acc.1 + pixel.g, acc.2 + pixel.b)
    });
    let count = pixels.len() as f32;
    let (mr, mg, mb) = (sum_r / count, sum_g / count, sum_b / count);
    let gray = (mr + mg + mb) / 3.0;
    let imbalance = clamp01(
        ((mr - gray)
            .abs()
            .max((mg - gray).abs())
            .max((mb - gray).abs()))
            / gray.max(1e-6),
    );
    QualityCheck::Measured {
        value: imbalance,
        deduction: crate::score::white_balance_deduction(imbalance),
    }
}

fn blur_check(image: &NormalizedImage) -> QualityCheck<f32> {
    if image.width < 3 || image.height < 3 {
        return QualityCheck::NotMeasured {
            reason: "image_too_small_for_sobel".to_string(),
            deduction: 0.0,
        };
    }
    let mut energies = Vec::new();
    let idx = |x: u32, y: u32| -> usize { (y * image.width + x) as usize };
    for y in 1..image.height - 1 {
        for x in 1..image.width - 1 {
            if !image.pixels[idx(x, y)].opaque {
                continue;
            }
            let p = |dx: i32, dy: i32| {
                image.pixels[idx((x as i32 + dx) as u32, (y as i32 + dy) as u32)].y
            };
            let gx = -p(-1, -1) + p(1, -1) - 2.0 * p(-1, 0) + 2.0 * p(1, 0) - p(-1, 1) + p(1, 1);
            let gy = -p(-1, -1) - 2.0 * p(0, -1) - p(1, -1) + p(-1, 1) + 2.0 * p(0, 1) + p(1, 1);
            energies.push(gx.mul_add(gx, gy * gy));
        }
    }
    if energies.is_empty() {
        return QualityCheck::NotMeasured {
            reason: "insufficient_sobel_pixels".to_string(),
            deduction: 0.0,
        };
    }
    let mean_energy = energies.iter().sum::<f32>() / energies.len() as f32;
    let blur_score = clamp01(mean_energy / 0.25);
    QualityCheck::Measured {
        value: blur_score,
        deduction: crate::score::blur_deduction(blur_score),
    }
}

fn shadow_check(image: &NormalizedImage, valid_pixels: &[LinearPixel]) -> QualityCheck<f32> {
    let mut tile_medians = Vec::new();
    let tile_width = image.width.div_ceil(4);
    let tile_height = image.height.div_ceil(4);
    let idx = |x: u32, y: u32| -> usize { (y * image.width + x) as usize };
    for ty in 0..4 {
        for tx in 0..4 {
            let x0 = tx * tile_width;
            let y0 = ty * tile_height;
            let x1 = ((tx + 1) * tile_width).min(image.width);
            let y1 = ((ty + 1) * tile_height).min(image.height);
            if x0 >= x1 || y0 >= y1 {
                continue;
            }
            let area = ((x1 - x0) * (y1 - y0)).max(1) as f32;
            let mut luminance = Vec::new();
            for y in y0..y1 {
                for x in x0..x1 {
                    let pixel = image.pixels[idx(x, y)];
                    if pixel.opaque {
                        luminance.push(pixel.y);
                    }
                }
            }
            if luminance.len() >= 64 && luminance.len() as f32 / area >= 0.10 {
                tile_medians.push(percentile(&mut luminance, 0.5));
            }
        }
    }
    if tile_medians.len() < 4 {
        return QualityCheck::NotMeasured {
            reason: "insufficient_luminance_tiles".to_string(),
            deduction: 0.0,
        };
    }
    let mut global: Vec<_> = valid_pixels.iter().map(|pixel| pixel.y).collect();
    let global_median = percentile(&mut global, 0.5);
    let p90 = percentile(&mut tile_medians.clone(), 0.9);
    let p10 = percentile(&mut tile_medians, 0.1);
    let unevenness = clamp01((p90 - p10) / global_median.max(0.05));
    QualityCheck::Measured {
        value: unevenness,
        deduction: crate::score::shadow_deduction(unevenness),
    }
}

fn percentile(values: &mut [f32], p: f32) -> f32 {
    values.sort_by(|a, b| a.total_cmp(b));
    let index = ((values.len() - 1) as f32 * p).round() as usize;
    values[index]
}