• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2024 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 // Not all functions are used from all test targets. So allow unused functions in this module.
16 #![allow(unused)]
17 
18 use crabby_avif::*;
19 use png;
20 use std::fs::File;
21 
22 #[cfg(test)]
get_test_file(filename: &str) -> String23 pub fn get_test_file(filename: &str) -> String {
24     let base_path = if cfg!(google3) {
25         format!(
26             "{}/google3/third_party/crabbyavif/",
27             std::env::var("TEST_SRCDIR").expect("TEST_SRCDIR is not defined")
28         )
29     } else {
30         "".to_string()
31     };
32     String::from(format!("{base_path}tests/data/{filename}"))
33 }
34 
35 #[cfg(test)]
get_decoder(filename: &str) -> decoder::Decoder36 pub fn get_decoder(filename: &str) -> decoder::Decoder {
37     let abs_filename = get_test_file(filename);
38     let mut decoder = decoder::Decoder::default();
39     let _ = decoder
40         .set_io_file(&abs_filename)
41         .expect("Failed to set IO");
42     decoder
43 }
44 
45 #[cfg(test)]
decode_png(filename: &str) -> Vec<u8>46 pub fn decode_png(filename: &str) -> Vec<u8> {
47     let decoder = png::Decoder::new(File::open(get_test_file(filename)).unwrap());
48     let mut reader = decoder.read_info().unwrap();
49     // Indexed colors are not supported.
50     assert_ne!(reader.output_color_type().0, png::ColorType::Indexed);
51     let mut pixels = vec![0; reader.output_buffer_size()];
52     let info = reader.next_frame(&mut pixels).unwrap();
53     pixels
54 }
55 
56 #[cfg(test)]
57 #[allow(dead_code)]
58 pub const HAS_DECODER: bool = if cfg!(any(
59     feature = "dav1d",
60     feature = "libgav1",
61     feature = "android_mediacodec"
62 )) {
63     true
64 } else {
65     false
66 };
67