From 620eed154b8be043bef7d31bbbbe8afa5f39a954 Mon Sep 17 00:00:00 2001 From: Syoyo Fujita Date: Sat, 2 Jan 2016 23:37:12 +0900 Subject: [PATCH] Add image/jpeg and image/png MIME support. Add simple unit test runner. --- README.md | 12 +++++++++ test_runner.py | 64 ++++++++++++++++++++++++++++++++++++++++++++++ tiny_gltf_loader.h | 55 ++++++++++++++++++++++++++++++--------- 3 files changed, 119 insertions(+), 12 deletions(-) create mode 100644 test_runner.py diff --git a/README.md b/README.md index a7865f4..aefdf7c 100644 --- a/README.md +++ b/README.md @@ -78,3 +78,15 @@ if (!ret) { } ``` +## Running tests. + +### Setup + +Python 2.6 or 2.7 required. +Git clone https://github.com/KhronosGroup/glTF to your local dir. + +### Run test + +After building `loader_test`, edit `test_runner.py`, then, + + $ python test_runner.py diff --git a/test_runner.py b/test_runner.py new file mode 100644 index 0000000..26d27e8 --- /dev/null +++ b/test_runner.py @@ -0,0 +1,64 @@ +#!/usr/bin/env python + +# Assume python 2.6 or 2.7 + +import glob +import os +import subprocess + +## Simple test runner. + +# -- config ----------------------- + +# Absolute path pointing to your cloned git repo of https://github.com/KhronosGroup/glTF/sampleModels +base_model_dir = "/Users/syoyo/work/glTF/sampleModels" + +kinds = [ "glTF", "glTF-Binary", "glTF-Embedded", "glTF-MaterialsCommon"] +# --------------------------------- + +failed = [] +success = [] + +def run(filename): + + print("Testing: " + filename) + cmd = ["./loader_test", filename] + try: + p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + (stdout, stderr) = p.communicate() + except: + print "Failed to execute: ", cmd + raise + + if p.returncode != 0: + failed.append(filename) + print(stdout) + print(stderr) + else: + success.append(filename) + + +def test(): + + for d in os.listdir(base_model_dir): + p = os.path.join(base_model_dir, d) + if os.path.isdir(p): + for k in kinds: + targetDir = os.path.join(p, k) + g = glob.glob(targetDir + "/*.gltf") + for gltf in g: + run(gltf) + + +def main(): + + test() + + print("Success : {0}".format(len(success))) + print("Failed : {0}".format(len(failed))) + + for fail in failed: + print("FAIL: " + fail) + +if __name__ == '__main__': + main() diff --git a/tiny_gltf_loader.h b/tiny_gltf_loader.h index c9ecd0c..49f150b 100644 --- a/tiny_gltf_loader.h +++ b/tiny_gltf_loader.h @@ -511,26 +511,57 @@ bool IsDataURI(const std::string &in) { if (in.find(header) == 0) { return true; } + + header = "data:image/png;base64,"; + if (in.find(header) == 0) { + return true; + } + + header = "data:image/jpeg;base64,"; + if (in.find(header) == 0) { + return true; + } + return false; } bool DecodeDataURI(std::vector &out, const std::string &in, size_t reqBytes, bool checkSize) { std::string header = "data:application/octet-stream;base64,"; + std::string data; if (in.find(header) == 0) { - std::string data = - base64_decode(in.substr(header.size())); // cut mime string. - if (checkSize) { - if (data.size() != reqBytes) { - return false; - } - out.resize(reqBytes); - } else { - out.resize(data.size()); - } - std::copy(data.begin(), data.end(), out.begin()); - return true; + data = base64_decode(in.substr(header.size())); // cut mime string. } + + if (data.empty()) { + header = "data:image/jpeg;base64,"; + if (in.find(header) == 0) { + data = base64_decode(in.substr(header.size())); // cut mime string. + } + } + + if (data.empty()) { + header = "data:image/png;base64,"; + if (in.find(header) == 0) { + data = base64_decode(in.substr(header.size())); // cut mime string. + } + } + + if (data.empty()) { + return false; + } + + if (checkSize) { + if (data.size() != reqBytes) { + return false; + } + out.resize(reqBytes); + } else { + out.resize(data.size()); + } + std::copy(data.begin(), data.end(), out.begin()); + return true; + return false; }