aboutsummaryrefslogtreecommitdiff
path: root/samples
diff options
context:
space:
mode:
authorasuonpaa <34128694+asuonpaa@users.noreply.github.com>2020-03-24 14:43:27 +0200
committerGitHub <noreply@github.com>2020-03-24 08:43:27 -0400
commit38fcbe6700f725b80a68168f046ac86020254500 (patch)
tree09e6984d6b052d418bd4c44e4533e684eee67cbc /samples
parentd4ab26cbc3d53f5dfcb38ede4ea4eadbb6da6be3 (diff)
downloadamber-38fcbe6700f725b80a68168f046ac86020254500.tar.gz
Added a support for loading a png file into a buffer (#823)
This CL adds the `FILE` parameter to `BUFFER` commands. This allows loading file data into the buffer. Currently only PNG data is supported.
Diffstat (limited to 'samples')
-rw-r--r--samples/amber.cc22
-rw-r--r--samples/png.cc19
-rw-r--r--samples/png.h8
3 files changed, 49 insertions, 0 deletions
diff --git a/samples/amber.cc b/samples/amber.cc
index 221da50..31e66f9 100644
--- a/samples/amber.cc
+++ b/samples/amber.cc
@@ -324,10 +324,29 @@ class SampleDelegate : public amber::Delegate {
return timestamp::SampleGetTimestampNs();
}
+ void SetScriptPath(std::string path) { path_ = path; }
+
+ amber::Result LoadBufferData(const std::string file_name,
+ amber::BufferInfo* buffer) const override {
+#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;
+#endif // AMBER_ENABLE_LODEPNG
+
+ // TODO(asuonpaa): Try to load a binary format.
+
+ return amber::Result("Failed to load buffer data " + file_name);
+ }
+
private:
bool log_graphics_calls_ = false;
bool log_graphics_calls_time_ = false;
bool log_execute_calls_ = false;
+ std::string path_ = "";
};
std::string disassemble(const std::string& env,
@@ -531,6 +550,9 @@ int main(int argc, const char** argv) {
const auto* recipe = recipe_data_elem.recipe.get();
const auto& file = recipe_data_elem.file;
+ // Parse file path and set it for delegate to use when loading buffer data.
+ delegate.SetScriptPath(file.substr(0, file.find_last_of("/\\") + 1));
+
amber::Amber am;
result = am.Execute(recipe, &amber_options);
if (!result.IsSuccess()) {
diff --git a/samples/png.cc b/samples/png.cc
index 93e2279..3dbf4fd 100644
--- a/samples/png.cc
+++ b/samples/png.cc
@@ -81,4 +81,23 @@ amber::Result ConvertToPNG(uint32_t width,
return {};
}
+amber::Result LoadPNG(const std::string file_name,
+ uint32_t* width,
+ uint32_t* height,
+ std::vector<amber::Value>* values) {
+ std::vector<uint8_t> decoded_buffer;
+ if (lodepng::decode(decoded_buffer, *width, *height, file_name,
+ LodePNGColorType::LCT_RGBA, 8) != 0) {
+ return amber::Result("lodepng::decode() returned non-zero");
+ }
+
+ for (auto d : decoded_buffer) {
+ amber::Value v;
+ v.SetIntValue(d);
+ values->push_back(v);
+ }
+
+ return {};
+}
+
} // namespace png
diff --git a/samples/png.h b/samples/png.h
index 74802e7..b08d74d 100644
--- a/samples/png.h
+++ b/samples/png.h
@@ -31,6 +31,14 @@ amber::Result ConvertToPNG(uint32_t width,
const std::vector<amber::Value>& values,
std::vector<uint8_t>* buffer);
+/// Loads a PNG image from |file_name|. Image dimensions of the loaded file are
+/// stored into |width| and |height|, and the image data is stored in a one
+/// byte per data element format into |values|.
+amber::Result LoadPNG(const std::string file_name,
+ uint32_t* width,
+ uint32_t* height,
+ std::vector<amber::Value>* values);
+
} // namespace png
#endif // SAMPLES_PNG_H_