aboutsummaryrefslogtreecommitdiff
path: root/src/decoder/footprint.h
blob: 47302cc3950bd077be51f921d64a64ea69addf01 (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
98
99
100
101
102
103
104
105
106
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef ASTC_CODEC_DECODER_FOOTPRINT_H_
#define ASTC_CODEC_DECODER_FOOTPRINT_H_

#include "include/astc-codec/astc-codec.h"
#include "src/base/optional.h"

#include <cstddef>

namespace astc_codec {

// An ASTC texture can be encoded with varying choices in block size. A set of
// predefined block sizes are specified in the ASTC specification. These are
// referred to in the literature as "footprints" available to an encoder when
// constructing an ASTC bitstream. This class provides various utility functions
// for interacting with these footprints.
class Footprint {
 public:
  Footprint() = delete;
  Footprint(const Footprint& footprint) = default;

  // Return the footprint type.
  FootprintType Type() const { return footprint_; }

  // Return logical descriptions of the dimensions.
  int Width() const { return width_; }
  int Height() const { return height_; }

  // Returns the number of pixels for a block with this footprint.
  int NumPixels() const { return width_ * height_; }

  // Returns the number of bytes needed to store an ASTC encoded image with the
  // given width and height.
  size_t StorageRequirements(int width, int height) const;

  // Returns the number of bits used per pixel.
  float Bitrate() const;

  static constexpr int NumValidFootprints() {
    return static_cast<int>(FootprintType::kCount);
  }

  bool operator==(const Footprint& other) const {
    return footprint_ == other.footprint_;
  }

  // These are the valid and available ASTC footprints.
  static Footprint Get4x4() { return Footprint(FootprintType::k4x4); }
  static Footprint Get5x4() { return Footprint(FootprintType::k5x4); }
  static Footprint Get5x5() { return Footprint(FootprintType::k5x5); }
  static Footprint Get6x5() { return Footprint(FootprintType::k6x5); }
  static Footprint Get6x6() { return Footprint(FootprintType::k6x6); }
  static Footprint Get8x5() { return Footprint(FootprintType::k8x5); }
  static Footprint Get8x6() { return Footprint(FootprintType::k8x6); }
  static Footprint Get8x8() { return Footprint(FootprintType::k8x8); }
  static Footprint Get10x5() { return Footprint(FootprintType::k10x5); }
  static Footprint Get10x6() { return Footprint(FootprintType::k10x6); }
  static Footprint Get10x8() { return Footprint(FootprintType::k10x8); }
  static Footprint Get10x10() { return Footprint(FootprintType::k10x10); }
  static Footprint Get12x10() { return Footprint(FootprintType::k12x10); }
  static Footprint Get12x12() { return Footprint(FootprintType::k12x12); }

  // Constructs a footprint from a string of the form "NxM", or no value if
  // width and height are not a valid footprint.
  static base::Optional<Footprint> Parse(const char* footprint_string);

  // Returns a footprint corresponding to a block of the given width and height,
  // or no value if it does not.
  static base::Optional<Footprint> FromDimensions(int width, int height);

  // Returns a Footprint for the given FootprintType.
  static base::Optional<Footprint> FromFootprintType(FootprintType type);

 private:
  // The only constructor.
  explicit Footprint(FootprintType footprint);

  // Returns the valid footprint for the width and height if possible.
  static base::Optional<FootprintType> GetValidFootprintForDimensions(
      int width, int height);

  // Returns the associated dimension for the given valid footprint.
  static int GetWidthForFootprint(FootprintType footprint);
  static int GetHeightForFootprint(FootprintType footprint);

  FootprintType footprint_;
  int width_;
  int height_;
};

}  // namespace astc_codec

#endif  // ASTC_CODEC_DECODER_FOOTPRINT_H_