pub fn format_indices(
		&mut self,
		palette: &VeraPalette,
		depth: VeraPixelDepth,
	) -> Result<(), Error> {
		if self.frame_data.is_empty() {
			return Err(ErrorKind::ImageSetEmpty(self.id.clone()).into());
		}
		let palette_range = 2u32.pow(depth as u32) - 1;
		info!(
			"Formatting imageset {} to palette at depth of {}",
			self.id, depth
		);
		for frame in self.frame_data.iter_mut() {
			if depth == VeraPixelDepth::BPP1 {
				for mut p in frame.data.iter_mut() {
					p.is_1bpp = true;
					if p.r == 0 && p.g == 0 && p.b == 0 {
						p.is_on = false;
					} else {
						p.is_on = true;
					}
				}
				frame.depth = depth;
				continue;
			} else {
				// remove the 1 BPP flag from all pixels
				for mut p in frame.data.iter_mut() {
					p.is_1bpp = false;
				}
			}
			let mut mappings = BTreeMap::new();
			for p in frame.data.iter_mut() {
				let (val, indices) = palette.all_indices_of_rgb(p.r, p.g, p.b);
				if indices.len() == 0 {
					return Err(ErrorKind::PaletteIndexMissing(p.r >> 4, p.g >> 4, p.b >> 4).into());
				}
				mappings.insert(val, indices);
			}
			let mut mappings: Vec<(VeraPaletteEntry, Vec<usize>)> =
				mappings.into_iter().map(|(k, v)| (k, v)).collect();

			trace!("Possible indices map: {:?}", mappings);
			let align_to_16 = match depth {
				VeraPixelDepth::BPP8 => false,
				_ => true,
			};
			let res = find_optimal_range(palette_range, &mut mappings, align_to_16)?;
			trace!("Found?: {}", res.0);
			if !res.0 {
				return Err(ErrorKind::DepthFormatError(
					frame.id.clone().into(),
					depth as u8,
					palette_range as usize,
				)
				.into());
			}
			trace!("Resolved mappings: {:?}", mappings);

			// And back to a map
			// TODO: Optimize
			let mut final_map: BTreeMap<VeraPaletteEntry, usize> = BTreeMap::new();
			for e in mappings.iter() {
				final_map.insert(e.0, e.1[0]);
			}

			// Now set the palette indices and offsets
			frame.pal_offset = res.1 as u8;
			if depth == VeraPixelDepth::BPP8 {
				frame.pal_offset = 0;
			}
			frame.depth = depth;
			for p in frame.data.iter_mut() {
				let entry = VeraPaletteEntry::new(p.r, p.g, p.b);
				let index = final_map.get(&entry);
				match index {
					Some(i) => p.pal_index = Some(*i as u8 - frame.pal_offset),
					None => {
						// shouldn't happen but belt and suspenders, etc
						return Err(
							ErrorKind::PaletteIndexMissing(p.r >> 4, p.g >> 4, p.b >> 4).into()
						);
					}
				}
			}
		}
		self.depth = Some(depth);
		self.formatted = true;
		Ok(())
	}
/// Format the stored indices with a given palette and colour depth
/// Should fail if any frame in the set contains a range of colours
/// that can't be found within a single 2^BPP length range in the
/// palette. If BPP is 1, just store on or off and disregard palette
Format the stored indices with a given palette and colour depth
Should fail if any frame in the set contains a range of colours
that can't be found within a single 2^BPP length range in the
palette. If BPP is 1, just store on or off and disregard palette
Format the stored indices with a given palette and colour depth Should fail if any frame in the set contains a range of colours that can ' t be found within a single 2^BPP length range in the palette . If BPP is 1 just store on or off and disregard palette
pub fn format_indices(
		&mut self,
		palette: &VeraPalette,
		depth: VeraPixelDepth,
	) -> Result<(), Error> {
		if self.frame_data.is_empty() {
			return Err(ErrorKind::ImageSetEmpty(self.id.clone()).into());
		}
		let palette_range = 2u32.pow(depth as u32) - 1;
		info!(
			"Formatting imageset {} to palette at depth of {}",
			self.id, depth
		);
		for frame in self.frame_data.iter_mut() {
			if depth == VeraPixelDepth::BPP1 {
				for mut p in frame.data.iter_mut() {
					p.is_1bpp = true;
					if p.r == 0 && p.g == 0 && p.b == 0 {
						p.is_on = false;
					} else {
						p.is_on = true;
					}
				}
				frame.depth = depth;
				continue;
			} else {
				// remove the 1 BPP flag from all pixels
				for mut p in frame.data.iter_mut() {
					p.is_1bpp = false;
				}
			}
			let mut mappings = BTreeMap::new();
			for p in frame.data.iter_mut() {
				let (val, indices) = palette.all_indices_of_rgb(p.r, p.g, p.b);
				if indices.len() == 0 {
					return Err(ErrorKind::PaletteIndexMissing(p.r >> 4, p.g >> 4, p.b >> 4).into());
				}
				mappings.insert(val, indices);
			}
			let mut mappings: Vec<(VeraPaletteEntry, Vec<usize>)> =
				mappings.into_iter().map(|(k, v)| (k, v)).collect();
			trace!("Possible indices map: {:?}", mappings);
			let align_to_16 = match depth {
				VeraPixelDepth::BPP8 => false,
				_ => true,
			};
			let res = find_optimal_range(palette_range, &mut mappings, align_to_16)?;
			trace!("Found?: {}", res.0);
			if !res.0 {
				return Err(ErrorKind::DepthFormatError(
					frame.id.clone().into(),
					depth as u8,
					palette_range as usize,
				)
				.into());
			}
			trace!("Resolved mappings: {:?}", mappings);
			// And back to a map
			// TODO: Optimize
			let mut final_map: BTreeMap<VeraPaletteEntry, usize> = BTreeMap::new();
			for e in mappings.iter() {
				final_map.insert(e.0, e.1[0]);
			}
			// Now set the palette indices and offsets
			frame.pal_offset = res.1 as u8;
			if depth == VeraPixelDepth::BPP8 {
				frame.pal_offset = 0;
			}
			frame.depth = depth;
			for p in frame.data.iter_mut() {
				let entry = VeraPaletteEntry::new(p.r, p.g, p.b);
				let index = final_map.get(&entry);
				match index {
					Some(i) => p.pal_index = Some(*i as u8 - frame.pal_offset),
					None => {
						// shouldn't happen but belt and suspenders, etc
						return Err(
							ErrorKind::PaletteIndexMissing(p.r >> 4, p.g >> 4, p.b >> 4).into()
						);
					}
				}
			}
		}
		self.depth = Some(depth);
		self.formatted = true;
		Ok(())
	}
