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_ASTC_FILE_H_
16 #define ASTC_CODEC_DECODER_ASTC_FILE_H_
17 
18 #include "src/base/optional.h"
19 #include "src/decoder/footprint.h"
20 #include "src/decoder/physical_astc_block.h"
21 
22 #include <memory>
23 #include <string>
24 
25 namespace astc_codec {
26 
27 // A thin wrapper around a .astc file on disk. This class simply reads the ASTC
28 // header, and stores the block data in memory.
29 class ASTCFile {
30  private:
31   struct Header {
HeaderHeader32     Header(size_t width, size_t height, size_t depth, size_t block_width,
33            size_t block_height, size_t block_depth)
34         : width_(width),
35           height_(height),
36           depth_(depth),
37           block_width_(block_width),
38           block_height_(block_height),
39           block_depth_(block_depth) {}
40 
41     size_t width_;
42     size_t height_;
43     size_t depth_;
44 
45     size_t block_width_;
46     size_t block_height_;
47     size_t block_depth_;
48   };
49 
50   ASTCFile(ASTCFile::Header&& header, std::string&& blocks);
51 
52  public:
53   // Load an ASTC file from memory.
54   // If loading failed, nullptr is returned and an error string is populated
55   // in the error parameter.
56   static std::unique_ptr<ASTCFile> LoadFromMemory(const char* data,
57                                                   size_t length,
58                                                   std::string* error);
59 
60   // Load an ASTC file from file.
61   // If loading failed, nullptr is returned and an error string is populated
62   // in the error parameter.
63   static std::unique_ptr<ASTCFile> LoadFile(const std::string& path,
64                                             std::string* error);
65 
66   // Returns the footprint for the file, if it is considered to be a valid
67   // footprint.
68   base::Optional<Footprint> GetFootprint() const;
69 
70   // Returns the string of the form "NxM" where N and M are the width and height
71   // of the block footprint, respectively.
72   std::string GetFootprintString() const;
73 
74   // Get the raw block data for the astc file.
75   const std::string& GetRawBlockData() const;
76 
77   // Returns the physical block at the associated block index.
78   PhysicalASTCBlock GetBlock(size_t block_idx) const;
79 
GetWidth()80   size_t GetWidth() const { return header_.width_; }
GetHeight()81   size_t GetHeight() const { return header_.height_; }
GetDepth()82   size_t GetDepth() const { return header_.depth_; }
83 
NumBlocks()84   size_t NumBlocks() const {
85     return blocks_.size() / PhysicalASTCBlock::kSizeInBytes;
86   }
87 
88  private:
89   static base::Optional<ASTCFile::Header> ParseHeader(const char* header);
90 
91   const Header header_;
92   const std::string blocks_;
93 };
94 
95 }  // namespace astc_codec
96 
97 #endif  // ASTC_CODEC_DECODER_ASTC_FILE_H_
98