summaryrefslogtreecommitdiffstats
path: root/third_party/jpeg-xl/lib/jxl/toc_test.cc
blob: 8c95f8bc2692fe36342f1b870052291d50dc5ce6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright (c) the JPEG XL Project Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

#include "lib/jxl/toc.h"

#include <vector>

#include "lib/jxl/base/common.h"
#include "lib/jxl/base/random.h"
#include "lib/jxl/base/span.h"
#include "lib/jxl/coeff_order_fwd.h"
#include "lib/jxl/enc_aux_out.h"
#include "lib/jxl/enc_toc.h"
#include "lib/jxl/testing.h"

namespace jxl {
namespace {

void Roundtrip(size_t num_entries, bool permute, Rng* rng) {
  // Generate a random permutation.
  std::vector<coeff_order_t> permutation;
  std::vector<coeff_order_t> inv_permutation(num_entries);
  for (size_t i = 0; i < num_entries; i++) {
    inv_permutation[i] = i;
  }
  if (permute) {
    permutation.resize(num_entries);
    for (size_t i = 0; i < num_entries; i++) {
      permutation[i] = i;
    }
    rng->Shuffle(permutation.data(), permutation.size());
    for (size_t i = 0; i < num_entries; i++) {
      inv_permutation[permutation[i]] = i;
    }
  }

  // Generate num_entries groups of random (byte-aligned) length
  std::vector<BitWriter> group_codes(num_entries);
  for (BitWriter& writer : group_codes) {
    const size_t max_bits = (*rng)() & 0xFFF;
    BitWriter::Allotment allotment(&writer, max_bits + kBitsPerByte);
    size_t i = 0;
    for (; i + BitWriter::kMaxBitsPerCall < max_bits;
         i += BitWriter::kMaxBitsPerCall) {
      writer.Write(BitWriter::kMaxBitsPerCall, 0);
    }
    for (; i < max_bits; i += 1) {
      writer.Write(/*n_bits=*/1, 0);
    }
    writer.ZeroPadToByte();
    AuxOut aux_out;
    allotment.ReclaimAndCharge(&writer, 0, &aux_out);
  }

  BitWriter writer;
  AuxOut aux_out;
  ASSERT_TRUE(WriteGroupOffsets(group_codes, permutation, &writer, &aux_out));

  BitReader reader(writer.GetSpan());
  std::vector<uint64_t> group_offsets;
  std::vector<uint32_t> group_sizes;
  uint64_t total_size;
  ASSERT_TRUE(ReadGroupOffsets(num_entries, &reader, &group_offsets,
                               &group_sizes, &total_size));
  ASSERT_EQ(num_entries, group_offsets.size());
  ASSERT_EQ(num_entries, group_sizes.size());
  EXPECT_TRUE(reader.Close());

  uint64_t prefix_sum = 0;
  for (size_t i = 0; i < num_entries; ++i) {
    EXPECT_EQ(prefix_sum, group_offsets[inv_permutation[i]]);

    EXPECT_EQ(0u, group_codes[i].BitsWritten() % kBitsPerByte);
    prefix_sum += group_codes[i].BitsWritten() / kBitsPerByte;

    if (i + 1 < num_entries) {
      EXPECT_EQ(
          group_offsets[inv_permutation[i]] + group_sizes[inv_permutation[i]],
          group_offsets[inv_permutation[i + 1]]);
    }
  }
  EXPECT_EQ(prefix_sum, total_size);
}

TEST(TocTest, Test) {
  Rng rng(0);
  for (size_t num_entries = 1; num_entries < 10; ++num_entries) {
    for (bool permute : std::vector<bool>{false, true}) {
      Roundtrip(num_entries, permute, &rng);
    }
  }
}

}  // namespace
}  // namespace jxl