tiff-reader 0.4.0

Pure-Rust, read-only TIFF/BigTIFF file decoder with no C dependencies
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
use crate::error::{Error, Result};
use crate::ifd::Ifd;
use tiff_core::{ColorMap, ColorModel, Compression, RasterLayout};

pub(crate) fn decode_pixels(
    ifd: &Ifd,
    storage_layout: &RasterLayout,
    width: usize,
    height: usize,
    sample_bytes: &[u8],
) -> Result<(RasterLayout, Vec<u8>)> {
    let decoded_layout = ifd.decoded_raster_layout()?;
    let decoded_layout = RasterLayout {
        width,
        height,
        ..decoded_layout
    };
    let color_model = ifd.color_model()?;
    if can_passthrough(ifd, &color_model, storage_layout, &decoded_layout) {
        return Ok((decoded_layout, sample_bytes.to_vec()));
    }

    let pixel_count = width
        .checked_mul(height)
        .ok_or_else(|| Error::InvalidImageLayout("decoded pixel count overflows usize".into()))?;
    let input_channels = storage_layout.samples_per_pixel;
    let expected_input_len = pixel_count
        .checked_mul(input_channels)
        .and_then(|samples| samples.checked_mul(storage_layout.bytes_per_sample))
        .ok_or_else(|| {
            Error::InvalidImageLayout("decoded sample buffer length overflows usize".into())
        })?;
    if sample_bytes.len() != expected_input_len {
        return Err(Error::InvalidImageLayout(format!(
            "decoded sample buffer length {} does not match expected {expected_input_len}",
            sample_bytes.len()
        )));
    }

    let mut out = Vec::with_capacity(
        pixel_count
            .checked_mul(decoded_layout.samples_per_pixel)
            .and_then(|samples| samples.checked_mul(decoded_layout.bytes_per_sample))
            .ok_or_else(|| {
                Error::InvalidImageLayout("decoded output buffer length overflows usize".into())
            })?,
    );

    match color_model {
        ColorModel::Grayscale {
            white_is_zero,
            extra_samples,
        } => {
            if storage_layout.sample_format != 1 {
                return Err(Error::InvalidImageLayout(
                    "grayscale color decoding requires unsigned integer source samples".into(),
                ));
            }
            for pixel in 0..pixel_count {
                let mut value = read_uint_sample(sample_bytes, storage_layout, pixel, 0);
                if white_is_zero {
                    value = invert_uint_bits(value, storage_layout.bits_per_sample);
                }
                write_uint_sample(
                    &mut out,
                    scale_uint_bits(
                        value,
                        storage_layout.bits_per_sample,
                        decoded_layout.bits_per_sample,
                    ),
                    decoded_layout.bits_per_sample,
                );
                copy_extra_samples(
                    &mut out,
                    sample_bytes,
                    storage_layout,
                    pixel,
                    1,
                    extra_samples.len(),
                    decoded_layout.bits_per_sample,
                );
            }
        }
        ColorModel::Palette {
            color_map,
            extra_samples,
        } => {
            for pixel in 0..pixel_count {
                let index = read_uint_sample(sample_bytes, storage_layout, pixel, 0) as usize;
                if index >= color_map.len() {
                    return Err(Error::InvalidImageLayout(format!(
                        "palette index {index} exceeds ColorMap length {}",
                        color_map.len()
                    )));
                }
                write_uint_sample(
                    &mut out,
                    palette_channel_value(&color_map, index, 0, decoded_layout.bits_per_sample),
                    decoded_layout.bits_per_sample,
                );
                write_uint_sample(
                    &mut out,
                    palette_channel_value(&color_map, index, 1, decoded_layout.bits_per_sample),
                    decoded_layout.bits_per_sample,
                );
                write_uint_sample(
                    &mut out,
                    palette_channel_value(&color_map, index, 2, decoded_layout.bits_per_sample),
                    decoded_layout.bits_per_sample,
                );
                copy_extra_samples(
                    &mut out,
                    sample_bytes,
                    storage_layout,
                    pixel,
                    1,
                    extra_samples.len(),
                    decoded_layout.bits_per_sample,
                );
            }
        }
        ColorModel::Rgb { extra_samples } => {
            for pixel in 0..pixel_count {
                for channel in 0..3 {
                    let value = read_uint_sample(sample_bytes, storage_layout, pixel, channel);
                    write_uint_sample(
                        &mut out,
                        scale_uint_bits(
                            value,
                            storage_layout.bits_per_sample,
                            decoded_layout.bits_per_sample,
                        ),
                        decoded_layout.bits_per_sample,
                    );
                }
                copy_extra_samples(
                    &mut out,
                    sample_bytes,
                    storage_layout,
                    pixel,
                    3,
                    extra_samples.len(),
                    decoded_layout.bits_per_sample,
                );
            }
        }
        ColorModel::TransparencyMask => {
            for pixel in 0..pixel_count {
                let value = read_uint_sample(sample_bytes, storage_layout, pixel, 0);
                write_uint_sample(
                    &mut out,
                    scale_uint_bits(
                        value,
                        storage_layout.bits_per_sample,
                        decoded_layout.bits_per_sample,
                    ),
                    decoded_layout.bits_per_sample,
                );
            }
        }
        ColorModel::Cmyk { extra_samples } => {
            for pixel in 0..pixel_count {
                let c = normalize_uint_sample(
                    read_uint_sample(sample_bytes, storage_layout, pixel, 0),
                    storage_layout.bits_per_sample,
                );
                let m = normalize_uint_sample(
                    read_uint_sample(sample_bytes, storage_layout, pixel, 1),
                    storage_layout.bits_per_sample,
                );
                let y = normalize_uint_sample(
                    read_uint_sample(sample_bytes, storage_layout, pixel, 2),
                    storage_layout.bits_per_sample,
                );
                let k = normalize_uint_sample(
                    read_uint_sample(sample_bytes, storage_layout, pixel, 3),
                    storage_layout.bits_per_sample,
                );
                for value in [
                    (1.0 - c) * (1.0 - k),
                    (1.0 - m) * (1.0 - k),
                    (1.0 - y) * (1.0 - k),
                ] {
                    write_uint_sample(
                        &mut out,
                        denormalize_uint_sample(value, decoded_layout.bits_per_sample),
                        decoded_layout.bits_per_sample,
                    );
                }
                copy_extra_samples(
                    &mut out,
                    sample_bytes,
                    storage_layout,
                    pixel,
                    4,
                    extra_samples.len(),
                    decoded_layout.bits_per_sample,
                );
            }
        }
        ColorModel::YCbCr { extra_samples, .. } => {
            if Compression::from_code(ifd.compression()) == Some(Compression::Jpeg) {
                return Ok((decoded_layout, sample_bytes.to_vec()));
            }
            let reference_black_white = ifd
                .reference_black_white()?
                .unwrap_or_else(|| default_reference_black_white(storage_layout.bits_per_sample));
            let chroma_denominator = bit_max(storage_layout.bits_per_sample) as f64;
            for pixel in 0..pixel_count {
                let y = read_uint_sample(sample_bytes, storage_layout, pixel, 0) as f64;
                let cb = read_uint_sample(sample_bytes, storage_layout, pixel, 1) as f64;
                let cr = read_uint_sample(sample_bytes, storage_layout, pixel, 2) as f64;
                let y_norm = normalize_reference_sample(
                    y,
                    reference_black_white[0],
                    reference_black_white[1],
                );
                let cb_delta = (cb - reference_black_white[2]) / chroma_denominator;
                let cr_delta = (cr - reference_black_white[4]) / chroma_denominator;
                let r = clamp01(y_norm + 1.402 * cr_delta);
                let g = clamp01(y_norm - 0.344_136_286 * cb_delta - 0.714_136_286 * cr_delta);
                let b = clamp01(y_norm + 1.772 * cb_delta);
                for value in [r, g, b] {
                    write_uint_sample(
                        &mut out,
                        denormalize_uint_sample(value, decoded_layout.bits_per_sample),
                        decoded_layout.bits_per_sample,
                    );
                }
                copy_extra_samples(
                    &mut out,
                    sample_bytes,
                    storage_layout,
                    pixel,
                    3,
                    extra_samples.len(),
                    decoded_layout.bits_per_sample,
                );
            }
        }
        ColorModel::Separated {
            color_channels,
            extra_samples,
            ..
        } => {
            for pixel in 0..pixel_count {
                for channel in 0..usize::from(color_channels) {
                    let value = read_uint_sample(sample_bytes, storage_layout, pixel, channel);
                    write_uint_sample(
                        &mut out,
                        scale_uint_bits(
                            value,
                            storage_layout.bits_per_sample,
                            decoded_layout.bits_per_sample,
                        ),
                        decoded_layout.bits_per_sample,
                    );
                }
                copy_extra_samples(
                    &mut out,
                    sample_bytes,
                    storage_layout,
                    pixel,
                    usize::from(color_channels),
                    extra_samples.len(),
                    decoded_layout.bits_per_sample,
                );
            }
        }
        ColorModel::CieLab { extra_samples } => {
            for pixel in 0..pixel_count {
                for channel in 0..3 {
                    let value = read_uint_sample(sample_bytes, storage_layout, pixel, channel);
                    write_uint_sample(
                        &mut out,
                        scale_uint_bits(
                            value,
                            storage_layout.bits_per_sample,
                            decoded_layout.bits_per_sample,
                        ),
                        decoded_layout.bits_per_sample,
                    );
                }
                copy_extra_samples(
                    &mut out,
                    sample_bytes,
                    storage_layout,
                    pixel,
                    3,
                    extra_samples.len(),
                    decoded_layout.bits_per_sample,
                );
            }
        }
    }

    Ok((decoded_layout, out))
}

