/src/libavif/include/avif/avif_cxx.h
Line | Count | Source |
1 | | // Copyright 2023 Google LLC |
2 | | // SPDX-License-Identifier: BSD-2-Clause |
3 | | |
4 | | #ifndef AVIF_AVIF_CXX_H |
5 | | #define AVIF_AVIF_CXX_H |
6 | | |
7 | | #if !defined(__cplusplus) |
8 | | #error "This a C++ only header. Use avif/avif.h for C." |
9 | | #endif |
10 | | |
11 | | #include <memory> |
12 | | |
13 | | #include "avif/avif.h" |
14 | | |
15 | | namespace avif |
16 | | { |
17 | | |
18 | | // Struct to call the destroy functions in a unique_ptr. |
19 | | struct UniquePtrDeleter |
20 | | { |
21 | 93.1k | void operator()(avifDecoder * decoder) const { avifDecoderDestroy(decoder); } |
22 | 53.8k | void operator()(avifEncoder * encoder) const { avifEncoderDestroy(encoder); } |
23 | 0 | void operator()(avifGainMap * gainMap) const { avifGainMapDestroy(gainMap); } |
24 | 182k | void operator()(avifImage * image) const { avifImageDestroy(image); } |
25 | | }; |
26 | | |
27 | | // Use these unique_ptr to ensure the structs are automatically destroyed. |
28 | | using DecoderPtr = std::unique_ptr<avifDecoder, UniquePtrDeleter>; |
29 | | using EncoderPtr = std::unique_ptr<avifEncoder, UniquePtrDeleter>; |
30 | | using GainMapPtr = std::unique_ptr<avifGainMap, UniquePtrDeleter>; |
31 | | using ImagePtr = std::unique_ptr<avifImage, UniquePtrDeleter>; |
32 | | |
33 | | // Automatically cleans the resources of the avifRGBImage. |
34 | | // To use when RGBImage actually owns the pixels. RGBImage can also be used as a view, in which case it does not own the pixels. |
35 | | class RGBImageCleanup |
36 | | { |
37 | | public: |
38 | 0 | RGBImageCleanup(avifRGBImage * rgb) : rgb_(rgb) {} |
39 | 0 | ~RGBImageCleanup() { avifRGBImageFreePixels(rgb_); } |
40 | | |
41 | | private: |
42 | | avifRGBImage * rgb_ = nullptr; |
43 | | }; |
44 | | |
45 | | } // namespace avif |
46 | | |
47 | | #endif // AVIF_AVIF_CXX_H |