Debian Codegraph @

lib/jxl/jpeg/dec_jpeg_output_chunk.h

raw
1 // Copyright (c) the JPEG XL Project Authors. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file.
5
6 #ifndef LIB_JXL_JPEG_DEC_JPEG_OUTPUT_CHUNK_H_
7 #define LIB_JXL_JPEG_DEC_JPEG_OUTPUT_CHUNK_H_
8
9 #include <stddef.h>
10 #include <stdint.h>
11
12 #include <initializer_list>
13 #include <memory>
14 #include <vector>
15
16 #include "lib/jxl/base/common.h"
17
18 namespace jxl {
19 namespace jpeg {
20
21 /**
22 * A chunk of output data.
23 *
24 * Data producer creates OutputChunks and adds them to the end output queue.
25 * Once control flow leaves the producer code, it is considered that chunk of
26 * data is final and can not be changed; to underline this fact |next| is a
27 * const-pointer.
28 *
29 * Data consumer removes OutputChunks from the beginning of the output queue.
30 * It is possible to consume OutputChunks partially, by updating |next| and
31 * |len|.
32 *
33 * There are 2 types of output chunks:
34 * - owning: actual data is stored in |buffer| field; producer fills data after
35 * the instance it created; it is legal to reduce |len| to show that not all
36 * the capacity of |buffer| is used
37 * - non-owning: represents the data stored (owned) somewhere else
38 */
39 struct OutputChunk {
40 // Non-owning
41 template <typename Bytes>
42 explicit OutputChunk(Bytes& bytes) : len(bytes.size()) {
43 // Deal both with const qualifier and data type.
44 const void* src = bytes.data();
45 next = reinterpret_cast<const uint8_t*>(src);
46 }
47
48 // Non-owning
49 OutputChunk(const uint8_t* data, size_t size) : next(data), len(size) {}
50
51 // Owning
52 explicit OutputChunk(size_t size = 0) {
53 buffer = jxl::make_unique<std::vector<uint8_t>>(size);
54 next = buffer->data();
55 len = size;
56 }
57
58 // Owning
59 OutputChunk(std::initializer_list<uint8_t> bytes) {
60 buffer = jxl::make_unique<std::vector<uint8_t>>(bytes);
61 next = buffer->data();
62 len = bytes.size();
63 }
64
65 const uint8_t* next;
66 size_t len;
67 // TODO(veluca): consider removing the unique_ptr.
68 std::unique_ptr<std::vector<uint8_t>> buffer;
69 };
70
71 } // namespace jpeg
72 } // namespace jxl
73
74 #endif // LIB_JXL_JPEG_DEC_JPEG_OUTPUT_CHUNK_H_