fn can_passthrough(
    ifd: &Ifd,
    color_model: &ColorModel,
    storage_layout: &RasterLayout,
    decoded_layout: &RasterLayout,
) -> bool {
    if storage_layout.bits_per_sample < 8 {
        return false;
    }
    if storage_layout.sample_format != decoded_layout.sample_format
        || storage_layout.bits_per_sample != decoded_layout.bits_per_sample
        || storage_layout.samples_per_pixel != decoded_layout.samples_per_pixel
    {
        return false;
    }
    match color_model {
        ColorModel::Grayscale {
            white_is_zero: false,
            ..
        }
        | ColorModel::Rgb { .. }
        | ColorModel::TransparencyMask
        | ColorModel::Separated { .. }
        | ColorModel::CieLab { .. } => true,
        ColorModel::YCbCr { .. } => {
            Compression::from_code(ifd.compression()) == Some(Compression::Jpeg)
        }
        _ => false,
    }
}

fn copy_extra_samples(
    out: &mut Vec<u8>,
    sample_bytes: &[u8],
    storage_layout: &RasterLayout,
    pixel_index: usize,
    first_extra_channel: usize,
    extra_count: usize,
    decoded_bits_per_sample: u16,
) {
    for channel in first_extra_channel..first_extra_channel + extra_count {
        let value = read_uint_sample(sample_bytes, storage_layout, pixel_index, channel);
        write_uint_sample(
            out,
            scale_uint_bits(
                value,
                storage_layout.bits_per_sample,
                decoded_bits_per_sample,
            ),
            decoded_bits_per_sample,
        );
    }
}

