Compare commits
147
Commits
v0.6.0-alpha
...
v0.7.0
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e8bc935618 | ||
|
|
150ff8303b | ||
|
|
e44b98ddf9 | ||
|
|
4df26011d9 | ||
|
|
4dc7c2a986 | ||
|
|
b7c4f668ae | ||
|
|
9fe9b2e0a0 | ||
|
|
b7e8f210d1 | ||
|
|
371a9b53c3 | ||
|
|
d0e4f37a28 | ||
|
|
46c7efd836 | ||
|
|
65832e01bd | ||
|
|
b9590e0842 | ||
|
|
521b58dc07 | ||
|
|
e70121efbb | ||
|
|
a6fb855121 | ||
|
|
a874b9c3a5 | ||
|
|
d4ce174236 | ||
|
|
a43aa1fbc7 | ||
|
|
69c34ac33c | ||
|
|
00e0eb9006 | ||
|
|
ebd0e266f7 | ||
|
|
9b94e9de88 | ||
|
|
47dfa0aa3e | ||
|
|
1eb9c4fab7 | ||
|
|
201b95f749 | ||
|
|
7641dcbd3f | ||
|
|
60c6e0cd90 | ||
|
|
65a1c553af | ||
|
|
fddc048849 | ||
|
|
68f2182918 | ||
|
|
b2a7e35c85 | ||
|
|
91fe529859 | ||
|
|
5ca2730416 | ||
|
|
8fcacc829f | ||
|
|
59d0b7389d | ||
|
|
0ac444b63f | ||
|
|
ce1ee6776f | ||
|
|
29a020e5ec | ||
|
|
9021985477 | ||
|
|
d4c958f915 | ||
|
|
dc04282290 | ||
|
|
4cfed1c7b1 | ||
|
|
745ed62808 | ||
|
|
6fe9b78509 | ||
|
|
f5754159cb | ||
|
|
53d0256585 | ||
|
|
09aa28d699 | ||
|
|
d086e289dd | ||
|
|
2cba04dfe7 | ||
|
|
3de87a7001 | ||
|
|
4900c82871 | ||
|
|
8a8e3ed753 | ||
|
|
cd2f4f6462 | ||
|
|
aa13e31894 | ||
|
|
6c211b9179 | ||
|
|
49f9b3941d | ||
|
|
332ab1dc4d | ||
|
|
ec98e32a3b | ||
|
|
7ec2b75c70 | ||
|
|
9788085786 | ||
|
|
fc80c5e3dd | ||
|
|
95f0a34bcc | ||
|
|
3e042df438 | ||
|
|
5346a441ae | ||
|
|
76173069a3 | ||
|
|
6cdd340933 | ||
|
|
ef004cee48 | ||
|
|
6d7d92c605 | ||
|
|
53437dd7b4 | ||
|
|
0c06d32ee4 | ||
|
|
4d8c64bf1e | ||
|
|
7da8f01f75 | ||
|
|
67105fe4ec | ||
|
|
1b38ff9410 | ||
|
|
c9703dcb8a | ||
|
|
e2caf86f6a | ||
|
|
923b37d647 | ||
|
|
9f4799912f | ||
|
|
4b218be4ac | ||
|
|
9a0041286e | ||
|
|
0a4b51055e | ||
|
|
96d6d52830 | ||
|
|
62a1a500a5 | ||
|
|
3953bd31e1 | ||
|
|
83004c8344 | ||
|
|
8ab9d30eff | ||
|
|
89aaab12dc | ||
|
|
481ef3781a | ||
|
|
369c602952 | ||
|
|
ed22865f04 | ||
|
|
459b4f3908 | ||
|
|
e72401d4ac | ||
|
|
6281360f10 | ||
|
|
d00ab0cc61 | ||
|
|
6353f71e28 | ||
|
|
7c4ee96aee | ||
|
|
cb5319c426 | ||
|
|
53aefea6a4 | ||
|
|
cb900819d9 | ||
|
|
35ea719d9e | ||
|
|
41050c14ed | ||
|
|
5fd0534ea2 | ||
|
|
72474b569e | ||
|
|
63f09b267a | ||
|
|
b455bd9f2b | ||
|
|
46918b395c | ||
|
|
a510b03e1c | ||
|
|
2cb57a4d04 | ||
|
|
da9bdac8a2 | ||
|
|
647de77b70 | ||
|
|
727d78a57c | ||
|
|
49da46bbd0 | ||
|
|
7b41c823e9 | ||
|
|
bf24b3f935 | ||
|
|
2b12c1b162 | ||
|
|
82c00a84d8 | ||
|
|
ec5b4b9a9e | ||
|
|
f4f11e6399 | ||
|
|
49b1f78ce5 | ||
|
|
e48691ca32 | ||
|
|
7ad6b0e70d | ||
|
|
7cc5b98d9c | ||
|
|
7928c219dd | ||
|
|
44c75a66ab | ||
|
|
d8bfa84d06 | ||
|
|
1492087426 | ||
|
|
23090a3513 | ||
|
|
200355d3bc | ||
|
|
469883aa79 | ||
|
|
99c038c929 | ||
|
|
f3eebee1e9 | ||
|
|
b853d841f3 | ||
|
|
418979f14b | ||
|
|
768e0d4af7 | ||
|
|
3b0da4d1ae | ||
|
|
54c3ebf358 | ||
|
|
5f48444fa0 | ||
|
|
71f3aa8b15 | ||
|
|
0ec8e14910 | ||
|
|
d79d8c3353 | ||
|
|
ae4ae9c36e | ||
|
|
8d4bdce7aa | ||
|
|
a41e33e42a | ||
|
|
c2084835fa | ||
|
|
97c888f166 | ||
|
|
bbc49291e4 |
No files matched your search
@@ -0,0 +1,40 @@
|
|||||||
|
name: Rust CI
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches: [ master ]
|
||||||
|
pull_request:
|
||||||
|
branches: [ master ]
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
strategy:
|
||||||
|
matrix:
|
||||||
|
rust: ["1.34.2", stable, beta, nightly]
|
||||||
|
command: [build, test]
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v2
|
||||||
|
- run: rustup default ${{ matrix.rust }}
|
||||||
|
- name: build
|
||||||
|
run: >
|
||||||
|
cargo build --verbose --no-default-features --features "$FEATURES"
|
||||||
|
- name: test
|
||||||
|
run: >
|
||||||
|
cargo test --tests --benches --no-default-features --features "$FEATURES"
|
||||||
|
if: ${{ matrix.rust != '1.34.2' }}
|
||||||
|
env:
|
||||||
|
FEATURES: ${{ matrix.features }}
|
||||||
|
rustfmt:
|
||||||
|
runs-on: ubuntu-latest
|
||||||
|
steps:
|
||||||
|
- uses: actions/checkout@v2
|
||||||
|
- uses: actions-rs/toolchain@v1
|
||||||
|
with:
|
||||||
|
toolchain: stable
|
||||||
|
override: true
|
||||||
|
components: rustfmt
|
||||||
|
- name: Run rustfmt check
|
||||||
|
uses: actions-rs/cargo@v1
|
||||||
|
with:
|
||||||
|
command: fmt
|
||||||
|
args: -- --check
|
||||||
|
|
||||||
@@ -1,6 +0,0 @@
|
|||||||
language: rust
|
|
||||||
rust:
|
|
||||||
- 1.34.2
|
|
||||||
- stable
|
|
||||||
- beta
|
|
||||||
- nightly
|
|
||||||
+58
-2
@@ -1,11 +1,67 @@
|
|||||||
# Version 0.6.0-alpha
|
# Version 0.7.0
|
||||||
|
|
||||||
|
New features:
|
||||||
|
* Support for encoding BigTiff ([#122](https://github.com/image-rs/image-tiff/pull/122))
|
||||||
|
* _Breaking:_ Encoder types now have a generic parameter to differentiate BigTiff and standard Tiff encoding. Defaults to standard Tiff.
|
||||||
|
* Basic tile decoding ([#125](https://github.com/image-rs/image-tiff/pull/125))
|
||||||
|
* _Breaking:_ There is a new `TiffError::UsageError` variant.
|
||||||
|
* Support for datatypes `Int8` and `Int16` ([#114](https://github.com/image-rs/image-tiff/pull/114))
|
||||||
|
* _Breaking:_ `DecodingResult` and `DecodingBuffer` have the two new variants `I8` and `I16`.
|
||||||
|
* Support for `i32` arrays ([#118](https://github.com/image-rs/image-tiff/pull/118/files))
|
||||||
|
* _Breaking:_ `DecodingResult` and `DecodingBuffer` have a new `I32` variant.
|
||||||
|
* Support for `Ifd` and `IfdBig` tag types and `I64` data type ([#119](https://github.com/image-rs/image-tiff/pull/119))
|
||||||
|
* _Breaking:_ `DecodingResult` and `DecodingBuffer` have a new `I64` variant.
|
||||||
|
* Add `SMinSampleValue` and `SMaxSampleValue` ([#123](https://github.com/image-rs/image-tiff/pull/123))
|
||||||
|
|
||||||
|
Changes:
|
||||||
|
* Improve deflate support ([#132](https://github.com/image-rs/image-tiff/pull/132))
|
||||||
|
* Switch to streaming decompression via `flate2`. Aside from performance improvements and lower RAM consumption, this fixes a bug where `max_uncompressed_length` was precalculated for a single tile but then used as a hard limit on the whole data, failing to decompress any tiled images.
|
||||||
|
* Add support for new `Deflate` tag in addition to `OldDeflate`.
|
||||||
|
* _Breaking:_ Remove `InflateError`, which is no longer needed with `flate2` ([#134](https://github.com/image-rs/image-tiff/pull/134))
|
||||||
|
* _Breaking:_ Support for `MinIsWhite` is restricted to unsigned and floating
|
||||||
|
point values. This is expected to be be re-added once some contradictory
|
||||||
|
interpretation regarding semantics for signed values is resolved.
|
||||||
|
|
||||||
|
Fixes:
|
||||||
|
* Validate that ASCII tags are valid ASCII and end with a null byte ([#121](https://github.com/image-rs/image-tiff/pull/121))
|
||||||
|
|
||||||
|
Internal:
|
||||||
|
* Simplify decompression logic ([#126](https://github.com/image-rs/image-tiff/pull/126))
|
||||||
|
* Simplify `expand_strip` ([#128](https://github.com/image-rs/image-tiff/pull/128))
|
||||||
|
|
||||||
|
# Version 0.6.1
|
||||||
|
|
||||||
|
New features:
|
||||||
|
* Support for reading `u16` and ascii string tags.
|
||||||
|
* Added `Limits::unlimited` for disabling all limits.
|
||||||
|
* Added `ImageEncoder::rows_per_strip` to overwrite the default.
|
||||||
|
|
||||||
|
Changes:
|
||||||
|
* The default strip size for chunked encoding is now 1MB, up from 8KB. This
|
||||||
|
should lead to more efficient decoding and compression.
|
||||||
|
|
||||||
|
Fixes:
|
||||||
|
* Fixed a bug where LZW compressed strips could not be decoded, instead
|
||||||
|
returning an error `Inconsistent sizes encountered`.
|
||||||
|
* Reading a tag with a complex type and a single value returns the proper Value
|
||||||
|
variant, instead of a vector with one entry.
|
||||||
|
|
||||||
|
# Version 0.6.0
|
||||||
|
|
||||||
|
New features:
|
||||||
* Support for decoding BigTIFF with 64-bit offsets
|
* Support for decoding BigTIFF with 64-bit offsets
|
||||||
|
* The value types byte, `f32`, `f64` are now recognized
|
||||||
|
* Support for Modern JPEG encoded images
|
||||||
|
|
||||||
|
Improvements:
|
||||||
* Better support for adding auxiliary tags before encoding image data
|
* Better support for adding auxiliary tags before encoding image data
|
||||||
|
* Switched to lzw decoder library `weezl` for performance
|
||||||
|
* The `ColorType` trait now supports `SAMPLE_ENCODING` hints
|
||||||
|
|
||||||
|
Fixes:
|
||||||
* Fixed decoding of inline ASCII in tags
|
* Fixed decoding of inline ASCII in tags
|
||||||
* Fixed handling after null terminator in ASCII data
|
* Fixed handling after null terminator in ASCII data
|
||||||
* Recognize tile and sample format tags
|
* Recognize tile and sample format tags
|
||||||
* Note: these notes will be merged into 0.6.0 when it is released
|
|
||||||
|
|
||||||
# Version 0.5.0
|
# Version 0.5.0
|
||||||
|
|
||||||
|
|||||||
+12
-4
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "tiff"
|
name = "tiff"
|
||||||
version = "0.6.0-alpha"
|
version = "0.7.0"
|
||||||
authors = [
|
authors = [
|
||||||
"ccgn",
|
"ccgn",
|
||||||
"bvssvni <bvssvni@gmail.com>",
|
"bvssvni <bvssvni@gmail.com>",
|
||||||
@@ -15,8 +15,16 @@ license = "MIT"
|
|||||||
description = "TIFF decoding and encoding library in pure Rust"
|
description = "TIFF decoding and encoding library in pure Rust"
|
||||||
repository = "https://github.com/image-rs/image-tiff"
|
repository = "https://github.com/image-rs/image-tiff"
|
||||||
exclude = ["tests/images/*", "tests/fuzz_images/*"]
|
exclude = ["tests/images/*", "tests/fuzz_images/*"]
|
||||||
|
edition = "2018"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
byteorder = "1.2"
|
weezl = "0.1.0"
|
||||||
lzw = "0.10"
|
jpeg = { package = "jpeg-decoder", version = "0.1.17", default-features = false }
|
||||||
miniz_oxide = "0.3"
|
flate2 = "1.0.20"
|
||||||
|
|
||||||
|
[dev-dependencies]
|
||||||
|
criterion = "0.3.1"
|
||||||
|
|
||||||
|
[[bench]]
|
||||||
|
name = "lzw"
|
||||||
|
harness = false
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
# image-tiff
|
# image-tiff
|
||||||
[](https://travis-ci.org/image-rs/image-tiff)
|
[](https://github.com/image-rs/image-tiff/actions)
|
||||||
[](https://docs.rs/tiff)
|
[](https://docs.rs/tiff)
|
||||||
[](https://crates.io/crates/tiff)
|
[](https://crates.io/crates/tiff)
|
||||||
|
|
||||||
@@ -10,6 +10,7 @@ TIFF decoding and encoding library in pure Rust
|
|||||||
### Features
|
### Features
|
||||||
- Baseline spec (other than formats and tags listed below as not supported)
|
- Baseline spec (other than formats and tags listed below as not supported)
|
||||||
- Multipage
|
- Multipage
|
||||||
|
- BigTIFF
|
||||||
- Incremental decoding
|
- Incremental decoding
|
||||||
|
|
||||||
### Formats
|
### Formats
|
||||||
@@ -17,12 +18,12 @@ This table lists photometric interpretations and sample formats which are suppor
|
|||||||
|
|
||||||
| `PhotometricInterpretation` | UINT Format | IEEEFP Format |
|
| `PhotometricInterpretation` | UINT Format | IEEEFP Format |
|
||||||
|-|-|-|
|
|-|-|-|
|
||||||
| `WhiteIsZero` | Gray(8\|16\|32\|64) | |
|
| `WhiteIsZero` | Gray(8\|16\|32\|64) | Gray(32\|64) |
|
||||||
| `BlackIsZero` | Gray(8\|16\|32\|64) | |
|
| `BlackIsZero` | Gray(8\|16\|32\|64) | Gray(32\|64) |
|
||||||
| `RGB` | RGB(8\|16\|32\|64), RGBA(8\|16\|32\|64) | |
|
| `RGB` | RGB(8\|16\|32\|64), RGBA(8\|16\|32\|64) | RGB(32\|64), RGBA(32\|64) |
|
||||||
| `RGBPalette` | | |
|
| `RGBPalette` | | |
|
||||||
| `Mask` | | |
|
| `Mask` | | |
|
||||||
| `CMYK` | CMYK(8\|16\|32\|64) | |
|
| `CMYK` | CMYK(8\|16\|32\|64) | CMYK(32\|64) |
|
||||||
| `YCbCr` | | |
|
| `YCbCr` | | |
|
||||||
| `CIELab` | | |
|
| `CIELab` | | |
|
||||||
|
|
||||||
@@ -43,4 +44,3 @@ Formats and interpretations not listed above or with empty entries are unsupport
|
|||||||
- Baseline tags
|
- Baseline tags
|
||||||
- `ExtraSamples`
|
- `ExtraSamples`
|
||||||
- Extension tags
|
- Extension tags
|
||||||
- BigTIFF
|
|
||||||
@@ -0,0 +1,82 @@
|
|||||||
|
extern crate criterion;
|
||||||
|
extern crate tiff;
|
||||||
|
|
||||||
|
use criterion::{
|
||||||
|
black_box, measurement::Measurement, BenchmarkGroup, BenchmarkId, Criterion, Throughput,
|
||||||
|
};
|
||||||
|
use tiff::decoder::Decoder;
|
||||||
|
|
||||||
|
fn read_image(image: &[u8]) {
|
||||||
|
let image = std::io::Cursor::new(image);
|
||||||
|
let decoder = Decoder::new(black_box(image));
|
||||||
|
let mut reader = decoder.unwrap();
|
||||||
|
|
||||||
|
while {
|
||||||
|
reader.read_image().unwrap();
|
||||||
|
reader.more_images()
|
||||||
|
} {}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
struct BenchDef {
|
||||||
|
data: &'static [u8],
|
||||||
|
id: &'static str,
|
||||||
|
sample_size: usize,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run_bench_def<M: Measurement>(group: &mut BenchmarkGroup<M>, def: BenchDef) {
|
||||||
|
group
|
||||||
|
.sample_size(def.sample_size)
|
||||||
|
.throughput(Throughput::Bytes(def.data.len() as u64))
|
||||||
|
.bench_with_input(
|
||||||
|
BenchmarkId::new(def.id, def.data.len()),
|
||||||
|
def.data,
|
||||||
|
|b, input| b.iter(|| read_image(input)),
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut c = Criterion::default().configure_from_args();
|
||||||
|
let mut group = c.benchmark_group("tiff-lzw");
|
||||||
|
|
||||||
|
macro_rules! data_file {
|
||||||
|
($name:literal) => {
|
||||||
|
include_bytes!(concat!(env!("CARGO_MANIFEST_DIR"), $name))
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
run_bench_def(
|
||||||
|
&mut group,
|
||||||
|
BenchDef {
|
||||||
|
data: data_file!("/tests/images/issue_69_lzw.tiff"),
|
||||||
|
id: "issue-69-lzw.tif",
|
||||||
|
sample_size: 500,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
run_bench_def(
|
||||||
|
&mut group,
|
||||||
|
BenchDef {
|
||||||
|
data: data_file!("/tests/benches/kodim02-lzw.tif"),
|
||||||
|
id: "kodim02-lzw.tif",
|
||||||
|
sample_size: 20,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
run_bench_def(
|
||||||
|
&mut group,
|
||||||
|
BenchDef {
|
||||||
|
data: data_file!("/tests/benches/kodim07-lzw.tif"),
|
||||||
|
id: "kodim07-lzw.tif",
|
||||||
|
sample_size: 20,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
|
run_bench_def(
|
||||||
|
&mut group,
|
||||||
|
BenchDef {
|
||||||
|
data: data_file!("/tests/benches/Transparency-lzw.tif"),
|
||||||
|
id: "Transparency-lzw.tif",
|
||||||
|
sample_size: 20,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
}
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
//! Trivial, internal byte transmutation.
|
||||||
|
//!
|
||||||
|
//! A dependency like bytemuck would give us extra assurance of the safety but overall would not
|
||||||
|
//! reduce the amount of total unsafety. We don't use it in the interface where the traits would
|
||||||
|
//! really become useful.
|
||||||
|
//!
|
||||||
|
//! SAFETY: These are benign casts as we apply them to fixed size integer types only. All of them
|
||||||
|
//! are naturally aligned, valid for all bit patterns and their alignment is surely at most their
|
||||||
|
//! size (we assert the latter fact since it is 'implementation defined' if following the letter of
|
||||||
|
//! the unsafe code guidelines).
|
||||||
|
//!
|
||||||
|
//! TODO: Would like to use std-lib here.
|
||||||
|
use std::{mem, slice};
|
||||||
|
|
||||||
|
macro_rules! integral_slice_as_bytes{($int:ty, $const:ident $(,$mut:ident)*) => {
|
||||||
|
pub(crate) fn $const(slice: &[$int]) -> &[u8] {
|
||||||
|
assert!(mem::align_of::<$int>() <= mem::size_of::<$int>());
|
||||||
|
unsafe { slice::from_raw_parts(slice.as_ptr() as *const u8, mem::size_of_val(slice)) }
|
||||||
|
}
|
||||||
|
$(pub(crate) fn $mut(slice: &mut [$int]) -> &mut [u8] {
|
||||||
|
assert!(mem::align_of::<$int>() <= mem::size_of::<$int>());
|
||||||
|
unsafe { slice::from_raw_parts_mut(slice.as_mut_ptr() as *mut u8, mem::size_of_val(slice)) }
|
||||||
|
})*
|
||||||
|
}}
|
||||||
|
|
||||||
|
integral_slice_as_bytes!(i8, i8_as_ne_bytes, i8_as_ne_mut_bytes);
|
||||||
|
integral_slice_as_bytes!(u16, u16_as_ne_bytes, u16_as_ne_mut_bytes);
|
||||||
|
integral_slice_as_bytes!(i16, i16_as_ne_bytes, i16_as_ne_mut_bytes);
|
||||||
|
integral_slice_as_bytes!(u32, u32_as_ne_bytes, u32_as_ne_mut_bytes);
|
||||||
|
integral_slice_as_bytes!(i32, i32_as_ne_bytes, i32_as_ne_mut_bytes);
|
||||||
|
integral_slice_as_bytes!(u64, u64_as_ne_bytes, u64_as_ne_mut_bytes);
|
||||||
|
integral_slice_as_bytes!(i64, i64_as_ne_bytes, i64_as_ne_mut_bytes);
|
||||||
|
integral_slice_as_bytes!(f32, f32_as_ne_bytes, f32_as_ne_mut_bytes);
|
||||||
|
integral_slice_as_bytes!(f64, f64_as_ne_bytes, f64_as_ne_mut_bytes);
|
||||||
+396
-173
@@ -1,39 +1,69 @@
|
|||||||
//! Function for reading TIFF tags
|
//! Function for reading TIFF tags
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::convert::{TryInto, TryFrom};
|
use std::convert::{TryFrom, TryInto};
|
||||||
use std::io::{self, Read, Seek};
|
use std::io::{self, Read, Seek};
|
||||||
use std::mem;
|
use std::mem;
|
||||||
|
use std::str;
|
||||||
|
|
||||||
use super::stream::{ByteOrder, EndianReader, SmartReader};
|
use super::stream::{ByteOrder, EndianReader, SmartReader};
|
||||||
use tags::{Tag, Type};
|
use crate::tags::{Tag, Type};
|
||||||
use {TiffError, TiffFormatError, TiffResult, TiffUnsupportedError};
|
use crate::{TiffError, TiffFormatError, TiffResult};
|
||||||
|
|
||||||
use self::Value::{Ascii, List, Rational, Unsigned, Signed, SRational, RationalBig, UnsignedBig, SignedBig, SRationalBig};
|
|
||||||
|
|
||||||
|
use self::Value::{
|
||||||
|
Ascii, Byte, Double, Float, Ifd, IfdBig, List, Rational, RationalBig, SRational, SRationalBig,
|
||||||
|
Short, Signed, SignedBig, Unsigned, UnsignedBig,
|
||||||
|
};
|
||||||
|
|
||||||
#[allow(unused_qualifications)]
|
#[allow(unused_qualifications)]
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
pub enum Value {
|
pub enum Value {
|
||||||
|
Byte(u8),
|
||||||
|
Short(u16),
|
||||||
Signed(i32),
|
Signed(i32),
|
||||||
SignedBig(i64),
|
SignedBig(i64),
|
||||||
Unsigned(u32),
|
Unsigned(u32),
|
||||||
UnsignedBig(u64),
|
UnsignedBig(u64),
|
||||||
|
Float(f32),
|
||||||
|
Double(f64),
|
||||||
List(Vec<Value>),
|
List(Vec<Value>),
|
||||||
Rational(u32, u32),
|
Rational(u32, u32),
|
||||||
RationalBig(u64, u64),
|
RationalBig(u64, u64),
|
||||||
SRational(i32, i32),
|
SRational(i32, i32),
|
||||||
SRationalBig(i64, i64),
|
SRationalBig(i64, i64),
|
||||||
Ascii(String),
|
Ascii(String),
|
||||||
|
Ifd(u32),
|
||||||
|
IfdBig(u64),
|
||||||
#[doc(hidden)] // Do not match against this.
|
#[doc(hidden)] // Do not match against this.
|
||||||
__NonExhaustive,
|
__NonExhaustive,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Value {
|
impl Value {
|
||||||
|
pub fn into_u8(self) -> TiffResult<u8> {
|
||||||
|
match self {
|
||||||
|
Byte(val) => Ok(val),
|
||||||
|
val => Err(TiffError::FormatError(TiffFormatError::ByteExpected(val))),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn into_u16(self) -> TiffResult<u16> {
|
||||||
|
match self {
|
||||||
|
Short(val) => Ok(val),
|
||||||
|
Unsigned(val) => Ok(u16::try_from(val)?),
|
||||||
|
UnsignedBig(val) => Ok(u16::try_from(val)?),
|
||||||
|
val => Err(TiffError::FormatError(
|
||||||
|
TiffFormatError::UnsignedIntegerExpected(val),
|
||||||
|
)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn into_u32(self) -> TiffResult<u32> {
|
pub fn into_u32(self) -> TiffResult<u32> {
|
||||||
match self {
|
match self {
|
||||||
|
Short(val) => Ok(val.into()),
|
||||||
Unsigned(val) => Ok(val),
|
Unsigned(val) => Ok(val),
|
||||||
UnsignedBig(val) => Ok(u32::try_from(val)?),
|
UnsignedBig(val) => Ok(u32::try_from(val)?),
|
||||||
|
Ifd(val) => Ok(val),
|
||||||
|
IfdBig(val) => Ok(u32::try_from(val)?),
|
||||||
val => Err(TiffError::FormatError(
|
val => Err(TiffError::FormatError(
|
||||||
TiffFormatError::UnsignedIntegerExpected(val),
|
TiffFormatError::UnsignedIntegerExpected(val),
|
||||||
)),
|
)),
|
||||||
@@ -52,8 +82,11 @@ impl Value {
|
|||||||
|
|
||||||
pub fn into_u64(self) -> TiffResult<u64> {
|
pub fn into_u64(self) -> TiffResult<u64> {
|
||||||
match self {
|
match self {
|
||||||
|
Short(val) => Ok(val.into()),
|
||||||
Unsigned(val) => Ok(val.into()),
|
Unsigned(val) => Ok(val.into()),
|
||||||
UnsignedBig(val) => Ok(val),
|
UnsignedBig(val) => Ok(val),
|
||||||
|
Ifd(val) => Ok(val.into()),
|
||||||
|
IfdBig(val) => Ok(val),
|
||||||
val => Err(TiffError::FormatError(
|
val => Err(TiffError::FormatError(
|
||||||
TiffFormatError::UnsignedIntegerExpected(val),
|
TiffFormatError::UnsignedIntegerExpected(val),
|
||||||
)),
|
)),
|
||||||
@@ -70,6 +103,33 @@ impl Value {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn into_f32(self) -> TiffResult<f32> {
|
||||||
|
match self {
|
||||||
|
Float(val) => Ok(val),
|
||||||
|
val => Err(TiffError::FormatError(
|
||||||
|
TiffFormatError::SignedIntegerExpected(val),
|
||||||
|
)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn into_f64(self) -> TiffResult<f64> {
|
||||||
|
match self {
|
||||||
|
Double(val) => Ok(val),
|
||||||
|
val => Err(TiffError::FormatError(
|
||||||
|
TiffFormatError::SignedIntegerExpected(val),
|
||||||
|
)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn into_string(self) -> TiffResult<String> {
|
||||||
|
match self {
|
||||||
|
Ascii(val) => Ok(val),
|
||||||
|
val => Err(TiffError::FormatError(
|
||||||
|
TiffFormatError::SignedIntegerExpected(val),
|
||||||
|
)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn into_u32_vec(self) -> TiffResult<Vec<u32>> {
|
pub fn into_u32_vec(self) -> TiffResult<Vec<u32>> {
|
||||||
match self {
|
match self {
|
||||||
List(vec) => {
|
List(vec) => {
|
||||||
@@ -82,7 +142,11 @@ impl Value {
|
|||||||
Unsigned(val) => Ok(vec![val]),
|
Unsigned(val) => Ok(vec![val]),
|
||||||
UnsignedBig(val) => Ok(vec![u32::try_from(val)?]),
|
UnsignedBig(val) => Ok(vec![u32::try_from(val)?]),
|
||||||
Rational(numerator, denominator) => Ok(vec![numerator, denominator]),
|
Rational(numerator, denominator) => Ok(vec![numerator, denominator]),
|
||||||
RationalBig(numerator, denominator) => Ok(vec![u32::try_from(numerator)?, u32::try_from(denominator)?]),
|
RationalBig(numerator, denominator) => {
|
||||||
|
Ok(vec![u32::try_from(numerator)?, u32::try_from(denominator)?])
|
||||||
|
}
|
||||||
|
Ifd(val) => Ok(vec![val]),
|
||||||
|
IfdBig(val) => Ok(vec![u32::try_from(val)?]),
|
||||||
Ascii(val) => Ok(val.chars().map(u32::from).collect()),
|
Ascii(val) => Ok(val.chars().map(u32::from).collect()),
|
||||||
val => Err(TiffError::FormatError(
|
val => Err(TiffError::FormatError(
|
||||||
TiffFormatError::UnsignedIntegerExpected(val),
|
TiffFormatError::UnsignedIntegerExpected(val),
|
||||||
@@ -90,6 +154,39 @@ impl Value {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn into_u8_vec(self) -> TiffResult<Vec<u8>> {
|
||||||
|
match self {
|
||||||
|
List(vec) => {
|
||||||
|
let mut new_vec = Vec::with_capacity(vec.len());
|
||||||
|
for v in vec {
|
||||||
|
new_vec.push(v.into_u8()?)
|
||||||
|
}
|
||||||
|
Ok(new_vec)
|
||||||
|
}
|
||||||
|
Byte(val) => Ok(vec![val]),
|
||||||
|
|
||||||
|
val => Err(TiffError::FormatError(
|
||||||
|
TiffFormatError::UnsignedIntegerExpected(val),
|
||||||
|
)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn into_u16_vec(self) -> TiffResult<Vec<u16>> {
|
||||||
|
match self {
|
||||||
|
List(vec) => {
|
||||||
|
let mut new_vec = Vec::with_capacity(vec.len());
|
||||||
|
for v in vec {
|
||||||
|
new_vec.push(v.into_u16()?)
|
||||||
|
}
|
||||||
|
Ok(new_vec)
|
||||||
|
}
|
||||||
|
Short(val) => Ok(vec![val]),
|
||||||
|
val => Err(TiffError::FormatError(
|
||||||
|
TiffFormatError::UnsignedIntegerExpected(val),
|
||||||
|
)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn into_i32_vec(self) -> TiffResult<Vec<i32>> {
|
pub fn into_i32_vec(self) -> TiffResult<Vec<i32>> {
|
||||||
match self {
|
match self {
|
||||||
List(vec) => {
|
List(vec) => {
|
||||||
@@ -112,13 +209,47 @@ impl Value {
|
|||||||
Signed(val) => Ok(vec![val]),
|
Signed(val) => Ok(vec![val]),
|
||||||
SignedBig(val) => Ok(vec![i32::try_from(val)?]),
|
SignedBig(val) => Ok(vec![i32::try_from(val)?]),
|
||||||
SRational(numerator, denominator) => Ok(vec![numerator, denominator]),
|
SRational(numerator, denominator) => Ok(vec![numerator, denominator]),
|
||||||
SRationalBig(numerator, denominator) => Ok(vec![i32::try_from(numerator)?, i32::try_from(denominator)?]),
|
SRationalBig(numerator, denominator) => {
|
||||||
|
Ok(vec![i32::try_from(numerator)?, i32::try_from(denominator)?])
|
||||||
|
}
|
||||||
val => Err(TiffError::FormatError(
|
val => Err(TiffError::FormatError(
|
||||||
TiffFormatError::SignedIntegerExpected(val),
|
TiffFormatError::SignedIntegerExpected(val),
|
||||||
)),
|
)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn into_f32_vec(self) -> TiffResult<Vec<f32>> {
|
||||||
|
match self {
|
||||||
|
List(vec) => {
|
||||||
|
let mut new_vec = Vec::with_capacity(vec.len());
|
||||||
|
for v in vec {
|
||||||
|
new_vec.push(v.into_f32()?)
|
||||||
|
}
|
||||||
|
Ok(new_vec)
|
||||||
|
}
|
||||||
|
Float(val) => Ok(vec![val]),
|
||||||
|
val => Err(TiffError::FormatError(
|
||||||
|
TiffFormatError::UnsignedIntegerExpected(val),
|
||||||
|
)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn into_f64_vec(self) -> TiffResult<Vec<f64>> {
|
||||||
|
match self {
|
||||||
|
List(vec) => {
|
||||||
|
let mut new_vec = Vec::with_capacity(vec.len());
|
||||||
|
for v in vec {
|
||||||
|
new_vec.push(v.into_f64()?)
|
||||||
|
}
|
||||||
|
Ok(new_vec)
|
||||||
|
}
|
||||||
|
Double(val) => Ok(vec![val]),
|
||||||
|
val => Err(TiffError::FormatError(
|
||||||
|
TiffFormatError::UnsignedIntegerExpected(val),
|
||||||
|
)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn into_u64_vec(self) -> TiffResult<Vec<u64>> {
|
pub fn into_u64_vec(self) -> TiffResult<Vec<u64>> {
|
||||||
match self {
|
match self {
|
||||||
List(vec) => {
|
List(vec) => {
|
||||||
@@ -132,6 +263,8 @@ impl Value {
|
|||||||
UnsignedBig(val) => Ok(vec![val]),
|
UnsignedBig(val) => Ok(vec![val]),
|
||||||
Rational(numerator, denominator) => Ok(vec![numerator.into(), denominator.into()]),
|
Rational(numerator, denominator) => Ok(vec![numerator.into(), denominator.into()]),
|
||||||
RationalBig(numerator, denominator) => Ok(vec![numerator, denominator]),
|
RationalBig(numerator, denominator) => Ok(vec![numerator, denominator]),
|
||||||
|
Ifd(val) => Ok(vec![val.into()]),
|
||||||
|
IfdBig(val) => Ok(vec![val]),
|
||||||
Ascii(val) => Ok(val.chars().map(u32::from).map(u64::from).collect()),
|
Ascii(val) => Ok(val.chars().map(u32::from).map(u64::from).collect()),
|
||||||
val => Err(TiffError::FormatError(
|
val => Err(TiffError::FormatError(
|
||||||
TiffFormatError::UnsignedIntegerExpected(val),
|
TiffFormatError::UnsignedIntegerExpected(val),
|
||||||
@@ -168,7 +301,7 @@ impl Value {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Entry {
|
pub struct Entry {
|
||||||
type_: Type,
|
type_: Type,
|
||||||
@@ -210,176 +343,266 @@ impl Entry {
|
|||||||
limits: &super::Limits,
|
limits: &super::Limits,
|
||||||
decoder: &mut super::Decoder<R>,
|
decoder: &mut super::Decoder<R>,
|
||||||
) -> TiffResult<Value> {
|
) -> TiffResult<Value> {
|
||||||
let bo = decoder.byte_order();
|
// Case 1: there are no values so we can return immediately.
|
||||||
let bigtiff_value = if decoder.bigtiff {
|
if self.count == 0 {
|
||||||
match (self.type_, self.count) {
|
return Ok(List(Vec::new()));
|
||||||
(Type::BYTE, 5) => Some(offset_to_bytes(5, self)?),
|
}
|
||||||
(Type::BYTE, 6) => Some(offset_to_bytes(6, self)?),
|
|
||||||
(Type::BYTE, 7) => Some(offset_to_bytes(7, self)?),
|
|
||||||
(Type::BYTE, 8) => Some(offset_to_bytes(8, self)?),
|
|
||||||
(Type::SBYTE, 5) => Some(offset_to_sbytes(5, self)?),
|
|
||||||
(Type::SBYTE, 6) => Some(offset_to_sbytes(6, self)?),
|
|
||||||
(Type::SBYTE, 7) => Some(offset_to_sbytes(7, self)?),
|
|
||||||
(Type::SBYTE, 8) => Some(offset_to_sbytes(8, self)?),
|
|
||||||
(Type::SHORT, 3) => {
|
|
||||||
let mut r = self.r(bo);
|
|
||||||
Some(List(vec![
|
|
||||||
Unsigned(u32::from(r.read_u16()?)),
|
|
||||||
Unsigned(u32::from(r.read_u16()?)),
|
|
||||||
Unsigned(u32::from(r.read_u16()?)),
|
|
||||||
]))
|
|
||||||
}
|
|
||||||
(Type::SSHORT, 3) => {
|
|
||||||
let mut r = self.r(bo);
|
|
||||||
Some(List(vec![
|
|
||||||
Signed(i32::from(r.read_i16()?)),
|
|
||||||
Signed(i32::from(r.read_i16()?)),
|
|
||||||
Signed(i32::from(r.read_i16()?)),
|
|
||||||
]))
|
|
||||||
}
|
|
||||||
(Type::SHORT, 4) => {
|
|
||||||
let mut r = self.r(bo);
|
|
||||||
Some(List(vec![
|
|
||||||
Unsigned(u32::from(r.read_u16()?)),
|
|
||||||
Unsigned(u32::from(r.read_u16()?)),
|
|
||||||
Unsigned(u32::from(r.read_u16()?)),
|
|
||||||
Unsigned(u32::from(r.read_u16()?)),
|
|
||||||
]))
|
|
||||||
}
|
|
||||||
(Type::SSHORT, 4) => {
|
|
||||||
let mut r = self.r(bo);
|
|
||||||
Some(List(vec![
|
|
||||||
Signed(i32::from(r.read_i16()?)),
|
|
||||||
Signed(i32::from(r.read_i16()?)),
|
|
||||||
Signed(i32::from(r.read_i16()?)),
|
|
||||||
Signed(i32::from(r.read_i16()?)),
|
|
||||||
]))
|
|
||||||
}
|
|
||||||
(Type::LONG, 2) => {
|
|
||||||
let mut r = self.r(bo);
|
|
||||||
Some(List(vec![
|
|
||||||
Unsigned(r.read_u32()?),
|
|
||||||
Unsigned(r.read_u32()?),
|
|
||||||
]))
|
|
||||||
}
|
|
||||||
(Type::SLONG, 2) => {
|
|
||||||
let mut r = self.r(bo);
|
|
||||||
Some(List(vec![
|
|
||||||
Signed(r.read_i32()?),
|
|
||||||
Signed(r.read_i32()?),
|
|
||||||
]))
|
|
||||||
}
|
|
||||||
(Type::RATIONAL, 1) => {
|
|
||||||
let mut r = self.r(bo);
|
|
||||||
let numerator = r.read_u32()?;
|
|
||||||
let denominator = r.read_u32()?;
|
|
||||||
Some(Rational(numerator, denominator))
|
|
||||||
}
|
|
||||||
(Type::SRATIONAL, 1) => {
|
|
||||||
let mut r = self.r(bo);
|
|
||||||
let numerator = r.read_i32()?;
|
|
||||||
let denominator = r.read_i32()?;
|
|
||||||
Some(SRational(numerator, denominator))
|
|
||||||
}
|
|
||||||
_ => None
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
};
|
|
||||||
|
|
||||||
if let Some(v) = bigtiff_value {
|
let bo = decoder.byte_order();
|
||||||
Ok(v)
|
let value_bytes = self.count
|
||||||
} else {
|
* match self.type_ {
|
||||||
match (self.type_, self.count) {
|
Type::BYTE | Type::SBYTE | Type::ASCII | Type::UNDEFINED => 1,
|
||||||
// TODO check if this could give wrong results
|
Type::SHORT | Type::SSHORT => 2,
|
||||||
// at a different endianess of file/computer.
|
Type::LONG | Type::SLONG | Type::FLOAT | Type::IFD => 4,
|
||||||
(Type::BYTE, 1) => Ok(Unsigned(u32::from(self.offset[0]))),
|
Type::LONG8
|
||||||
(Type::BYTE, 2) => offset_to_bytes(2, self),
|
| Type::SLONG8
|
||||||
(Type::BYTE, 3) => offset_to_bytes(3, self),
|
| Type::DOUBLE
|
||||||
(Type::BYTE, 4) => offset_to_bytes(4, self),
|
| Type::RATIONAL
|
||||||
(Type::BYTE, n) => self.decode_offset(n, bo, limits, decoder, |decoder| {
|
| Type::SRATIONAL
|
||||||
Ok(UnsignedBig(u64::from(decoder.read_byte()?)))
|
| Type::IFD8 => 8,
|
||||||
}),
|
Type::__NonExhaustive => unreachable!(),
|
||||||
(Type::SBYTE, 1) => Ok(Signed(i32::from(self.offset[0] as i8))),
|
};
|
||||||
(Type::SBYTE, 2) => offset_to_sbytes(2, self),
|
|
||||||
(Type::SBYTE, 3) => offset_to_sbytes(3, self),
|
// Case 2: there is one value.
|
||||||
(Type::SBYTE, 4) => offset_to_sbytes(4, self),
|
if self.count == 1 {
|
||||||
(Type::SBYTE, n) => self.decode_offset(n, bo, limits, decoder, |decoder| {
|
// 2a: the value is 5-8 bytes and we're in BigTiff mode.
|
||||||
Ok(SignedBig(i64::from(decoder.read_byte()? as i8)))
|
if decoder.bigtiff && value_bytes > 4 && value_bytes <= 8 {
|
||||||
}),
|
return Ok(match self.type_ {
|
||||||
(Type::SHORT, 1) => Ok(Unsigned(u32::from(self.r(bo).read_u16()?))),
|
Type::LONG8 => UnsignedBig(self.r(bo).read_u64()?),
|
||||||
(Type::SSHORT, 1) => Ok(Signed(i32::from(self.r(bo).read_i16()?))),
|
Type::SLONG8 => SignedBig(self.r(bo).read_i64()?),
|
||||||
(Type::SHORT, 2) => {
|
Type::DOUBLE => Double(self.r(bo).read_f64()?),
|
||||||
let mut r = self.r(bo);
|
Type::RATIONAL => {
|
||||||
Ok(List(vec![
|
let mut r = self.r(bo);
|
||||||
Unsigned(u32::from(r.read_u16()?)),
|
Rational(r.read_u32()?, r.read_u32()?)
|
||||||
Unsigned(u32::from(r.read_u16()?)),
|
|
||||||
]))
|
|
||||||
}
|
|
||||||
(Type::SSHORT, 2) => {
|
|
||||||
let mut r = self.r(bo);
|
|
||||||
Ok(List(vec![
|
|
||||||
Signed(i32::from(r.read_i16()?)),
|
|
||||||
Signed(i32::from(r.read_i16()?)),
|
|
||||||
]))
|
|
||||||
}
|
|
||||||
(Type::SHORT, n) => self.decode_offset(n, bo, limits, decoder, |decoder| {
|
|
||||||
Ok(UnsignedBig(u64::from(decoder.read_short()?)))
|
|
||||||
}),
|
|
||||||
(Type::SSHORT, n) => self.decode_offset(n, bo, limits, decoder, |decoder| {
|
|
||||||
Ok(SignedBig(i64::from(decoder.read_sshort()?)))
|
|
||||||
}),
|
|
||||||
(Type::LONG, 1) => Ok(Unsigned(self.r(bo).read_u32()?)),
|
|
||||||
(Type::SLONG, 1) => Ok(Signed(self.r(bo).read_i32()?)),
|
|
||||||
(Type::LONG, n) => self.decode_offset(n, bo, limits, decoder, |decoder| {
|
|
||||||
Ok(Unsigned(decoder.read_long()?))
|
|
||||||
}),
|
|
||||||
(Type::SLONG, n) => self.decode_offset(n, bo, limits, decoder, |decoder| {
|
|
||||||
Ok(Signed(decoder.read_slong()?))
|
|
||||||
}),
|
|
||||||
(Type::RATIONAL, n) => self.decode_offset(n, bo, limits, decoder, |decoder| {
|
|
||||||
Ok(Rational(decoder.read_long()?, decoder.read_long()?))
|
|
||||||
}),
|
|
||||||
(Type::SRATIONAL, n) => self.decode_offset(n, bo, limits, decoder, |decoder| {
|
|
||||||
Ok(SRational(decoder.read_slong()?, decoder.read_slong()?))
|
|
||||||
}),
|
|
||||||
(Type::LONG8, n) => self.decode_offset(n, bo, limits, decoder, |decoder| {
|
|
||||||
Ok(UnsignedBig(decoder.read_long8()?))
|
|
||||||
}),
|
|
||||||
(Type::ASCII, n) => {
|
|
||||||
let n = usize::try_from(n)?;
|
|
||||||
if n > limits.decoding_buffer_size {
|
|
||||||
return Err(TiffError::LimitsExceeded);
|
|
||||||
}
|
}
|
||||||
|
Type::SRATIONAL => {
|
||||||
if (n <= 4 && !decoder.bigtiff) || (n <= 8 && decoder.bigtiff) {
|
let mut r = self.r(bo);
|
||||||
let mut buf = vec![0; n];
|
SRational(r.read_i32()?, r.read_i32()?)
|
||||||
self.r(bo).read_exact(&mut buf )?;
|
|
||||||
let v = String::from_utf8(buf)?;
|
|
||||||
let v = v.trim_matches(char::from(0));
|
|
||||||
Ok(Ascii(v.into()))
|
|
||||||
} else {
|
|
||||||
if decoder.bigtiff {
|
|
||||||
decoder.goto_offset_u64(self.r(bo).read_u64()?)?
|
|
||||||
} else {
|
|
||||||
decoder.goto_offset(self.r(bo).read_u32()?)?
|
|
||||||
}
|
|
||||||
let string = decoder.read_string(n)?;
|
|
||||||
Ok(Ascii(string))
|
|
||||||
}
|
}
|
||||||
}
|
Type::IFD8 => IfdBig(self.r(bo).read_u64()?),
|
||||||
_ => Err(TiffError::UnsupportedError(
|
Type::BYTE
|
||||||
TiffUnsupportedError::UnsupportedDataType,
|
| Type::SBYTE
|
||||||
)),
|
| Type::ASCII
|
||||||
|
| Type::UNDEFINED
|
||||||
|
| Type::SHORT
|
||||||
|
| Type::SSHORT
|
||||||
|
| Type::LONG
|
||||||
|
| Type::SLONG
|
||||||
|
| Type::FLOAT
|
||||||
|
| Type::IFD => unreachable!(),
|
||||||
|
Type::__NonExhaustive => unreachable!(),
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 2b: the value is at most 4 bytes or doesn't fit in the offset field.
|
||||||
|
return Ok(match self.type_ {
|
||||||
|
Type::BYTE => Unsigned(u32::from(self.offset[0])),
|
||||||
|
Type::SBYTE => Signed(i32::from(self.offset[0] as i8)),
|
||||||
|
Type::UNDEFINED => Byte(self.offset[0]),
|
||||||
|
Type::SHORT => Unsigned(u32::from(self.r(bo).read_u16()?)),
|
||||||
|
Type::SSHORT => Signed(i32::from(self.r(bo).read_i16()?)),
|
||||||
|
Type::LONG => Unsigned(self.r(bo).read_u32()?),
|
||||||
|
Type::SLONG => Signed(self.r(bo).read_i32()?),
|
||||||
|
Type::FLOAT => Float(self.r(bo).read_f32()?),
|
||||||
|
Type::ASCII => {
|
||||||
|
if self.offset[0] == 0 {
|
||||||
|
Ascii("".to_string())
|
||||||
|
} else {
|
||||||
|
return Err(TiffError::FormatError(TiffFormatError::InvalidTag));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Type::LONG8 => {
|
||||||
|
decoder.goto_offset(self.r(bo).read_u32()?)?;
|
||||||
|
UnsignedBig(decoder.read_long8()?)
|
||||||
|
}
|
||||||
|
Type::SLONG8 => {
|
||||||
|
decoder.goto_offset(self.r(bo).read_u32()?)?;
|
||||||
|
SignedBig(decoder.read_slong8()?)
|
||||||
|
}
|
||||||
|
Type::DOUBLE => {
|
||||||
|
decoder.goto_offset(self.r(bo).read_u32()?)?;
|
||||||
|
Double(decoder.read_double()?)
|
||||||
|
}
|
||||||
|
Type::RATIONAL => {
|
||||||
|
decoder.goto_offset(self.r(bo).read_u32()?)?;
|
||||||
|
Rational(decoder.read_long()?, decoder.read_long()?)
|
||||||
|
}
|
||||||
|
Type::SRATIONAL => {
|
||||||
|
decoder.goto_offset(self.r(bo).read_u32()?)?;
|
||||||
|
SRational(decoder.read_slong()?, decoder.read_slong()?)
|
||||||
|
}
|
||||||
|
Type::IFD => Ifd(self.r(bo).read_u32()?),
|
||||||
|
Type::IFD8 => {
|
||||||
|
decoder.goto_offset(self.r(bo).read_u32()?)?;
|
||||||
|
IfdBig(decoder.read_long8()?)
|
||||||
|
}
|
||||||
|
Type::__NonExhaustive => unreachable!(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Case 3: There is more than one value, but it fits in the offset field.
|
||||||
|
if value_bytes <= 4 || decoder.bigtiff && value_bytes <= 8 {
|
||||||
|
match self.type_ {
|
||||||
|
Type::BYTE => return Ok(offset_to_bytes(self.count as usize, self)?),
|
||||||
|
Type::SBYTE => return Ok(offset_to_sbytes(self.count as usize, self)?),
|
||||||
|
Type::ASCII => {
|
||||||
|
let mut buf = vec![0; self.count as usize];
|
||||||
|
self.r(bo).read_exact(&mut buf)?;
|
||||||
|
if buf.is_ascii() && buf.ends_with(&[0]) {
|
||||||
|
let v = str::from_utf8(&buf)?;
|
||||||
|
let v = v.trim_matches(char::from(0));
|
||||||
|
return Ok(Ascii(v.into()));
|
||||||
|
} else {
|
||||||
|
return Err(TiffError::FormatError(TiffFormatError::InvalidTag));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Type::UNDEFINED => {
|
||||||
|
return Ok(List(
|
||||||
|
self.offset[0..self.count as usize]
|
||||||
|
.iter()
|
||||||
|
.map(|&b| Byte(b))
|
||||||
|
.collect(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
Type::SHORT => {
|
||||||
|
let mut r = self.r(bo);
|
||||||
|
let mut v = Vec::new();
|
||||||
|
for _ in 0..self.count {
|
||||||
|
v.push(Short(r.read_u16()?));
|
||||||
|
}
|
||||||
|
return Ok(List(v));
|
||||||
|
}
|
||||||
|
Type::SSHORT => {
|
||||||
|
let mut r = self.r(bo);
|
||||||
|
let mut v = Vec::new();
|
||||||
|
for _ in 0..self.count {
|
||||||
|
v.push(Signed(i32::from(r.read_i16()?)));
|
||||||
|
}
|
||||||
|
return Ok(List(v));
|
||||||
|
}
|
||||||
|
Type::LONG => {
|
||||||
|
let mut r = self.r(bo);
|
||||||
|
let mut v = Vec::new();
|
||||||
|
for _ in 0..self.count {
|
||||||
|
v.push(Unsigned(r.read_u32()?));
|
||||||
|
}
|
||||||
|
return Ok(List(v));
|
||||||
|
}
|
||||||
|
Type::SLONG => {
|
||||||
|
let mut r = self.r(bo);
|
||||||
|
let mut v = Vec::new();
|
||||||
|
for _ in 0..self.count {
|
||||||
|
v.push(Signed(r.read_i32()?));
|
||||||
|
}
|
||||||
|
return Ok(List(v));
|
||||||
|
}
|
||||||
|
Type::FLOAT => {
|
||||||
|
let mut r = self.r(bo);
|
||||||
|
let mut v = Vec::new();
|
||||||
|
for _ in 0..self.count {
|
||||||
|
v.push(Float(r.read_f32()?));
|
||||||
|
}
|
||||||
|
return Ok(List(v));
|
||||||
|
}
|
||||||
|
Type::IFD => {
|
||||||
|
let mut r = self.r(bo);
|
||||||
|
let mut v = Vec::new();
|
||||||
|
for _ in 0..self.count {
|
||||||
|
v.push(Ifd(r.read_u32()?));
|
||||||
|
}
|
||||||
|
return Ok(List(v));
|
||||||
|
}
|
||||||
|
Type::LONG8
|
||||||
|
| Type::SLONG8
|
||||||
|
| Type::RATIONAL
|
||||||
|
| Type::SRATIONAL
|
||||||
|
| Type::DOUBLE
|
||||||
|
| Type::IFD8 => {
|
||||||
|
unreachable!()
|
||||||
|
}
|
||||||
|
Type::__NonExhaustive => unreachable!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Case 4: there is more than one value, and it doesn't fit in the offset field.
|
||||||
|
match self.type_ {
|
||||||
|
// TODO check if this could give wrong results
|
||||||
|
// at a different endianess of file/computer.
|
||||||
|
Type::BYTE => self.decode_offset(self.count, bo, limits, decoder, |decoder| {
|
||||||
|
Ok(UnsignedBig(u64::from(decoder.read_byte()?)))
|
||||||
|
}),
|
||||||
|
Type::SBYTE => self.decode_offset(self.count, bo, limits, decoder, |decoder| {
|
||||||
|
Ok(SignedBig(i64::from(decoder.read_byte()? as i8)))
|
||||||
|
}),
|
||||||
|
Type::SHORT => self.decode_offset(self.count, bo, limits, decoder, |decoder| {
|
||||||
|
Ok(UnsignedBig(u64::from(decoder.read_short()?)))
|
||||||
|
}),
|
||||||
|
Type::SSHORT => self.decode_offset(self.count, bo, limits, decoder, |decoder| {
|
||||||
|
Ok(SignedBig(i64::from(decoder.read_sshort()?)))
|
||||||
|
}),
|
||||||
|
Type::LONG => self.decode_offset(self.count, bo, limits, decoder, |decoder| {
|
||||||
|
Ok(Unsigned(decoder.read_long()?))
|
||||||
|
}),
|
||||||
|
Type::SLONG => self.decode_offset(self.count, bo, limits, decoder, |decoder| {
|
||||||
|
Ok(Signed(decoder.read_slong()?))
|
||||||
|
}),
|
||||||
|
Type::FLOAT => self.decode_offset(self.count, bo, limits, decoder, |decoder| {
|
||||||
|
Ok(Float(decoder.read_float()?))
|
||||||
|
}),
|
||||||
|
Type::DOUBLE => self.decode_offset(self.count, bo, limits, decoder, |decoder| {
|
||||||
|
Ok(Double(decoder.read_double()?))
|
||||||
|
}),
|
||||||
|
Type::RATIONAL => self.decode_offset(self.count, bo, limits, decoder, |decoder| {
|
||||||
|
Ok(Rational(decoder.read_long()?, decoder.read_long()?))
|
||||||
|
}),
|
||||||
|
Type::SRATIONAL => self.decode_offset(self.count, bo, limits, decoder, |decoder| {
|
||||||
|
Ok(SRational(decoder.read_slong()?, decoder.read_slong()?))
|
||||||
|
}),
|
||||||
|
Type::LONG8 => self.decode_offset(self.count, bo, limits, decoder, |decoder| {
|
||||||
|
Ok(UnsignedBig(decoder.read_long8()?))
|
||||||
|
}),
|
||||||
|
Type::SLONG8 => self.decode_offset(self.count, bo, limits, decoder, |decoder| {
|
||||||
|
Ok(SignedBig(decoder.read_slong8()?))
|
||||||
|
}),
|
||||||
|
Type::IFD => self.decode_offset(self.count, bo, limits, decoder, |decoder| {
|
||||||
|
Ok(Ifd(decoder.read_long()?))
|
||||||
|
}),
|
||||||
|
Type::IFD8 => self.decode_offset(self.count, bo, limits, decoder, |decoder| {
|
||||||
|
Ok(IfdBig(decoder.read_long8()?))
|
||||||
|
}),
|
||||||
|
Type::UNDEFINED => self.decode_offset(self.count, bo, limits, decoder, |decoder| {
|
||||||
|
Ok(Byte(u8::from(decoder.read_byte()?)))
|
||||||
|
}),
|
||||||
|
Type::ASCII => {
|
||||||
|
let n = usize::try_from(self.count)?;
|
||||||
|
if n > limits.decoding_buffer_size {
|
||||||
|
return Err(TiffError::LimitsExceeded);
|
||||||
|
}
|
||||||
|
|
||||||
|
if decoder.bigtiff {
|
||||||
|
decoder.goto_offset_u64(self.r(bo).read_u64()?)?
|
||||||
|
} else {
|
||||||
|
decoder.goto_offset(self.r(bo).read_u32()?)?
|
||||||
|
}
|
||||||
|
let string = decoder.read_string(n)?;
|
||||||
|
Ok(Ascii(string))
|
||||||
|
}
|
||||||
|
Type::__NonExhaustive => unreachable!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn decode_offset<R, F>(&self, value_count: u64, bo: ByteOrder, limits: &super::Limits, decoder: &mut super::Decoder<R>, decode_fn: F) -> TiffResult<Value>
|
fn decode_offset<R, F>(
|
||||||
where
|
&self,
|
||||||
R: Read + Seek,
|
value_count: u64,
|
||||||
F: Fn(&mut super::Decoder<R>) -> TiffResult<Value>,
|
bo: ByteOrder,
|
||||||
|
limits: &super::Limits,
|
||||||
|
decoder: &mut super::Decoder<R>,
|
||||||
|
decode_fn: F,
|
||||||
|
) -> TiffResult<Value>
|
||||||
|
where
|
||||||
|
R: Read + Seek,
|
||||||
|
F: Fn(&mut super::Decoder<R>) -> TiffResult<Value>,
|
||||||
{
|
{
|
||||||
let value_count = usize::try_from(value_count)?;
|
let value_count = usize::try_from(value_count)?;
|
||||||
if value_count > limits.decoding_buffer_size / mem::size_of::<Value>() {
|
if value_count > limits.decoding_buffer_size / mem::size_of::<Value>() {
|
||||||
@@ -406,7 +629,7 @@ fn offset_to_bytes(n: usize, entry: &Entry) -> TiffResult<Value> {
|
|||||||
entry.offset[0..n]
|
entry.offset[0..n]
|
||||||
.iter()
|
.iter()
|
||||||
.map(|&e| Unsigned(u32::from(e)))
|
.map(|&e| Unsigned(u32::from(e)))
|
||||||
.collect()
|
.collect(),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -417,7 +640,7 @@ fn offset_to_sbytes(n: usize, entry: &Entry) -> TiffResult<Value> {
|
|||||||
entry.offset[0..n]
|
entry.offset[0..n]
|
||||||
.iter()
|
.iter()
|
||||||
.map(|&e| Signed(i32::from(e as i8)))
|
.map(|&e| Signed(i32::from(e as i8)))
|
||||||
.collect()
|
.collect(),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+974
-219
File diff suppressed because it is too large.
Load diff
+201
-166
@@ -1,9 +1,5 @@
|
|||||||
//! All IO functionality needed for TIFF decoding
|
//! All IO functionality needed for TIFF decoding
|
||||||
|
|
||||||
use crate::error::{TiffError, TiffResult};
|
|
||||||
use byteorder::{BigEndian, LittleEndian, ReadBytesExt};
|
|
||||||
use lzw;
|
|
||||||
use miniz_oxide::inflate;
|
|
||||||
use std::io::{self, Read, Seek};
|
use std::io::{self, Read, Seek};
|
||||||
|
|
||||||
/// Byte order of the TIFF file.
|
/// Byte order of the TIFF file.
|
||||||
@@ -23,135 +19,120 @@ pub trait EndianReader: Read {
|
|||||||
/// Reads an u16
|
/// Reads an u16
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn read_u16(&mut self) -> Result<u16, io::Error> {
|
fn read_u16(&mut self) -> Result<u16, io::Error> {
|
||||||
match self.byte_order() {
|
let mut n = [0u8; 2];
|
||||||
ByteOrder::LittleEndian => <Self as ReadBytesExt>::read_u16::<LittleEndian>(self),
|
self.read_exact(&mut n)?;
|
||||||
ByteOrder::BigEndian => <Self as ReadBytesExt>::read_u16::<BigEndian>(self),
|
Ok(match self.byte_order() {
|
||||||
}
|
ByteOrder::LittleEndian => u16::from_le_bytes(n),
|
||||||
|
ByteOrder::BigEndian => u16::from_be_bytes(n),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Reads an i8
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn read_u16_into(&mut self, buffer: &mut [u16]) -> Result<(), io::Error> {
|
fn read_i8(&mut self) -> Result<i8, io::Error> {
|
||||||
match self.byte_order() {
|
let mut n = [0u8; 1];
|
||||||
ByteOrder::LittleEndian => {
|
self.read_exact(&mut n)?;
|
||||||
<Self as ReadBytesExt>::read_u16_into::<LittleEndian>(self, buffer)
|
Ok(match self.byte_order() {
|
||||||
}
|
ByteOrder::LittleEndian => i8::from_le_bytes(n),
|
||||||
ByteOrder::BigEndian => {
|
ByteOrder::BigEndian => i8::from_be_bytes(n),
|
||||||
<Self as ReadBytesExt>::read_u16_into::<BigEndian>(self, buffer)
|
})
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Reads an i16
|
/// Reads an i16
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn read_i16(&mut self) -> Result<i16, io::Error> {
|
fn read_i16(&mut self) -> Result<i16, io::Error> {
|
||||||
match self.byte_order() {
|
let mut n = [0u8; 2];
|
||||||
ByteOrder::LittleEndian => <Self as ReadBytesExt>::read_i16::<LittleEndian>(self),
|
self.read_exact(&mut n)?;
|
||||||
ByteOrder::BigEndian => <Self as ReadBytesExt>::read_i16::<BigEndian>(self),
|
Ok(match self.byte_order() {
|
||||||
}
|
ByteOrder::LittleEndian => i16::from_le_bytes(n),
|
||||||
|
ByteOrder::BigEndian => i16::from_be_bytes(n),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Reads an u32
|
/// Reads an u32
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn read_u32(&mut self) -> Result<u32, io::Error> {
|
fn read_u32(&mut self) -> Result<u32, io::Error> {
|
||||||
match self.byte_order() {
|
let mut n = [0u8; 4];
|
||||||
ByteOrder::LittleEndian => <Self as ReadBytesExt>::read_u32::<LittleEndian>(self),
|
self.read_exact(&mut n)?;
|
||||||
ByteOrder::BigEndian => <Self as ReadBytesExt>::read_u32::<BigEndian>(self),
|
Ok(match self.byte_order() {
|
||||||
}
|
ByteOrder::LittleEndian => u32::from_le_bytes(n),
|
||||||
}
|
ByteOrder::BigEndian => u32::from_be_bytes(n),
|
||||||
|
})
|
||||||
#[inline(always)]
|
|
||||||
fn read_u32_into(&mut self, buffer: &mut [u32]) -> Result<(), io::Error> {
|
|
||||||
match self.byte_order() {
|
|
||||||
ByteOrder::LittleEndian => {
|
|
||||||
<Self as ReadBytesExt>::read_u32_into::<LittleEndian>(self, buffer)
|
|
||||||
}
|
|
||||||
ByteOrder::BigEndian => {
|
|
||||||
<Self as ReadBytesExt>::read_u32_into::<BigEndian>(self, buffer)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Reads an u64
|
|
||||||
#[inline(always)]
|
|
||||||
fn read_u64(&mut self) -> Result<u64, io::Error> {
|
|
||||||
match self.byte_order() {
|
|
||||||
ByteOrder::LittleEndian => <Self as ReadBytesExt>::read_u64::<LittleEndian>(self),
|
|
||||||
ByteOrder::BigEndian => <Self as ReadBytesExt>::read_u64::<BigEndian>(self),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline(always)]
|
|
||||||
fn read_u64_into(&mut self, buffer: &mut [u64]) -> Result<(), io::Error> {
|
|
||||||
match self.byte_order() {
|
|
||||||
ByteOrder::LittleEndian => {
|
|
||||||
<Self as ReadBytesExt>::read_u64_into::<LittleEndian>(self, buffer)
|
|
||||||
}
|
|
||||||
ByteOrder::BigEndian => {
|
|
||||||
<Self as ReadBytesExt>::read_u64_into::<BigEndian>(self, buffer)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Reads an i32
|
/// Reads an i32
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn read_i32(&mut self) -> Result<i32, io::Error> {
|
fn read_i32(&mut self) -> Result<i32, io::Error> {
|
||||||
match self.byte_order() {
|
let mut n = [0u8; 4];
|
||||||
ByteOrder::LittleEndian => <Self as ReadBytesExt>::read_i32::<LittleEndian>(self),
|
self.read_exact(&mut n)?;
|
||||||
ByteOrder::BigEndian => <Self as ReadBytesExt>::read_i32::<BigEndian>(self),
|
Ok(match self.byte_order() {
|
||||||
}
|
ByteOrder::LittleEndian => i32::from_le_bytes(n),
|
||||||
|
ByteOrder::BigEndian => i32::from_be_bytes(n),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Reads an u64
|
||||||
|
#[inline(always)]
|
||||||
|
fn read_u64(&mut self) -> Result<u64, io::Error> {
|
||||||
|
let mut n = [0u8; 8];
|
||||||
|
self.read_exact(&mut n)?;
|
||||||
|
Ok(match self.byte_order() {
|
||||||
|
ByteOrder::LittleEndian => u64::from_le_bytes(n),
|
||||||
|
ByteOrder::BigEndian => u64::from_be_bytes(n),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Reads an i64
|
||||||
|
#[inline(always)]
|
||||||
|
fn read_i64(&mut self) -> Result<i64, io::Error> {
|
||||||
|
let mut n = [0u8; 8];
|
||||||
|
self.read_exact(&mut n)?;
|
||||||
|
Ok(match self.byte_order() {
|
||||||
|
ByteOrder::LittleEndian => i64::from_le_bytes(n),
|
||||||
|
ByteOrder::BigEndian => i64::from_be_bytes(n),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Reads an f32
|
||||||
|
#[inline(always)]
|
||||||
|
fn read_f32(&mut self) -> Result<f32, io::Error> {
|
||||||
|
let mut n = [0u8; 4];
|
||||||
|
self.read_exact(&mut n)?;
|
||||||
|
Ok(f32::from_bits(match self.byte_order() {
|
||||||
|
ByteOrder::LittleEndian => u32::from_le_bytes(n),
|
||||||
|
ByteOrder::BigEndian => u32::from_be_bytes(n),
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Reads an f64
|
||||||
|
#[inline(always)]
|
||||||
|
fn read_f64(&mut self) -> Result<f64, io::Error> {
|
||||||
|
let mut n = [0u8; 8];
|
||||||
|
self.read_exact(&mut n)?;
|
||||||
|
Ok(f64::from_bits(match self.byte_order() {
|
||||||
|
ByteOrder::LittleEndian => u64::from_le_bytes(n),
|
||||||
|
ByteOrder::BigEndian => u64::from_be_bytes(n),
|
||||||
|
}))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Reader that decompresses DEFLATE streams
|
///
|
||||||
pub struct DeflateReader {
|
/// # READERS
|
||||||
buffer: io::Cursor<Vec<u8>>,
|
///
|
||||||
byte_order: ByteOrder,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl DeflateReader {
|
///
|
||||||
pub fn new<R: Read + Seek>(
|
/// ## Deflate Reader
|
||||||
reader: &mut SmartReader<R>,
|
///
|
||||||
max_uncompressed_length: usize,
|
|
||||||
) -> TiffResult<(usize, Self)> {
|
|
||||||
let byte_order = reader.byte_order;
|
|
||||||
let mut compressed = Vec::new();
|
|
||||||
reader.read_to_end(&mut compressed)?;
|
|
||||||
|
|
||||||
// TODO: Implement streaming compression, and remove this (temporary) and somewhat
|
pub type DeflateReader<'r, R> = flate2::read::ZlibDecoder<&'r mut SmartReader<R>>;
|
||||||
// misleading workaround.
|
|
||||||
if compressed.len() > max_uncompressed_length {
|
|
||||||
return Err(TiffError::LimitsExceeded);
|
|
||||||
}
|
|
||||||
|
|
||||||
let uncompressed =
|
///
|
||||||
inflate::decompress_to_vec_zlib(&compressed).map_err(TiffError::from_inflate_status)?;
|
/// ## LZW Reader
|
||||||
|
///
|
||||||
Ok((
|
|
||||||
uncompressed.len(),
|
|
||||||
Self {
|
|
||||||
byte_order,
|
|
||||||
buffer: io::Cursor::new(uncompressed),
|
|
||||||
},
|
|
||||||
))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Read for DeflateReader {
|
|
||||||
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
|
||||||
self.buffer.read(buf)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl EndianReader for DeflateReader {
|
|
||||||
fn byte_order(&self) -> ByteOrder {
|
|
||||||
self.byte_order
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Reader that decompresses LZW streams
|
/// Reader that decompresses LZW streams
|
||||||
pub struct LZWReader {
|
pub struct LZWReader {
|
||||||
buffer: io::Cursor<Vec<u8>>,
|
buffer: io::Cursor<Vec<u8>>,
|
||||||
byte_order: ByteOrder,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl LZWReader {
|
impl LZWReader {
|
||||||
@@ -164,24 +145,40 @@ impl LZWReader {
|
|||||||
where
|
where
|
||||||
R: Read + Seek,
|
R: Read + Seek,
|
||||||
{
|
{
|
||||||
let order = reader.byte_order;
|
|
||||||
let mut compressed = vec![0; compressed_length as usize];
|
let mut compressed = vec![0; compressed_length as usize];
|
||||||
reader.read_exact(&mut compressed[..])?;
|
reader.read_exact(&mut compressed[..])?;
|
||||||
let mut uncompressed = Vec::with_capacity(max_uncompressed_length);
|
let mut uncompressed = vec![0; max_uncompressed_length];
|
||||||
let mut decoder = lzw::DecoderEarlyChange::new(lzw::MsbReader::new(), 8);
|
let mut decoder = weezl::decode::Decoder::with_tiff_size_switch(weezl::BitOrder::Msb, 8);
|
||||||
|
|
||||||
let mut bytes_read = 0;
|
let mut bytes_read = 0;
|
||||||
while bytes_read < compressed_length && uncompressed.len() < max_uncompressed_length {
|
let mut bytes_written = 0;
|
||||||
let (len, bytes) = decoder.decode_bytes(&compressed[bytes_read..])?;
|
|
||||||
bytes_read += len;
|
while bytes_written < max_uncompressed_length {
|
||||||
uncompressed.extend_from_slice(bytes);
|
let result = decoder.decode_bytes(
|
||||||
|
&compressed[bytes_read..],
|
||||||
|
&mut uncompressed[bytes_written..],
|
||||||
|
);
|
||||||
|
bytes_read += result.consumed_in;
|
||||||
|
bytes_written += result.consumed_out;
|
||||||
|
|
||||||
|
match result.status {
|
||||||
|
Ok(weezl::LzwStatus::Ok) => {}
|
||||||
|
Ok(weezl::LzwStatus::Done) => break,
|
||||||
|
Ok(weezl::LzwStatus::NoProgress) => {
|
||||||
|
return Err(io::Error::new(
|
||||||
|
io::ErrorKind::UnexpectedEof,
|
||||||
|
"no lzw end code found",
|
||||||
|
))
|
||||||
|
}
|
||||||
|
Err(err) => return Err(io::Error::new(io::ErrorKind::InvalidData, err)),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let bytes = uncompressed.len();
|
uncompressed.resize(bytes_written, 0);
|
||||||
Ok((
|
Ok((
|
||||||
bytes,
|
uncompressed.len(),
|
||||||
LZWReader {
|
LZWReader {
|
||||||
buffer: io::Cursor::new(uncompressed),
|
buffer: io::Cursor::new(uncompressed),
|
||||||
byte_order: order,
|
|
||||||
},
|
},
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
@@ -194,68 +191,110 @@ impl Read for LZWReader {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl EndianReader for LZWReader {
|
///
|
||||||
#[inline(always)]
|
/// ## JPEG Reader (for "new-style" JPEG format (TIFF compression tag 7))
|
||||||
fn byte_order(&self) -> ByteOrder {
|
///
|
||||||
self.byte_order
|
|
||||||
|
pub(crate) struct JpegReader {
|
||||||
|
buffer: io::Cursor<Vec<u8>>,
|
||||||
|
}
|
||||||
|
impl JpegReader {
|
||||||
|
/// Constructs new JpegReader wrapping a SmartReader.
|
||||||
|
/// Because JPEG compression in TIFF allows to save quantization and/or huffman tables in one
|
||||||
|
/// central location, the constructor accepts this data as `jpeg_tables` here containing either
|
||||||
|
/// or both.
|
||||||
|
/// These `jpeg_tables` are simply prepended to the remaining jpeg image data.
|
||||||
|
/// Because these `jpeg_tables` start with a `SOI` (HEX: `0xFFD8`) or __start of image__ marker
|
||||||
|
/// which is also at the beginning of the remaining JPEG image data and would
|
||||||
|
/// confuse the JPEG renderer, one of these has to be taken off. In this case the first two
|
||||||
|
/// bytes of the remaining JPEG data is removed because it follows `jpeg_tables`.
|
||||||
|
/// Similary, `jpeg_tables` ends with a `EOI` (HEX: `0xFFD9`) or __end of image__ marker,
|
||||||
|
/// this has to be removed as well (last two bytes of `jpeg_tables`).
|
||||||
|
|
||||||
|
pub fn new<R>(
|
||||||
|
reader: &mut SmartReader<R>,
|
||||||
|
length: u32,
|
||||||
|
jpeg_tables: &Option<Vec<u8>>,
|
||||||
|
) -> io::Result<JpegReader>
|
||||||
|
where
|
||||||
|
R: Read + Seek,
|
||||||
|
{
|
||||||
|
// Read jpeg image data
|
||||||
|
let mut segment = vec![0; length as usize];
|
||||||
|
reader.read_exact(&mut segment[..])?;
|
||||||
|
|
||||||
|
match jpeg_tables {
|
||||||
|
Some(tables) => {
|
||||||
|
let mut jpeg_data = tables.clone();
|
||||||
|
let truncated_length = jpeg_data.len() - 2;
|
||||||
|
jpeg_data.truncate(truncated_length);
|
||||||
|
jpeg_data.extend_from_slice(&segment[2..]);
|
||||||
|
|
||||||
|
Ok(JpegReader {
|
||||||
|
buffer: io::Cursor::new(jpeg_data),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
None => Ok(JpegReader {
|
||||||
|
buffer: io::Cursor::new(segment),
|
||||||
|
}),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Read for JpegReader {
|
||||||
|
// #[inline]
|
||||||
|
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
||||||
|
self.buffer.read(buf)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
///
|
||||||
|
/// ## PackBits Reader
|
||||||
|
///
|
||||||
|
|
||||||
/// Reader that unpacks Apple's `PackBits` format
|
/// Reader that unpacks Apple's `PackBits` format
|
||||||
pub struct PackBitsReader {
|
pub struct PackBitsReader {
|
||||||
buffer: io::Cursor<Vec<u8>>,
|
buffer: io::Cursor<Vec<u8>>,
|
||||||
byte_order: ByteOrder,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PackBitsReader {
|
impl PackBitsReader {
|
||||||
/// Wraps a reader
|
/// Wraps a reader
|
||||||
pub fn new<R: Read + Seek>(
|
pub fn new<R: Read + Seek>(
|
||||||
mut reader: R,
|
mut reader: R,
|
||||||
byte_order: ByteOrder,
|
|
||||||
length: usize,
|
length: usize,
|
||||||
) -> io::Result<(usize, PackBitsReader)> {
|
) -> io::Result<(usize, PackBitsReader)> {
|
||||||
let mut buffer = Vec::new();
|
let mut buffer = Vec::new();
|
||||||
let mut read: usize = 0;
|
let mut header: [u8; 1] = [0];
|
||||||
while read < length {
|
let mut data: [u8; 1] = [0];
|
||||||
let lread = read_packbits_run(&mut reader, &mut buffer)?;
|
|
||||||
if lread == 0 {
|
|
||||||
return Err(io::ErrorKind::UnexpectedEof.into());
|
|
||||||
}
|
|
||||||
read += lread;
|
|
||||||
}
|
|
||||||
Ok((
|
|
||||||
buffer.len(),
|
|
||||||
PackBitsReader {
|
|
||||||
buffer: io::Cursor::new(buffer),
|
|
||||||
byte_order,
|
|
||||||
},
|
|
||||||
))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn read_packbits_run<R: Read + Seek>(reader: &mut R, buffer: &mut Vec<u8>) -> io::Result<usize> {
|
let mut bytes_read = 0;
|
||||||
let mut header: [u8; 1] = [0];
|
while bytes_read < length {
|
||||||
|
reader.read_exact(&mut header)?;
|
||||||
|
bytes_read += 1;
|
||||||
|
|
||||||
let bytes = reader.read(&mut header)?;
|
let h = header[0] as i8;
|
||||||
|
if h >= -127 && h <= -1 {
|
||||||
match bytes {
|
|
||||||
0 => Ok(0),
|
|
||||||
_ => match header[0] as i8 {
|
|
||||||
-128 => Ok(1),
|
|
||||||
h if h >= -127 && h <= -1 => {
|
|
||||||
let new_len = buffer.len() + (1 - h as isize) as usize;
|
let new_len = buffer.len() + (1 - h as isize) as usize;
|
||||||
reader.read_exact(&mut header)?;
|
reader.read_exact(&mut data)?;
|
||||||
buffer.resize(new_len, header[0]);
|
buffer.resize(new_len, data[0]);
|
||||||
Ok(2)
|
bytes_read += 1;
|
||||||
}
|
} else if h >= 0 {
|
||||||
h => {
|
|
||||||
let num_vals = h as usize + 1;
|
let num_vals = h as usize + 1;
|
||||||
let start = buffer.len();
|
let start = buffer.len();
|
||||||
buffer.resize(start + num_vals, 0);
|
buffer.resize(start + num_vals, 0);
|
||||||
reader.read_exact(&mut buffer[start..])?;
|
reader.read_exact(&mut buffer[start..])?;
|
||||||
Ok(num_vals + 1)
|
bytes_read += num_vals
|
||||||
|
} else {
|
||||||
|
// h = -128 is a no-op.
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
|
|
||||||
|
Ok((
|
||||||
|
buffer.len(),
|
||||||
|
PackBitsReader {
|
||||||
|
buffer: io::Cursor::new(buffer),
|
||||||
|
},
|
||||||
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -266,18 +305,15 @@ impl Read for PackBitsReader {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl EndianReader for PackBitsReader {
|
///
|
||||||
#[inline(always)]
|
/// ## SmartReader Reader
|
||||||
fn byte_order(&self) -> ByteOrder {
|
///
|
||||||
self.byte_order
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Reader that is aware of the byte order.
|
/// Reader that is aware of the byte order.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct SmartReader<R>
|
pub struct SmartReader<R>
|
||||||
where
|
where
|
||||||
R: Read + Seek,
|
R: Read,
|
||||||
{
|
{
|
||||||
reader: R,
|
reader: R,
|
||||||
pub byte_order: ByteOrder,
|
pub byte_order: ByteOrder,
|
||||||
@@ -285,7 +321,7 @@ where
|
|||||||
|
|
||||||
impl<R> SmartReader<R>
|
impl<R> SmartReader<R>
|
||||||
where
|
where
|
||||||
R: Read + Seek,
|
R: Read,
|
||||||
{
|
{
|
||||||
/// Wraps a reader
|
/// Wraps a reader
|
||||||
pub fn wrap(reader: R, byte_order: ByteOrder) -> SmartReader<R> {
|
pub fn wrap(reader: R, byte_order: ByteOrder) -> SmartReader<R> {
|
||||||
@@ -295,7 +331,7 @@ where
|
|||||||
|
|
||||||
impl<R> EndianReader for SmartReader<R>
|
impl<R> EndianReader for SmartReader<R>
|
||||||
where
|
where
|
||||||
R: Read + Seek,
|
R: Read,
|
||||||
{
|
{
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
fn byte_order(&self) -> ByteOrder {
|
fn byte_order(&self) -> ByteOrder {
|
||||||
@@ -303,7 +339,7 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<R: Read + Seek> Read for SmartReader<R> {
|
impl<R: Read> Read for SmartReader<R> {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
||||||
self.reader.read(buf)
|
self.reader.read(buf)
|
||||||
@@ -330,8 +366,7 @@ mod test {
|
|||||||
let encoded_len = encoded.len();
|
let encoded_len = encoded.len();
|
||||||
|
|
||||||
let buff = io::Cursor::new(encoded);
|
let buff = io::Cursor::new(encoded);
|
||||||
let (_, mut decoder) =
|
let (_, mut decoder) = PackBitsReader::new(buff, encoded_len).unwrap();
|
||||||
PackBitsReader::new(buff, ByteOrder::LittleEndian, encoded_len).unwrap();
|
|
||||||
|
|
||||||
let mut decoded = Vec::new();
|
let mut decoded = Vec::new();
|
||||||
decoder.read_to_end(&mut decoded).unwrap();
|
decoder.read_to_end(&mut decoded).unwrap();
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
use tags::PhotometricInterpretation;
|
use crate::tags::{PhotometricInterpretation, SampleFormat};
|
||||||
|
|
||||||
/// Trait for different colortypes that can be encoded.
|
/// Trait for different colortypes that can be encoded.
|
||||||
pub trait ColorType {
|
pub trait ColorType {
|
||||||
@@ -8,6 +8,8 @@ pub trait ColorType {
|
|||||||
const TIFF_VALUE: PhotometricInterpretation;
|
const TIFF_VALUE: PhotometricInterpretation;
|
||||||
/// The value of the tiff tag `BitsPerSample`
|
/// The value of the tiff tag `BitsPerSample`
|
||||||
const BITS_PER_SAMPLE: &'static [u16];
|
const BITS_PER_SAMPLE: &'static [u16];
|
||||||
|
/// The value of the tiff tag `SampleFormat`
|
||||||
|
const SAMPLE_FORMAT: &'static [SampleFormat];
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Gray8;
|
pub struct Gray8;
|
||||||
@@ -15,6 +17,7 @@ impl ColorType for Gray8 {
|
|||||||
type Inner = u8;
|
type Inner = u8;
|
||||||
const TIFF_VALUE: PhotometricInterpretation = PhotometricInterpretation::BlackIsZero;
|
const TIFF_VALUE: PhotometricInterpretation = PhotometricInterpretation::BlackIsZero;
|
||||||
const BITS_PER_SAMPLE: &'static [u16] = &[8];
|
const BITS_PER_SAMPLE: &'static [u16] = &[8];
|
||||||
|
const SAMPLE_FORMAT: &'static [SampleFormat] = &[SampleFormat::Uint];
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Gray16;
|
pub struct Gray16;
|
||||||
@@ -22,6 +25,7 @@ impl ColorType for Gray16 {
|
|||||||
type Inner = u16;
|
type Inner = u16;
|
||||||
const TIFF_VALUE: PhotometricInterpretation = PhotometricInterpretation::BlackIsZero;
|
const TIFF_VALUE: PhotometricInterpretation = PhotometricInterpretation::BlackIsZero;
|
||||||
const BITS_PER_SAMPLE: &'static [u16] = &[16];
|
const BITS_PER_SAMPLE: &'static [u16] = &[16];
|
||||||
|
const SAMPLE_FORMAT: &'static [SampleFormat] = &[SampleFormat::Uint];
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Gray32;
|
pub struct Gray32;
|
||||||
@@ -29,6 +33,15 @@ impl ColorType for Gray32 {
|
|||||||
type Inner = u32;
|
type Inner = u32;
|
||||||
const TIFF_VALUE: PhotometricInterpretation = PhotometricInterpretation::BlackIsZero;
|
const TIFF_VALUE: PhotometricInterpretation = PhotometricInterpretation::BlackIsZero;
|
||||||
const BITS_PER_SAMPLE: &'static [u16] = &[32];
|
const BITS_PER_SAMPLE: &'static [u16] = &[32];
|
||||||
|
const SAMPLE_FORMAT: &'static [SampleFormat] = &[SampleFormat::Uint];
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Gray32Float;
|
||||||
|
impl ColorType for Gray32Float {
|
||||||
|
type Inner = f32;
|
||||||
|
const TIFF_VALUE: PhotometricInterpretation = PhotometricInterpretation::BlackIsZero;
|
||||||
|
const BITS_PER_SAMPLE: &'static [u16] = &[32];
|
||||||
|
const SAMPLE_FORMAT: &'static [SampleFormat] = &[SampleFormat::IEEEFP];
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Gray64;
|
pub struct Gray64;
|
||||||
@@ -36,6 +49,15 @@ impl ColorType for Gray64 {
|
|||||||
type Inner = u64;
|
type Inner = u64;
|
||||||
const TIFF_VALUE: PhotometricInterpretation = PhotometricInterpretation::BlackIsZero;
|
const TIFF_VALUE: PhotometricInterpretation = PhotometricInterpretation::BlackIsZero;
|
||||||
const BITS_PER_SAMPLE: &'static [u16] = &[64];
|
const BITS_PER_SAMPLE: &'static [u16] = &[64];
|
||||||
|
const SAMPLE_FORMAT: &'static [SampleFormat] = &[SampleFormat::Uint];
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Gray64Float;
|
||||||
|
impl ColorType for Gray64Float {
|
||||||
|
type Inner = f64;
|
||||||
|
const TIFF_VALUE: PhotometricInterpretation = PhotometricInterpretation::BlackIsZero;
|
||||||
|
const BITS_PER_SAMPLE: &'static [u16] = &[64];
|
||||||
|
const SAMPLE_FORMAT: &'static [SampleFormat] = &[SampleFormat::IEEEFP];
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct RGB8;
|
pub struct RGB8;
|
||||||
@@ -43,6 +65,7 @@ impl ColorType for RGB8 {
|
|||||||
type Inner = u8;
|
type Inner = u8;
|
||||||
const TIFF_VALUE: PhotometricInterpretation = PhotometricInterpretation::RGB;
|
const TIFF_VALUE: PhotometricInterpretation = PhotometricInterpretation::RGB;
|
||||||
const BITS_PER_SAMPLE: &'static [u16] = &[8, 8, 8];
|
const BITS_PER_SAMPLE: &'static [u16] = &[8, 8, 8];
|
||||||
|
const SAMPLE_FORMAT: &'static [SampleFormat] = &[SampleFormat::Uint; 3];
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct RGB16;
|
pub struct RGB16;
|
||||||
@@ -50,6 +73,7 @@ impl ColorType for RGB16 {
|
|||||||
type Inner = u16;
|
type Inner = u16;
|
||||||
const TIFF_VALUE: PhotometricInterpretation = PhotometricInterpretation::RGB;
|
const TIFF_VALUE: PhotometricInterpretation = PhotometricInterpretation::RGB;
|
||||||
const BITS_PER_SAMPLE: &'static [u16] = &[16, 16, 16];
|
const BITS_PER_SAMPLE: &'static [u16] = &[16, 16, 16];
|
||||||
|
const SAMPLE_FORMAT: &'static [SampleFormat] = &[SampleFormat::Uint; 3];
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct RGB32;
|
pub struct RGB32;
|
||||||
@@ -57,6 +81,15 @@ impl ColorType for RGB32 {
|
|||||||
type Inner = u32;
|
type Inner = u32;
|
||||||
const TIFF_VALUE: PhotometricInterpretation = PhotometricInterpretation::RGB;
|
const TIFF_VALUE: PhotometricInterpretation = PhotometricInterpretation::RGB;
|
||||||
const BITS_PER_SAMPLE: &'static [u16] = &[32, 32, 32];
|
const BITS_PER_SAMPLE: &'static [u16] = &[32, 32, 32];
|
||||||
|
const SAMPLE_FORMAT: &'static [SampleFormat] = &[SampleFormat::Uint; 3];
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct RGB32Float;
|
||||||
|
impl ColorType for RGB32Float {
|
||||||
|
type Inner = f32;
|
||||||
|
const TIFF_VALUE: PhotometricInterpretation = PhotometricInterpretation::RGB;
|
||||||
|
const BITS_PER_SAMPLE: &'static [u16] = &[32, 32, 32];
|
||||||
|
const SAMPLE_FORMAT: &'static [SampleFormat] = &[SampleFormat::IEEEFP; 3];
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct RGB64;
|
pub struct RGB64;
|
||||||
@@ -64,6 +97,15 @@ impl ColorType for RGB64 {
|
|||||||
type Inner = u64;
|
type Inner = u64;
|
||||||
const TIFF_VALUE: PhotometricInterpretation = PhotometricInterpretation::RGB;
|
const TIFF_VALUE: PhotometricInterpretation = PhotometricInterpretation::RGB;
|
||||||
const BITS_PER_SAMPLE: &'static [u16] = &[64, 64, 64];
|
const BITS_PER_SAMPLE: &'static [u16] = &[64, 64, 64];
|
||||||
|
const SAMPLE_FORMAT: &'static [SampleFormat] = &[SampleFormat::Uint; 3];
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct RGB64Float;
|
||||||
|
impl ColorType for RGB64Float {
|
||||||
|
type Inner = f64;
|
||||||
|
const TIFF_VALUE: PhotometricInterpretation = PhotometricInterpretation::RGB;
|
||||||
|
const BITS_PER_SAMPLE: &'static [u16] = &[64, 64, 64];
|
||||||
|
const SAMPLE_FORMAT: &'static [SampleFormat] = &[SampleFormat::IEEEFP; 3];
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct RGBA8;
|
pub struct RGBA8;
|
||||||
@@ -71,6 +113,7 @@ impl ColorType for RGBA8 {
|
|||||||
type Inner = u8;
|
type Inner = u8;
|
||||||
const TIFF_VALUE: PhotometricInterpretation = PhotometricInterpretation::RGB;
|
const TIFF_VALUE: PhotometricInterpretation = PhotometricInterpretation::RGB;
|
||||||
const BITS_PER_SAMPLE: &'static [u16] = &[8, 8, 8, 8];
|
const BITS_PER_SAMPLE: &'static [u16] = &[8, 8, 8, 8];
|
||||||
|
const SAMPLE_FORMAT: &'static [SampleFormat] = &[SampleFormat::Uint; 4];
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct RGBA16;
|
pub struct RGBA16;
|
||||||
@@ -78,20 +121,39 @@ impl ColorType for RGBA16 {
|
|||||||
type Inner = u16;
|
type Inner = u16;
|
||||||
const TIFF_VALUE: PhotometricInterpretation = PhotometricInterpretation::RGB;
|
const TIFF_VALUE: PhotometricInterpretation = PhotometricInterpretation::RGB;
|
||||||
const BITS_PER_SAMPLE: &'static [u16] = &[16, 16, 16, 16];
|
const BITS_PER_SAMPLE: &'static [u16] = &[16, 16, 16, 16];
|
||||||
|
const SAMPLE_FORMAT: &'static [SampleFormat] = &[SampleFormat::Uint; 4];
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct RGBA32;
|
pub struct RGBA32;
|
||||||
impl ColorType for RGBA32 {
|
impl ColorType for RGBA32 {
|
||||||
type Inner = u16;
|
type Inner = u32;
|
||||||
const TIFF_VALUE: PhotometricInterpretation = PhotometricInterpretation::RGB;
|
const TIFF_VALUE: PhotometricInterpretation = PhotometricInterpretation::RGB;
|
||||||
const BITS_PER_SAMPLE: &'static [u16] = &[32, 32, 32, 32];
|
const BITS_PER_SAMPLE: &'static [u16] = &[32, 32, 32, 32];
|
||||||
|
const SAMPLE_FORMAT: &'static [SampleFormat] = &[SampleFormat::Uint; 4];
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct RGBA32Float;
|
||||||
|
impl ColorType for RGBA32Float {
|
||||||
|
type Inner = f32;
|
||||||
|
const TIFF_VALUE: PhotometricInterpretation = PhotometricInterpretation::RGB;
|
||||||
|
const BITS_PER_SAMPLE: &'static [u16] = &[32, 32, 32, 32];
|
||||||
|
const SAMPLE_FORMAT: &'static [SampleFormat] = &[SampleFormat::IEEEFP; 4];
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct RGBA64;
|
pub struct RGBA64;
|
||||||
impl ColorType for RGBA64 {
|
impl ColorType for RGBA64 {
|
||||||
type Inner = u16;
|
type Inner = u64;
|
||||||
const TIFF_VALUE: PhotometricInterpretation = PhotometricInterpretation::RGB;
|
const TIFF_VALUE: PhotometricInterpretation = PhotometricInterpretation::RGB;
|
||||||
const BITS_PER_SAMPLE: &'static [u16] = &[64, 64, 64, 64];
|
const BITS_PER_SAMPLE: &'static [u16] = &[64, 64, 64, 64];
|
||||||
|
const SAMPLE_FORMAT: &'static [SampleFormat] = &[SampleFormat::Uint; 4];
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct RGBA64Float;
|
||||||
|
impl ColorType for RGBA64Float {
|
||||||
|
type Inner = f64;
|
||||||
|
const TIFF_VALUE: PhotometricInterpretation = PhotometricInterpretation::RGB;
|
||||||
|
const BITS_PER_SAMPLE: &'static [u16] = &[64, 64, 64, 64];
|
||||||
|
const SAMPLE_FORMAT: &'static [SampleFormat] = &[SampleFormat::IEEEFP; 4];
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct CMYK8;
|
pub struct CMYK8;
|
||||||
@@ -99,6 +161,7 @@ impl ColorType for CMYK8 {
|
|||||||
type Inner = u8;
|
type Inner = u8;
|
||||||
const TIFF_VALUE: PhotometricInterpretation = PhotometricInterpretation::CMYK;
|
const TIFF_VALUE: PhotometricInterpretation = PhotometricInterpretation::CMYK;
|
||||||
const BITS_PER_SAMPLE: &'static [u16] = &[8, 8, 8, 8];
|
const BITS_PER_SAMPLE: &'static [u16] = &[8, 8, 8, 8];
|
||||||
|
const SAMPLE_FORMAT: &'static [SampleFormat] = &[SampleFormat::Uint; 4];
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct CMYK16;
|
pub struct CMYK16;
|
||||||
@@ -106,6 +169,7 @@ impl ColorType for CMYK16 {
|
|||||||
type Inner = u16;
|
type Inner = u16;
|
||||||
const TIFF_VALUE: PhotometricInterpretation = PhotometricInterpretation::CMYK;
|
const TIFF_VALUE: PhotometricInterpretation = PhotometricInterpretation::CMYK;
|
||||||
const BITS_PER_SAMPLE: &'static [u16] = &[16, 16, 16, 16];
|
const BITS_PER_SAMPLE: &'static [u16] = &[16, 16, 16, 16];
|
||||||
|
const SAMPLE_FORMAT: &'static [SampleFormat] = &[SampleFormat::Uint; 4];
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct CMYK32;
|
pub struct CMYK32;
|
||||||
@@ -113,6 +177,15 @@ impl ColorType for CMYK32 {
|
|||||||
type Inner = u32;
|
type Inner = u32;
|
||||||
const TIFF_VALUE: PhotometricInterpretation = PhotometricInterpretation::CMYK;
|
const TIFF_VALUE: PhotometricInterpretation = PhotometricInterpretation::CMYK;
|
||||||
const BITS_PER_SAMPLE: &'static [u16] = &[32, 32, 32, 32];
|
const BITS_PER_SAMPLE: &'static [u16] = &[32, 32, 32, 32];
|
||||||
|
const SAMPLE_FORMAT: &'static [SampleFormat] = &[SampleFormat::Uint; 4];
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct CMYK32Float;
|
||||||
|
impl ColorType for CMYK32Float {
|
||||||
|
type Inner = f32;
|
||||||
|
const TIFF_VALUE: PhotometricInterpretation = PhotometricInterpretation::CMYK;
|
||||||
|
const BITS_PER_SAMPLE: &'static [u16] = &[32, 32, 32, 32];
|
||||||
|
const SAMPLE_FORMAT: &'static [SampleFormat] = &[SampleFormat::IEEEFP; 4];
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct CMYK64;
|
pub struct CMYK64;
|
||||||
@@ -120,4 +193,13 @@ impl ColorType for CMYK64 {
|
|||||||
type Inner = u64;
|
type Inner = u64;
|
||||||
const TIFF_VALUE: PhotometricInterpretation = PhotometricInterpretation::CMYK;
|
const TIFF_VALUE: PhotometricInterpretation = PhotometricInterpretation::CMYK;
|
||||||
const BITS_PER_SAMPLE: &'static [u16] = &[64, 64, 64, 64];
|
const BITS_PER_SAMPLE: &'static [u16] = &[64, 64, 64, 64];
|
||||||
|
const SAMPLE_FORMAT: &'static [SampleFormat] = &[SampleFormat::Uint; 4];
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct CMYK64Float;
|
||||||
|
impl ColorType for CMYK64Float {
|
||||||
|
type Inner = f64;
|
||||||
|
const TIFF_VALUE: PhotometricInterpretation = PhotometricInterpretation::CMYK;
|
||||||
|
const BITS_PER_SAMPLE: &'static [u16] = &[64, 64, 64, 64];
|
||||||
|
const SAMPLE_FORMAT: &'static [SampleFormat] = &[SampleFormat::IEEEFP; 4];
|
||||||
}
|
}
|
||||||
+295
-412
@@ -1,354 +1,31 @@
|
|||||||
use byteorder::NativeEndian;
|
pub use tiff_value::*;
|
||||||
use std::collections::BTreeMap;
|
|
||||||
use std::convert::TryFrom;
|
|
||||||
use std::io::{Seek, Write};
|
|
||||||
use std::mem;
|
|
||||||
|
|
||||||
use tags::{self, ResolutionUnit, Tag, Type};
|
use std::{
|
||||||
use error::{TiffError, TiffFormatError, TiffResult};
|
cmp,
|
||||||
|
collections::BTreeMap,
|
||||||
|
convert::{TryFrom, TryInto},
|
||||||
|
io::{self, Seek, Write},
|
||||||
|
marker::PhantomData,
|
||||||
|
mem,
|
||||||
|
num::TryFromIntError,
|
||||||
|
};
|
||||||
|
|
||||||
|
use crate::{
|
||||||
|
error::TiffResult,
|
||||||
|
tags::{self, ResolutionUnit, Tag},
|
||||||
|
};
|
||||||
|
|
||||||
pub mod colortype;
|
pub mod colortype;
|
||||||
|
mod tiff_value;
|
||||||
mod writer;
|
mod writer;
|
||||||
|
|
||||||
use self::colortype::*;
|
use self::colortype::*;
|
||||||
use self::writer::*;
|
use self::writer::*;
|
||||||
|
|
||||||
/// Type to represent tiff values of type `RATIONAL`
|
/// Encoder for Tiff and BigTiff files.
|
||||||
#[derive(Clone)]
|
|
||||||
pub struct Rational {
|
|
||||||
pub n: u32,
|
|
||||||
pub d: u32,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Type to represent tiff values of type `SRATIONAL`
|
|
||||||
pub struct SRational {
|
|
||||||
pub n: i32,
|
|
||||||
pub d: i32,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Trait for types that can be encoded in a tiff file
|
|
||||||
pub trait TiffValue {
|
|
||||||
const BYTE_LEN: u32;
|
|
||||||
const FIELD_TYPE: Type;
|
|
||||||
fn count(&self) -> u32;
|
|
||||||
fn bytes(&self) -> u32 {
|
|
||||||
self.count() * Self::BYTE_LEN
|
|
||||||
}
|
|
||||||
fn write<W: Write>(&self, writer: &mut TiffWriter<W>) -> TiffResult<()>;
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TiffValue for [u8] {
|
|
||||||
const BYTE_LEN: u32 = 1;
|
|
||||||
const FIELD_TYPE: Type = Type::BYTE;
|
|
||||||
|
|
||||||
fn count(&self) -> u32 {
|
|
||||||
self.len() as u32
|
|
||||||
}
|
|
||||||
|
|
||||||
fn write<W: Write>(&self, writer: &mut TiffWriter<W>) -> TiffResult<()> {
|
|
||||||
writer.write_bytes(self)?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TiffValue for [i8] {
|
|
||||||
const BYTE_LEN: u32 = 1;
|
|
||||||
const FIELD_TYPE: Type = Type::SBYTE;
|
|
||||||
|
|
||||||
fn count(&self) -> u32 {
|
|
||||||
self.len() as u32
|
|
||||||
}
|
|
||||||
|
|
||||||
fn write<W: Write>(&self, writer: &mut TiffWriter<W>) -> TiffResult<()> {
|
|
||||||
// We write using nativeedian so this should be safe
|
|
||||||
let slice =
|
|
||||||
unsafe { ::std::slice::from_raw_parts(self.as_ptr() as *const u8, self.len()) };
|
|
||||||
writer.write_bytes(slice)?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TiffValue for [u16] {
|
|
||||||
const BYTE_LEN: u32 = 2;
|
|
||||||
const FIELD_TYPE: Type = Type::SHORT;
|
|
||||||
|
|
||||||
fn count(&self) -> u32 {
|
|
||||||
self.len() as u32
|
|
||||||
}
|
|
||||||
|
|
||||||
fn write<W: Write>(&self, writer: &mut TiffWriter<W>) -> TiffResult<()> {
|
|
||||||
// We write using nativeedian so this sould be safe
|
|
||||||
let slice =
|
|
||||||
unsafe { ::std::slice::from_raw_parts(self.as_ptr() as *const u8, self.len() * 2) };
|
|
||||||
writer.write_bytes(slice)?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TiffValue for [i16] {
|
|
||||||
const BYTE_LEN: u32 = 2;
|
|
||||||
const FIELD_TYPE: Type = Type::SSHORT;
|
|
||||||
|
|
||||||
fn count(&self) -> u32 {
|
|
||||||
self.len() as u32
|
|
||||||
}
|
|
||||||
|
|
||||||
fn write<W: Write>(&self, writer: &mut TiffWriter<W>) -> TiffResult<()> {
|
|
||||||
// We write using nativeedian so this should be safe
|
|
||||||
let slice =
|
|
||||||
unsafe { ::std::slice::from_raw_parts(self.as_ptr() as *const u8, self.len() * Self::BYTE_LEN as usize) };
|
|
||||||
writer.write_bytes(slice)?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TiffValue for [u32] {
|
|
||||||
const BYTE_LEN: u32 = 4;
|
|
||||||
const FIELD_TYPE: Type = Type::LONG;
|
|
||||||
|
|
||||||
fn count(&self) -> u32 {
|
|
||||||
self.len() as u32
|
|
||||||
}
|
|
||||||
|
|
||||||
fn write<W: Write>(&self, writer: &mut TiffWriter<W>) -> TiffResult<()> {
|
|
||||||
// We write using nativeedian so this sould be safe
|
|
||||||
let slice =
|
|
||||||
unsafe { ::std::slice::from_raw_parts(self.as_ptr() as *const u8, self.len() * 4) };
|
|
||||||
writer.write_bytes(slice)?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TiffValue for [i32] {
|
|
||||||
const BYTE_LEN: u32 = 4;
|
|
||||||
const FIELD_TYPE: Type = Type::SLONG;
|
|
||||||
|
|
||||||
fn count(&self) -> u32 {
|
|
||||||
self.len() as u32
|
|
||||||
}
|
|
||||||
|
|
||||||
fn write<W: Write>(&self, writer: &mut TiffWriter<W>) -> TiffResult<()> {
|
|
||||||
// We write using nativeedian so this should be safe
|
|
||||||
let slice =
|
|
||||||
unsafe { ::std::slice::from_raw_parts(self.as_ptr() as *const u8, self.len() * Self::BYTE_LEN as usize) };
|
|
||||||
writer.write_bytes(slice)?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TiffValue for [u64] {
|
|
||||||
const BYTE_LEN: u32 = 8;
|
|
||||||
const FIELD_TYPE: Type = Type::LONG8;
|
|
||||||
|
|
||||||
fn count(&self) -> u32 {
|
|
||||||
self.len() as u32
|
|
||||||
}
|
|
||||||
|
|
||||||
fn write<W: Write>(&self, writer: &mut TiffWriter<W>) -> TiffResult<()> {
|
|
||||||
// We write using nativeedian so this sould be safe
|
|
||||||
let slice =
|
|
||||||
unsafe { ::std::slice::from_raw_parts(self.as_ptr() as *const u8, self.len() * 8) };
|
|
||||||
writer.write_bytes(slice)?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TiffValue for [Rational] {
|
|
||||||
const BYTE_LEN: u32 = 8;
|
|
||||||
const FIELD_TYPE: Type = Type::RATIONAL;
|
|
||||||
|
|
||||||
fn count(&self) -> u32 {
|
|
||||||
self.len() as u32
|
|
||||||
}
|
|
||||||
|
|
||||||
fn write<W: Write>(&self, writer: &mut TiffWriter<W>) -> TiffResult<()> {
|
|
||||||
for x in self {
|
|
||||||
x.write(writer)?;
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TiffValue for [SRational] {
|
|
||||||
const BYTE_LEN: u32 = 8;
|
|
||||||
const FIELD_TYPE: Type = Type::SRATIONAL;
|
|
||||||
|
|
||||||
fn count(&self) -> u32 {
|
|
||||||
self.len() as u32
|
|
||||||
}
|
|
||||||
|
|
||||||
fn write<W: Write>(&self, writer: &mut TiffWriter<W>) -> TiffResult<()> {
|
|
||||||
for x in self {
|
|
||||||
x.write(writer)?;
|
|
||||||
}
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TiffValue for u8 {
|
|
||||||
const BYTE_LEN: u32 = 1;
|
|
||||||
const FIELD_TYPE: Type = Type::BYTE;
|
|
||||||
|
|
||||||
fn count(&self) -> u32 {
|
|
||||||
1
|
|
||||||
}
|
|
||||||
|
|
||||||
fn write<W: Write>(&self, writer: &mut TiffWriter<W>) -> TiffResult<()> {
|
|
||||||
writer.write_u8(*self)?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TiffValue for i8 {
|
|
||||||
const BYTE_LEN: u32 = 1;
|
|
||||||
const FIELD_TYPE: Type = Type::SBYTE;
|
|
||||||
|
|
||||||
fn count(&self) -> u32 {
|
|
||||||
1
|
|
||||||
}
|
|
||||||
|
|
||||||
fn write<W: Write>(&self, writer: &mut TiffWriter<W>) -> TiffResult<()> {
|
|
||||||
writer.write_i8(*self)?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TiffValue for u16 {
|
|
||||||
const BYTE_LEN: u32 = 2;
|
|
||||||
const FIELD_TYPE: Type = Type::SHORT;
|
|
||||||
|
|
||||||
fn count(&self) -> u32 {
|
|
||||||
1
|
|
||||||
}
|
|
||||||
|
|
||||||
fn write<W: Write>(&self, writer: &mut TiffWriter<W>) -> TiffResult<()> {
|
|
||||||
writer.write_u16(*self)?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TiffValue for i16 {
|
|
||||||
const BYTE_LEN: u32 = 2;
|
|
||||||
const FIELD_TYPE: Type = Type::SSHORT;
|
|
||||||
|
|
||||||
fn count(&self) -> u32 {
|
|
||||||
1
|
|
||||||
}
|
|
||||||
|
|
||||||
fn write<W: Write>(&self, writer: &mut TiffWriter<W>) -> TiffResult<()> {
|
|
||||||
writer.write_i16(*self)?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TiffValue for u32 {
|
|
||||||
const BYTE_LEN: u32 = 4;
|
|
||||||
const FIELD_TYPE: Type = Type::LONG;
|
|
||||||
|
|
||||||
fn count(&self) -> u32 {
|
|
||||||
1
|
|
||||||
}
|
|
||||||
|
|
||||||
fn write<W: Write>(&self, writer: &mut TiffWriter<W>) -> TiffResult<()> {
|
|
||||||
writer.write_u32(*self)?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TiffValue for i32 {
|
|
||||||
const BYTE_LEN: u32 = 4;
|
|
||||||
const FIELD_TYPE: Type = Type::SLONG;
|
|
||||||
|
|
||||||
fn count(&self) -> u32 {
|
|
||||||
1
|
|
||||||
}
|
|
||||||
|
|
||||||
fn write<W: Write>(&self, writer: &mut TiffWriter<W>) -> TiffResult<()> {
|
|
||||||
writer.write_i32(*self)?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TiffValue for u64 {
|
|
||||||
const BYTE_LEN: u32 = 8;
|
|
||||||
const FIELD_TYPE: Type = Type::LONG8;
|
|
||||||
|
|
||||||
fn count(&self) -> u32 {
|
|
||||||
1
|
|
||||||
}
|
|
||||||
|
|
||||||
fn write<W: Write>(&self, writer: &mut TiffWriter<W>) -> TiffResult<()> {
|
|
||||||
writer.write_u64(*self)?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TiffValue for Rational {
|
|
||||||
const BYTE_LEN: u32 = 8;
|
|
||||||
const FIELD_TYPE: Type = Type::RATIONAL;
|
|
||||||
|
|
||||||
fn count(&self) -> u32 {
|
|
||||||
1
|
|
||||||
}
|
|
||||||
|
|
||||||
fn write<W: Write>(&self, writer: &mut TiffWriter<W>) -> TiffResult<()> {
|
|
||||||
writer.write_u32(self.n)?;
|
|
||||||
writer.write_u32(self.d)?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TiffValue for SRational {
|
|
||||||
const BYTE_LEN: u32 = 8;
|
|
||||||
const FIELD_TYPE: Type = Type::SRATIONAL;
|
|
||||||
|
|
||||||
fn count(&self) -> u32 {
|
|
||||||
1
|
|
||||||
}
|
|
||||||
|
|
||||||
fn write<W: Write>(&self, writer: &mut TiffWriter<W>) -> TiffResult<()> {
|
|
||||||
writer.write_i32(self.n)?;
|
|
||||||
writer.write_i32(self.d)?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TiffValue for str {
|
|
||||||
const BYTE_LEN: u32 = 1;
|
|
||||||
const FIELD_TYPE: Type = Type::ASCII;
|
|
||||||
|
|
||||||
fn count(&self) -> u32 {
|
|
||||||
self.len() as u32 + 1
|
|
||||||
}
|
|
||||||
|
|
||||||
fn write<W: Write>(&self, writer: &mut TiffWriter<W>) -> TiffResult<()> {
|
|
||||||
if self.is_ascii() && !self.bytes().any(|b| b == 0) {
|
|
||||||
writer.write_bytes(self.as_bytes())?;
|
|
||||||
writer.write_u8(0)?;
|
|
||||||
Ok(())
|
|
||||||
} else {
|
|
||||||
Err(TiffError::FormatError(TiffFormatError::InvalidTag))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a, T: TiffValue + ?Sized> TiffValue for &'a T {
|
|
||||||
const BYTE_LEN: u32 = T::BYTE_LEN;
|
|
||||||
const FIELD_TYPE: Type = T::FIELD_TYPE;
|
|
||||||
|
|
||||||
fn count(&self) -> u32 {
|
|
||||||
(*self).count()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn write<W: Write>(&self, writer: &mut TiffWriter<W>) -> TiffResult<()> {
|
|
||||||
(*self).write(writer)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Tiff encoder.
|
|
||||||
///
|
///
|
||||||
/// With this type you can get a `DirectoryEncoder` or a `ImageEncoder`
|
/// With this type you can get a `DirectoryEncoder` or a `ImageEncoder`
|
||||||
/// to encode tiff ifd directories with images.
|
/// to encode Tiff/BigTiff ifd directories with images.
|
||||||
///
|
///
|
||||||
/// See `DirectoryEncoder` and `ImageEncoder`.
|
/// See `DirectoryEncoder` and `ImageEncoder`.
|
||||||
///
|
///
|
||||||
@@ -360,39 +37,68 @@ impl<'a, T: TiffValue + ?Sized> TiffValue for &'a T {
|
|||||||
/// # let image_data = vec![0; 100*100*3];
|
/// # let image_data = vec![0; 100*100*3];
|
||||||
/// use tiff::encoder::*;
|
/// use tiff::encoder::*;
|
||||||
///
|
///
|
||||||
|
/// // create a standard Tiff file
|
||||||
/// let mut tiff = TiffEncoder::new(&mut file).unwrap();
|
/// let mut tiff = TiffEncoder::new(&mut file).unwrap();
|
||||||
///
|
|
||||||
/// tiff.write_image::<colortype::RGB8>(100, 100, &image_data).unwrap();
|
/// tiff.write_image::<colortype::RGB8>(100, 100, &image_data).unwrap();
|
||||||
|
///
|
||||||
|
/// // create a BigTiff file
|
||||||
|
/// let mut bigtiff = TiffEncoder::new_big(&mut file).unwrap();
|
||||||
|
/// bigtiff.write_image::<colortype::RGB8>(100, 100, &image_data).unwrap();
|
||||||
|
///
|
||||||
/// # }
|
/// # }
|
||||||
/// ```
|
/// ```
|
||||||
pub struct TiffEncoder<W> {
|
pub struct TiffEncoder<W, K: TiffKind = TiffKindStandard> {
|
||||||
writer: TiffWriter<W>,
|
writer: TiffWriter<W>,
|
||||||
|
kind: PhantomData<K>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Constructor functions to create standard Tiff files.
|
||||||
impl<W: Write + Seek> TiffEncoder<W> {
|
impl<W: Write + Seek> TiffEncoder<W> {
|
||||||
pub fn new(writer: W) -> TiffResult<TiffEncoder<W>> {
|
/// Creates a new encoder for standard Tiff files.
|
||||||
|
///
|
||||||
|
/// To create BigTiff files, use [`new_big`][TiffEncoder::new_big] or
|
||||||
|
/// [`new_generic`][TiffEncoder::new_generic].
|
||||||
|
pub fn new(writer: W) -> TiffResult<TiffEncoder<W, TiffKindStandard>> {
|
||||||
|
TiffEncoder::new_generic(writer)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Constructor functions to create BigTiff files.
|
||||||
|
impl<W: Write + Seek> TiffEncoder<W, TiffKindBig> {
|
||||||
|
/// Creates a new encoder for BigTiff files.
|
||||||
|
///
|
||||||
|
/// To create standard Tiff files, use [`new`][TiffEncoder::new] or
|
||||||
|
/// [`new_generic`][TiffEncoder::new_generic].
|
||||||
|
pub fn new_big(writer: W) -> TiffResult<Self> {
|
||||||
|
TiffEncoder::new_generic(writer)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Generic functions that are available for both Tiff and BigTiff encoders.
|
||||||
|
impl<W: Write + Seek, K: TiffKind> TiffEncoder<W, K> {
|
||||||
|
/// Creates a new Tiff or BigTiff encoder, inferred from the return type.
|
||||||
|
pub fn new_generic(writer: W) -> TiffResult<Self> {
|
||||||
let mut encoder = TiffEncoder {
|
let mut encoder = TiffEncoder {
|
||||||
writer: TiffWriter::new(writer),
|
writer: TiffWriter::new(writer),
|
||||||
|
kind: PhantomData,
|
||||||
};
|
};
|
||||||
|
|
||||||
NativeEndian::write_header(&mut encoder.writer)?;
|
K::write_header(&mut encoder.writer)?;
|
||||||
// blank the IFD offset location
|
|
||||||
encoder.writer.write_u32(0)?;
|
|
||||||
|
|
||||||
Ok(encoder)
|
Ok(encoder)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create a `DirectoryEncoder` to encode an ifd directory.
|
/// Create a [`DirectoryEncoder`] to encode an ifd directory.
|
||||||
pub fn new_directory(&mut self) -> TiffResult<DirectoryEncoder<W>> {
|
pub fn new_directory(&mut self) -> TiffResult<DirectoryEncoder<W, K>> {
|
||||||
DirectoryEncoder::new(&mut self.writer)
|
DirectoryEncoder::new(&mut self.writer)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create an 'ImageEncoder' to encode an image one slice at a time.
|
/// Create an [`ImageEncoder`] to encode an image one slice at a time.
|
||||||
pub fn new_image<C: ColorType>(
|
pub fn new_image<C: ColorType>(
|
||||||
&mut self,
|
&mut self,
|
||||||
width: u32,
|
width: u32,
|
||||||
height: u32,
|
height: u32,
|
||||||
) -> TiffResult<ImageEncoder<W, C>> {
|
) -> TiffResult<ImageEncoder<W, C, K>> {
|
||||||
let encoder = DirectoryEncoder::new(&mut self.writer)?;
|
let encoder = DirectoryEncoder::new(&mut self.writer)?;
|
||||||
ImageEncoder::new(encoder, width, height)
|
ImageEncoder::new(encoder, width, height)
|
||||||
}
|
}
|
||||||
@@ -408,7 +114,7 @@ impl<W: Write + Seek> TiffEncoder<W> {
|
|||||||
[C::Inner]: TiffValue,
|
[C::Inner]: TiffValue,
|
||||||
{
|
{
|
||||||
let encoder = DirectoryEncoder::new(&mut self.writer)?;
|
let encoder = DirectoryEncoder::new(&mut self.writer)?;
|
||||||
let image: ImageEncoder<W, C> = ImageEncoder::new(encoder, width, height)?;
|
let image: ImageEncoder<W, C, K> = ImageEncoder::new(encoder, width, height)?;
|
||||||
image.write_data(data)
|
image.write_data(data)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -417,19 +123,19 @@ impl<W: Write + Seek> TiffEncoder<W> {
|
|||||||
///
|
///
|
||||||
/// You should call `finish` on this when you are finished with it.
|
/// You should call `finish` on this when you are finished with it.
|
||||||
/// Encoding can silently fail while this is dropping.
|
/// Encoding can silently fail while this is dropping.
|
||||||
pub struct DirectoryEncoder<'a, W: 'a + Write + Seek> {
|
pub struct DirectoryEncoder<'a, W: 'a + Write + Seek, K: TiffKind> {
|
||||||
writer: &'a mut TiffWriter<W>,
|
writer: &'a mut TiffWriter<W>,
|
||||||
dropped: bool,
|
dropped: bool,
|
||||||
// We use BTreeMap to make sure tags are written in correct order
|
// We use BTreeMap to make sure tags are written in correct order
|
||||||
ifd_pointer_pos: u64,
|
ifd_pointer_pos: u64,
|
||||||
ifd: BTreeMap<u16, (u16, u32, Vec<u8>)>,
|
ifd: BTreeMap<u16, DirectoryEntry<K::OffsetType>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, W: 'a + Write + Seek> DirectoryEncoder<'a, W> {
|
impl<'a, W: 'a + Write + Seek, K: TiffKind> DirectoryEncoder<'a, W, K> {
|
||||||
fn new(writer: &'a mut TiffWriter<W>) -> TiffResult<DirectoryEncoder<'a, W>> {
|
fn new(writer: &'a mut TiffWriter<W>) -> TiffResult<Self> {
|
||||||
// the previous word is the IFD offset position
|
// the previous word is the IFD offset position
|
||||||
let ifd_pointer_pos = writer.offset() - mem::size_of::<u32>() as u64;
|
let ifd_pointer_pos = writer.offset() - mem::size_of::<K::OffsetType>() as u64;
|
||||||
writer.pad_word_boundary()?;
|
writer.pad_word_boundary()?; // TODO: Do we need to adjust this for BigTiff?
|
||||||
Ok(DirectoryEncoder {
|
Ok(DirectoryEncoder {
|
||||||
writer,
|
writer,
|
||||||
dropped: false,
|
dropped: false,
|
||||||
@@ -440,30 +146,41 @@ impl<'a, W: 'a + Write + Seek> DirectoryEncoder<'a, W> {
|
|||||||
|
|
||||||
/// Write a single ifd tag.
|
/// Write a single ifd tag.
|
||||||
pub fn write_tag<T: TiffValue>(&mut self, tag: Tag, value: T) -> TiffResult<()> {
|
pub fn write_tag<T: TiffValue>(&mut self, tag: Tag, value: T) -> TiffResult<()> {
|
||||||
let len = <T>::BYTE_LEN * value.count();
|
let mut bytes = Vec::with_capacity(value.bytes());
|
||||||
let mut bytes = Vec::with_capacity(usize::try_from(len)?);
|
|
||||||
{
|
{
|
||||||
let mut writer = TiffWriter::new(&mut bytes);
|
let mut writer = TiffWriter::new(&mut bytes);
|
||||||
value.write(&mut writer)?;
|
value.write(&mut writer)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
self.ifd
|
self.ifd.insert(
|
||||||
.insert(tag.to_u16(), (<T>::FIELD_TYPE.to_u16(), value.count(), bytes));
|
tag.to_u16(),
|
||||||
|
DirectoryEntry {
|
||||||
|
data_type: <T>::FIELD_TYPE.to_u16(),
|
||||||
|
count: value.count().try_into()?,
|
||||||
|
data: bytes,
|
||||||
|
},
|
||||||
|
);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn write_directory(&mut self) -> TiffResult<u64> {
|
fn write_directory(&mut self) -> TiffResult<u64> {
|
||||||
// Start by writing out all values
|
// Start by writing out all values
|
||||||
for &mut (_, _, ref mut bytes) in self.ifd.values_mut() {
|
for &mut DirectoryEntry {
|
||||||
if bytes.len() > 4 {
|
data: ref mut bytes,
|
||||||
|
..
|
||||||
|
} in self.ifd.values_mut()
|
||||||
|
{
|
||||||
|
let data_bytes = mem::size_of::<K::OffsetType>();
|
||||||
|
|
||||||
|
if bytes.len() > data_bytes {
|
||||||
let offset = self.writer.offset();
|
let offset = self.writer.offset();
|
||||||
self.writer.write_bytes(bytes)?;
|
self.writer.write_bytes(bytes)?;
|
||||||
*bytes = vec![0, 0, 0, 0];
|
*bytes = vec![0; data_bytes];
|
||||||
let mut writer = TiffWriter::new(bytes as &mut [u8]);
|
let mut writer = TiffWriter::new(bytes as &mut [u8]);
|
||||||
writer.write_u32(u32::try_from(offset)?)?;
|
K::write_offset(&mut writer, offset)?;
|
||||||
} else {
|
} else {
|
||||||
while bytes.len() < 4 {
|
while bytes.len() < data_bytes {
|
||||||
bytes.push(0);
|
bytes.push(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -471,11 +188,19 @@ impl<'a, W: 'a + Write + Seek> DirectoryEncoder<'a, W> {
|
|||||||
|
|
||||||
let offset = self.writer.offset();
|
let offset = self.writer.offset();
|
||||||
|
|
||||||
self.writer.write_u16(u16::try_from(self.ifd.len())?)?;
|
K::write_entry_count(&mut self.writer, self.ifd.len())?;
|
||||||
for (tag, &(ref field_type, ref count, ref offset)) in self.ifd.iter() {
|
for (
|
||||||
|
tag,
|
||||||
|
&DirectoryEntry {
|
||||||
|
data_type: ref field_type,
|
||||||
|
ref count,
|
||||||
|
data: ref offset,
|
||||||
|
},
|
||||||
|
) in self.ifd.iter()
|
||||||
|
{
|
||||||
self.writer.write_u16(*tag)?;
|
self.writer.write_u16(*tag)?;
|
||||||
self.writer.write_u16(*field_type)?;
|
self.writer.write_u16(*field_type)?;
|
||||||
self.writer.write_u32(*count)?;
|
(*count).write(&mut self.writer)?;
|
||||||
self.writer.write_bytes(offset)?;
|
self.writer.write_bytes(offset)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -496,9 +221,9 @@ impl<'a, W: 'a + Write + Seek> DirectoryEncoder<'a, W> {
|
|||||||
let curr_pos = self.writer.offset();
|
let curr_pos = self.writer.offset();
|
||||||
|
|
||||||
self.writer.goto_offset(self.ifd_pointer_pos)?;
|
self.writer.goto_offset(self.ifd_pointer_pos)?;
|
||||||
self.writer.write_u32(u32::try_from(ifd_pointer)?)?;
|
K::write_offset(&mut self.writer, ifd_pointer)?;
|
||||||
self.writer.goto_offset(curr_pos)?;
|
self.writer.goto_offset(curr_pos)?;
|
||||||
self.writer.write_u32(0)?;
|
K::write_offset(&mut self.writer, 0)?;
|
||||||
|
|
||||||
self.dropped = true;
|
self.dropped = true;
|
||||||
|
|
||||||
@@ -511,7 +236,7 @@ impl<'a, W: 'a + Write + Seek> DirectoryEncoder<'a, W> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, W: Write + Seek> Drop for DirectoryEncoder<'a, W> {
|
impl<'a, W: Write + Seek, K: TiffKind> Drop for DirectoryEncoder<'a, W, K> {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
if !self.dropped {
|
if !self.dropped {
|
||||||
let _ = self.finish_internal();
|
let _ = self.finish_internal();
|
||||||
@@ -536,9 +261,12 @@ impl<'a, W: Write + Seek> Drop for DirectoryEncoder<'a, W> {
|
|||||||
/// let mut tiff = TiffEncoder::new(&mut file).unwrap();
|
/// let mut tiff = TiffEncoder::new(&mut file).unwrap();
|
||||||
/// let mut image = tiff.new_image::<colortype::RGB8>(100, 100).unwrap();
|
/// let mut image = tiff.new_image::<colortype::RGB8>(100, 100).unwrap();
|
||||||
///
|
///
|
||||||
/// // You can encode tags here
|
/// // You can encode tags here
|
||||||
/// image.encoder().write_tag(Tag::Artist, "Image-tiff").unwrap();
|
/// image.encoder().write_tag(Tag::Artist, "Image-tiff").unwrap();
|
||||||
///
|
///
|
||||||
|
/// // Strip size can be configured before writing data
|
||||||
|
/// image.rows_per_strip(2).unwrap();
|
||||||
|
///
|
||||||
/// let mut idx = 0;
|
/// let mut idx = 0;
|
||||||
/// while image.next_strip_sample_count() > 0 {
|
/// while image.next_strip_sample_count() > 0 {
|
||||||
/// let sample_count = image.next_strip_sample_count() as usize;
|
/// let sample_count = image.next_strip_sample_count() as usize;
|
||||||
@@ -549,31 +277,28 @@ impl<'a, W: Write + Seek> Drop for DirectoryEncoder<'a, W> {
|
|||||||
/// # }
|
/// # }
|
||||||
/// ```
|
/// ```
|
||||||
/// You can also call write_data function wich will encode by strip and finish
|
/// You can also call write_data function wich will encode by strip and finish
|
||||||
pub struct ImageEncoder<'a, W: 'a + Write + Seek, C: ColorType> {
|
pub struct ImageEncoder<'a, W: 'a + Write + Seek, C: ColorType, K: TiffKind> {
|
||||||
encoder: DirectoryEncoder<'a, W>,
|
encoder: DirectoryEncoder<'a, W, K>,
|
||||||
strip_idx: u64,
|
strip_idx: u64,
|
||||||
strip_count: u64,
|
strip_count: u64,
|
||||||
row_samples: u64,
|
row_samples: u64,
|
||||||
width: u32,
|
width: u32,
|
||||||
height: u32,
|
height: u32,
|
||||||
rows_per_strip: u64,
|
rows_per_strip: u64,
|
||||||
strip_offsets: Vec<u32>,
|
strip_offsets: Vec<K::OffsetType>,
|
||||||
strip_byte_count: Vec<u32>,
|
strip_byte_count: Vec<K::OffsetType>,
|
||||||
dropped: bool,
|
dropped: bool,
|
||||||
_phantom: ::std::marker::PhantomData<C>,
|
_phantom: ::std::marker::PhantomData<C>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, W: 'a + Write + Seek, T: ColorType> ImageEncoder<'a, W, T> {
|
impl<'a, W: 'a + Write + Seek, T: ColorType, K: TiffKind> ImageEncoder<'a, W, T, K> {
|
||||||
fn new(
|
fn new(mut encoder: DirectoryEncoder<'a, W, K>, width: u32, height: u32) -> TiffResult<Self> {
|
||||||
mut encoder: DirectoryEncoder<'a, W>,
|
|
||||||
width: u32,
|
|
||||||
height: u32,
|
|
||||||
) -> TiffResult<ImageEncoder<'a, W, T>> {
|
|
||||||
let row_samples = u64::from(width) * u64::try_from(<T>::BITS_PER_SAMPLE.len())?;
|
let row_samples = u64::from(width) * u64::try_from(<T>::BITS_PER_SAMPLE.len())?;
|
||||||
let row_bytes = row_samples * u64::from(<T::Inner>::BYTE_LEN);
|
let row_bytes = row_samples * u64::from(<T::Inner>::BYTE_LEN);
|
||||||
|
|
||||||
// As per tiff spec each strip should be about 8k long
|
// Limit the strip size to prevent potential memory and security issues.
|
||||||
let rows_per_strip = (8000 + row_bytes - 1) / row_bytes;
|
// Also keep the multiple strip handling 'oiled'
|
||||||
|
let rows_per_strip = (1_000_000 + row_bytes - 1) / row_bytes;
|
||||||
|
|
||||||
let strip_count = (u64::from(height) + rows_per_strip - 1) / rows_per_strip;
|
let strip_count = (u64::from(height) + rows_per_strip - 1) / rows_per_strip;
|
||||||
|
|
||||||
@@ -582,11 +307,16 @@ impl<'a, W: 'a + Write + Seek, T: ColorType> ImageEncoder<'a, W, T> {
|
|||||||
encoder.write_tag(Tag::Compression, tags::CompressionMethod::None.to_u16())?;
|
encoder.write_tag(Tag::Compression, tags::CompressionMethod::None.to_u16())?;
|
||||||
|
|
||||||
encoder.write_tag(Tag::BitsPerSample, <T>::BITS_PER_SAMPLE)?;
|
encoder.write_tag(Tag::BitsPerSample, <T>::BITS_PER_SAMPLE)?;
|
||||||
|
let sample_format: Vec<_> = <T>::SAMPLE_FORMAT.iter().map(|s| s.to_u16()).collect();
|
||||||
|
encoder.write_tag(Tag::SampleFormat, &sample_format[..])?;
|
||||||
encoder.write_tag(Tag::PhotometricInterpretation, <T>::TIFF_VALUE.to_u16())?;
|
encoder.write_tag(Tag::PhotometricInterpretation, <T>::TIFF_VALUE.to_u16())?;
|
||||||
|
|
||||||
encoder.write_tag(Tag::RowsPerStrip, u32::try_from(rows_per_strip)?)?;
|
encoder.write_tag(Tag::RowsPerStrip, u32::try_from(rows_per_strip)?)?;
|
||||||
|
|
||||||
encoder.write_tag(Tag::SamplesPerPixel, u16::try_from(<T>::BITS_PER_SAMPLE.len())?)?;
|
encoder.write_tag(
|
||||||
|
Tag::SamplesPerPixel,
|
||||||
|
u16::try_from(<T>::BITS_PER_SAMPLE.len())?,
|
||||||
|
)?;
|
||||||
encoder.write_tag(Tag::XResolution, Rational { n: 1, d: 1 })?;
|
encoder.write_tag(Tag::XResolution, Rational { n: 1, d: 1 })?;
|
||||||
encoder.write_tag(Tag::YResolution, Rational { n: 1, d: 1 })?;
|
encoder.write_tag(Tag::YResolution, Rational { n: 1, d: 1 })?;
|
||||||
encoder.write_tag(Tag::ResolutionUnit, ResolutionUnit::None.to_u16())?;
|
encoder.write_tag(Tag::ResolutionUnit, ResolutionUnit::None.to_u16())?;
|
||||||
@@ -612,11 +342,9 @@ impl<'a, W: 'a + Write + Seek, T: ColorType> ImageEncoder<'a, W, T> {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
let start_row = ::std::cmp::min(u64::from(self.height), self.strip_idx * self.rows_per_strip);
|
let raw_start_row = self.strip_idx * self.rows_per_strip;
|
||||||
let end_row = ::std::cmp::min(
|
let start_row = cmp::min(u64::from(self.height), raw_start_row);
|
||||||
u64::from(self.height),
|
let end_row = cmp::min(u64::from(self.height), raw_start_row + self.rows_per_strip);
|
||||||
(self.strip_idx + 1) * self.rows_per_strip,
|
|
||||||
);
|
|
||||||
|
|
||||||
(end_row - start_row) * self.row_samples
|
(end_row - start_row) * self.row_samples
|
||||||
}
|
}
|
||||||
@@ -629,14 +357,16 @@ impl<'a, W: 'a + Write + Seek, T: ColorType> ImageEncoder<'a, W, T> {
|
|||||||
// TODO: Compression
|
// TODO: Compression
|
||||||
let samples = self.next_strip_sample_count();
|
let samples = self.next_strip_sample_count();
|
||||||
if u64::try_from(value.len())? != samples {
|
if u64::try_from(value.len())? != samples {
|
||||||
return Err(::std::io::Error::new(
|
return Err(io::Error::new(
|
||||||
::std::io::ErrorKind::InvalidData,
|
io::ErrorKind::InvalidData,
|
||||||
"Slice is wrong size for strip").into());
|
"Slice is wrong size for strip",
|
||||||
|
)
|
||||||
|
.into());
|
||||||
}
|
}
|
||||||
|
|
||||||
let offset = self.encoder.write_data(value)?;
|
let offset = self.encoder.write_data(value)?;
|
||||||
self.strip_offsets.push(u32::try_from(offset)?);
|
self.strip_offsets.push(K::convert_offset(offset)?);
|
||||||
self.strip_byte_count.push(value.bytes());
|
self.strip_byte_count.push(value.bytes().try_into()?);
|
||||||
|
|
||||||
self.strip_idx += 1;
|
self.strip_idx += 1;
|
||||||
Ok(())
|
Ok(())
|
||||||
@@ -647,14 +377,20 @@ impl<'a, W: 'a + Write + Seek, T: ColorType> ImageEncoder<'a, W, T> {
|
|||||||
where
|
where
|
||||||
[T::Inner]: TiffValue,
|
[T::Inner]: TiffValue,
|
||||||
{
|
{
|
||||||
let num_pix = usize::try_from(self.width)?.checked_mul(usize::try_from(self.height)?)
|
let num_pix = usize::try_from(self.width)?
|
||||||
.ok_or_else(|| ::std::io::Error::new(
|
.checked_mul(usize::try_from(self.height)?)
|
||||||
::std::io::ErrorKind::InvalidInput,
|
.ok_or_else(|| {
|
||||||
"Image width * height exceeds usize"))?;
|
io::Error::new(
|
||||||
|
io::ErrorKind::InvalidInput,
|
||||||
|
"Image width * height exceeds usize",
|
||||||
|
)
|
||||||
|
})?;
|
||||||
if data.len() < num_pix {
|
if data.len() < num_pix {
|
||||||
return Err(::std::io::Error::new(
|
return Err(io::Error::new(
|
||||||
::std::io::ErrorKind::InvalidData,
|
io::ErrorKind::InvalidData,
|
||||||
"Input data slice is undersized for provided dimensions").into());
|
"Input data slice is undersized for provided dimensions",
|
||||||
|
)
|
||||||
|
.into());
|
||||||
}
|
}
|
||||||
let mut idx = 0;
|
let mut idx = 0;
|
||||||
while self.next_strip_sample_count() > 0 {
|
while self.next_strip_sample_count() > 0 {
|
||||||
@@ -668,14 +404,20 @@ impl<'a, W: 'a + Write + Seek, T: ColorType> ImageEncoder<'a, W, T> {
|
|||||||
|
|
||||||
/// Set image resolution
|
/// Set image resolution
|
||||||
pub fn resolution(&mut self, unit: ResolutionUnit, value: Rational) {
|
pub fn resolution(&mut self, unit: ResolutionUnit, value: Rational) {
|
||||||
self.encoder.write_tag(Tag::ResolutionUnit, unit.to_u16()).unwrap();
|
self.encoder
|
||||||
self.encoder.write_tag(Tag::XResolution, value.clone()).unwrap();
|
.write_tag(Tag::ResolutionUnit, unit.to_u16())
|
||||||
|
.unwrap();
|
||||||
|
self.encoder
|
||||||
|
.write_tag(Tag::XResolution, value.clone())
|
||||||
|
.unwrap();
|
||||||
self.encoder.write_tag(Tag::YResolution, value).unwrap();
|
self.encoder.write_tag(Tag::YResolution, value).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set image resolution unit
|
/// Set image resolution unit
|
||||||
pub fn resolution_unit(&mut self, unit: ResolutionUnit) {
|
pub fn resolution_unit(&mut self, unit: ResolutionUnit) {
|
||||||
self.encoder.write_tag(Tag::ResolutionUnit, unit.to_u16()).unwrap();
|
self.encoder
|
||||||
|
.write_tag(Tag::ResolutionUnit, unit.to_u16())
|
||||||
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set image x-resolution
|
/// Set image x-resolution
|
||||||
@@ -688,18 +430,42 @@ impl<'a, W: 'a + Write + Seek, T: ColorType> ImageEncoder<'a, W, T> {
|
|||||||
self.encoder.write_tag(Tag::YResolution, value).unwrap();
|
self.encoder.write_tag(Tag::YResolution, value).unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Set image number of lines per strip
|
||||||
|
///
|
||||||
|
/// This function needs to be called before any calls to `write_data` or
|
||||||
|
/// `write_strip` and will return an error otherwise.
|
||||||
|
pub fn rows_per_strip(&mut self, value: u32) -> TiffResult<()> {
|
||||||
|
if self.strip_idx != 0 {
|
||||||
|
return Err(io::Error::new(
|
||||||
|
io::ErrorKind::InvalidInput,
|
||||||
|
"Cannot change strip size after data was written",
|
||||||
|
)
|
||||||
|
.into());
|
||||||
|
}
|
||||||
|
// Write tag as 32 bits
|
||||||
|
self.encoder.write_tag(Tag::RowsPerStrip, value)?;
|
||||||
|
|
||||||
|
let value: u64 = value as u64;
|
||||||
|
self.strip_count = (self.height as u64 + value - 1) / value;
|
||||||
|
self.rows_per_strip = value;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
fn finish_internal(&mut self) -> TiffResult<()> {
|
fn finish_internal(&mut self) -> TiffResult<()> {
|
||||||
self.encoder
|
self.encoder
|
||||||
.write_tag(Tag::StripOffsets, &*self.strip_offsets)?;
|
.write_tag(Tag::StripOffsets, K::convert_slice(&self.strip_offsets))?;
|
||||||
self.encoder
|
self.encoder.write_tag(
|
||||||
.write_tag(Tag::StripByteCounts, &*self.strip_byte_count)?;
|
Tag::StripByteCounts,
|
||||||
|
K::convert_slice(&self.strip_byte_count),
|
||||||
|
)?;
|
||||||
self.dropped = true;
|
self.dropped = true;
|
||||||
|
|
||||||
self.encoder.finish_internal()
|
self.encoder.finish_internal()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Get a reference of the underlying `DirectoryEncoder`
|
/// Get a reference of the underlying `DirectoryEncoder`
|
||||||
pub fn encoder(&mut self) -> &mut DirectoryEncoder<'a, W> {
|
pub fn encoder(&mut self) -> &mut DirectoryEncoder<'a, W, K> {
|
||||||
&mut self.encoder
|
&mut self.encoder
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -709,10 +475,127 @@ impl<'a, W: 'a + Write + Seek, T: ColorType> ImageEncoder<'a, W, T> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, W: Write + Seek, C: ColorType> Drop for ImageEncoder<'a, W, C> {
|
impl<'a, W: Write + Seek, C: ColorType, K: TiffKind> Drop for ImageEncoder<'a, W, C, K> {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
if !self.dropped {
|
if !self.dropped {
|
||||||
let _ = self.finish_internal();
|
let _ = self.finish_internal();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct DirectoryEntry<S> {
|
||||||
|
data_type: u16,
|
||||||
|
count: S,
|
||||||
|
data: Vec<u8>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Trait to abstract over Tiff/BigTiff differences.
|
||||||
|
///
|
||||||
|
/// Implemented for [`TiffKindStandard`] and [`TiffKindBig`].
|
||||||
|
pub trait TiffKind {
|
||||||
|
/// The type of offset fields, `u32` for normal Tiff, `u64` for BigTiff.
|
||||||
|
type OffsetType: TryFrom<usize, Error = TryFromIntError> + Into<u64> + TiffValue;
|
||||||
|
|
||||||
|
/// Needed for the `convert_slice` method.
|
||||||
|
type OffsetArrayType: ?Sized + TiffValue;
|
||||||
|
|
||||||
|
/// Write the (Big)Tiff header.
|
||||||
|
fn write_header<W: Write>(writer: &mut TiffWriter<W>) -> TiffResult<()>;
|
||||||
|
|
||||||
|
/// Convert a file offset to `Self::OffsetType`.
|
||||||
|
///
|
||||||
|
/// This returns an error for normal Tiff if the offset is larger than `u32::MAX`.
|
||||||
|
fn convert_offset(offset: u64) -> TiffResult<Self::OffsetType>;
|
||||||
|
|
||||||
|
/// Write an offset value to the given writer.
|
||||||
|
///
|
||||||
|
/// Like `convert_offset`, this errors if `offset > u32::MAX` for normal Tiff.
|
||||||
|
fn write_offset<W: Write>(writer: &mut TiffWriter<W>, offset: u64) -> TiffResult<()>;
|
||||||
|
|
||||||
|
/// Write the IFD entry count field with the given `count` value.
|
||||||
|
///
|
||||||
|
/// The entry count field is an `u16` for normal Tiff and `u64` for BigTiff. Errors
|
||||||
|
/// if the given `usize` is larger than the representable values.
|
||||||
|
fn write_entry_count<W: Write>(writer: &mut TiffWriter<W>, count: usize) -> TiffResult<()>;
|
||||||
|
|
||||||
|
/// Internal helper method for satisfying Rust's type checker.
|
||||||
|
///
|
||||||
|
/// The `TiffValue` trait is implemented for both primitive values (e.g. `u8`, `u32`) and
|
||||||
|
/// slices of primitive values (e.g. `[u8]`, `[u32]`). However, this is not represented in
|
||||||
|
/// the type system, so there is no guarantee that that for all `T: TiffValue` there is also
|
||||||
|
/// an implementation of `TiffValue` for `[T]`. This method works around that problem by
|
||||||
|
/// providing a conversion from `[T]` to some value that implements `TiffValue`, thereby
|
||||||
|
/// making all slices of `OffsetType` usable with `write_tag` and similar methods.
|
||||||
|
///
|
||||||
|
/// Implementations of this trait should always set `OffsetArrayType` to `[OffsetType]`.
|
||||||
|
fn convert_slice(slice: &[Self::OffsetType]) -> &Self::OffsetArrayType;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create a standard Tiff file.
|
||||||
|
pub struct TiffKindStandard;
|
||||||
|
|
||||||
|
impl TiffKind for TiffKindStandard {
|
||||||
|
type OffsetType = u32;
|
||||||
|
type OffsetArrayType = [u32];
|
||||||
|
|
||||||
|
fn write_header<W: Write>(writer: &mut TiffWriter<W>) -> TiffResult<()> {
|
||||||
|
write_tiff_header(writer)?;
|
||||||
|
// blank the IFD offset location
|
||||||
|
writer.write_u32(0)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn convert_offset(offset: u64) -> TiffResult<Self::OffsetType> {
|
||||||
|
Ok(Self::OffsetType::try_from(offset)?)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write_offset<W: Write>(writer: &mut TiffWriter<W>, offset: u64) -> TiffResult<()> {
|
||||||
|
writer.write_u32(u32::try_from(offset)?)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write_entry_count<W: Write>(writer: &mut TiffWriter<W>, count: usize) -> TiffResult<()> {
|
||||||
|
writer.write_u16(u16::try_from(count)?)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn convert_slice(slice: &[Self::OffsetType]) -> &Self::OffsetArrayType {
|
||||||
|
slice
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Create a BigTiff file.
|
||||||
|
pub struct TiffKindBig;
|
||||||
|
|
||||||
|
impl TiffKind for TiffKindBig {
|
||||||
|
type OffsetType = u64;
|
||||||
|
type OffsetArrayType = [u64];
|
||||||
|
|
||||||
|
fn write_header<W: Write>(writer: &mut TiffWriter<W>) -> TiffResult<()> {
|
||||||
|
write_bigtiff_header(writer)?;
|
||||||
|
// blank the IFD offset location
|
||||||
|
writer.write_u64(0)?;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn convert_offset(offset: u64) -> TiffResult<Self::OffsetType> {
|
||||||
|
Ok(offset)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write_offset<W: Write>(writer: &mut TiffWriter<W>, offset: u64) -> TiffResult<()> {
|
||||||
|
writer.write_u64(offset)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write_entry_count<W: Write>(writer: &mut TiffWriter<W>, count: usize) -> TiffResult<()> {
|
||||||
|
writer.write_u64(u64::try_from(count)?)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn convert_slice(slice: &[Self::OffsetType]) -> &Self::OffsetArrayType {
|
||||||
|
slice
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,483 @@
|
|||||||
|
use std::io::Write;
|
||||||
|
|
||||||
|
use crate::{bytecast, tags::Type, TiffError, TiffFormatError, TiffResult};
|
||||||
|
|
||||||
|
use super::writer::TiffWriter;
|
||||||
|
|
||||||
|
/// Trait for types that can be encoded in a tiff file
|
||||||
|
pub trait TiffValue {
|
||||||
|
const BYTE_LEN: u8;
|
||||||
|
const FIELD_TYPE: Type;
|
||||||
|
fn count(&self) -> usize;
|
||||||
|
fn bytes(&self) -> usize {
|
||||||
|
self.count() * usize::from(Self::BYTE_LEN)
|
||||||
|
}
|
||||||
|
fn write<W: Write>(&self, writer: &mut TiffWriter<W>) -> TiffResult<()>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TiffValue for [u8] {
|
||||||
|
const BYTE_LEN: u8 = 1;
|
||||||
|
const FIELD_TYPE: Type = Type::BYTE;
|
||||||
|
|
||||||
|
fn count(&self) -> usize {
|
||||||
|
self.len()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write<W: Write>(&self, writer: &mut TiffWriter<W>) -> TiffResult<()> {
|
||||||
|
writer.write_bytes(self)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TiffValue for [i8] {
|
||||||
|
const BYTE_LEN: u8 = 1;
|
||||||
|
const FIELD_TYPE: Type = Type::SBYTE;
|
||||||
|
|
||||||
|
fn count(&self) -> usize {
|
||||||
|
self.len()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write<W: Write>(&self, writer: &mut TiffWriter<W>) -> TiffResult<()> {
|
||||||
|
let slice = bytecast::i8_as_ne_bytes(self);
|
||||||
|
writer.write_bytes(slice)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TiffValue for [u16] {
|
||||||
|
const BYTE_LEN: u8 = 2;
|
||||||
|
const FIELD_TYPE: Type = Type::SHORT;
|
||||||
|
|
||||||
|
fn count(&self) -> usize {
|
||||||
|
self.len()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write<W: Write>(&self, writer: &mut TiffWriter<W>) -> TiffResult<()> {
|
||||||
|
let slice = bytecast::u16_as_ne_bytes(self);
|
||||||
|
writer.write_bytes(slice)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TiffValue for [i16] {
|
||||||
|
const BYTE_LEN: u8 = 2;
|
||||||
|
const FIELD_TYPE: Type = Type::SSHORT;
|
||||||
|
|
||||||
|
fn count(&self) -> usize {
|
||||||
|
self.len()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write<W: Write>(&self, writer: &mut TiffWriter<W>) -> TiffResult<()> {
|
||||||
|
let slice = bytecast::i16_as_ne_bytes(self);
|
||||||
|
writer.write_bytes(slice)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TiffValue for [u32] {
|
||||||
|
const BYTE_LEN: u8 = 4;
|
||||||
|
const FIELD_TYPE: Type = Type::LONG;
|
||||||
|
|
||||||
|
fn count(&self) -> usize {
|
||||||
|
self.len()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write<W: Write>(&self, writer: &mut TiffWriter<W>) -> TiffResult<()> {
|
||||||
|
let slice = bytecast::u32_as_ne_bytes(self);
|
||||||
|
writer.write_bytes(slice)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TiffValue for [i32] {
|
||||||
|
const BYTE_LEN: u8 = 4;
|
||||||
|
const FIELD_TYPE: Type = Type::SLONG;
|
||||||
|
|
||||||
|
fn count(&self) -> usize {
|
||||||
|
self.len()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write<W: Write>(&self, writer: &mut TiffWriter<W>) -> TiffResult<()> {
|
||||||
|
let slice = bytecast::i32_as_ne_bytes(self);
|
||||||
|
writer.write_bytes(slice)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TiffValue for [u64] {
|
||||||
|
const BYTE_LEN: u8 = 8;
|
||||||
|
const FIELD_TYPE: Type = Type::LONG8;
|
||||||
|
|
||||||
|
fn count(&self) -> usize {
|
||||||
|
self.len()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write<W: Write>(&self, writer: &mut TiffWriter<W>) -> TiffResult<()> {
|
||||||
|
let slice = bytecast::u64_as_ne_bytes(self);
|
||||||
|
writer.write_bytes(slice)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TiffValue for [i64] {
|
||||||
|
const BYTE_LEN: u8 = 8;
|
||||||
|
const FIELD_TYPE: Type = Type::SLONG8;
|
||||||
|
|
||||||
|
fn count(&self) -> usize {
|
||||||
|
self.len()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write<W: Write>(&self, writer: &mut TiffWriter<W>) -> TiffResult<()> {
|
||||||
|
let slice = bytecast::i64_as_ne_bytes(self);
|
||||||
|
writer.write_bytes(slice)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TiffValue for [f32] {
|
||||||
|
const BYTE_LEN: u8 = 4;
|
||||||
|
const FIELD_TYPE: Type = Type::FLOAT;
|
||||||
|
|
||||||
|
fn count(&self) -> usize {
|
||||||
|
self.len()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write<W: Write>(&self, writer: &mut TiffWriter<W>) -> TiffResult<()> {
|
||||||
|
// We write using nativeedian so this sould be safe
|
||||||
|
let slice = bytecast::f32_as_ne_bytes(self);
|
||||||
|
writer.write_bytes(slice)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TiffValue for [f64] {
|
||||||
|
const BYTE_LEN: u8 = 8;
|
||||||
|
const FIELD_TYPE: Type = Type::DOUBLE;
|
||||||
|
|
||||||
|
fn count(&self) -> usize {
|
||||||
|
self.len()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write<W: Write>(&self, writer: &mut TiffWriter<W>) -> TiffResult<()> {
|
||||||
|
// We write using nativeedian so this sould be safe
|
||||||
|
let slice = bytecast::f64_as_ne_bytes(self);
|
||||||
|
writer.write_bytes(slice)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TiffValue for [Ifd] {
|
||||||
|
const BYTE_LEN: u8 = 4;
|
||||||
|
const FIELD_TYPE: Type = Type::IFD;
|
||||||
|
|
||||||
|
fn count(&self) -> usize {
|
||||||
|
self.len()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write<W: Write>(&self, writer: &mut TiffWriter<W>) -> TiffResult<()> {
|
||||||
|
for x in self {
|
||||||
|
x.write(writer)?;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TiffValue for [Ifd8] {
|
||||||
|
const BYTE_LEN: u8 = 8;
|
||||||
|
const FIELD_TYPE: Type = Type::IFD8;
|
||||||
|
|
||||||
|
fn count(&self) -> usize {
|
||||||
|
self.len()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write<W: Write>(&self, writer: &mut TiffWriter<W>) -> TiffResult<()> {
|
||||||
|
for x in self {
|
||||||
|
x.write(writer)?;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TiffValue for [Rational] {
|
||||||
|
const BYTE_LEN: u8 = 8;
|
||||||
|
const FIELD_TYPE: Type = Type::RATIONAL;
|
||||||
|
|
||||||
|
fn count(&self) -> usize {
|
||||||
|
self.len()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write<W: Write>(&self, writer: &mut TiffWriter<W>) -> TiffResult<()> {
|
||||||
|
for x in self {
|
||||||
|
x.write(writer)?;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TiffValue for [SRational] {
|
||||||
|
const BYTE_LEN: u8 = 8;
|
||||||
|
const FIELD_TYPE: Type = Type::SRATIONAL;
|
||||||
|
|
||||||
|
fn count(&self) -> usize {
|
||||||
|
self.len()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write<W: Write>(&self, writer: &mut TiffWriter<W>) -> TiffResult<()> {
|
||||||
|
for x in self {
|
||||||
|
x.write(writer)?;
|
||||||
|
}
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TiffValue for u8 {
|
||||||
|
const BYTE_LEN: u8 = 1;
|
||||||
|
const FIELD_TYPE: Type = Type::BYTE;
|
||||||
|
|
||||||
|
fn count(&self) -> usize {
|
||||||
|
1
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write<W: Write>(&self, writer: &mut TiffWriter<W>) -> TiffResult<()> {
|
||||||
|
writer.write_u8(*self)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TiffValue for i8 {
|
||||||
|
const BYTE_LEN: u8 = 1;
|
||||||
|
const FIELD_TYPE: Type = Type::SBYTE;
|
||||||
|
|
||||||
|
fn count(&self) -> usize {
|
||||||
|
1
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write<W: Write>(&self, writer: &mut TiffWriter<W>) -> TiffResult<()> {
|
||||||
|
writer.write_i8(*self)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TiffValue for u16 {
|
||||||
|
const BYTE_LEN: u8 = 2;
|
||||||
|
const FIELD_TYPE: Type = Type::SHORT;
|
||||||
|
|
||||||
|
fn count(&self) -> usize {
|
||||||
|
1
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write<W: Write>(&self, writer: &mut TiffWriter<W>) -> TiffResult<()> {
|
||||||
|
writer.write_u16(*self)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TiffValue for i16 {
|
||||||
|
const BYTE_LEN: u8 = 2;
|
||||||
|
const FIELD_TYPE: Type = Type::SSHORT;
|
||||||
|
|
||||||
|
fn count(&self) -> usize {
|
||||||
|
1
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write<W: Write>(&self, writer: &mut TiffWriter<W>) -> TiffResult<()> {
|
||||||
|
writer.write_i16(*self)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TiffValue for u32 {
|
||||||
|
const BYTE_LEN: u8 = 4;
|
||||||
|
const FIELD_TYPE: Type = Type::LONG;
|
||||||
|
|
||||||
|
fn count(&self) -> usize {
|
||||||
|
1
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write<W: Write>(&self, writer: &mut TiffWriter<W>) -> TiffResult<()> {
|
||||||
|
writer.write_u32(*self)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TiffValue for i32 {
|
||||||
|
const BYTE_LEN: u8 = 4;
|
||||||
|
const FIELD_TYPE: Type = Type::SLONG;
|
||||||
|
|
||||||
|
fn count(&self) -> usize {
|
||||||
|
1
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write<W: Write>(&self, writer: &mut TiffWriter<W>) -> TiffResult<()> {
|
||||||
|
writer.write_i32(*self)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TiffValue for u64 {
|
||||||
|
const BYTE_LEN: u8 = 8;
|
||||||
|
const FIELD_TYPE: Type = Type::LONG8;
|
||||||
|
|
||||||
|
fn count(&self) -> usize {
|
||||||
|
1
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write<W: Write>(&self, writer: &mut TiffWriter<W>) -> TiffResult<()> {
|
||||||
|
writer.write_u64(*self)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TiffValue for i64 {
|
||||||
|
const BYTE_LEN: u8 = 8;
|
||||||
|
const FIELD_TYPE: Type = Type::SLONG8;
|
||||||
|
|
||||||
|
fn count(&self) -> usize {
|
||||||
|
1
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write<W: Write>(&self, writer: &mut TiffWriter<W>) -> TiffResult<()> {
|
||||||
|
writer.write_i64(*self)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TiffValue for f32 {
|
||||||
|
const BYTE_LEN: u8 = 4;
|
||||||
|
const FIELD_TYPE: Type = Type::FLOAT;
|
||||||
|
|
||||||
|
fn count(&self) -> usize {
|
||||||
|
1
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write<W: Write>(&self, writer: &mut TiffWriter<W>) -> TiffResult<()> {
|
||||||
|
writer.write_f32(*self)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TiffValue for f64 {
|
||||||
|
const BYTE_LEN: u8 = 8;
|
||||||
|
const FIELD_TYPE: Type = Type::DOUBLE;
|
||||||
|
|
||||||
|
fn count(&self) -> usize {
|
||||||
|
1
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write<W: Write>(&self, writer: &mut TiffWriter<W>) -> TiffResult<()> {
|
||||||
|
writer.write_f64(*self)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TiffValue for Ifd {
|
||||||
|
const BYTE_LEN: u8 = 4;
|
||||||
|
const FIELD_TYPE: Type = Type::IFD;
|
||||||
|
|
||||||
|
fn count(&self) -> usize {
|
||||||
|
1
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write<W: Write>(&self, writer: &mut TiffWriter<W>) -> TiffResult<()> {
|
||||||
|
writer.write_u32(self.0)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TiffValue for Ifd8 {
|
||||||
|
const BYTE_LEN: u8 = 8;
|
||||||
|
const FIELD_TYPE: Type = Type::IFD8;
|
||||||
|
|
||||||
|
fn count(&self) -> usize {
|
||||||
|
1
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write<W: Write>(&self, writer: &mut TiffWriter<W>) -> TiffResult<()> {
|
||||||
|
writer.write_u64(self.0)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TiffValue for Rational {
|
||||||
|
const BYTE_LEN: u8 = 8;
|
||||||
|
const FIELD_TYPE: Type = Type::RATIONAL;
|
||||||
|
|
||||||
|
fn count(&self) -> usize {
|
||||||
|
1
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write<W: Write>(&self, writer: &mut TiffWriter<W>) -> TiffResult<()> {
|
||||||
|
writer.write_u32(self.n)?;
|
||||||
|
writer.write_u32(self.d)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TiffValue for SRational {
|
||||||
|
const BYTE_LEN: u8 = 8;
|
||||||
|
const FIELD_TYPE: Type = Type::SRATIONAL;
|
||||||
|
|
||||||
|
fn count(&self) -> usize {
|
||||||
|
1
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write<W: Write>(&self, writer: &mut TiffWriter<W>) -> TiffResult<()> {
|
||||||
|
writer.write_i32(self.n)?;
|
||||||
|
writer.write_i32(self.d)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TiffValue for str {
|
||||||
|
const BYTE_LEN: u8 = 1;
|
||||||
|
const FIELD_TYPE: Type = Type::ASCII;
|
||||||
|
|
||||||
|
fn count(&self) -> usize {
|
||||||
|
self.len() + 1
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write<W: Write>(&self, writer: &mut TiffWriter<W>) -> TiffResult<()> {
|
||||||
|
if self.is_ascii() && !self.bytes().any(|b| b == 0) {
|
||||||
|
writer.write_bytes(self.as_bytes())?;
|
||||||
|
writer.write_u8(0)?;
|
||||||
|
Ok(())
|
||||||
|
} else {
|
||||||
|
Err(TiffError::FormatError(TiffFormatError::InvalidTag))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a, T: TiffValue + ?Sized> TiffValue for &'a T {
|
||||||
|
const BYTE_LEN: u8 = T::BYTE_LEN;
|
||||||
|
const FIELD_TYPE: Type = T::FIELD_TYPE;
|
||||||
|
|
||||||
|
fn count(&self) -> usize {
|
||||||
|
(*self).count()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write<W: Write>(&self, writer: &mut TiffWriter<W>) -> TiffResult<()> {
|
||||||
|
(*self).write(writer)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Type to represent tiff values of type `IFD`
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct Ifd(pub u32);
|
||||||
|
|
||||||
|
/// Type to represent tiff values of type `IFD8`
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct Ifd8(pub u64);
|
||||||
|
|
||||||
|
/// Type to represent tiff values of type `RATIONAL`
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct Rational {
|
||||||
|
pub n: u32,
|
||||||
|
pub d: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Type to represent tiff values of type `SRATIONAL`
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct SRational {
|
||||||
|
pub n: i32,
|
||||||
|
pub d: i32,
|
||||||
|
}
|
||||||
+60
-26
@@ -1,29 +1,42 @@
|
|||||||
use error::TiffResult;
|
use crate::error::TiffResult;
|
||||||
use byteorder::{BigEndian, ByteOrder, LittleEndian, NativeEndian, WriteBytesExt};
|
|
||||||
use std::io::{self, Seek, SeekFrom, Write};
|
use std::io::{self, Seek, SeekFrom, Write};
|
||||||
|
|
||||||
pub trait TiffByteOrder: ByteOrder {
|
pub fn write_tiff_header<W: Write>(writer: &mut TiffWriter<W>) -> TiffResult<()> {
|
||||||
fn write_header<W: Write>(writer: &mut TiffWriter<W>) -> TiffResult<()>;
|
#[cfg(target_endian = "little")]
|
||||||
|
let boi: u8 = 0x49;
|
||||||
|
#[cfg(not(target_endian = "little"))]
|
||||||
|
let boi: u8 = 0x4d;
|
||||||
|
|
||||||
|
writer.writer.write_all(&[boi, boi])?;
|
||||||
|
writer.writer.write_all(&42u16.to_ne_bytes())?;
|
||||||
|
writer.offset += 4;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TiffByteOrder for LittleEndian {
|
/// Writes a BigTiff header, excluding the IFD offset field.
|
||||||
fn write_header<W: Write>(writer: &mut TiffWriter<W>) -> TiffResult<()> {
|
///
|
||||||
writer.writer.write_u16::<LittleEndian>(0x4949)?;
|
/// Writes the byte order, version number, offset byte size, and zero constant fields. Does
|
||||||
writer.writer.write_u16::<LittleEndian>(42)?;
|
// _not_ write the offset to the first IFD, this should be done by the caller.
|
||||||
writer.offset += 4;
|
pub fn write_bigtiff_header<W: Write>(writer: &mut TiffWriter<W>) -> TiffResult<()> {
|
||||||
|
#[cfg(target_endian = "little")]
|
||||||
|
let boi: u8 = 0x49;
|
||||||
|
#[cfg(not(target_endian = "little"))]
|
||||||
|
let boi: u8 = 0x4d;
|
||||||
|
|
||||||
Ok(())
|
// byte order indication
|
||||||
}
|
writer.writer.write_all(&[boi, boi])?;
|
||||||
}
|
// version number
|
||||||
|
writer.writer.write_all(&43u16.to_ne_bytes())?;
|
||||||
|
// bytesize of offsets (pointer size)
|
||||||
|
writer.writer.write_all(&8u16.to_ne_bytes())?;
|
||||||
|
// always 0
|
||||||
|
writer.writer.write_all(&0u16.to_ne_bytes())?;
|
||||||
|
|
||||||
impl TiffByteOrder for BigEndian {
|
// we wrote 8 bytes, so set the internal offset accordingly
|
||||||
fn write_header<W: Write>(writer: &mut TiffWriter<W>) -> TiffResult<()> {
|
writer.offset += 8;
|
||||||
writer.writer.write_u16::<BigEndian>(0x4d4d)?;
|
|
||||||
writer.writer.write_u16::<BigEndian>(42)?;
|
|
||||||
writer.offset += 4;
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct TiffWriter<W> {
|
pub struct TiffWriter<W> {
|
||||||
@@ -47,47 +60,68 @@ impl<W: Write> TiffWriter<W> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn write_u8(&mut self, n: u8) -> Result<(), io::Error> {
|
pub fn write_u8(&mut self, n: u8) -> Result<(), io::Error> {
|
||||||
self.writer.write_u8(n)?;
|
self.writer.write_all(&n.to_ne_bytes())?;
|
||||||
self.offset += 1;
|
self.offset += 1;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn write_i8(&mut self, n: i8) -> Result<(), io::Error> {
|
pub fn write_i8(&mut self, n: i8) -> Result<(), io::Error> {
|
||||||
self.writer.write_i8(n)?;
|
self.writer.write_all(&n.to_ne_bytes())?;
|
||||||
self.offset += 1;
|
self.offset += 1;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn write_u16(&mut self, n: u16) -> Result<(), io::Error> {
|
pub fn write_u16(&mut self, n: u16) -> Result<(), io::Error> {
|
||||||
self.writer.write_u16::<NativeEndian>(n)?;
|
self.writer.write_all(&n.to_ne_bytes())?;
|
||||||
self.offset += 2;
|
self.offset += 2;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn write_i16(&mut self, n: i16) -> Result<(), io::Error> {
|
pub fn write_i16(&mut self, n: i16) -> Result<(), io::Error> {
|
||||||
self.writer.write_i16::<NativeEndian>(n)?;
|
self.writer.write_all(&n.to_ne_bytes())?;
|
||||||
self.offset += 2;
|
self.offset += 2;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn write_u32(&mut self, n: u32) -> Result<(), io::Error> {
|
pub fn write_u32(&mut self, n: u32) -> Result<(), io::Error> {
|
||||||
self.writer.write_u32::<NativeEndian>(n)?;
|
self.writer.write_all(&n.to_ne_bytes())?;
|
||||||
self.offset += 4;
|
self.offset += 4;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn write_i32(&mut self, n: i32) -> Result<(), io::Error> {
|
pub fn write_i32(&mut self, n: i32) -> Result<(), io::Error> {
|
||||||
self.writer.write_i32::<NativeEndian>(n)?;
|
self.writer.write_all(&n.to_ne_bytes())?;
|
||||||
self.offset += 4;
|
self.offset += 4;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn write_u64(&mut self, n: u64) -> Result<(), io::Error> {
|
pub fn write_u64(&mut self, n: u64) -> Result<(), io::Error> {
|
||||||
self.writer.write_u64::<NativeEndian>(n)?;
|
self.writer.write_all(&n.to_ne_bytes())?;
|
||||||
|
self.offset += 8;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn write_i64(&mut self, n: i64) -> Result<(), io::Error> {
|
||||||
|
self.writer.write_all(&n.to_ne_bytes())?;
|
||||||
|
self.offset += 8;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn write_f32(&mut self, n: f32) -> Result<(), io::Error> {
|
||||||
|
self.writer.write_all(&u32::to_ne_bytes(n.to_bits()))?;
|
||||||
|
self.offset += 4;
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn write_f64(&mut self, n: f64) -> Result<(), io::Error> {
|
||||||
|
self.writer.write_all(&u64::to_ne_bytes(n.to_bits()))?;
|
||||||
self.offset += 8;
|
self.offset += 8;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
+82
-27
@@ -1,12 +1,14 @@
|
|||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::io;
|
use std::io;
|
||||||
|
use std::str;
|
||||||
use std::string;
|
use std::string;
|
||||||
|
|
||||||
use decoder::ifd::{Value};
|
use crate::decoder::{ifd::Value, ChunkType};
|
||||||
use tags::{CompressionMethod, PhotometricInterpretation, PlanarConfiguration, Tag};
|
use crate::tags::{
|
||||||
use miniz_oxide::inflate::TINFLStatus;
|
CompressionMethod, PhotometricInterpretation, PlanarConfiguration, SampleFormat, Tag,
|
||||||
use ColorType;
|
};
|
||||||
|
use crate::ColorType;
|
||||||
|
|
||||||
/// Tiff error kinds.
|
/// Tiff error kinds.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@@ -26,6 +28,9 @@ pub enum TiffError {
|
|||||||
/// An integer conversion to or from a platform size failed, either due to
|
/// An integer conversion to or from a platform size failed, either due to
|
||||||
/// limits of the platform size or limits of the format.
|
/// limits of the platform size or limits of the format.
|
||||||
IntSizeError,
|
IntSizeError,
|
||||||
|
|
||||||
|
/// The image does not support the requested operation
|
||||||
|
UsageError(UsageError),
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The image is not formatted properly.
|
/// The image is not formatted properly.
|
||||||
@@ -35,19 +40,29 @@ pub enum TiffError {
|
|||||||
///
|
///
|
||||||
/// The list of variants may grow to incorporate errors of future features. Matching against this
|
/// The list of variants may grow to incorporate errors of future features. Matching against this
|
||||||
/// exhaustively is not covered by interface stability guarantees.
|
/// exhaustively is not covered by interface stability guarantees.
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
pub enum TiffFormatError {
|
pub enum TiffFormatError {
|
||||||
TiffSignatureNotFound,
|
TiffSignatureNotFound,
|
||||||
TiffSignatureInvalid,
|
TiffSignatureInvalid,
|
||||||
ImageFileDirectoryNotFound,
|
ImageFileDirectoryNotFound,
|
||||||
InconsistentSizesEncountered,
|
InconsistentSizesEncountered,
|
||||||
|
UnexpectedCompressedData {
|
||||||
|
actual_bytes: usize,
|
||||||
|
required_bytes: usize,
|
||||||
|
},
|
||||||
|
InconsistentStripSamples {
|
||||||
|
actual_samples: usize,
|
||||||
|
required_samples: usize,
|
||||||
|
},
|
||||||
InvalidTag,
|
InvalidTag,
|
||||||
InvalidTagValueType(Tag),
|
InvalidTagValueType(Tag),
|
||||||
RequiredTagNotFound(Tag),
|
RequiredTagNotFound(Tag),
|
||||||
UnknownPredictor(u16),
|
UnknownPredictor(u16),
|
||||||
|
ByteExpected(Value),
|
||||||
UnsignedIntegerExpected(Value),
|
UnsignedIntegerExpected(Value),
|
||||||
SignedIntegerExpected(Value),
|
SignedIntegerExpected(Value),
|
||||||
InflateError(InflateError),
|
Format(String),
|
||||||
|
RequiredTagEmpty(Tag),
|
||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
/// Do not match against this variant. It may get removed.
|
/// Do not match against this variant. It may get removed.
|
||||||
__NonExhaustive,
|
__NonExhaustive,
|
||||||
@@ -61,43 +76,48 @@ impl fmt::Display for TiffFormatError {
|
|||||||
TiffSignatureInvalid => write!(fmt, "TIFF signature invalid."),
|
TiffSignatureInvalid => write!(fmt, "TIFF signature invalid."),
|
||||||
ImageFileDirectoryNotFound => write!(fmt, "Image file directory not found."),
|
ImageFileDirectoryNotFound => write!(fmt, "Image file directory not found."),
|
||||||
InconsistentSizesEncountered => write!(fmt, "Inconsistent sizes encountered."),
|
InconsistentSizesEncountered => write!(fmt, "Inconsistent sizes encountered."),
|
||||||
|
UnexpectedCompressedData {
|
||||||
|
actual_bytes,
|
||||||
|
required_bytes,
|
||||||
|
} => {
|
||||||
|
write!(
|
||||||
|
fmt,
|
||||||
|
"Decompression returned different amount of bytes than expected: got {}, expected {}.",
|
||||||
|
actual_bytes, required_bytes
|
||||||
|
)
|
||||||
|
}
|
||||||
|
InconsistentStripSamples {
|
||||||
|
actual_samples,
|
||||||
|
required_samples,
|
||||||
|
} => {
|
||||||
|
write!(
|
||||||
|
fmt,
|
||||||
|
"Inconsistent elements in strip: got {}, expected {}.",
|
||||||
|
actual_samples, required_samples
|
||||||
|
)
|
||||||
|
}
|
||||||
InvalidTag => write!(fmt, "Image contains invalid tag."),
|
InvalidTag => write!(fmt, "Image contains invalid tag."),
|
||||||
InvalidTagValueType(ref tag) =>
|
InvalidTagValueType(ref tag) => {
|
||||||
write!(fmt, "Tag `{:?}` did not have the expected value type.", tag),
|
write!(fmt, "Tag `{:?}` did not have the expected value type.", tag)
|
||||||
|
}
|
||||||
RequiredTagNotFound(ref tag) => write!(fmt, "Required tag `{:?}` not found.", tag),
|
RequiredTagNotFound(ref tag) => write!(fmt, "Required tag `{:?}` not found.", tag),
|
||||||
UnknownPredictor(ref predictor) => {
|
UnknownPredictor(ref predictor) => {
|
||||||
write!(fmt, "Unknown predictor “{}” encountered", predictor)
|
write!(fmt, "Unknown predictor “{}” encountered", predictor)
|
||||||
}
|
}
|
||||||
|
ByteExpected(ref val) => write!(fmt, "Expected byte, {:?} found.", val),
|
||||||
UnsignedIntegerExpected(ref val) => {
|
UnsignedIntegerExpected(ref val) => {
|
||||||
write!(fmt, "Expected unsigned integer, {:?} found.", val)
|
write!(fmt, "Expected unsigned integer, {:?} found.", val)
|
||||||
}
|
}
|
||||||
SignedIntegerExpected(ref val) => {
|
SignedIntegerExpected(ref val) => {
|
||||||
write!(fmt, "Expected signed integer, {:?} found.", val)
|
write!(fmt, "Expected signed integer, {:?} found.", val)
|
||||||
}
|
}
|
||||||
InflateError(_) => write!(fmt, "Failed to decode inflate data."),
|
Format(ref val) => write!(fmt, "Invalid format: {:?}.", val),
|
||||||
|
RequiredTagEmpty(ref val) => write!(fmt, "Required tag {:?} was empty.", val),
|
||||||
__NonExhaustive => unreachable!(),
|
__NonExhaustive => unreachable!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Decompression failed due to faulty compressed data.
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
|
||||||
pub struct InflateError {
|
|
||||||
status: TINFLStatus,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl InflateError {
|
|
||||||
pub(crate) fn new(status: TINFLStatus) -> Self {
|
|
||||||
Self { status }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TiffError {
|
|
||||||
pub(crate) fn from_inflate_status(status: TINFLStatus) -> Self {
|
|
||||||
TiffError::FormatError(TiffFormatError::InflateError(InflateError::new(status)))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// The Decoder does not support features required by the image.
|
/// The Decoder does not support features required by the image.
|
||||||
///
|
///
|
||||||
/// This only captures known failures for which the standard either does not require support or an
|
/// This only captures known failures for which the standard either does not require support or an
|
||||||
@@ -114,6 +134,7 @@ pub enum TiffUnsupportedError {
|
|||||||
UnknownCompressionMethod,
|
UnknownCompressionMethod,
|
||||||
UnsupportedCompressionMethod(CompressionMethod),
|
UnsupportedCompressionMethod(CompressionMethod),
|
||||||
UnsupportedSampleDepth(u8),
|
UnsupportedSampleDepth(u8),
|
||||||
|
UnsupportedSampleFormat(Vec<SampleFormat>),
|
||||||
UnsupportedColorType(ColorType),
|
UnsupportedColorType(ColorType),
|
||||||
UnsupportedBitsPerChannel(u8),
|
UnsupportedBitsPerChannel(u8),
|
||||||
UnsupportedPlanarConfig(Option<PlanarConfiguration>),
|
UnsupportedPlanarConfig(Option<PlanarConfiguration>),
|
||||||
@@ -148,6 +169,9 @@ impl fmt::Display for TiffUnsupportedError {
|
|||||||
UnsupportedSampleDepth(samples) => {
|
UnsupportedSampleDepth(samples) => {
|
||||||
write!(fmt, "{} samples per pixel is unsupported.", samples)
|
write!(fmt, "{} samples per pixel is unsupported.", samples)
|
||||||
}
|
}
|
||||||
|
UnsupportedSampleFormat(ref formats) => {
|
||||||
|
write!(fmt, "Sample format {:?} is unsupported.", formats)
|
||||||
|
}
|
||||||
UnsupportedColorType(color_type) => {
|
UnsupportedColorType(color_type) => {
|
||||||
write!(fmt, "Color type {:?} is unsupported", color_type)
|
write!(fmt, "Color type {:?} is unsupported", color_type)
|
||||||
}
|
}
|
||||||
@@ -163,6 +187,29 @@ impl fmt::Display for TiffUnsupportedError {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// User attempted to use the Decoder in a way that is incompatible with a specific image.
|
||||||
|
///
|
||||||
|
/// For example: attempting to read a tile from a stripped image.
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum UsageError {
|
||||||
|
InvalidChunkType(ChunkType, ChunkType),
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for UsageError {
|
||||||
|
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
use self::UsageError::*;
|
||||||
|
match *self {
|
||||||
|
InvalidChunkType(expected, actual) => {
|
||||||
|
write!(
|
||||||
|
fmt,
|
||||||
|
"Requested operation is only valid for images with chunk encoding of type: {:?}, got {:?}.",
|
||||||
|
expected, actual
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl fmt::Display for TiffError {
|
impl fmt::Display for TiffError {
|
||||||
fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
||||||
match *self {
|
match *self {
|
||||||
@@ -176,6 +223,7 @@ impl fmt::Display for TiffError {
|
|||||||
TiffError::IoError(ref e) => e.fmt(fmt),
|
TiffError::IoError(ref e) => e.fmt(fmt),
|
||||||
TiffError::LimitsExceeded => write!(fmt, "The Decoder limits are exceeded"),
|
TiffError::LimitsExceeded => write!(fmt, "The Decoder limits are exceeded"),
|
||||||
TiffError::IntSizeError => write!(fmt, "Platform or format size limits exceeded"),
|
TiffError::IntSizeError => write!(fmt, "Platform or format size limits exceeded"),
|
||||||
|
TiffError::UsageError(ref e) => write!(fmt, "Usage error: {}", e),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -188,6 +236,7 @@ impl Error for TiffError {
|
|||||||
TiffError::IoError(..) => "IO error",
|
TiffError::IoError(..) => "IO error",
|
||||||
TiffError::LimitsExceeded => "Decoder limits exceeded",
|
TiffError::LimitsExceeded => "Decoder limits exceeded",
|
||||||
TiffError::IntSizeError => "Platform or format size limits exceeded",
|
TiffError::IntSizeError => "Platform or format size limits exceeded",
|
||||||
|
TiffError::UsageError(..) => "Invalid usage",
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -205,6 +254,12 @@ impl From<io::Error> for TiffError {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl From<str::Utf8Error> for TiffError {
|
||||||
|
fn from(_err: str::Utf8Error) -> TiffError {
|
||||||
|
TiffError::FormatError(TiffFormatError::InvalidTag)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl From<string::FromUtf8Error> for TiffError {
|
impl From<string::FromUtf8Error> for TiffError {
|
||||||
fn from(_err: string::FromUtf8Error) -> TiffError {
|
fn from(_err: string::FromUtf8Error) -> TiffError {
|
||||||
TiffError::FormatError(TiffFormatError::InvalidTag)
|
TiffError::FormatError(TiffFormatError::InvalidTag)
|
||||||
|
|||||||
+4
-4
@@ -6,16 +6,16 @@
|
|||||||
//! # Related Links
|
//! # Related Links
|
||||||
//! * <https://www.adobe.io/open/standards/TIFF.html> - The TIFF specification
|
//! * <https://www.adobe.io/open/standards/TIFF.html> - The TIFF specification
|
||||||
|
|
||||||
extern crate byteorder;
|
extern crate jpeg;
|
||||||
extern crate lzw;
|
extern crate weezl;
|
||||||
extern crate miniz_oxide;
|
|
||||||
|
|
||||||
|
mod bytecast;
|
||||||
pub mod decoder;
|
pub mod decoder;
|
||||||
pub mod encoder;
|
pub mod encoder;
|
||||||
mod error;
|
mod error;
|
||||||
pub mod tags;
|
pub mod tags;
|
||||||
|
|
||||||
pub use self::error::{InflateError, TiffError, TiffFormatError, TiffResult, TiffUnsupportedError};
|
pub use self::error::{TiffError, TiffFormatError, TiffResult, TiffUnsupportedError, UsageError};
|
||||||
|
|
||||||
/// An enumeration over supported color types and their bit depths
|
/// An enumeration over supported color types and their bit depths
|
||||||
#[derive(Copy, PartialEq, Eq, Debug, Clone, Hash)]
|
#[derive(Copy, PartialEq, Eq, Debug, Clone, Hash)]
|
||||||
|
|||||||
+60
-12
@@ -39,16 +39,24 @@ macro_rules! tags {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
tags!($name, $ty);
|
tags!($name, $ty, $($unknown_doc)*);
|
||||||
};
|
};
|
||||||
// For u16 tags, provide direct inherent primitive conversion methods.
|
// For u16 tags, provide direct inherent primitive conversion methods.
|
||||||
($name:tt, u16) => {
|
($name:tt, u16, $($unknown_doc:literal)*) => {
|
||||||
impl $name {
|
impl $name {
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn from_u16(val: u16) -> Option<Self> {
|
pub fn from_u16(val: u16) -> Option<Self> {
|
||||||
Self::__from_inner_type(val).ok()
|
Self::__from_inner_type(val).ok()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$(
|
||||||
|
#[inline(always)]
|
||||||
|
pub fn from_u16_exhaustive(val: u16) -> Self {
|
||||||
|
$unknown_doc;
|
||||||
|
Self::__from_inner_type(val).unwrap_or_else(|_| $name::Unknown(val))
|
||||||
|
}
|
||||||
|
)*
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
pub fn to_u16(&self) -> u16 {
|
pub fn to_u16(&self) -> u16 {
|
||||||
Self::__to_inner_type(self)
|
Self::__to_inner_type(self)
|
||||||
@@ -57,7 +65,7 @@ macro_rules! tags {
|
|||||||
};
|
};
|
||||||
// For other tag types, do nothing for now. With concat_idents one could
|
// For other tag types, do nothing for now. With concat_idents one could
|
||||||
// provide inherent conversion methods for all types.
|
// provide inherent conversion methods for all types.
|
||||||
($name:tt, $ty:tt) => {};
|
($name:tt, $ty:tt, $($unknown_doc:literal)*) => {};
|
||||||
}
|
}
|
||||||
|
|
||||||
// Note: These tags appear in the order they are mentioned in the TIFF reference
|
// Note: These tags appear in the order they are mentioned in the TIFF reference
|
||||||
@@ -109,35 +117,64 @@ pub enum Tag(u16) unknown("A private or extension tag") {
|
|||||||
TileLength = 323,
|
TileLength = 323,
|
||||||
TileOffsets = 324,
|
TileOffsets = 324,
|
||||||
TileByteCounts = 325,
|
TileByteCounts = 325,
|
||||||
|
// Data Sample Format
|
||||||
SampleFormat = 339,
|
SampleFormat = 339,
|
||||||
|
SMinSampleValue = 340, // TODO add support
|
||||||
|
SMaxSampleValue = 341, // TODO add support
|
||||||
|
// JPEG
|
||||||
|
JPEGTables = 347,
|
||||||
|
// GeoTIFF
|
||||||
|
ModelPixelScaleTag = 33550, // (SoftDesk)
|
||||||
|
ModelTransformationTag = 34264, // (JPL Carto Group)
|
||||||
|
ModelTiepointTag = 33922, // (Intergraph)
|
||||||
|
GeoKeyDirectoryTag = 34735, // (SPOT)
|
||||||
|
GeoDoubleParamsTag = 34736, // (SPOT)
|
||||||
|
GeoAsciiParamsTag = 34737, // (SPOT)
|
||||||
|
GdalNodata = 42113, // Contains areas with missing data
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Tag {
|
|
||||||
pub fn from_u16_exhaustive(val: u16) -> Self {
|
|
||||||
Self::from_u16(val).unwrap_or_else(|| Tag::Unknown(val))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
tags! {
|
tags! {
|
||||||
/// The type of an IFD entry (a 2 byte field).
|
/// The type of an IFD entry (a 2 byte field).
|
||||||
pub enum Type(u16) {
|
pub enum Type(u16) {
|
||||||
|
/// 8-bit unsigned integer
|
||||||
BYTE = 1,
|
BYTE = 1,
|
||||||
|
/// 8-bit byte that contains a 7-bit ASCII code; the last byte must be zero
|
||||||
ASCII = 2,
|
ASCII = 2,
|
||||||
|
/// 16-bit unsigned integer
|
||||||
SHORT = 3,
|
SHORT = 3,
|
||||||
|
/// 32-bit unsigned integer
|
||||||
LONG = 4,
|
LONG = 4,
|
||||||
|
/// Fraction stored as two 32-bit unsigned integers
|
||||||
RATIONAL = 5,
|
RATIONAL = 5,
|
||||||
|
/// 8-bit signed integer
|
||||||
SBYTE = 6,
|
SBYTE = 6,
|
||||||
|
/// 8-bit byte that may contain anything, depending on the field
|
||||||
|
UNDEFINED = 7,
|
||||||
|
/// 16-bit signed integer
|
||||||
SSHORT = 8,
|
SSHORT = 8,
|
||||||
|
/// 32-bit signed integer
|
||||||
SLONG = 9,
|
SLONG = 9,
|
||||||
|
/// Fraction stored as two 32-bit signed integers
|
||||||
SRATIONAL = 10,
|
SRATIONAL = 10,
|
||||||
|
/// 32-bit IEEE floating point
|
||||||
|
FLOAT = 11,
|
||||||
|
/// 64-bit IEEE floating point
|
||||||
|
DOUBLE = 12,
|
||||||
|
/// 32-bit unsigned integer (offset)
|
||||||
|
IFD = 13,
|
||||||
/// BigTIFF 64-bit unsigned integer
|
/// BigTIFF 64-bit unsigned integer
|
||||||
LONG8 = 16,
|
LONG8 = 16,
|
||||||
|
/// BigTIFF 64-bit signed integer
|
||||||
|
SLONG8 = 17,
|
||||||
|
/// BigTIFF 64-bit unsigned integer (offset)
|
||||||
|
IFD8 = 18,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
tags! {
|
tags! {
|
||||||
|
/// See [TIFF compression tags](https://www.awaresystems.be/imaging/tiff/tifftags/compression.html)
|
||||||
|
/// for reference.
|
||||||
pub enum CompressionMethod(u16) {
|
pub enum CompressionMethod(u16) {
|
||||||
None = 1,
|
None = 1,
|
||||||
Huffman = 2,
|
Huffman = 2,
|
||||||
@@ -145,6 +182,8 @@ pub enum CompressionMethod(u16) {
|
|||||||
Fax4 = 4,
|
Fax4 = 4,
|
||||||
LZW = 5,
|
LZW = 5,
|
||||||
JPEG = 6,
|
JPEG = 6,
|
||||||
|
// "Extended JPEG" or "new JPEG" style
|
||||||
|
ModernJPEG = 7,
|
||||||
Deflate = 8,
|
Deflate = 8,
|
||||||
OldDeflate = 0x80B2,
|
OldDeflate = 0x80B2,
|
||||||
PackBits = 0x8005,
|
PackBits = 0x8005,
|
||||||
@@ -178,11 +217,20 @@ pub enum Predictor(u16) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
tags!{
|
tags! {
|
||||||
/// Type to represent resolution units
|
/// Type to represent resolution units
|
||||||
pub enum ResolutionUnit(u16) {
|
pub enum ResolutionUnit(u16) {
|
||||||
None = 1,
|
None = 1,
|
||||||
Inch = 2,
|
Inch = 2,
|
||||||
Centimeter = 3,
|
Centimeter = 3,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tags! {
|
||||||
|
pub enum SampleFormat(u16) unknown("An unknown extension sample format") {
|
||||||
|
Uint = 1,
|
||||||
|
Int = 2,
|
||||||
|
IEEEFP = 3,
|
||||||
|
Void = 4,
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,4 @@
|
|||||||
|
Copyrights:
|
||||||
|
|
||||||
|
kodim*.png: Eastman Kodak Company, released for unrestricted use
|
||||||
|
Transparency.png: Public Domain, according to Wikimedia
|
||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
+121
-2
@@ -21,7 +21,7 @@ macro_rules! test_image_sum {
|
|||||||
DecodingResult::$buffer(res) => {
|
DecodingResult::$buffer(res) => {
|
||||||
let sum: $sum_ty = res.into_iter().map(<$sum_ty>::from).sum();
|
let sum: $sum_ty = res.into_iter().map(<$sum_ty>::from).sum();
|
||||||
assert_eq!(sum, expected_sum);
|
assert_eq!(sum, expected_sum);
|
||||||
},
|
}
|
||||||
_ => panic!("Wrong bit depth"),
|
_ => panic!("Wrong bit depth"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -29,9 +29,13 @@ macro_rules! test_image_sum {
|
|||||||
}
|
}
|
||||||
|
|
||||||
test_image_sum!(test_image_sum_u8, U8, u64);
|
test_image_sum!(test_image_sum_u8, U8, u64);
|
||||||
|
test_image_sum!(test_image_sum_i8, I8, i64);
|
||||||
test_image_sum!(test_image_sum_u16, U16, u64);
|
test_image_sum!(test_image_sum_u16, U16, u64);
|
||||||
|
test_image_sum!(test_image_sum_i16, I16, i64);
|
||||||
test_image_sum!(test_image_sum_u32, U32, u64);
|
test_image_sum!(test_image_sum_u32, U32, u64);
|
||||||
test_image_sum!(test_image_sum_u64, U64, u64);
|
test_image_sum!(test_image_sum_u64, U64, u64);
|
||||||
|
test_image_sum!(test_image_sum_f32, F32, f32);
|
||||||
|
test_image_sum!(test_image_sum_f64, F64, f64);
|
||||||
|
|
||||||
/// Tests that a decoder can be constructed for an image and the color type
|
/// Tests that a decoder can be constructed for an image and the color type
|
||||||
/// read from the IFD and is of the appropriate type, but the type is
|
/// read from the IFD and is of the appropriate type, but the type is
|
||||||
@@ -42,7 +46,9 @@ fn test_image_color_type_unsupported(file: &str, expected_type: ColorType) {
|
|||||||
let mut decoder = Decoder::new(img_file).expect("Cannot create decoder");
|
let mut decoder = Decoder::new(img_file).expect("Cannot create decoder");
|
||||||
assert_eq!(decoder.colortype().unwrap(), expected_type);
|
assert_eq!(decoder.colortype().unwrap(), expected_type);
|
||||||
assert!(match decoder.read_image() {
|
assert!(match decoder.read_image() {
|
||||||
Err(tiff::TiffError::UnsupportedError(tiff::TiffUnsupportedError::UnsupportedColorType(_))) => true,
|
Err(tiff::TiffError::UnsupportedError(
|
||||||
|
tiff::TiffUnsupportedError::UnsupportedColorType(_),
|
||||||
|
)) => true,
|
||||||
_ => false,
|
_ => false,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
@@ -57,6 +63,11 @@ fn test_cmyk_u16() {
|
|||||||
test_image_sum_u16("cmyk-3c-16b.tiff", ColorType::CMYK(16), 2181426827);
|
test_image_sum_u16("cmyk-3c-16b.tiff", ColorType::CMYK(16), 2181426827);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_cmyk_f32() {
|
||||||
|
test_image_sum_f32("cmyk-3c-32b-float.tiff", ColorType::CMYK(32), 496.0405);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_gray_u8() {
|
fn test_gray_u8() {
|
||||||
test_image_sum_u8("minisblack-1c-8b.tiff", ColorType::Gray(8), 2840893);
|
test_image_sum_u8("minisblack-1c-8b.tiff", ColorType::Gray(8), 2840893);
|
||||||
@@ -82,6 +93,20 @@ fn test_gray_u64() {
|
|||||||
test_image_sum_u64("gradient-1c-64b.tiff", ColorType::Gray(64), 549892913787);
|
test_image_sum_u64("gradient-1c-64b.tiff", ColorType::Gray(64), 549892913787);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_gray_f32() {
|
||||||
|
test_image_sum_f32("gradient-1c-32b-float.tiff", ColorType::Gray(32), 128.03194);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_gray_f64() {
|
||||||
|
test_image_sum_f64(
|
||||||
|
"gradient-1c-64b-float.tiff",
|
||||||
|
ColorType::Gray(64),
|
||||||
|
128.0319210877642,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_rgb_u8() {
|
fn test_rgb_u8() {
|
||||||
test_image_sum_u8("rgb-3c-8b.tiff", ColorType::RGB(8), 7842108);
|
test_image_sum_u8("rgb-3c-8b.tiff", ColorType::RGB(8), 7842108);
|
||||||
@@ -107,6 +132,31 @@ fn test_rgb_u64() {
|
|||||||
test_image_sum_u64("gradient-3c-64b.tiff", ColorType::RGB(64), 2030834111716);
|
test_image_sum_u64("gradient-3c-64b.tiff", ColorType::RGB(64), 2030834111716);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_rgb_f32() {
|
||||||
|
test_image_sum_f32("gradient-3c-32b-float.tiff", ColorType::RGB(32), 472.8405);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_int8() {
|
||||||
|
test_image_sum_i8("int8.tif", ColorType::Gray(8), 3111)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_int8_rgb() {
|
||||||
|
test_image_sum_i8("int8_rgb.tif", ColorType::RGB(8), -10344)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_int16() {
|
||||||
|
test_image_sum_i16("int16.tif", ColorType::Gray(16), 354396);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_int16_rgb() {
|
||||||
|
test_image_sum_i16("int16_rgb.tif", ColorType::RGB(16), 1063188);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_string_tags() {
|
fn test_string_tags() {
|
||||||
// these files have null-terminated strings for their Software tag. One has extra bytes after
|
// these files have null-terminated strings for their Software tag. One has extra bytes after
|
||||||
@@ -165,3 +215,72 @@ fn issue_69() {
|
|||||||
//let img_res = decoder.read_image();
|
//let img_res = decoder.read_image();
|
||||||
//assert!(img_res.is_ok());
|
//assert!(img_res.is_ok());
|
||||||
//}
|
//}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_tiled_rgb_u8() {
|
||||||
|
test_image_sum_u8("tiled-rgb-u8.tif", ColorType::RGB(8), 39528948);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_tiled_rect_rgb_u8() {
|
||||||
|
test_image_sum_u8("tiled-rect-rgb-u8.tif", ColorType::RGB(8), 62081032);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* #[test]
|
||||||
|
fn test_tiled_jpeg_rgb_u8() {
|
||||||
|
test_image_sum_u8("tiled-jpeg-rgb-u8.tif", ColorType::RGB(8), 93031606);
|
||||||
|
} */
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_tiled_oversize_gray_i8() {
|
||||||
|
test_image_sum_i8("tiled-oversize-gray-i8.tif", ColorType::Gray(8), 1214996);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_tiled_cmyk_i8() {
|
||||||
|
test_image_sum_i8("tiled-cmyk-i8.tif", ColorType::CMYK(8), 1759101);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_tiled_incremental() {
|
||||||
|
let file = "tiled-rgb-u8.tif";
|
||||||
|
let expected_type = ColorType::RGB(8);
|
||||||
|
let sums = [
|
||||||
|
188760, 195639, 108148, 81986, 665088, 366140, 705317, 423366, 172033, 324455, 244102,
|
||||||
|
81853, 181258, 247971, 129486, 55600, 565625, 422102, 730888, 379271, 232142, 292549,
|
||||||
|
244045, 86866, 188141, 115036, 150785, 84389, 353170, 459325, 719619, 329594, 278663,
|
||||||
|
220474, 243048, 113563, 189152, 109684, 179391, 122188, 279651, 622093, 724682, 302459,
|
||||||
|
268428, 204499, 224255, 124674, 170668, 121868, 192768, 183367, 378029, 585651, 657712,
|
||||||
|
296790, 241444, 197083, 198429, 134869, 182318, 86034, 203655, 182338, 297255, 601284,
|
||||||
|
633813, 242531, 228578, 206441, 193552, 125412, 181527, 165439, 202531, 159538, 268388,
|
||||||
|
565790, 611382, 272967, 236497, 215154, 158881, 90806, 106114, 182342, 191824, 186138,
|
||||||
|
215174, 393193, 701228, 198866, 227944, 193830, 166330, 49008, 55719, 122820, 197316,
|
||||||
|
161969, 203152, 170986, 624427, 188605, 186187, 111064, 115192, 39538, 48626, 163929,
|
||||||
|
144682, 135796, 194141, 154198, 584125, 180255, 153524, 121433, 132641, 35743, 47798,
|
||||||
|
152343, 162874, 167664, 160175, 133038, 659882, 138339, 166470, 124173, 118929, 51317,
|
||||||
|
45267, 155776, 161331, 161006, 130052, 137618, 337291, 106481, 161999, 127343, 87724,
|
||||||
|
59540, 63907, 155677, 140668, 141523, 108061, 168657, 186482, 98599, 147614, 139963, 90444,
|
||||||
|
56602, 92547, 125644, 134212, 126569, 144153, 179800, 174516, 133969, 129399, 117681,
|
||||||
|
83305, 55075, 110737, 115108, 128572, 128911, 130922, 179986, 143288, 145884, 155856,
|
||||||
|
96683, 94057, 56238, 79649, 71651, 70182, 75010, 77009, 98855, 78979, 74341, 83482, 53403,
|
||||||
|
59842, 30305,
|
||||||
|
];
|
||||||
|
|
||||||
|
let path = PathBuf::from(TEST_IMAGE_DIR).join(file);
|
||||||
|
let img_file = File::open(path).expect("Cannot find test image!");
|
||||||
|
let mut decoder = Decoder::new(img_file).expect("Cannot create decoder");
|
||||||
|
assert_eq!(decoder.colortype().unwrap(), expected_type);
|
||||||
|
|
||||||
|
let tiles = decoder.tile_count().unwrap();
|
||||||
|
assert_eq!(tiles as usize, sums.len());
|
||||||
|
|
||||||
|
for tile in 0..tiles {
|
||||||
|
match decoder.read_tile().unwrap() {
|
||||||
|
DecodingResult::U8(res) => {
|
||||||
|
let sum: u64 = res.into_iter().map(<u64>::from).sum();
|
||||||
|
assert_eq!(sum, sums[tile as usize]);
|
||||||
|
}
|
||||||
|
_ => panic!("Wrong bit depth"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+303
-48
@@ -1,9 +1,9 @@
|
|||||||
extern crate tiff;
|
extern crate tiff;
|
||||||
|
|
||||||
use tiff::decoder::{Decoder, DecodingResult, ifd};
|
use tiff::decoder::{ifd, Decoder, DecodingResult};
|
||||||
use tiff::encoder::{colortype, TiffEncoder, SRational};
|
use tiff::encoder::{colortype, Ifd, Ifd8, SRational, TiffEncoder};
|
||||||
use tiff::ColorType;
|
|
||||||
use tiff::tags::Tag;
|
use tiff::tags::Tag;
|
||||||
|
use tiff::ColorType;
|
||||||
|
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::{Cursor, Seek, SeekFrom};
|
use std::io::{Cursor, Seek, SeekFrom};
|
||||||
@@ -24,18 +24,22 @@ fn encode_decode() {
|
|||||||
{
|
{
|
||||||
let mut tiff = TiffEncoder::new(&mut file).unwrap();
|
let mut tiff = TiffEncoder::new(&mut file).unwrap();
|
||||||
|
|
||||||
let mut image = tiff.new_image::<colortype::RGB8>(100, 100)
|
let mut image = tiff.new_image::<colortype::RGB8>(100, 100).unwrap();
|
||||||
|
image
|
||||||
|
.encoder()
|
||||||
|
.write_tag(Tag::Artist, "Image-tiff")
|
||||||
.unwrap();
|
.unwrap();
|
||||||
image.encoder().write_tag(Tag::Artist, "Image-tiff").unwrap();
|
|
||||||
image.write_data(&image_data).unwrap();
|
image.write_data(&image_data).unwrap();
|
||||||
|
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
file.seek(SeekFrom::Start(0)).unwrap();
|
file.seek(SeekFrom::Start(0)).unwrap();
|
||||||
let mut decoder = Decoder::new(&mut file).unwrap();
|
let mut decoder = Decoder::new(&mut file).unwrap();
|
||||||
assert_eq!(decoder.colortype().unwrap(), ColorType::RGB(8));
|
assert_eq!(decoder.colortype().unwrap(), ColorType::RGB(8));
|
||||||
assert_eq!(decoder.dimensions().unwrap(), (100, 100));
|
assert_eq!(decoder.dimensions().unwrap(), (100, 100));
|
||||||
assert_eq!(decoder.get_tag(Tag::Artist).unwrap(), ifd::Value::Ascii("Image-tiff".into()));
|
assert_eq!(
|
||||||
|
decoder.get_tag(Tag::Artist).unwrap(),
|
||||||
|
ifd::Value::Ascii("Image-tiff".into())
|
||||||
|
);
|
||||||
if let DecodingResult::U8(img_res) = decoder.read_image().unwrap() {
|
if let DecodingResult::U8(img_res) = decoder.read_image().unwrap() {
|
||||||
assert_eq!(image_data, img_res);
|
assert_eq!(image_data, img_res);
|
||||||
} else {
|
} else {
|
||||||
@@ -44,6 +48,92 @@ fn encode_decode() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn encode_decode_big() {
|
||||||
|
let mut image_data = Vec::new();
|
||||||
|
for x in 0..100 {
|
||||||
|
for y in 0..100u8 {
|
||||||
|
let val = x + y;
|
||||||
|
image_data.push(val);
|
||||||
|
image_data.push(val);
|
||||||
|
image_data.push(val);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let mut file = Cursor::new(Vec::new());
|
||||||
|
{
|
||||||
|
let mut tiff = TiffEncoder::new_big(&mut file).unwrap();
|
||||||
|
|
||||||
|
let mut image = tiff.new_image::<colortype::RGB8>(100, 100).unwrap();
|
||||||
|
image
|
||||||
|
.encoder()
|
||||||
|
.write_tag(Tag::Artist, "Image-tiff")
|
||||||
|
.unwrap();
|
||||||
|
image.write_data(&image_data).unwrap();
|
||||||
|
}
|
||||||
|
{
|
||||||
|
file.seek(SeekFrom::Start(0)).unwrap();
|
||||||
|
let mut decoder = Decoder::new(&mut file).unwrap();
|
||||||
|
assert_eq!(decoder.colortype().unwrap(), ColorType::RGB(8));
|
||||||
|
assert_eq!(decoder.dimensions().unwrap(), (100, 100));
|
||||||
|
assert_eq!(
|
||||||
|
decoder.get_tag(Tag::Artist).unwrap(),
|
||||||
|
ifd::Value::Ascii("Image-tiff".into())
|
||||||
|
);
|
||||||
|
if let DecodingResult::U8(img_res) = decoder.read_image().unwrap() {
|
||||||
|
assert_eq!(image_data, img_res);
|
||||||
|
} else {
|
||||||
|
panic!("Wrong data type");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_encode_ifd() {
|
||||||
|
let mut data = Cursor::new(Vec::new());
|
||||||
|
|
||||||
|
{
|
||||||
|
let mut tiff = TiffEncoder::new(&mut data).unwrap();
|
||||||
|
let mut image_encoder = tiff.new_image::<colortype::Gray8>(1, 1).unwrap();
|
||||||
|
let encoder = image_encoder.encoder();
|
||||||
|
|
||||||
|
// Use the "reusable" tags section as per the TIFF6 spec
|
||||||
|
encoder.write_tag(Tag::Unknown(65000), Ifd(42u32)).unwrap();
|
||||||
|
encoder
|
||||||
|
.write_tag(Tag::Unknown(65001), &[Ifd(100u32)][..])
|
||||||
|
.unwrap();
|
||||||
|
encoder
|
||||||
|
.write_tag(Tag::Unknown(65002), &[Ifd(1u32), Ifd(2u32), Ifd(3u32)][..])
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
encoder.write_tag(Tag::Unknown(65010), Ifd8(43u64)).unwrap();
|
||||||
|
encoder
|
||||||
|
.write_tag(Tag::Unknown(65011), &[Ifd8(100u64)][..])
|
||||||
|
.unwrap();
|
||||||
|
encoder
|
||||||
|
.write_tag(
|
||||||
|
Tag::Unknown(65012),
|
||||||
|
&[Ifd8(1u64), Ifd8(2u64), Ifd8(3u64)][..],
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Rewind the cursor for reading
|
||||||
|
data.set_position(0);
|
||||||
|
{
|
||||||
|
let mut decoder = Decoder::new(&mut data).unwrap();
|
||||||
|
|
||||||
|
assert_eq!(decoder.assert_tag_u32(65000), 42);
|
||||||
|
assert_eq!(decoder.assert_tag_u32_vec(65000), [42]);
|
||||||
|
assert_eq!(decoder.assert_tag_u32_vec(65001), [100]);
|
||||||
|
assert_eq!(decoder.assert_tag_u32_vec(65002), [1, 2, 3]);
|
||||||
|
|
||||||
|
assert_eq!(decoder.assert_tag_u64(65010), 43);
|
||||||
|
assert_eq!(decoder.assert_tag_u64_vec(65010), [43]);
|
||||||
|
assert_eq!(decoder.assert_tag_u64_vec(65011), [100]);
|
||||||
|
assert_eq!(decoder.assert_tag_u64_vec(65012), [1, 2, 3]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
/// Test that attempting to encode when the input buffer is undersized returns
|
/// Test that attempting to encode when the input buffer is undersized returns
|
||||||
/// an error rather than panicking.
|
/// an error rather than panicking.
|
||||||
@@ -62,7 +152,10 @@ const TEST_IMAGE_DIR: &str = "./tests/images/";
|
|||||||
|
|
||||||
macro_rules! test_roundtrip {
|
macro_rules! test_roundtrip {
|
||||||
($name:ident, $buffer:ident, $buffer_ty:ty) => {
|
($name:ident, $buffer:ident, $buffer_ty:ty) => {
|
||||||
fn $name<C: colortype::ColorType<Inner=$buffer_ty>>(file: &str, expected_type: ColorType) {
|
fn $name<C: colortype::ColorType<Inner = $buffer_ty>>(
|
||||||
|
file: &str,
|
||||||
|
expected_type: ColorType,
|
||||||
|
) {
|
||||||
let path = PathBuf::from(TEST_IMAGE_DIR).join(file);
|
let path = PathBuf::from(TEST_IMAGE_DIR).join(file);
|
||||||
let img_file = File::open(path).expect("Cannot find test image!");
|
let img_file = File::open(path).expect("Cannot find test image!");
|
||||||
let mut decoder = Decoder::new(img_file).expect("Cannot create decoder");
|
let mut decoder = Decoder::new(img_file).expect("Cannot create decoder");
|
||||||
@@ -78,8 +171,7 @@ macro_rules! test_roundtrip {
|
|||||||
let mut tiff = TiffEncoder::new(&mut file).unwrap();
|
let mut tiff = TiffEncoder::new(&mut file).unwrap();
|
||||||
|
|
||||||
let (width, height) = decoder.dimensions().unwrap();
|
let (width, height) = decoder.dimensions().unwrap();
|
||||||
tiff.write_image::<C>(width, height, &image_data)
|
tiff.write_image::<C>(width, height, &image_data).unwrap();
|
||||||
.unwrap();
|
|
||||||
}
|
}
|
||||||
file.seek(SeekFrom::Start(0)).unwrap();
|
file.seek(SeekFrom::Start(0)).unwrap();
|
||||||
{
|
{
|
||||||
@@ -98,6 +190,8 @@ test_roundtrip!(test_u8_roundtrip, U8, u8);
|
|||||||
test_roundtrip!(test_u16_roundtrip, U16, u16);
|
test_roundtrip!(test_u16_roundtrip, U16, u16);
|
||||||
test_roundtrip!(test_u32_roundtrip, U32, u32);
|
test_roundtrip!(test_u32_roundtrip, U32, u32);
|
||||||
test_roundtrip!(test_u64_roundtrip, U64, u64);
|
test_roundtrip!(test_u64_roundtrip, U64, u64);
|
||||||
|
test_roundtrip!(test_f32_roundtrip, F32, f32);
|
||||||
|
test_roundtrip!(test_f64_roundtrip, F64, f64);
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_gray_u8_roundtrip() {
|
fn test_gray_u8_roundtrip() {
|
||||||
@@ -149,6 +243,76 @@ fn test_rgb_u64_roundtrip() {
|
|||||||
test_u64_roundtrip::<colortype::RGB64>("gradient-3c-64b.tiff", ColorType::RGB(64));
|
test_u64_roundtrip::<colortype::RGB64>("gradient-3c-64b.tiff", ColorType::RGB(64));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_gray_f32_roundtrip() {
|
||||||
|
test_f32_roundtrip::<colortype::Gray32Float>("gradient-1c-32b-float.tiff", ColorType::Gray(32));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_rgb_f32_roundtrip() {
|
||||||
|
test_f32_roundtrip::<colortype::RGB32Float>("gradient-3c-32b-float.tiff", ColorType::RGB(32));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_cmyk_f32_roundtrip() {
|
||||||
|
test_f32_roundtrip::<colortype::CMYK32Float>("cmyk-3c-32b-float.tiff", ColorType::CMYK(32));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_gray_f64_roundtrip() {
|
||||||
|
test_f64_roundtrip::<colortype::Gray64Float>("gradient-1c-64b-float.tiff", ColorType::Gray(64));
|
||||||
|
}
|
||||||
|
|
||||||
|
trait AssertDecode {
|
||||||
|
fn assert_tag_u32(&mut self, tag: u16) -> u32;
|
||||||
|
fn assert_tag_u32_vec(&mut self, tag: u16) -> Vec<u32>;
|
||||||
|
fn assert_tag_i32(&mut self, tag: u16) -> i32;
|
||||||
|
fn assert_tag_i32_vec(&mut self, tag: u16) -> Vec<i32>;
|
||||||
|
fn assert_tag_u64(&mut self, tag: u16) -> u64;
|
||||||
|
fn assert_tag_u64_vec(&mut self, tag: u16) -> Vec<u64>;
|
||||||
|
fn assert_tag_i64(&mut self, tag: u16) -> i64;
|
||||||
|
fn assert_tag_i64_vec(&mut self, tag: u16) -> Vec<i64>;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<R: std::io::Read + std::io::Seek> AssertDecode for Decoder<R> {
|
||||||
|
fn assert_tag_u32(&mut self, tag: u16) -> u32 {
|
||||||
|
self.get_tag(Tag::Unknown(tag)).unwrap().into_u32().unwrap()
|
||||||
|
}
|
||||||
|
fn assert_tag_u32_vec(&mut self, tag: u16) -> Vec<u32> {
|
||||||
|
self.get_tag(Tag::Unknown(tag))
|
||||||
|
.unwrap()
|
||||||
|
.into_u32_vec()
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
|
fn assert_tag_i32(&mut self, tag: u16) -> i32 {
|
||||||
|
self.get_tag(Tag::Unknown(tag)).unwrap().into_i32().unwrap()
|
||||||
|
}
|
||||||
|
fn assert_tag_i32_vec(&mut self, tag: u16) -> Vec<i32> {
|
||||||
|
self.get_tag(Tag::Unknown(tag))
|
||||||
|
.unwrap()
|
||||||
|
.into_i32_vec()
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
|
fn assert_tag_u64(&mut self, tag: u16) -> u64 {
|
||||||
|
self.get_tag(Tag::Unknown(tag)).unwrap().into_u64().unwrap()
|
||||||
|
}
|
||||||
|
fn assert_tag_u64_vec(&mut self, tag: u16) -> Vec<u64> {
|
||||||
|
self.get_tag(Tag::Unknown(tag))
|
||||||
|
.unwrap()
|
||||||
|
.into_u64_vec()
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
|
fn assert_tag_i64(&mut self, tag: u16) -> i64 {
|
||||||
|
self.get_tag(Tag::Unknown(tag)).unwrap().into_i64().unwrap()
|
||||||
|
}
|
||||||
|
fn assert_tag_i64_vec(&mut self, tag: u16) -> Vec<i64> {
|
||||||
|
self.get_tag(Tag::Unknown(tag))
|
||||||
|
.unwrap()
|
||||||
|
.into_i64_vec()
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_multiple_byte() {
|
fn test_multiple_byte() {
|
||||||
let mut data = Cursor::new(Vec::new());
|
let mut data = Cursor::new(Vec::new());
|
||||||
@@ -159,21 +323,29 @@ fn test_multiple_byte() {
|
|||||||
let encoder = image_encoder.encoder();
|
let encoder = image_encoder.encoder();
|
||||||
|
|
||||||
encoder.write_tag(Tag::Unknown(65000), &[1_u8][..]).unwrap();
|
encoder.write_tag(Tag::Unknown(65000), &[1_u8][..]).unwrap();
|
||||||
encoder.write_tag(Tag::Unknown(65001), &[1_u8, 2][..]).unwrap();
|
encoder
|
||||||
encoder.write_tag(Tag::Unknown(65002), &[1_u8, 2, 3][..]).unwrap();
|
.write_tag(Tag::Unknown(65001), &[1_u8, 2][..])
|
||||||
encoder.write_tag(Tag::Unknown(65003), &[1_u8, 2, 3, 4][..]).unwrap();
|
.unwrap();
|
||||||
encoder.write_tag(Tag::Unknown(65004), &[1_u8, 2, 3, 4, 5][..]).unwrap();
|
encoder
|
||||||
|
.write_tag(Tag::Unknown(65002), &[1_u8, 2, 3][..])
|
||||||
|
.unwrap();
|
||||||
|
encoder
|
||||||
|
.write_tag(Tag::Unknown(65003), &[1_u8, 2, 3, 4][..])
|
||||||
|
.unwrap();
|
||||||
|
encoder
|
||||||
|
.write_tag(Tag::Unknown(65004), &[1_u8, 2, 3, 4, 5][..])
|
||||||
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
data.set_position(0);
|
data.set_position(0);
|
||||||
{
|
{
|
||||||
let mut decoder = Decoder::new(&mut data).unwrap();
|
let mut decoder = Decoder::new(&mut data).unwrap();
|
||||||
|
|
||||||
assert_eq!(decoder.get_tag(Tag::Unknown(65000)).unwrap().into_u32_vec().unwrap(), [1]);
|
assert_eq!(decoder.assert_tag_u32_vec(65000), [1]);
|
||||||
assert_eq!(decoder.get_tag(Tag::Unknown(65001)).unwrap().into_u32_vec().unwrap(), [1, 2]);
|
assert_eq!(decoder.assert_tag_u32_vec(65001), [1, 2]);
|
||||||
assert_eq!(decoder.get_tag(Tag::Unknown(65002)).unwrap().into_u32_vec().unwrap(), [1, 2, 3]);
|
assert_eq!(decoder.assert_tag_u32_vec(65002), [1, 2, 3]);
|
||||||
assert_eq!(decoder.get_tag(Tag::Unknown(65003)).unwrap().into_u32_vec().unwrap(), [1, 2, 3, 4]);
|
assert_eq!(decoder.assert_tag_u32_vec(65003), [1, 2, 3, 4]);
|
||||||
assert_eq!(decoder.get_tag(Tag::Unknown(65004)).unwrap().into_u32_vec().unwrap(), [1, 2, 3, 4, 5]);
|
assert_eq!(decoder.assert_tag_u32_vec(65004), [1, 2, 3, 4, 5]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -181,6 +353,9 @@ fn test_multiple_byte() {
|
|||||||
/// Test writing signed tags from TIFF 6.0
|
/// Test writing signed tags from TIFF 6.0
|
||||||
fn test_signed() {
|
fn test_signed() {
|
||||||
let mut data = Cursor::new(Vec::new());
|
let mut data = Cursor::new(Vec::new());
|
||||||
|
fn make_srational(i: i32) -> SRational {
|
||||||
|
SRational { n: i, d: 100 }
|
||||||
|
}
|
||||||
|
|
||||||
{
|
{
|
||||||
let mut tiff = TiffEncoder::new(&mut data).unwrap();
|
let mut tiff = TiffEncoder::new(&mut data).unwrap();
|
||||||
@@ -189,23 +364,56 @@ fn test_signed() {
|
|||||||
|
|
||||||
//Use the "reusable" tags section as per the TIFF6 spec
|
//Use the "reusable" tags section as per the TIFF6 spec
|
||||||
encoder.write_tag(Tag::Unknown(65000), -1_i8).unwrap();
|
encoder.write_tag(Tag::Unknown(65000), -1_i8).unwrap();
|
||||||
encoder.write_tag(Tag::Unknown(65001), &[-1_i8][..]).unwrap();
|
encoder
|
||||||
encoder.write_tag(Tag::Unknown(65002), &[-1_i8, 2][..]).unwrap();
|
.write_tag(Tag::Unknown(65001), &[-1_i8][..])
|
||||||
encoder.write_tag(Tag::Unknown(65003), &[-1_i8, 2, -3][..]).unwrap();
|
.unwrap();
|
||||||
encoder.write_tag(Tag::Unknown(65004), &[-1_i8, 2, -3, 4][..]).unwrap();
|
encoder
|
||||||
encoder.write_tag(Tag::Unknown(65005), &[-1_i8, 2, -3, 4, -5][..]).unwrap();
|
.write_tag(Tag::Unknown(65002), &[-1_i8, 2][..])
|
||||||
|
.unwrap();
|
||||||
|
encoder
|
||||||
|
.write_tag(Tag::Unknown(65003), &[-1_i8, 2, -3][..])
|
||||||
|
.unwrap();
|
||||||
|
encoder
|
||||||
|
.write_tag(Tag::Unknown(65004), &[-1_i8, 2, -3, 4][..])
|
||||||
|
.unwrap();
|
||||||
|
encoder
|
||||||
|
.write_tag(Tag::Unknown(65005), &[-1_i8, 2, -3, 4, -5][..])
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
encoder.write_tag(Tag::Unknown(65010), -1_i16).unwrap();
|
encoder.write_tag(Tag::Unknown(65010), -1_i16).unwrap();
|
||||||
encoder.write_tag(Tag::Unknown(65011), -1_i16).unwrap();
|
encoder.write_tag(Tag::Unknown(65011), -1_i16).unwrap();
|
||||||
encoder.write_tag(Tag::Unknown(65012), &[-1_i16, 2][..]).unwrap();
|
encoder
|
||||||
encoder.write_tag(Tag::Unknown(65013), &[-1_i16, 2, -3][..]).unwrap();
|
.write_tag(Tag::Unknown(65012), &[-1_i16, 2][..])
|
||||||
|
.unwrap();
|
||||||
|
encoder
|
||||||
|
.write_tag(Tag::Unknown(65013), &[-1_i16, 2, -3][..])
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
encoder.write_tag(Tag::Unknown(65020), -1_i32).unwrap();
|
encoder.write_tag(Tag::Unknown(65020), -1_i32).unwrap();
|
||||||
encoder.write_tag(Tag::Unknown(65021), &[-1_i32][..]).unwrap();
|
encoder
|
||||||
encoder.write_tag(Tag::Unknown(65022), &[-1_i32, 2][..]).unwrap();
|
.write_tag(Tag::Unknown(65021), &[-1_i32][..])
|
||||||
|
.unwrap();
|
||||||
|
encoder
|
||||||
|
.write_tag(Tag::Unknown(65022), &[-1_i32, 2][..])
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
encoder.write_tag(Tag::Unknown(65030), SRational { n: -1, d: 100 }).unwrap();
|
encoder.write_tag(Tag::Unknown(65030), -1_i64).unwrap();
|
||||||
encoder.write_tag(Tag::Unknown(65031), &[SRational { n: -1, d: 100 }, SRational { n: 2, d: 100 }][..]).unwrap();
|
encoder
|
||||||
|
.write_tag(Tag::Unknown(65031), &[-1_i64][..])
|
||||||
|
.unwrap();
|
||||||
|
encoder
|
||||||
|
.write_tag(Tag::Unknown(65032), &[-1_i64, 2][..])
|
||||||
|
.unwrap();
|
||||||
|
|
||||||
|
encoder
|
||||||
|
.write_tag(Tag::Unknown(65040), make_srational(-1))
|
||||||
|
.unwrap();
|
||||||
|
encoder
|
||||||
|
.write_tag(
|
||||||
|
Tag::Unknown(65041),
|
||||||
|
&[make_srational(-1), make_srational(2)][..],
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
//Rewind the cursor for reading
|
//Rewind the cursor for reading
|
||||||
@@ -213,24 +421,28 @@ fn test_signed() {
|
|||||||
{
|
{
|
||||||
let mut decoder = Decoder::new(&mut data).unwrap();
|
let mut decoder = Decoder::new(&mut data).unwrap();
|
||||||
|
|
||||||
assert_eq!(decoder.get_tag(Tag::Unknown(65000)).unwrap().into_i32().unwrap(), -1, );
|
assert_eq!(decoder.assert_tag_i32(65000), -1);
|
||||||
assert_eq!(decoder.get_tag(Tag::Unknown(65001)).unwrap().into_i32_vec().unwrap(), [-1]);
|
assert_eq!(decoder.assert_tag_i32_vec(65001), [-1]);
|
||||||
assert_eq!(decoder.get_tag(Tag::Unknown(65002)).unwrap().into_i32_vec().unwrap(), [-1, 2]);
|
assert_eq!(decoder.assert_tag_i32_vec(65002), [-1, 2]);
|
||||||
assert_eq!(decoder.get_tag(Tag::Unknown(65003)).unwrap().into_i32_vec().unwrap(), [-1, 2, -3]);
|
assert_eq!(decoder.assert_tag_i32_vec(65003), [-1, 2, -3]);
|
||||||
assert_eq!(decoder.get_tag(Tag::Unknown(65004)).unwrap().into_i32_vec().unwrap(), [-1, 2, -3, 4]);
|
assert_eq!(decoder.assert_tag_i32_vec(65004), [-1, 2, -3, 4]);
|
||||||
assert_eq!(decoder.get_tag(Tag::Unknown(65005)).unwrap().into_i32_vec().unwrap(), [-1, 2, -3, 4, -5], );
|
assert_eq!(decoder.assert_tag_i32_vec(65005), [-1, 2, -3, 4, -5],);
|
||||||
|
|
||||||
assert_eq!(decoder.get_tag(Tag::Unknown(65010)).unwrap().into_i32().unwrap(), -1);
|
assert_eq!(decoder.assert_tag_i32(65010), -1);
|
||||||
assert_eq!(decoder.get_tag(Tag::Unknown(65011)).unwrap().into_i32_vec().unwrap(), [-1]);
|
assert_eq!(decoder.assert_tag_i32_vec(65011), [-1]);
|
||||||
assert_eq!(decoder.get_tag(Tag::Unknown(65012)).unwrap().into_i32_vec().unwrap(), [-1, 2]);
|
assert_eq!(decoder.assert_tag_i32_vec(65012), [-1, 2]);
|
||||||
assert_eq!(decoder.get_tag(Tag::Unknown(65013)).unwrap().into_i32_vec().unwrap(), [-1, 2, -3]);
|
assert_eq!(decoder.assert_tag_i32_vec(65013), [-1, 2, -3]);
|
||||||
|
|
||||||
assert_eq!(decoder.get_tag(Tag::Unknown(65020)).unwrap().into_i32().unwrap(), -1);
|
assert_eq!(decoder.assert_tag_i32(65020), -1);
|
||||||
assert_eq!(decoder.get_tag(Tag::Unknown(65021)).unwrap().into_i32_vec().unwrap(), [-1]);
|
assert_eq!(decoder.assert_tag_i32_vec(65021), [-1]);
|
||||||
assert_eq!(decoder.get_tag(Tag::Unknown(65022)).unwrap().into_i32_vec().unwrap(), [-1, 2]);
|
assert_eq!(decoder.assert_tag_i32_vec(65022), [-1, 2]);
|
||||||
|
|
||||||
assert_eq!(decoder.get_tag(Tag::Unknown(65030)).unwrap().into_i32_vec().unwrap(), [-1, 100]);
|
assert_eq!(decoder.assert_tag_i64(65030), -1);
|
||||||
assert_eq!(decoder.get_tag(Tag::Unknown(65031)).unwrap().into_i32_vec().unwrap(), [-1_i32, 100, 2, 100]);
|
assert_eq!(decoder.assert_tag_i64_vec(65031), [-1]);
|
||||||
|
assert_eq!(decoder.assert_tag_i64_vec(65032), [-1, 2]);
|
||||||
|
|
||||||
|
assert_eq!(decoder.assert_tag_i32_vec(65040), [-1, 100]);
|
||||||
|
assert_eq!(decoder.assert_tag_i32_vec(65041), [-1_i32, 100, 2, 100]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -245,10 +457,14 @@ fn test_multipage_image() {
|
|||||||
|
|
||||||
// write first grayscale image (2x2 16-bit)
|
// write first grayscale image (2x2 16-bit)
|
||||||
let img1: Vec<u16> = [1, 2, 3, 4].to_vec();
|
let img1: Vec<u16> = [1, 2, 3, 4].to_vec();
|
||||||
img_encoder.write_image::<colortype::Gray16>(2, 2, &img1[..]).unwrap();
|
img_encoder
|
||||||
|
.write_image::<colortype::Gray16>(2, 2, &img1[..])
|
||||||
|
.unwrap();
|
||||||
// write second grayscale image (3x3 8-bit)
|
// write second grayscale image (3x3 8-bit)
|
||||||
let img2: Vec<u8> = [9, 8, 7, 6, 5, 4, 3, 2, 1].to_vec();
|
let img2: Vec<u8> = [9, 8, 7, 6, 5, 4, 3, 2, 1].to_vec();
|
||||||
img_encoder.write_image::<colortype::Gray8>(3, 3, &img2[..]).unwrap();
|
img_encoder
|
||||||
|
.write_image::<colortype::Gray8>(3, 3, &img2[..])
|
||||||
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
// seek to the beginning of the file, so that it can be decoded
|
// seek to the beginning of the file, so that it can be decoded
|
||||||
@@ -263,4 +479,43 @@ fn test_multipage_image() {
|
|||||||
// check the dimensions of the image in the second page
|
// check the dimensions of the image in the second page
|
||||||
assert_eq!(img_decoder.dimensions().unwrap(), (3, 3));
|
assert_eq!(img_decoder.dimensions().unwrap(), (3, 3));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
/// verify rows per strip setting
|
||||||
|
fn test_rows_per_strip() {
|
||||||
|
let mut file = Cursor::new(Vec::new());
|
||||||
|
{
|
||||||
|
let mut img_encoder = TiffEncoder::new(&mut file).unwrap();
|
||||||
|
|
||||||
|
let mut image = img_encoder.new_image::<colortype::Gray8>(100, 100).unwrap();
|
||||||
|
assert_eq!(image.next_strip_sample_count(), 100 * 100);
|
||||||
|
image.rows_per_strip(2).unwrap();
|
||||||
|
assert_eq!(image.next_strip_sample_count(), 2 * 100);
|
||||||
|
|
||||||
|
let img2: Vec<u8> = vec![0; 2 * 100];
|
||||||
|
image.write_strip(&img2[..]).unwrap();
|
||||||
|
assert!(image.rows_per_strip(5).is_err());
|
||||||
|
for i in 1..50 {
|
||||||
|
let img2: Vec<u8> = vec![i; 2 * 100];
|
||||||
|
image.write_strip(&img2[..]).unwrap();
|
||||||
|
}
|
||||||
|
assert!(image.write_strip(&img2[..]).is_err());
|
||||||
|
image.finish().unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
|
file.seek(SeekFrom::Start(0)).unwrap();
|
||||||
|
{
|
||||||
|
let mut decoder = Decoder::new(&mut file).unwrap();
|
||||||
|
assert_eq!(decoder.get_tag_u64(Tag::RowsPerStrip).unwrap(), 2);
|
||||||
|
assert_eq!(decoder.strip_count().unwrap(), 50);
|
||||||
|
|
||||||
|
for i in 0..50 {
|
||||||
|
let img2 = [i; 2 * 100];
|
||||||
|
match decoder.read_strip().unwrap() {
|
||||||
|
DecodingResult::U8(data) => assert_eq!(&img2[..], &data[..]),
|
||||||
|
other => panic!("Incorrect strip type {:?}", other),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Executable
BIN
Binary file not shown.
Executable
BIN
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Reference in new issue
Block a user