tiff-reader 0.8.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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
//! Strip-based data access for TIFF images.

use std::sync::Arc;

#[cfg(feature = "rayon")]
use parking_lot::Mutex;
#[cfg(feature = "rayon")]
use rayon::prelude::*;

use crate::block_decode;
use crate::cache::{BlockCache, BlockKey, BlockKind};
use crate::error::{Error, Result};
use crate::header::ByteOrder;
use crate::ifd::{Ifd, RasterLayout};
use crate::source::TiffSource;
use crate::{
    allocate_decode_output, checked_layout_add, checked_layout_mul, read_block_payload,
    read_gdal_block_payload, validate_decode_output_len, DecodeReadOptions, Window,
};

pub(crate) fn read_window(
    source: &dyn TiffSource,
    ifd: &Ifd,
    byte_order: ByteOrder,
    cache: &BlockCache,
    window: Window,
    options: DecodeReadOptions<'_>,
) -> Result<Vec<u8>> {
    let layout = ifd.raster_layout()?;
    if window.is_empty() {
        return Ok(Vec::new());
    }
    let ifd_offset = ifd.offset();
    let context = block_decode::BlockDecodeContext::new(ifd, layout, byte_order)?;

    let output_len = window.output_len(&layout)?;
    let mut output = allocate_decode_output(output_len, options.decode_output_bytes)?;

    let relevant_specs = collect_strip_specs_for_window(ifd, &layout, window, None)?;

    #[cfg(feature = "rayon")]
    {
        let output = Mutex::new(output.as_mut_slice());
        relevant_specs.par_iter().try_for_each(|&spec| {
            let block = read_strip_block(source, ifd_offset, cache, spec, &context, options)?;
            copy_strip_window_block(&mut output.lock(), block.as_slice(), spec, &layout, window)?;
            Ok::<(), Error>(())
        })?;
    }

    #[cfg(not(feature = "rayon"))]
    for spec in relevant_specs {
        let block = read_strip_block(source, ifd_offset, cache, spec, &context, options)?;
        copy_strip_window_block(&mut output, block.as_slice(), spec, &layout, window)?;
    }

    Ok(output)
}

pub(crate) fn read_window_band(
    source: &dyn TiffSource,
    ifd: &Ifd,
    byte_order: ByteOrder,
    cache: &BlockCache,
    window: Window,
    band_index: usize,
    options: DecodeReadOptions<'_>,
) -> Result<Vec<u8>> {
    let layout = ifd.raster_layout()?;
    if band_index >= layout.samples_per_pixel {
        return Err(Error::BandIndexOutOfBounds {
            index: band_index,
            band_count: layout.samples_per_pixel,
        });
    }
    if window.is_empty() {
        return Ok(Vec::new());
    }
    let ifd_offset = ifd.offset();
    let context = block_decode::BlockDecodeContext::new(ifd, layout, byte_order)?;

    let output_len = window.band_output_len(&layout)?;
    let mut output = allocate_decode_output(output_len, options.decode_output_bytes)?;

    let relevant_specs = collect_strip_specs_for_window(ifd, &layout, window, Some(band_index))?;

    #[cfg(feature = "rayon")]
    {
        let output = Mutex::new(output.as_mut_slice());
        relevant_specs.par_iter().try_for_each(|&spec| {
            let block = read_strip_block(source, ifd_offset, cache, spec, &context, options)?;
            copy_strip_band_window_block(
                &mut output.lock(),
                block.as_slice(),
                spec,
                &layout,
                window,
                band_index,
            )?;
            Ok::<(), Error>(())
        })?;
    }

    #[cfg(not(feature = "rayon"))]
    for spec in relevant_specs {
        let block = read_strip_block(source, ifd_offset, cache, spec, &context, options)?;
        copy_strip_band_window_block(
            &mut output,
            block.as_slice(),
            spec,
            &layout,
            window,
            band_index,
        )?;
    }

    Ok(output)
}