fn read_uint_sample(
    sample_bytes: &[u8],
    layout: &RasterLayout,
    pixel_index: usize,
    channel: usize,
) -> u64 {
    let offset = (pixel_index * layout.samples_per_pixel + channel) * layout.bytes_per_sample;
    match layout.bytes_per_sample {
        1 => u64::from(sample_bytes[offset]),
        2 => u64::from(u16::from_ne_bytes(
            sample_bytes[offset..offset + 2].try_into().unwrap(),
        )),
        4 => u64::from(u32::from_ne_bytes(
            sample_bytes[offset..offset + 4].try_into().unwrap(),
        )),
        8 => u64::from_ne_bytes(sample_bytes[offset..offset + 8].try_into().unwrap()),
        _ => unreachable!(),
    }
}

fn write_uint_sample(out: &mut Vec<u8>, value: u64, bits_per_sample: u16) {
    match bits_per_sample {
        8 => out.push(value as u8),
        16 => out.extend_from_slice(&(value as u16).to_ne_bytes()),
        32 => out.extend_from_slice(&(value as u32).to_ne_bytes()),
        64 => out.extend_from_slice(&value.to_ne_bytes()),
        other => unreachable!("unsupported decoded integer bit depth {other}"),
    }
}

fn scale_uint_bits(value: u64, from_bits: u16, to_bits: u16) -> u64 {
    if from_bits == to_bits {
        return value;
    }
    let from_max = bit_max(from_bits);
    let to_max = bit_max(to_bits);
    (((value as u128 * to_max) + from_max / 2) / from_max) as u64
}

fn invert_uint_bits(value: u64, bits_per_sample: u16) -> u64 {
    bit_max(bits_per_sample) as u64 - value
}

fn normalize_uint_sample(value: u64, bits_per_sample: u16) -> f64 {
    value as f64 / bit_max(bits_per_sample) as f64
}

fn denormalize_uint_sample(value: f64, bits_per_sample: u16) -> u64 {
    (clamp01(value) * bit_max(bits_per_sample) as f64).round() as u64
}

fn bit_max(bits_per_sample: u16) -> u128 {
    (1u128 << bits_per_sample) - 1
}

fn palette_channel_value(
    color_map: &ColorMap,
    index: usize,
    channel: usize,
    decoded_bits_per_sample: u16,
) -> u64 {
    let value = match channel {
        0 => color_map.red()[index],
        1 => color_map.green()[index],
        2 => color_map.blue()[index],
        _ => unreachable!(),
    };
    scale_uint_bits(u64::from(value), 16, decoded_bits_per_sample)
}

fn default_reference_black_white(bits_per_sample: u16) -> [f64; 6] {
    let max = bit_max(bits_per_sample) as f64;
    let chroma_zero = (1u128 << bits_per_sample.saturating_sub(1)) as f64;
    [0.0, max, chroma_zero, max, chroma_zero, max]
}

fn normalize_reference_sample(value: f64, black: f64, white: f64) -> f64 {
    if (white - black).abs() < f64::EPSILON {
        0.0
    } else {
        (value - black) / (white - black)
    }
}

fn clamp01(value: f64) -> f64 {
    value.clamp(0.0, 1.0)
}