libjxl

FORK: libjxl patches used on blog
git clone https://git.neptards.moe/blog/libjxl.git
Log | Files | Refs | Submodules | README | LICENSE

dec_jpeg_output_chunk.h (2090B)


      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 namespace jxl {
     17 namespace jpeg {
     18 
     19 /**
     20  * A chunk of output data.
     21  *
     22  * Data producer creates OutputChunks and adds them to the end output queue.
     23  * Once control flow leaves the producer code, it is considered that chunk of
     24  * data is final and can not be changed; to underline this fact |next| is a
     25  * const-pointer.
     26  *
     27  * Data consumer removes OutputChunks from the beginning of the output queue.
     28  * It is possible to consume OutputChunks partially, by updating |next| and
     29  * |len|.
     30  *
     31  * There are 2 types of output chunks:
     32  *  - owning: actual data is stored in |buffer| field; producer fills data after
     33  *    the instance it created; it is legal to reduce |len| to show that not all
     34  *    the capacity of |buffer| is used
     35  *  - non-owning: represents the data stored (owned) somewhere else
     36  */
     37 struct OutputChunk {
     38   // Non-owning
     39   template <typename Bytes>
     40   explicit OutputChunk(Bytes& bytes) : len(bytes.size()) {
     41     // Deal both with const qualifier and data type.
     42     const void* src = bytes.data();
     43     next = reinterpret_cast<const uint8_t*>(src);
     44   }
     45 
     46   // Non-owning
     47   OutputChunk(const uint8_t* data, size_t size) : next(data), len(size) {}
     48 
     49   // Owning
     50   explicit OutputChunk(size_t size = 0) {
     51     buffer.reset(new std::vector<uint8_t>(size));
     52     next = buffer->data();
     53     len = size;
     54   }
     55 
     56   // Owning
     57   OutputChunk(std::initializer_list<uint8_t> bytes) {
     58     buffer.reset(new std::vector<uint8_t>(bytes));
     59     next = buffer->data();
     60     len = bytes.size();
     61   }
     62 
     63   const uint8_t* next;
     64   size_t len;
     65   // TODO(veluca): consider removing the unique_ptr.
     66   std::unique_ptr<std::vector<uint8_t>> buffer;
     67 };
     68 
     69 }  // namespace jpeg
     70 }  // namespace jxl
     71 
     72 #endif  // LIB_JXL_JPEG_DEC_JPEG_OUTPUT_CHUNK_H_