Add lodepng to save 16bit PNG.

Suppress clang/gcc warnings.
This commit is contained in:
Syoyo Fujita 2019-03-03 17:04:49 +09:00
parent 962552c5c8
commit 7bdfed3bec
7 changed files with 8018 additions and 26 deletions

View File

@ -50,8 +50,9 @@ std::map<int, GLuint> bindMesh(std::map<int, GLuint> vbos,
From spec2.0 readme:
https://github.com/KhronosGroup/glTF/tree/master/specification/2.0
... drawArrays function should be used with a count equal to
the count property of any of the accessors referenced by the attributes
property (they are all equal for a given primitive).
the count property of any of the accessors referenced by the
attributes property (they are all equal for a given
primitive).
*/
}
@ -112,8 +113,29 @@ std::map<int, GLuint> bindMesh(std::map<int, GLuint> vbos,
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
GLenum format = GL_RGBA;
if (image.component == 1) {
format = GL_RED;
} else if (image.component == 2) {
format = GL_RG;
} else if (image.component == 3) {
format = GL_RGB;
} else {
// ???
}
GLenum type = GL_UNSIGNED_BYTE;
if (image.bits == 8) {
// ok
} else if (image.bits == 16) {
type = GL_UNSIGNED_SHORT;
} else {
// ???
}
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image.width, image.height, 0,
GL_RGBA, GL_UNSIGNED_BYTE, &image.image.at(0));
format, type, &image.image.at(0));
}
return vbos;
@ -255,7 +277,7 @@ void displayLoop(Window &window, const std::string &filename) {
glm::vec3 model_pos = glm::vec3(-3, 0, -3);
// generate a camera view, based on eye-position and lookAt world-position
glm::mat4 view_mat = genView(glm::vec3(2, 2, 2), model_pos);
glm::mat4 view_mat = genView(glm::vec3(2, 2, 20), model_pos);
glm::vec3 sun_position = glm::vec3(3.0, 10.0, -5.0);
glm::vec3 sun_color = glm::vec3(1.0);
@ -287,6 +309,11 @@ void displayLoop(Window &window, const std::string &filename) {
}
}
static void error_callback(int error, const char *description) {
(void)error;
fprintf(stderr, "Error: %s\n", description);
}
int main(int argc, char **argv) {
std::string filename = "../../models/Cube/Cube.gltf";
@ -294,14 +321,18 @@ int main(int argc, char **argv) {
filename = argv[1];
}
glfwSetErrorCallback(error_callback);
if (!glfwInit()) return -1;
// Force create 3.3 profile.
// Force create OpenGL 3.3
// NOTE(syoyo): Linux + NVIDIA driver segfaults for some reason? commenting out glfwWindowHint will work.
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
#ifdef __APPLE__
glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
#endif
Window window = Window(800, 600, "TinyGLTF basic example");

View File

@ -0,0 +1,21 @@
Copyright (c) 2005-2018 Lode Vandevenne
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.

5992
examples/common/lodepng.cpp Normal file

File diff suppressed because it is too large Load Diff

1919
examples/common/lodepng.h Normal file

File diff suppressed because it is too large Load Diff

View File

@ -4,9 +4,10 @@ project(gltfutil)
set(CMAKE_CXX_STANDARD 11)
include_directories(../../)
include_directories(../common/)
file(GLOB gltfutil_sources *.cc *.h)
add_executable(gltfutil ${gltfutil_sources})
add_executable(gltfutil ${gltfutil_sources} ../common/lodepng.cpp)
install ( TARGETS
gltfutil

View File

@ -4,12 +4,30 @@
#include "stb_image_write.h"
#include "texture_dumper.h"
#include "lodepng.h" // ../common
#include <tiny_gltf.h>
using namespace gltfutil;
using namespace tinygltf;
using std::cout;
static LodePNGColorType GetLodePNGColorType(int channels)
{
if (channels == 1) {
return LodePNGColorType::LCT_GREY;
} else if (channels == 2) {
return LodePNGColorType::LCT_GREY_ALPHA;
} else if (channels == 3) {
return LodePNGColorType::LCT_RGB;
} else if (channels == 4) {
return LodePNGColorType::LCT_RGBA;
} else {
std::cerr << "??? unsupported channels " << channels << "\n";
return LodePNGColorType::LCT_RGB; // FIXME(syoyo): Raise error
}
}
texture_dumper::texture_dumper(const Model& input)
: model(input), configured_format(texture_output_format::png) {
cout << "Texture dumper\n";
@ -29,31 +47,24 @@ void texture_dumper::dump_to_folder(const std::string& path) {
cout << "pixel bit depth :" << image.bits << '\n';
std::string name = image.name.empty() ? std::to_string(index) : image.name;
// TODO stb_image_write doesn't support any 16bit wirtes;
unsigned char* bytes_to_write =
const_cast<unsigned char*>(image.image.data());
unsigned char* tmp_buffer = nullptr;
if (image.pixel_type == TINYGLTF_COMPONENT_TYPE_UNSIGNED_SHORT) {
unsigned short max = 0xFFFF;
tmp_buffer = new unsigned char[image.width * image.height *
image.component * sizeof(unsigned char)];
unsigned short* buffer_as_shorts = (unsigned short*)bytes_to_write;
for (int i = 0; i < image.component * image.width * image.height; ++i) {
tmp_buffer[i] =
(unsigned char)(0xFF * (float(buffer_as_shorts[i]) / float(max)));
}
bytes_to_write =
tmp_buffer; // swap the pointer, but keep tmp_buffer around as we
// need to delete that memory later
}
switch (configured_format) {
case texture_output_format::png:
name = path + "/" + name + ".png";
std::cout << "Image will be written to " << name << '\n';
stbi_write_png(name.c_str(), image.width, image.height, image.component,
bytes_to_write, 0);
if (image.pixel_type == TINYGLTF_COMPONENT_TYPE_UNSIGNED_SHORT) {
// Use lodepng to save 16bit PNG.
// TODO(syoyo): check status
unsigned ret = lodepng::encode(name, bytes_to_write, image.width, image.height, GetLodePNGColorType(image.component), /* bits */16);
assert(ret == 0); // 0 = no err.
} else {
// TODO(syoyo): check status
stbi_write_png(name.c_str(), image.width, image.height, image.component,
bytes_to_write, 0);
}
break;
case texture_output_format::bmp:
std::cout << "Image will be written to " << name << '\n';
@ -68,8 +79,6 @@ void texture_dumper::dump_to_folder(const std::string& path) {
bytes_to_write);
break;
}
delete[] tmp_buffer;
}
}

View File

@ -1077,7 +1077,19 @@ class TinyGLTF {
#if __has_warning("-Wnewline-eof")
#pragma clang diagnostic ignored "-Wnewline-eof"
#endif
#if __has_warning("-Wunused-parameter")
#pragma clang diagnostic ignored "-Wunused-parameter"
#endif
#if __has_warning("-Wmismatched-tags")
#pragma clang diagnostic ignored "-Wmismatched-tags"
#endif
#endif
// Disable GCC warnigs
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wtype-limits"
#endif // __GNUC__
#include "json.hpp"
@ -1098,6 +1110,10 @@ class TinyGLTF {
#pragma clang diagnostic pop
#endif
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif
#ifdef _WIN32
// issue 143.
@ -1638,6 +1654,7 @@ void TinyGLTF::SetImageLoader(LoadImageDataFunction func, void *user_data) {
bool LoadImageData(Image *image, const int image_idx, std::string *err,
std::string *warn, int req_width, int req_height,
const unsigned char *bytes, int size, void *user_data) {
(void)user_data;
(void)warn;
int w, h, comp, req_comp;
@ -3111,6 +3128,8 @@ static bool ParsePrimitive(Primitive *primitive, Model *model, std::string *err,
{
ParseDracoExtension(primitive, model, err, dracoExtension->second);
}
#else
(void)model;
#endif
return true;