• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "components/zucchini/encoded_view.h"
6 
7 #include <algorithm>
8 #include <utility>
9 
10 #include "base/check_op.h"
11 
12 namespace zucchini {
13 
EncodedView(const ImageIndex & image_index)14 EncodedView::EncodedView(const ImageIndex& image_index)
15     : image_index_(image_index), pool_infos_(image_index.PoolCount()) {}
16 EncodedView::~EncodedView() = default;
17 
Projection(offset_t location) const18 EncodedView::value_type EncodedView::Projection(offset_t location) const {
19   DCHECK_LT(location, image_index_.size());
20 
21   // Find out what lies at |location|.
22   TypeTag type = image_index_.LookupType(location);
23 
24   // |location| points into raw data.
25   if (type == kNoTypeTag) {
26     // The projection is the identity function on raw content.
27     return image_index_.GetRawValue(location);
28   }
29 
30   // |location| points into a Reference.
31   const ReferenceSet& ref_set = image_index_.refs(type);
32   Reference ref = ref_set.at(location);
33   DCHECK_GE(location, ref.location);
34   DCHECK_LT(location, ref.location + ref_set.width());
35 
36   // |location| is not the first byte of the reference.
37   if (location != ref.location) {
38     // Trailing bytes of a reference are all projected to the same value.
39     return kReferencePaddingProjection;
40   }
41 
42   PoolTag pool_tag = ref_set.pool_tag();
43   const auto& target_pool = ref_set.target_pool();
44 
45   // Targets with an associated Label will use its Label index in projection.
46   DCHECK_EQ(target_pool.size(), pool_infos_[pool_tag.value()].labels.size());
47   uint32_t label = pool_infos_[pool_tag.value()]
48                        .labels[target_pool.KeyForOffset(ref.target)];
49 
50   // Projection is done on (|target|, |type|), shifted by
51   // kBaseReferenceProjection to avoid collisions with raw content.
52   value_type projection = label;
53   projection *= image_index_.TypeCount();
54   projection += type.value();
55   return projection + kBaseReferenceProjection;
56 }
57 
Cardinality() const58 size_t EncodedView::Cardinality() const {
59   size_t max_width = 0;
60   for (const auto& pool_info : pool_infos_)
61     max_width = std::max(max_width, pool_info.bound);
62   return max_width * image_index_.TypeCount() + kBaseReferenceProjection;
63 }
64 
SetLabels(PoolTag pool,std::vector<uint32_t> && labels,size_t bound)65 void EncodedView::SetLabels(PoolTag pool,
66                             std::vector<uint32_t>&& labels,
67                             size_t bound) {
68   DCHECK_EQ(labels.size(), image_index_.pool(pool).size());
69   DCHECK(labels.empty() || *max_element(labels.begin(), labels.end()) < bound);
70   pool_infos_[pool.value()].labels = std::move(labels);
71   pool_infos_[pool.value()].bound = bound;
72 }
73 
74 EncodedView::PoolInfo::PoolInfo() = default;
75 EncodedView::PoolInfo::PoolInfo(PoolInfo&&) = default;
76 EncodedView::PoolInfo::~PoolInfo() = default;
77 
78 }  // namespace zucchini
79