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
|
use libheif_rs::{
Channel, ChromaDownsamplingAlgorithm, ChromaUpsamplingAlgorithm, ColorSpace, CompressionFormat,
EncoderParameterValue, EncoderQuality, EncodingOptions, HeifContext, Image, ImageOrientation,
LibHeif, Result, RgbChroma,
};
pub fn create_image(width: u32, height: u32) -> Result<Image> {
let mut image = Image::new(width, height, ColorSpace::Rgb(RgbChroma::Rgb))?;
image.create_plane(Channel::Interleaved, width, height, 24)?;
let planes = image.planes_mut();
let plane = planes.interleaved.unwrap();
let stride = plane.stride;
let data = plane.data;
for y in 0..height {
let mut row_start = stride * y as usize;
for x in 0..width {
let color = x * y;
data[row_start] = ((color & 0x00_ff_00_00) >> 16) as u8;
data[row_start + 1] = ((color & 0x00_00_ff_00) >> 8) as u8;
data[row_start + 2] = (color & 0x00_00_00_ff) as u8;
row_start += 3;
}
}
Ok(image)
}
#[test]
#[ignore = "broken in debian, skipping"]
fn create_and_encode_image() -> Result<()> {
let width = 640;
let height = 480;
let image = create_image(width, height)?;
let lib_heif = LibHeif::new();
let mut context = HeifContext::new()?;
let mut encoder = lib_heif.encoder_for_format(CompressionFormat::Av1)?;
encoder.set_quality(EncoderQuality::LossLess)?;
let encoding_options: EncodingOptions = Default::default();
context.encode_image(&image, &mut encoder, Some(encoding_options))?;
let buf = context.write_to_bytes()?;
// Check result of encoding by decode it
let context = HeifContext::read_from_bytes(&buf)?;
let handle = context.primary_image_handle()?;
assert_eq!(handle.width(), width);
assert_eq!(handle.height(), height);
// Decode the image
let image = lib_heif.decode(&handle, ColorSpace::Rgb(RgbChroma::Rgb), None)?;
assert_eq!(image.color_space(), Some(ColorSpace::Rgb(RgbChroma::Rgb)));
let planes = image.planes();
let plan = planes.interleaved.unwrap();
assert_eq!(plan.width, width);
assert_eq!(plan.height, height);
Ok(())
}
#[test]
#[ignore = "broken in debian, skipping"]
fn create_and_encode_monochrome_image() -> Result<()> {
let width = 640;
let height = 480;
let mut image = Image::new(width, height, ColorSpace::Monochrome)?;
image.create_plane(Channel::Y, width, height, 8)?;
let planes = image.planes_mut();
let plane_a = planes.y.unwrap();
let stride = plane_a.stride;
let data_a = plane_a.data;
for y in 0..height {
let mut row_start = stride * y as usize;
for x in 0..width {
let color = ((x + y) % 255) as u8;
data_a[row_start] = color;
row_start += 1;
}
}
let lib_heif = LibHeif::new();
let mut context = HeifContext::new()?;
let mut encoder = lib_heif.encoder_for_format(CompressionFormat::Av1)?;
encoder.set_quality(EncoderQuality::LossLess)?;
let encoding_options = EncodingOptions::new()?;
context.encode_image(&image, &mut encoder, Some(encoding_options))?;
let _buf = context.write_to_bytes()?;
Ok(())
}
#[test]
#[ignore = "broken in debian, skipping"]
fn set_encoder_param() -> Result<()> {
let width = 640;
let height = 480;
let image = create_image(width, height)?;
let lib_heif = LibHeif::new();
let mut context = HeifContext::new()?;
let mut encoder = lib_heif.encoder_for_format(CompressionFormat::Av1)?;
encoder.set_parameter_value("speed", EncoderParameterValue::Int(5))?;
let encoding_options = EncodingOptions::new()?;
context.encode_image(&image, &mut encoder, Some(encoding_options))?;
let buf = context.write_to_bytes()?;
// Check result of encoding by decode it
let context = HeifContext::read_from_bytes(&buf)?;
let handle = context.primary_image_handle()?;
assert_eq!(handle.width(), width);
assert_eq!(handle.height(), height);
// Decode the image
let image = lib_heif.decode(&handle, ColorSpace::Rgb(RgbChroma::Rgb), None)?;
assert_eq!(image.color_space(), Some(ColorSpace::Rgb(RgbChroma::Rgb)));
let planes = image.planes();
let plan = planes.interleaved.unwrap();
assert_eq!(plan.width, width);
assert_eq!(plan.height, height);
Ok(())
}
#[test]
#[ignore = "broken in debian, skipping"]
fn add_metadata() -> Result<()> {
let width = 640;
let height = 480;
let image = create_image(width, height)?;
let lib_heif = LibHeif::new();
let mut context = HeifContext::new()?;
let mut encoder = lib_heif.encoder_for_format(CompressionFormat::Av1)?;
let handle = context.encode_image(&image, &mut encoder, None)?;
let item_type = b"MyDt";
let item_data = b"custom data";
let exif_data = b"MM\0*FakeExif";
let content_type = Some("text/plain");
context.add_generic_metadata(&handle, item_data, item_type, content_type)?;
context.add_exif_metadata(&handle, exif_data)?;
context.add_xmp_metadata(&handle, item_data)?;
// Write result HEIF file into vector
let buf = context.write_to_bytes()?;
// Check stored meta data in the encoded result
let context = HeifContext::read_from_bytes(&buf)?;
let handle = context.primary_image_handle()?;
// Custom meta data block "MyDt"
let mut item_ids = vec![0; 1];
let count = handle.metadata_block_ids(&mut item_ids, item_type);
assert_eq!(count, 1);
let md_data = handle.metadata(item_ids[0])?;
assert_eq!(&md_data, item_data);
let md_content_type = handle.metadata_content_type(item_ids[0]);
// content_type is stored in HEIF only for "mime" type of meta data.
assert_eq!(md_content_type, Some(""));
// Exif
let count = handle.metadata_block_ids(&mut item_ids, b"Exif");
assert_eq!(count, 1);
let md_data = handle.metadata(item_ids[0])?;
assert_eq!(&md_data, b"\0\0\0\0MM\0*FakeExif");
// Xmp
let count = handle.metadata_block_ids(&mut item_ids, b"mime");
assert_eq!(count, 1);
let md_data = handle.metadata(item_ids[0])?;
assert_eq!(&md_data, item_data);
let md_content_type = handle.metadata_content_type(item_ids[0]);
assert_eq!(md_content_type, Some("application/rdf+xml"));
Ok(())
}
#[test]
fn test_encoder_hevc() -> Result<()> {
let lib_heif = LibHeif::new();
let mut encoder = match lib_heif.encoder_for_format(CompressionFormat::Hevc) {
Ok(e) => e,
Err(_) => {
println!(
"WARNING: Hevc encoder is absent. The test that check encoding of heic file has skipped."
);
return Ok(());
}
};
assert!(encoder.name().starts_with("x265 HEVC encoder"));
let mut params = encoder.parameters_names();
params.sort();
assert_eq!(params.len(), 7);
let expect = vec![
"chroma".to_string(),
"complexity".to_string(),
"lossless".to_string(),
"preset".to_string(),
"quality".to_string(),
"tu-intra-depth".to_string(),
"tune".to_string(),
];
assert_eq!(params, expect);
assert_eq!(
encoder.parameter("lossless")?,
Some(EncoderParameterValue::Bool(false))
);
encoder.set_quality(EncoderQuality::LossLess)?;
assert_eq!(
encoder.parameter("lossless")?,
Some(EncoderParameterValue::Bool(true))
);
Ok(())
}
#[test]
#[ignore = "broken in debian, skipping"]
fn test_encoder_av1() -> Result<()> {
let lib_heif = LibHeif::new();
let mut encoder = lib_heif.encoder_for_format(CompressionFormat::Av1)?;
assert!(encoder.name().starts_with("AOMedia Project AV1 Encoder"));
let params = encoder.parameters_names();
assert!(params.len() >= 13);
assert_eq!(
encoder.parameter("lossless")?,
Some(EncoderParameterValue::Bool(false))
);
encoder.set_quality(EncoderQuality::LossLess)?;
assert_eq!(
encoder.parameter("lossless")?,
Some(EncoderParameterValue::Bool(true))
);
Ok(())
}
#[test]
fn test_encoding_options() -> Result<()> {
let enc_options = EncodingOptions::new().unwrap();
assert!(enc_options.version() >= 5);
// Test defaults
assert!(enc_options.save_alpha_channel());
assert!(!enc_options.mac_os_compatibility_workaround());
assert!(!enc_options.mac_os_compatibility_workaround_no_nclx_profile());
assert!(!enc_options.save_two_colr_boxes_when_icc_and_nclx_available());
assert_eq!(enc_options.image_orientation(), ImageOrientation::Normal);
let color_options = enc_options.color_conversion_options();
assert_eq!(
color_options.preferred_chroma_downsampling_algorithm,
ChromaDownsamplingAlgorithm::Average
);
assert_eq!(
color_options.preferred_chroma_upsampling_algorithm,
ChromaUpsamplingAlgorithm::Bilinear
);
assert!(!color_options.only_use_preferred_chroma_algorithm);
Ok(())
}
#[cfg(feature = "v1_18")]
mod v1_18 {
use super::*;
use std::num::NonZeroU16;
#[test]
fn test_encode_grid() -> Result<()> {
let lib_heif = LibHeif::new();
let sequence_ctx = HeifContext::read_from_file("./data/sequence.heif")?;
let handles = sequence_ctx.top_level_image_handles();
assert_eq!(handles.len(), 4);
let mut tiles = Vec::with_capacity(4);
for handle in handles {
let image = lib_heif.decode(&handle, ColorSpace::Rgb(RgbChroma::Rgb), None)?;
assert_eq!(image.width(), 480);
assert_eq!(image.height(), 360);
assert_eq!(image.color_space(), Some(ColorSpace::Rgb(RgbChroma::Rgb)));
tiles.push(image);
}
let mut encoder = lib_heif.encoder_for_format(CompressionFormat::Av1)?;
encoder.set_quality(EncoderQuality::LossLess)?;
let encoding_options: EncodingOptions = Default::default();
let mut grid_ctx = HeifContext::new()?;
let grid_handle = grid_ctx
.encode_grid(
&tiles,
NonZeroU16::new(2).unwrap(),
&mut encoder,
Some(encoding_options),
)?
.unwrap();
// Hmm, it's strange
assert_eq!(grid_handle.width(), 0);
assert_eq!(grid_handle.height(), 0);
let buf = grid_ctx.write_to_bytes()?;
// Check the result of encoding with the help of decoding
let context = HeifContext::read_from_bytes(&buf)?;
let handle = context.primary_image_handle()?;
assert_eq!(handle.width(), 480 * 2);
assert_eq!(handle.height(), 360 * 2);
Ok(())
}
}
|