pub fn format_indices ( & mut self , palette : & VeraPalette , depth : VeraPixelDepth , ) -> Result < ( ) , Error > { if self . frame_data . is_empty ( ) { return Err ( ErrorKind :: ImageSetEmpty ( self . id . clone ( ) ) . into ( ) ) ; } let palette_range = 2u32 . pow ( depth as u32 ) - 1 ; info ! ( " " self id depth ) ; for frame in self . frame_data . iter_mut ( ) { if depth == VeraPixelDepth :: BPP1 { for mut p in frame . data . iter_mut ( ) { p . is_1bpp = true ; if p . r == 0 && p . g == 0 && p . b == 0 { p . is_on = false ; } else { p . is_on = true ; } } frame . depth = depth ; continue ; } else { // remove the 1 BPP flag from all pixels for mut p in frame . data . iter_mut ( ) { p . is_1bpp = false ; } } let mut mappings = BTreeMap :: new ( ) ; for p in frame . data . iter_mut ( ) { let ( val , indices ) = palette . all_indices_of_rgb ( p . r , p . g , p . b ) ; if indices . len ( ) == 0 { return Err ( ErrorKind :: PaletteIndexMissing ( p . r >> 4 , p . g >> 4 , p . b >> 4 ) . into ( ) ) ; } mappings . insert ( val , indices ) ; } let mut mappings : Vec < ( VeraPaletteEntry , Vec < usize > ) > = mappings . into_iter ( ) . map ( | ( k , v ) | ( k , v ) ) . collect ( ) ; trace ! ( " " mappings ) ; let align_to_16 = match depth { VeraPixelDepth :: BPP8 => false , _ => true , } ; let res = find_optimal_range ( palette_range , & mut mappings , align_to_16 ) ? ; trace ! ( " " res 0 ) ; if ! res . 0 { return Err ( ErrorKind :: DepthFormatError ( frame . id . clone ( ) . into ( ) , depth as u8 , palette_range as usize , ) . into ( ) ) ; } trace ! ( " " mappings ) ; // And back to a map // TODO: Optimize let mut final_map : BTreeMap < VeraPaletteEntry , usize > = BTreeMap :: new ( ) ; for e in mappings . iter ( ) { final_map . insert ( e . 0 , e . 1 [ 0 ] ) ; } // Now set the palette indices and offsets frame . pal_offset = res . 1 as u8 ; if depth == VeraPixelDepth :: BPP8 { frame . pal_offset = 0 ; } frame . depth = depth ; for p in frame . data . iter_mut ( ) { let entry = VeraPaletteEntry :: new ( p . r , p . g , p . b ) ; let index = final_map . get ( & entry ) ; match index { Some ( i ) => p . pal_index = Some ( * i as u8 - frame . pal_offset ) , None => { // shouldn't happen but belt and suspenders, etc return Err ( ErrorKind :: PaletteIndexMissing ( p . r >> 4 , p . g >> 4 , p . b >> 4 ) . into ( ) ) ; } } } } self . depth = Some ( depth ) ; self . formatted = true ; Ok ( ( ) ) }
Format the stored indices with a given palette and colour depth
Should fail if any frame in the set contains a range of colours
that can't be found within a single 2^BPP length range in the
palette.
Format the stored indices with a given palette and colour depth Should fail if any frame in the set contains a range of colours that can ' t be found within a single 2^BPP length range in the palette .
&mut self
palette
&VeraPalette
depth
VeraPixelDepth