[][src]Trait tiff_encoder::write::Datablock

pub trait Datablock {
    fn size(&self) -> u32;
fn write_to(self, file: &mut EndianFile) -> Result<()>; }

A block of data in the file pointed to by a field value, but that isn't part of the field itself (such as image strips).

It is also possible to store any block of data in a ByteBlock, but that would require to know the Endianness of the file beforehand, so the bytes are written in the correct order.

Using a Datablock, on the other hand, allows to make use of the functionality of an EndianFile, so the data can be written without worrying about the endianness.

Examples

Creating a DataBlock for Vec<u32>:

use std::io;
use tiff_encoder::write::{Datablock, EndianFile};
use tiff_encoder::ifd::values::Offsets;

// Create a block that wraps the u32 data.
struct U32Block(Vec<u32>);
// Implement datablock functions
impl Datablock for U32Block {
    fn size(&self) -> u32 {
        // Each u32 occupies 4 bytes.
        self.0.len() as u32 * 4
    }
    fn write_to(self, file: &mut EndianFile) -> io::Result<()> {
        for val in self.0 {
            file.write_u32(val)?
        }
        Ok(())
    }
}
// (Optional) implement some convenient functions to construct Offsets
impl U32Block {
    // Construct an Offsets to multiple U32Block
    pub fn offsets(blocks: Vec<Vec<u32>>) -> Offsets<Self> {
        Offsets::new(blocks.into_iter().map(|block| U32Block(block)).collect())
    }
    // Construct an Offsets to a single U32Block
    pub fn single(block: Vec<u32>) -> Offsets<Self> {
        U32Block::offsets(vec![block])
    }
}

// A vector holding arbitrary u32 data.
// This is the data we want to store in the U32Block.
let data_32bits: Vec<u32> = vec![0; 65536];

// This is the value that can be used directly as an IFD entry value.
let byte_block = U32Block::single(data_32bits);

Required methods

fn size(&self) -> u32

The number of bytes occupied by this Datablock.

Panics

The number of written bytes to the EndianFile in write_to(self, &mut EndianFile) must be the same value returned by this function.

Failing to meet that specification will panic.

fn write_to(self, file: &mut EndianFile) -> Result<()>

Writes this Datablock to an EndianFile. The number of bytes written must be exactly same number as returned by size(&self).

Panics

Failing to write the exact same number of bytes as indicated in size(&self) will panic.

Loading content...

Implementors

impl Datablock for ByteBlock[src]

Loading content...