1 /*
2 * Copyright 2024 Google LLC
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <fstream>
18 #include <iostream>
19 #include <iterator>
20 #include <memory>
21 #include <vector>
22
23 #include "avif/avif.h"
24 #include "avif/libavif_compat.h"
25 #include "gtest/gtest.h"
26
27 using namespace crabbyavif;
28
29 // Used instead of CHECK if needing to return a specific error on failure,
30 // instead of AVIF_FALSE
31 #define AVIF_CHECKERR(A, ERR) \
32 do { \
33 if (!(A)) { \
34 return ERR; \
35 } \
36 } while (0)
37
38 // Forward any error to the caller now or continue execution.
39 #define AVIF_CHECKRES(A) \
40 do { \
41 const avifResult result__ = (A); \
42 if (result__ != AVIF_RESULT_OK) { \
43 return result__; \
44 } \
45 } while (0)
46
47 namespace avif {
48
49 // Struct to call the destroy functions in a unique_ptr.
50 struct UniquePtrDeleter {
operatorUniquePtrDeleter51 void operator()(avifDecoder* decoder) const { avifDecoderDestroy(decoder); }
operatorUniquePtrDeleter52 void operator()(avifImage * image) const { avifImageDestroy(image); }
53 };
54
55 // Use these unique_ptr to ensure the structs are automatically destroyed.
56 using DecoderPtr = std::unique_ptr<avifDecoder, UniquePtrDeleter>;
57 using ImagePtr = std::unique_ptr<avifImage, UniquePtrDeleter>;
58
59 } // namespace avif
60
61 namespace testutil {
62
Av1DecoderAvailable()63 bool Av1DecoderAvailable() { return true; }
64
read_file(const char * file_name)65 std::vector<uint8_t> read_file(const char* file_name) {
66 std::ifstream file(file_name, std::ios::binary);
67 EXPECT_TRUE(file.is_open());
68 // Get file size.
69 file.seekg(0, std::ios::end);
70 auto size = file.tellg();
71 file.seekg(0, std::ios::beg);
72 std::vector<uint8_t> data(size);
73 file.read(reinterpret_cast<char*>(data.data()), size);
74 file.close();
75 return data;
76 }
77
78 } // namespace testutil
79