1 // Copyright 2018 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef ASTC_CODEC_DECODER_FOOTPRINT_H_
16 #define ASTC_CODEC_DECODER_FOOTPRINT_H_
17 
18 #include "include/astc-codec/astc-codec.h"
19 #include "src/base/optional.h"
20 
21 #include <cstddef>
22 
23 namespace astc_codec {
24 
25 // An ASTC texture can be encoded with varying choices in block size. A set of
26 // predefined block sizes are specified in the ASTC specification. These are
27 // referred to in the literature as "footprints" available to an encoder when
28 // constructing an ASTC bitstream. This class provides various utility functions
29 // for interacting with these footprints.
30 class Footprint {
31  public:
32   Footprint() = delete;
33   Footprint(const Footprint& footprint) = default;
34 
35   // Return the footprint type.
Type()36   FootprintType Type() const { return footprint_; }
37 
38   // Return logical descriptions of the dimensions.
Width()39   int Width() const { return width_; }
Height()40   int Height() const { return height_; }
41 
42   // Returns the number of pixels for a block with this footprint.
NumPixels()43   int NumPixels() const { return width_ * height_; }
44 
45   // Returns the number of bytes needed to store an ASTC encoded image with the
46   // given width and height.
47   size_t StorageRequirements(int width, int height) const;
48 
49   // Returns the number of bits used per pixel.
50   float Bitrate() const;
51 
NumValidFootprints()52   static constexpr int NumValidFootprints() {
53     return static_cast<int>(FootprintType::kCount);
54   }
55 
56   bool operator==(const Footprint& other) const {
57     return footprint_ == other.footprint_;
58   }
59 
60   // These are the valid and available ASTC footprints.
Get4x4()61   static Footprint Get4x4() { return Footprint(FootprintType::k4x4); }
Get5x4()62   static Footprint Get5x4() { return Footprint(FootprintType::k5x4); }
Get5x5()63   static Footprint Get5x5() { return Footprint(FootprintType::k5x5); }
Get6x5()64   static Footprint Get6x5() { return Footprint(FootprintType::k6x5); }
Get6x6()65   static Footprint Get6x6() { return Footprint(FootprintType::k6x6); }
Get8x5()66   static Footprint Get8x5() { return Footprint(FootprintType::k8x5); }
Get8x6()67   static Footprint Get8x6() { return Footprint(FootprintType::k8x6); }
Get8x8()68   static Footprint Get8x8() { return Footprint(FootprintType::k8x8); }
Get10x5()69   static Footprint Get10x5() { return Footprint(FootprintType::k10x5); }
Get10x6()70   static Footprint Get10x6() { return Footprint(FootprintType::k10x6); }
Get10x8()71   static Footprint Get10x8() { return Footprint(FootprintType::k10x8); }
Get10x10()72   static Footprint Get10x10() { return Footprint(FootprintType::k10x10); }
Get12x10()73   static Footprint Get12x10() { return Footprint(FootprintType::k12x10); }
Get12x12()74   static Footprint Get12x12() { return Footprint(FootprintType::k12x12); }
75 
76   // Constructs a footprint from a string of the form "NxM", or no value if
77   // width and height are not a valid footprint.
78   static base::Optional<Footprint> Parse(const char* footprint_string);
79 
80   // Returns a footprint corresponding to a block of the given width and height,
81   // or no value if it does not.
82   static base::Optional<Footprint> FromDimensions(int width, int height);
83 
84   // Returns a Footprint for the given FootprintType.
85   static base::Optional<Footprint> FromFootprintType(FootprintType type);
86 
87  private:
88   // The only constructor.
89   explicit Footprint(FootprintType footprint);
90 
91   // Returns the valid footprint for the width and height if possible.
92   static base::Optional<FootprintType> GetValidFootprintForDimensions(
93       int width, int height);
94 
95   // Returns the associated dimension for the given valid footprint.
96   static int GetWidthForFootprint(FootprintType footprint);
97   static int GetHeightForFootprint(FootprintType footprint);
98 
99   FootprintType footprint_;
100   int width_;
101   int height_;
102 };
103 
104 }  // namespace astc_codec
105 
106 #endif  // ASTC_CODEC_DECODER_FOOTPRINT_H_
107