fn copy_strip_window_block(
    output: &mut [u8],
    block: &[u8],
    spec: StripBlockSpec,
    layout: &RasterLayout,
    window: Window,
) -> Result<()> {
    let pixel_stride = layout.checked_pixel_stride_bytes()?;
    let window_row_end = window.row_end();
    let output_row_bytes = checked_layout_mul(window.cols, pixel_stride, "window row byte count")?;
    let block_row_end = checked_layout_add(spec.row_start, spec.rows_in_strip, "strip row range")?;
    let copy_row_start = spec.row_start.max(window.row_off);
    let copy_row_end = block_row_end.min(window_row_end);

    if layout.planar_configuration == 1 {
        let src_row_bytes = layout.checked_row_bytes()?;
        let src_col_offset =
            checked_layout_mul(window.col_off, pixel_stride, "strip source column offset")?;
        let copy_bytes_per_row = output_row_bytes;
        for row in copy_row_start..copy_row_end {
            let src_row_index = row - spec.row_start;
            let dest_row_index = row - window.row_off;
            let src_offset = checked_layout_add(
                checked_layout_mul(src_row_index, src_row_bytes, "strip source row offset")?,
                src_col_offset,
                "strip source offset",
            )?;
            let dest_offset =
                checked_layout_mul(dest_row_index, output_row_bytes, "strip output row offset")?;
            let src_end =
                checked_layout_add(src_offset, copy_bytes_per_row, "strip source copy range")?;
            let dest_end =
                checked_layout_add(dest_offset, copy_bytes_per_row, "strip output copy range")?;
            output[dest_offset..dest_end].copy_from_slice(&block[src_offset..src_end]);
        }
    } else {
        let src_row_bytes = layout.checked_sample_plane_row_bytes()?;
        let plane_offset = checked_layout_mul(
            spec.plane,
            layout.bytes_per_sample,
            "strip plane byte offset",
        )?;
        for row in copy_row_start..copy_row_end {
            let src_row_index = row - spec.row_start;
            let dest_row_index = row - window.row_off;
            let src_row_offset =
                checked_layout_mul(src_row_index, src_row_bytes, "strip source row offset")?;
            let src_row_end =
                checked_layout_add(src_row_offset, src_row_bytes, "strip source row range")?;
            let dest_row_offset =
                checked_layout_mul(dest_row_index, output_row_bytes, "strip output row offset")?;
            let dest_row_end =
                checked_layout_add(dest_row_offset, output_row_bytes, "strip output row range")?;
            let src_row = &block[src_row_offset..src_row_end];
            let dest_row = &mut output[dest_row_offset..dest_row_end];
            for col in window.col_off..window.col_end() {
                let src_offset =
                    checked_layout_mul(col, layout.bytes_per_sample, "strip source column offset")?;
                let src_end = checked_layout_add(
                    src_offset,
                    layout.bytes_per_sample,
                    "strip source sample range",
                )?;
                let src = &src_row[src_offset..src_end];
                let dest_col_index = col - window.col_off;
                let pixel_base = checked_layout_add(
                    checked_layout_mul(dest_col_index, pixel_stride, "strip output pixel offset")?,
                    plane_offset,
                    "strip output sample offset",
                )?;
                let pixel_end = checked_layout_add(
                    pixel_base,
                    layout.bytes_per_sample,
                    "strip output sample range",
                )?;
                dest_row[pixel_base..pixel_end].copy_from_slice(src);
            }
        }
    }
    Ok(())
}

