From af654dcf8c14f4a296de754c865e10c751c2a91a Mon Sep 17 00:00:00 2001 From: asuonpaa <34128694+asuonpaa@users.noreply.github.com> Date: Tue, 14 Apr 2020 16:06:06 +0300 Subject: Added implementation for loading buffer data from a binary file. (#838) * Added implementation for loading buffer data from a binary file. * Use PNG, BINARY, and TEXT to select the type of file to load buffer data from. --- samples/amber.cc | 40 +++++++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 11 deletions(-) (limited to 'samples/amber.cc') diff --git a/samples/amber.cc b/samples/amber.cc index 31e66f9..01ff159 100644 --- a/samples/amber.cc +++ b/samples/amber.cc @@ -254,7 +254,7 @@ bool ParseArgs(const std::vector& args, Options* opts) { return true; } -std::string ReadFile(const std::string& input_file) { +std::vector ReadFile(const std::string& input_file) { FILE* file = nullptr; #if defined(_MSC_VER) fopen_s(&file, input_file.c_str(), "rb"); @@ -287,7 +287,7 @@ std::string ReadFile(const std::string& input_file) { return {}; } - return std::string(data.begin(), data.end()); + return data; } class SampleDelegate : public amber::Delegate { @@ -327,19 +327,36 @@ class SampleDelegate : public amber::Delegate { void SetScriptPath(std::string path) { path_ = path; } amber::Result LoadBufferData(const std::string file_name, + amber::BufferDataFileType file_type, amber::BufferInfo* buffer) const override { + if (file_type == amber::BufferDataFileType::kPng) { #if AMBER_ENABLE_LODEPNG - // Try to load as png first. - amber::Result r = png::LoadPNG(path_ + file_name, &buffer->width, - &buffer->height, &buffer->values); - - if (r.IsSuccess()) - return r; + return png::LoadPNG(path_ + file_name, &buffer->width, &buffer->height, + &buffer->values); +#else + return amber::Result("PNG support is not enabled in compile options."); #endif // AMBER_ENABLE_LODEPNG + } else if (file_type == amber::BufferDataFileType::kBinary) { + auto data = ReadFile(path_ + file_name); + if (data.empty()) + return amber::Result("Failed to load buffer data " + file_name); + + for (auto d : data) { + amber::Value v; + v.SetIntValue(static_cast(d)); + buffer->values.push_back(v); + } + + buffer->width = 1; + buffer->height = 1; - // TODO(asuonpaa): Try to load a binary format. + } else { + assert(file_type == amber::BufferDataFileType::kText); - return amber::Result("Failed to load buffer data " + file_name); + // TODO(asuonpaa): Read text file and pass it to parser. + } + + return {}; } private: @@ -432,7 +449,8 @@ int main(int argc, const char** argv) { }; std::vector recipe_data; for (const auto& file : options.input_filenames) { - auto data = ReadFile(file); + auto char_data = ReadFile(file); + auto data = std::string(char_data.begin(), char_data.end()); if (data.empty()) { std::cerr << file << " is empty." << std::endl; failures.push_back(file); -- cgit v1.2.3