1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
//! The TIFF encoder.
use gamut_core::{Dimensions, Encoder, Error, Result};
use crate::compression::{Compression, ccitt, lzw, packbits, predictor};
use crate::ifd::{PhotometricInterpretation, Predictor};
use crate::{tags, writer};
use gamut_ifd::{ByteOrder, Ifd, Value, Variant};
/// The on-disk sample layout of an image, shared by the 8-bit and bilevel encode paths.
struct SampleLayout {
spp: usize,
bits_per_sample: u16,
stored_row_bytes: usize,
photometric: PhotometricInterpretation,
}
/// Encoder for baseline TIFF images.
///
/// Writes chunky (`PlanarConfiguration = 1`) strips, optionally PackBits-compressed
/// ([`Self::with_compression`]). Supports 8-bit grayscale/RGB and 1-bit bilevel; richer colour
/// modes and compression schemes are added in later phases. Emits classic TIFF by default, or
/// BigTIFF (64-bit offsets) when [`Self::with_big_tiff`] is set.
#[derive(Debug, Clone)]
pub struct TiffEncoder {
order: ByteOrder,
compression: Compression,
predictor: Predictor,
tiling: Option<(u32, u32)>,
big_tiff: bool,
}
impl Default for TiffEncoder {
fn default() -> Self {
Self {
order: ByteOrder::LittleEndian,
compression: Compression::None,
predictor: Predictor::None,
tiling: None,
big_tiff: false,
}
}
}
impl TiffEncoder {
/// Creates an encoder that writes little-endian (`II`) TIFF.
#[must_use]
pub fn new() -> Self {
Self::default()
}
/// Returns a copy of this encoder that writes in the given byte order.
#[must_use]
pub fn with_byte_order(mut self, order: ByteOrder) -> Self {
self.order = order;
self
}
/// Returns a copy of this encoder that compresses image data with `compression`.
#[must_use]
pub fn with_compression(mut self, compression: Compression) -> Self {
self.compression = compression;
self
}
/// Returns a copy of this encoder that applies `predictor` before compression.
///
/// [`Predictor::HorizontalDifferencing`] requires 8-bit samples and pairs well with LZW.
#[must_use]
pub fn with_predictor(mut self, predictor: Predictor) -> Self {
self.predictor = predictor;
self
}
/// Returns a copy of this encoder that writes the image as tiles of `tile_width × tile_height`
/// pixels instead of strips.
///
/// Both dimensions must be positive multiples of 16. Tiling is currently supported for 8-bit
/// images compressed with `None`/PackBits/LZW (no predictor).
#[must_use]
pub fn with_tiling(mut self, tile_width: u32, tile_height: u32) -> Self {
self.tiling = Some((tile_width, tile_height));
self
}
/// Returns a copy of this encoder that writes BigTIFF (magic `43`, 64-bit offsets) instead of
/// classic TIFF.
///
/// BigTIFF only widens the container's structural fields; every colour mode, compression
/// scheme, strip/tile layout, and multi-page feature applies unchanged, so this composes with
/// the other builders. Its 64-bit offsets let a file exceed the 4 GiB classic limit. A reader
/// detects the variant from the header magic, so no decoder flag is needed. Defaults to off.
#[must_use]
pub fn with_big_tiff(mut self, big_tiff: bool) -> Self {
self.big_tiff = big_tiff;
self
}
/// The container variant this encoder writes (BigTIFF when [`Self::with_big_tiff`] is set).
fn variant(&self) -> Variant {
if self.big_tiff {
Variant::Big
} else {
Variant::Classic
}
}
/// Encodes an 8-bit grayscale image: one sample per pixel, `BlackIsZero`.
///
/// `pixels` is `width * height` bytes, row-major. Returns the number of bytes written.
///
/// # Errors
///
/// Returns [`Error::InvalidInput`] if `pixels` does not match `dims` or `dims` is empty.
pub fn encode_gray8(
&self,
pixels: &[u8],
dims: Dimensions,
out: &mut Vec<u8>,
) -> Result<usize> {
self.encode_8bit(pixels, dims, 1, PhotometricInterpretation::BlackIsZero, out)
}
/// Encodes an 8-bit RGB image: three interleaved samples per pixel (`RGBRGB…`).
///
/// `pixels` is `width * height * 3` bytes, row-major. Returns the number of bytes written.
///
/// # Errors
///
/// Returns [`Error::InvalidInput`] if `pixels` does not match `dims` or `dims` is empty.
pub fn encode_rgb8(&self, pixels: &[u8], dims: Dimensions, out: &mut Vec<u8>) -> Result<usize> {
self.encode_8bit(pixels, dims, 3, PhotometricInterpretation::Rgb, out)
}
/// Encodes an 8-bit RGBA image: four interleaved samples per pixel (`RGBARGBA…`).
///
/// The fourth sample is stored as *unassociated* alpha (`ExtraSamples = 2`, not premultiplied).
/// `pixels` is `width * height * 4` bytes, row-major. Returns the number of bytes written.
///
/// # Errors
///
/// Returns [`Error::InvalidInput`] if `pixels` does not match `dims` or `dims` is empty.
pub fn encode_rgba8(
&self,
pixels: &[u8],
dims: Dimensions,
out: &mut Vec<u8>,
) -> Result<usize> {
let (w, h) = (dims.width as usize, dims.height as usize);
if w == 0 || h == 0 {
return Err(Error::InvalidInput("TIFF: zero-sized image"));
}
let expected = w
.checked_mul(h)
.and_then(|n| n.checked_mul(4))
.ok_or(Error::InvalidInput("TIFF: image too large"))?;
if pixels.len() != expected {
return Err(Error::InvalidInput(
"TIFF: pixel buffer length does not match dimensions",
));
}
self.encode_packed(
pixels,
dims,
&SampleLayout {
spp: 4,
bits_per_sample: 8,
stored_row_bytes: w * 4,
photometric: PhotometricInterpretation::Rgb,
},
&[(tags::EXTRA_SAMPLES, Value::Short(vec![2]))], // unassociated alpha
out,
)
}
/// Encodes an 8-bit CMYK image: four interleaved ink samples per pixel (`CMYKCMYK…`).
///
/// `PhotometricInterpretation = Separated` (5); each sample is ink dot coverage where 0 is 0 %
/// and 255 is 100 % (TIFF 6.0 §16). `pixels` is `width * height * 4` bytes, row-major. Returns
/// the number of bytes written.
///
/// # Errors
///
/// Returns [`Error::InvalidInput`] if `pixels` does not match `dims` or `dims` is empty.
pub fn encode_cmyk8(
&self,
pixels: &[u8],
dims: Dimensions,
out: &mut Vec<u8>,
) -> Result<usize> {
self.encode_8bit(pixels, dims, 4, PhotometricInterpretation::Cmyk, out)
}
/// Encodes a 1-bit bilevel image, stored as `BlackIsZero` (one bit per pixel, MSB-first).
///
/// `pixels` is `width * height` bytes, one per pixel: zero is black, any non-zero value is
/// white. Returns the number of bytes written.
///
/// # Errors
///
/// Returns [`Error::InvalidInput`] if `pixels` does not match `dims` or `dims` is empty.
pub fn encode_bilevel(
&self,
pixels: &[u8],
dims: Dimensions,
out: &mut Vec<u8>,
) -> Result<usize> {
let (w, h) = (dims.width as usize, dims.height as usize);
if w == 0 || h == 0 {
return Err(Error::InvalidInput("TIFF: zero-sized image"));
}
if pixels.len()
!= w.checked_mul(h)
.ok_or(Error::InvalidInput("TIFF: image too large"))?
{
return Err(Error::InvalidInput(
"TIFF: pixel buffer length does not match dimensions",
));
}
let stored_row_bytes = w.div_ceil(8);
let mut packed = vec![0u8; stored_row_bytes * h];
for y in 0..h {
let row = &pixels[y * w..(y + 1) * w];
let dst = &mut packed[y * stored_row_bytes..(y + 1) * stored_row_bytes];
for (x, &p) in row.iter().enumerate() {
if p != 0 {
dst[x / 8] |= 0x80 >> (x % 8);
}
}
}
self.encode_packed(
&packed,
dims,
&SampleLayout {
spp: 1,
bits_per_sample: 1,
stored_row_bytes,
photometric: PhotometricInterpretation::BlackIsZero,
},
&[],
out,
)
}
/// Encodes an 8-bit palette-colour image.
///
/// `indices` is `width * height` bytes (one palette index per pixel); `palette` is `256 * 3`
/// bytes of 8-bit RGB (entry `i` is `palette[3*i..3*i+3]`). Returns the number of bytes written.
///
/// # Errors
///
/// Returns [`Error::InvalidInput`] if `indices` does not match `dims`, `dims` is empty, or
/// `palette` is not exactly 256 RGB entries.
pub fn encode_palette8(
&self,
indices: &[u8],
palette: &[u8],
dims: Dimensions,
out: &mut Vec<u8>,
) -> Result<usize> {
let (w, h) = (dims.width as usize, dims.height as usize);
if w == 0 || h == 0 {
return Err(Error::InvalidInput("TIFF: zero-sized image"));
}
if indices.len()
!= w.checked_mul(h)
.ok_or(Error::InvalidInput("TIFF: image too large"))?
{
return Err(Error::InvalidInput(
"TIFF: index buffer length does not match dimensions",
));
}
if palette.len() != 256 * 3 {
return Err(Error::InvalidInput("TIFF: palette must be 256 RGB entries"));
}
// ColorMap: 3×256 16-bit values (all reds, then greens, then blues); 8-bit → 16-bit by ×257.
let mut colormap = vec![0u16; 3 * 256];
for i in 0..256 {
colormap[i] = u16::from(palette[3 * i]) * 257;
colormap[256 + i] = u16::from(palette[3 * i + 1]) * 257;
colormap[512 + i] = u16::from(palette[3 * i + 2]) * 257;
}
self.encode_packed(
indices,
dims,
&SampleLayout {
spp: 1,
bits_per_sample: 8,
stored_row_bytes: w,
photometric: PhotometricInterpretation::Palette,
},
&[(tags::COLOR_MAP, Value::Short(colormap))],
out,
)
}
fn encode_8bit(
&self,
pixels: &[u8],
dims: Dimensions,
spp: usize,
photometric: PhotometricInterpretation,
out: &mut Vec<u8>,
) -> Result<usize> {
let (w, h) = (dims.width as usize, dims.height as usize);
if w == 0 || h == 0 {
return Err(Error::InvalidInput("TIFF: zero-sized image"));
}
let row_bytes = w
.checked_mul(spp)
.ok_or(Error::InvalidInput("TIFF: image too large"))?;
let expected = row_bytes
.checked_mul(h)
.ok_or(Error::InvalidInput("TIFF: image too large"))?;
if pixels.len() != expected {
return Err(Error::InvalidInput(
"TIFF: pixel buffer length does not match dimensions",
));
}
self.encode_packed(
pixels,
dims,
&SampleLayout {
spp,
bits_per_sample: 8,
stored_row_bytes: row_bytes,
photometric,
},
&[],
out,
)
}
/// Lays out an image from already-packed sample bytes (`height * stored_row_bytes`), applying
/// the strip codec and building the directory.
fn encode_packed(
&self,
packed: &[u8],
dims: Dimensions,
layout: &SampleLayout,
extra_fields: &[(u16, Value)],
out: &mut Vec<u8>,
) -> Result<usize> {
if let Some((tw, tl)) = self.tiling {
return self.encode_tiled(packed, dims, layout, extra_fields, tw, tl, out);
}
let (ifd, strips) = self.build_strip_image(packed, dims, layout, extra_fields)?;
let bytes = writer::write_image(self.order, self.variant(), &ifd, &strips);
out.extend_from_slice(&bytes);
Ok(bytes.len())
}
/// Builds one strip image's directory (without `StripOffsets`/`StripByteCounts`) and its
/// compressed strips, applying the predictor and strip codec.
fn build_strip_image(
&self,
packed: &[u8],
dims: Dimensions,
layout: &SampleLayout,
extra_fields: &[(u16, Value)],
) -> Result<(Ifd, Vec<Vec<u8>>)> {
let h = dims.height as usize;
let stored_row_bytes = layout.stored_row_bytes;
// Apply the horizontal-differencing predictor (8-bit only) before compression.
let predicting = self.predictor == Predictor::HorizontalDifferencing;
if predicting && layout.bits_per_sample != 8 {
return Err(Error::Unsupported("TIFF: predictor requires 8-bit samples"));
}
let predicted = predicting.then(|| {
let mut buf = packed.to_vec();
predictor::forward(&mut buf, stored_row_bytes, layout.spp);
buf
});
let packed: &[u8] = predicted.as_deref().unwrap_or(packed);
// Partition rows into strips of roughly 8 KB (TIFF 6.0 §7), then apply the strip codec.
let rows_per_strip = (8192 / stored_row_bytes.max(1)).clamp(1, h);
let mut strips: Vec<Vec<u8>> = Vec::new();
let mut row = 0;
while row < h {
let rows = rows_per_strip.min(h - row);
let start = row * stored_row_bytes;
let raw = &packed[start..start + rows * stored_row_bytes];
strips.push(self.compress_strip(raw, dims, layout)?);
row += rows;
}
let mut ifd = Ifd::new();
ifd.set(tags::IMAGE_WIDTH, dim_value(dims.width));
ifd.set(tags::IMAGE_LENGTH, dim_value(dims.height));
ifd.set(
tags::BITS_PER_SAMPLE,
Value::Short(vec![layout.bits_per_sample; layout.spp]),
);
ifd.set(
tags::COMPRESSION,
Value::Short(vec![self.compression.code()]),
);
ifd.set(
tags::PHOTOMETRIC_INTERPRETATION,
Value::Short(vec![layout.photometric.code()]),
);
ifd.set(
tags::SAMPLES_PER_PIXEL,
Value::Short(vec![layout.spp as u16]),
);
ifd.set(tags::ROWS_PER_STRIP, dim_value(rows_per_strip as u32));
ifd.set(tags::X_RESOLUTION, Value::Rational(vec![(72, 1)]));
ifd.set(tags::Y_RESOLUTION, Value::Rational(vec![(72, 1)]));
ifd.set(tags::RESOLUTION_UNIT, Value::Short(vec![2])); // inch
if predicting {
ifd.set(tags::PREDICTOR, Value::Short(vec![2]));
}
for (tag, value) in extra_fields {
ifd.set(*tag, value.clone());
}
Ok((ifd, strips))
}
/// Encodes several 8-bit RGB images as the pages of one multi-page TIFF.
///
/// Each page is `(pixels, dims)` with `pixels` of length `width * height * 3`. Returns the
/// number of bytes written.
///
/// # Errors
///
/// Returns [`Error::InvalidInput`] if `pages` is empty or any page's buffer does not match its
/// dimensions.
pub fn encode_pages_rgb8(
&self,
pages: &[(&[u8], Dimensions)],
out: &mut Vec<u8>,
) -> Result<usize> {
if pages.is_empty() {
return Err(Error::InvalidInput("TIFF: no pages to encode"));
}
let total = pages.len() as u16;
let mut images: Vec<(Ifd, Vec<Vec<u8>>)> = Vec::with_capacity(pages.len());
for (i, &(pixels, dims)) in pages.iter().enumerate() {
let (w, h) = (dims.width as usize, dims.height as usize);
if w == 0 || h == 0 {
return Err(Error::InvalidInput("TIFF: zero-sized image"));
}
let row_bytes = w
.checked_mul(3)
.ok_or(Error::InvalidInput("TIFF: image too large"))?;
if pixels.len() != row_bytes * h {
return Err(Error::InvalidInput(
"TIFF: pixel buffer length does not match dimensions",
));
}
let extra = [
(tags::NEW_SUBFILE_TYPE, Value::Long(vec![2])), // bit 1: page of a multi-page image
(tags::PAGE_NUMBER, Value::Short(vec![i as u16, total])),
];
images.push(self.build_strip_image(
pixels,
dims,
&SampleLayout {
spp: 3,
bits_per_sample: 8,
stored_row_bytes: row_bytes,
photometric: PhotometricInterpretation::Rgb,
},
&extra,
)?);
}
let bytes = writer::write_multipage(self.order, self.variant(), &images);
out.extend_from_slice(&bytes);
Ok(bytes.len())
}
/// Applies the selected compression to one strip's already-packed bytes.
fn compress_strip(
&self,
raw: &[u8],
dims: Dimensions,
layout: &SampleLayout,
) -> Result<Vec<u8>> {
let row_bytes = layout.stored_row_bytes;
match self.compression {
Compression::CcittRle => {
if layout.bits_per_sample != 1 {
return Err(Error::Unsupported(
"TIFF: Modified Huffman requires a bilevel image",
));
}
ccitt::mh_encode_strip(raw, row_bytes, dims.width as usize)
}
Compression::CcittGroup4Fax => {
if layout.bits_per_sample != 1 {
return Err(Error::Unsupported(
"TIFF: Group 4 fax requires a bilevel image",
));
}
let rows = raw.len() / row_bytes;
ccitt::g4_encode_strip(raw, row_bytes, rows, dims.width as usize)
}
_ => self.compress_bytes(raw, row_bytes),
}
}
/// Byte-level compression of one strip/tile (the schemes that work on raw bytes).
fn compress_bytes(&self, raw: &[u8], row_bytes: usize) -> Result<Vec<u8>> {
match self.compression {
Compression::None => Ok(raw.to_vec()),
Compression::PackBits => {
let mut out = Vec::new();
for row in raw.chunks(row_bytes) {
packbits::encode_row(row, &mut out);
}
Ok(out)
}
Compression::Lzw => Ok(lzw::encode(raw)),
_ => Err(Error::Unsupported(
"TIFF: unsupported compression for encoding",
)),
}
}
/// Lays out an 8-bit image as a grid of `tile_w × tile_h` tiles (edge tiles zero-padded).
#[allow(clippy::too_many_arguments)]
fn encode_tiled(
&self,
packed: &[u8],
dims: Dimensions,
layout: &SampleLayout,
extra_fields: &[(u16, Value)],
tile_w: u32,
tile_h: u32,
out: &mut Vec<u8>,
) -> Result<usize> {
if layout.bits_per_sample != 8 {
return Err(Error::Unsupported(
"TIFF: tiling supported only for 8-bit images so far",
));
}
if self.predictor != Predictor::None {
return Err(Error::Unsupported(
"TIFF: predictor with tiling not supported yet",
));
}
let (tw, th) = (tile_w as usize, tile_h as usize);
if tw == 0 || th == 0 || tw % 16 != 0 || th % 16 != 0 {
return Err(Error::InvalidInput(
"TIFF: tile dimensions must be positive multiples of 16",
));
}
let (w, h, spp) = (dims.width as usize, dims.height as usize, layout.spp);
let stored_row_bytes = layout.stored_row_bytes;
let tile_row_bytes = tw * spp;
let tiles_across = w.div_ceil(tw);
let tiles_down = h.div_ceil(th);
let mut tiles: Vec<Vec<u8>> = Vec::with_capacity(tiles_across * tiles_down);
for ty in 0..tiles_down {
for tx in 0..tiles_across {
let mut tile = vec![0u8; th * tile_row_bytes];
for r in 0..th {
let src_row = ty * th + r;
if src_row >= h {
break;
}
let copy_cols = tw.min(w - tx * tw);
let src = (src_row * stored_row_bytes) + (tx * tw) * spp;
let dst = r * tile_row_bytes;
tile[dst..dst + copy_cols * spp]
.copy_from_slice(&packed[src..src + copy_cols * spp]);
}
tiles.push(self.compress_bytes(&tile, tile_row_bytes)?);
}
}
let mut ifd = Ifd::new();
ifd.set(tags::IMAGE_WIDTH, dim_value(dims.width));
ifd.set(tags::IMAGE_LENGTH, dim_value(dims.height));
ifd.set(
tags::BITS_PER_SAMPLE,
Value::Short(vec![layout.bits_per_sample; spp]),
);
ifd.set(
tags::COMPRESSION,
Value::Short(vec![self.compression.code()]),
);
ifd.set(
tags::PHOTOMETRIC_INTERPRETATION,
Value::Short(vec![layout.photometric.code()]),
);
ifd.set(tags::SAMPLES_PER_PIXEL, Value::Short(vec![spp as u16]));
ifd.set(tags::TILE_WIDTH, dim_value(tile_w));
ifd.set(tags::TILE_LENGTH, dim_value(tile_h));
ifd.set(tags::X_RESOLUTION, Value::Rational(vec![(72, 1)]));
ifd.set(tags::Y_RESOLUTION, Value::Rational(vec![(72, 1)]));
ifd.set(tags::RESOLUTION_UNIT, Value::Short(vec![2])); // inch
for (tag, value) in extra_fields {
ifd.set(*tag, value.clone());
}
let bytes = writer::write_image_tiled(self.order, self.variant(), &ifd, &tiles);
out.extend_from_slice(&bytes);
Ok(bytes.len())
}
}
impl Encoder for TiffEncoder {
fn encode(&self, pixels: &[u8], dims: Dimensions, out: &mut Vec<u8>) -> Result<usize> {
self.encode_rgb8(pixels, dims, out)
}
}
/// Stores a dimension/count as `SHORT` when it fits, else `LONG` (both are valid per §2).
fn dim_value(n: u32) -> Value {
if n <= u32::from(u16::MAX) {
Value::Short(vec![n as u16])
} else {
Value::Long(vec![n])
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn rejects_mismatched_buffer() {
let enc = TiffEncoder::new();
let mut out = Vec::new();
let dims = Dimensions {
width: 2,
height: 2,
};
assert!(enc.encode_rgb8(&[0; 11], dims, &mut out).is_err());
assert!(enc.encode_gray8(&[0; 3], dims, &mut out).is_err());
assert!(enc.encode_bilevel(&[0; 3], dims, &mut out).is_err());
assert!(
enc.encode_rgb8(
&[],
Dimensions {
width: 0,
height: 1
},
&mut out
)
.is_err()
);
}
#[test]
fn writes_a_well_formed_header() {
let enc = TiffEncoder::new();
let mut out = Vec::new();
let n = enc
.encode_rgb8(
&[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
Dimensions {
width: 2,
height: 2,
},
&mut out,
)
.expect("encode");
assert_eq!(n, out.len());
assert_eq!(&out[0..2], b"II");
// Classic TIFF by default: magic 42.
assert_eq!(out[2], 42);
}
#[test]
fn with_big_tiff_emits_bigtiff_header() {
let mut out = Vec::new();
TiffEncoder::new()
.with_big_tiff(true)
.encode_rgb8(
&[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
Dimensions {
width: 2,
height: 2,
},
&mut out,
)
.expect("encode");
// Magic 43, the fixed offset-size 8, and a 16-byte header (first IFD at offset >= 16).
let (order, variant, first) = gamut_ifd::read_header(&out).expect("header");
assert_eq!(order, ByteOrder::LittleEndian);
assert_eq!(variant, Variant::Big);
assert_eq!(out[2], 0x2b);
assert!(first >= 16);
}
}