fn copy_strip_band_window_block(
    output: &mut [u8],
    block: &[u8],
    spec: StripBlockSpec,
    layout: &RasterLayout,
    window: Window,
    band_index: usize,
) -> Result<()> {
    let pixel_stride = layout.checked_pixel_stride_bytes()?;
    let window_row_end = window.row_end();
    let output_row_bytes = checked_layout_mul(
        window.cols,
        layout.bytes_per_sample,
        "window band row byte count",
    )?;
    let block_row_end = checked_layout_add(spec.row_start, spec.rows_in_strip, "strip row range")?;
    let copy_row_start = spec.row_start.max(window.row_off);
    let copy_row_end = block_row_end.min(window_row_end);

    if layout.planar_configuration == 1 {
        let src_row_bytes = layout.checked_row_bytes()?;
        let band_offset =
            checked_layout_mul(band_index, layout.bytes_per_sample, "band byte offset")?;
        for row in copy_row_start..copy_row_end {
            let src_row_index = row - spec.row_start;
            let dest_row_index = row - window.row_off;
            let src_row_offset =
                checked_layout_mul(src_row_index, src_row_bytes, "strip source row offset")?;
            let src_row_end =
                checked_layout_add(src_row_offset, src_row_bytes, "strip source row range")?;
            let dest_row_offset =
                checked_layout_mul(dest_row_index, output_row_bytes, "strip output row offset")?;
            let dest_row_end =
                checked_layout_add(dest_row_offset, output_row_bytes, "strip output row range")?;
            let src_row = &block[src_row_offset..src_row_end];
            let dest_row = &mut output[dest_row_offset..dest_row_end];
            for col in window.col_off..window.col_end() {
                let src_base = checked_layout_add(
                    checked_layout_mul(col, pixel_stride, "strip source column offset")?,
                    band_offset,
                    "strip source band offset",
                )?;
                let dest_col_index = col - window.col_off;
                let dest_base = checked_layout_mul(
                    dest_col_index,
                    layout.bytes_per_sample,
                    "strip output sample offset",
                )?;
                let src_end = checked_layout_add(
                    src_base,
                    layout.bytes_per_sample,
                    "strip source sample range",
                )?;
                let dest_end = checked_layout_add(
                    dest_base,
                    layout.bytes_per_sample,
                    "strip output sample range",
                )?;
                dest_row[dest_base..dest_end].copy_from_slice(&src_row[src_base..src_end]);
            }
        }
    } else {
        let src_row_bytes = layout.checked_sample_plane_row_bytes()?;
        let src_col_offset = checked_layout_mul(
            window.col_off,
            layout.bytes_per_sample,
            "strip source column offset",
        )?;
        let copy_bytes_per_row = output_row_bytes;
        for row in copy_row_start..copy_row_end {
            let src_row_index = row - spec.row_start;
            let dest_row_index = row - window.row_off;
            let src_offset = checked_layout_add(
                checked_layout_mul(src_row_index, src_row_bytes, "strip source row offset")?,
                src_col_offset,
                "strip source offset",
            )?;
            let dest_offset =
                checked_layout_mul(dest_row_index, output_row_bytes, "strip output row offset")?;
            let src_end =
                checked_layout_add(src_offset, copy_bytes_per_row, "strip source copy range")?;
            let dest_end =
                checked_layout_add(dest_offset, copy_bytes_per_row, "strip output copy range")?;
            output[dest_offset..dest_end].copy_from_slice(&block[src_offset..src_end]);
        }
    }
    Ok(())
}

