enc_toc.cc (1757B)
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 #include "lib/jxl/enc_toc.h" 7 8 #include <memory> 9 10 #include "lib/jxl/base/common.h" 11 #include "lib/jxl/base/status.h" 12 #include "lib/jxl/enc_aux_out.h" 13 #include "lib/jxl/enc_coeff_order.h" 14 #include "lib/jxl/field_encodings.h" 15 #include "lib/jxl/fields.h" 16 #include "lib/jxl/toc.h" 17 18 namespace jxl { 19 Status WriteGroupOffsets( 20 const std::vector<std::unique_ptr<BitWriter>>& group_codes, 21 const std::vector<coeff_order_t>& permutation, 22 BitWriter* JXL_RESTRICT writer, AuxOut* aux_out) { 23 return writer->WithMaxBits( 24 MaxBits(group_codes.size()), LayerType::Toc, aux_out, [&]() -> Status { 25 if (!permutation.empty() && !group_codes.empty()) { 26 // Don't write a permutation at all for an empty group_codes. 27 writer->Write(1, 1); // permutation 28 JXL_ENSURE(permutation.size() == group_codes.size()); 29 JXL_RETURN_IF_ERROR(EncodePermutation(permutation.data(), /*skip=*/0, 30 permutation.size(), writer, 31 LayerType::Header, aux_out)); 32 33 } else { 34 writer->Write(1, 0); // no permutation 35 } 36 writer->ZeroPadToByte(); // before TOC entries 37 38 for (const auto& bw : group_codes) { 39 JXL_ENSURE(bw->BitsWritten() % kBitsPerByte == 0); 40 const size_t group_size = bw->BitsWritten() / kBitsPerByte; 41 JXL_RETURN_IF_ERROR(U32Coder::Write(kTocDist, group_size, writer)); 42 } 43 writer->ZeroPadToByte(); // before first group 44 return true; 45 }); 46 } 47 48 } // namespace jxl