-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathwrite_test.rs
175 lines (142 loc) · 5.76 KB
/
write_test.rs
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
use libheif_rs::{
Channel, ColorSpace, CompressionFormat, EncoderParameterValue, EncoderQuality, EncodingOptions,
HeifContext, Image, Result, RgbChroma,
};
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]
fn create_and_encode_image() -> Result<()> {
let width = 640;
let height = 480;
let image = create_image(width, height)?;
let mut context = HeifContext::new()?;
let mut encoder = context.encoder_for_format(CompressionFormat::Hevc)?;
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 = handle.decode(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]
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 mut context = HeifContext::new()?;
let mut encoder = context.encoder_for_format(CompressionFormat::Hevc)?;
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()?;
Ok(())
}
#[test]
fn set_encoder_param() -> Result<()> {
let width = 640;
let height = 480;
let image = create_image(width, height)?;
let mut context = HeifContext::new()?;
let mut encoder = context.encoder_for_format(CompressionFormat::Av1)?;
encoder.set_parameter_value("speed", EncoderParameterValue::Int(5))?;
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 = handle.decode(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]
fn add_metadata() -> Result<()> {
let width = 640;
let height = 480;
let image = create_image(width, height)?;
let mut context = HeifContext::new()?;
let mut encoder = context.encoder_for_format(CompressionFormat::Hevc)?;
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(())
}