fn collect_strip_specs_for_window(
    ifd: &Ifd,
    layout: &RasterLayout,
    window: Window,
    band_index: Option<usize>,
) -> Result<Vec<StripBlockSpec>> {
    let offsets = ifd
        .strip_offsets()
        .ok_or(Error::TagNotFound(crate::ifd::TAG_STRIP_OFFSETS))?;
    let counts = ifd
        .strip_byte_counts()
        .ok_or(Error::TagNotFound(crate::ifd::TAG_STRIP_BYTE_COUNTS))?;
    if offsets.len() != counts.len() {
        return Err(Error::InvalidImageLayout(format!(
            "StripOffsets has {} entries but StripByteCounts has {}",
            offsets.len(),
            counts.len()
        )));
    }

    let rows_per_strip = ifd.rows_per_strip();
    if rows_per_strip == 0 {
        return Err(Error::InvalidImageLayout(
            "RowsPerStrip must be greater than zero".into(),
        ));
    }
    let rows_per_strip = rows_per_strip as usize;
    let strips_per_plane = layout.height.div_ceil(rows_per_strip);
    let expected = match layout.planar_configuration {
        1 => strips_per_plane,
        2 => strips_per_plane
            .checked_mul(layout.samples_per_pixel)
            .ok_or_else(strip_count_overflow)?,
        planar => return Err(Error::UnsupportedPlanarConfiguration(planar)),
    };
    if offsets.len() != expected {
        return Err(Error::InvalidImageLayout(format!(
            "expected {expected} strips, found {}",
            offsets.len()
        )));
    }

    let first_strip = window.row_off / rows_per_strip;
    let last_strip = window
        .row_end()
        .div_ceil(rows_per_strip)
        .min(strips_per_plane);
    let plane_range = if layout.planar_configuration == 1 {
        0..1
    } else if let Some(band_index) = band_index {
        band_index..band_index + 1
    } else {
        0..layout.samples_per_pixel
    };
    let spec_count = (last_strip - first_strip)
        .checked_mul(plane_range.end - plane_range.start)
        .ok_or_else(strip_count_overflow)?;
    let mut specs = Vec::with_capacity(spec_count);

    for plane in plane_range {
        for plane_strip_index in first_strip..last_strip {
            let strip_index = if layout.planar_configuration == 1 {
                plane_strip_index
            } else {
                plane
                    .checked_mul(strips_per_plane)
                    .and_then(|base| base.checked_add(plane_strip_index))
                    .ok_or_else(strip_count_overflow)?
            };
            let row_start = plane_strip_index
                .checked_mul(rows_per_strip)
                .ok_or_else(strip_count_overflow)?;
            let rows_in_strip = rows_per_strip.min(layout.height.saturating_sub(row_start));
            specs.push(StripBlockSpec {
                index: strip_index,
                plane,
                row_start,
                offset: offsets[strip_index],
                byte_count: counts[strip_index],
                rows_in_strip,
            });
        }
    }

    Ok(specs)
}

fn strip_count_overflow() -> Error {
    Error::InvalidImageLayout("strip count overflows usize".into())
}

#[derive(Clone, Copy)]
struct StripBlockSpec {
    index: usize,
    plane: usize,
    row_start: usize,
    offset: u64,
    byte_count: u64,
    rows_in_strip: usize,
}

fn read_strip_block(
    source: &dyn TiffSource,
    ifd_offset: u64,
    cache: &BlockCache,
    spec: StripBlockSpec,
    context: &block_decode::BlockDecodeContext<'_>,
    options: DecodeReadOptions<'_>,
) -> Result<Arc<Vec<u8>>> {
    let decode_request = block_decode::BlockDecodeRequest {
        context,
        compressed: &[],
        index: spec.index,
        block_width: context.layout.width,
        block_height: spec.rows_in_strip,
    };
    let decoded_len = block_decode::decoded_block_len(&decode_request)?;
    validate_decode_output_len(decoded_len, options.decode_output_bytes)?;

    let cache_key = BlockKey {
        ifd_offset,
        kind: BlockKind::Strip,
        block_index: spec.index,
    };
    if let Some(cached) = cache.get(&cache_key) {
        return Ok(cached);
    }

    // GDAL SPARSE_OK semantics: a block with no on-disk payload (zero offset
    // or zero byte count) decodes as implicit zero fill.
    if spec.offset == 0 || spec.byte_count == 0 {
        let decoded = allocate_decode_output(decoded_len, options.decode_output_bytes)?;
        return Ok(cache.insert(cache_key, decoded));
    }

    let byte_count_limit = block_decode::compressed_block_byte_count_limit(&decode_request)?;
    let compressed = match options.gdal_structural_metadata {
        Some(metadata) => read_gdal_block_payload(
            source,
            metadata,
            context.byte_order,
            spec.offset,
            spec.byte_count,
            byte_count_limit,
            spec.index,
        )?,
        None => read_block_payload(
            source,
            spec.offset,
            spec.byte_count,
            byte_count_limit,
            spec.index,
        )?,
    };

    let decoded = block_decode::decode_compressed_block(block_decode::BlockDecodeRequest {
        context,
        compressed: &compressed,
        index: spec.index,
        block_width: context.layout.width,
        block_height: spec.rows_in_strip,
    })?;
    Ok(cache.insert(cache_key, decoded))
}