about summary refs log tree commit diff
path: root/src/lib.rs
blob: 0a11fdc062fc561c3654c70a34c95d54830ba90b (plain)
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
mod color;
mod colorimage;
mod gif;
mod lzw;

pub mod block;
pub mod reader;
pub mod writer;

use core::fmt;
use std::error::Error;

pub use color::Color;
pub use colorimage::ColorImage;
pub use gif::Gif;
pub use lzw::LZW;

/// Perform the algorithm to get the length of a color table from
/// the value of the packed field. The max value here is 256
pub(crate) fn packed_to_color_table_length(packed: u8) -> usize {
	2usize.pow(packed as u32 + 1)
}

//TODO: Be sure to check that fields in LSD and Img. Desc. that were reserved
//in 87a aren't set if version is 87a, or that we return a warning, etc. Just
//remember about this.
//bottom of page 24 in 89a

#[derive(Clone, Copy, Debug)]
pub enum EncodingError {
	TooManyColors,
	NoColorTable,
	IndicieSizeMismatch { expected: usize, got: usize },
}

impl fmt::Display for EncodingError {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		match self {
			Self::TooManyColors => write!(f, "A palette is limited to 256 colors"),
			Self::NoColorTable => write!(
				f,
				"Refusing to set the background color index when no color table is set!"
			),
			Self::IndicieSizeMismatch { expected, got } => {
				write!(f, "Expected to have {} indicies but got {}", expected, got)
			}
		}
	}
}

impl Error for EncodingError {}