mirror of
https://github.com/syoyo/tinygltf.git
synced 2026-10-05 00:15:34 +00:00
16bit images are 16bit images: added Image::bits and Image::pixel_type
This commit is contained in:
@@ -20,8 +20,8 @@ int usage(int ret = 0) {
|
||||
"[path to output directory])\n\n"
|
||||
//<< "\t\t -i: start in interactive mode\n"
|
||||
<< "\t\t -d: dump enclosed content (image assets)\n"
|
||||
<< "\t\t -f: file format for image output"
|
||||
<< "\t\t -o: ouptput directory path"
|
||||
<< "\t\t -f: file format for image output\n"
|
||||
<< "\t\t -o: ouptput directory path\n"
|
||||
<< "\t\t -h: print this help\n";
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
|
||||
#include "stb_image_write.h"
|
||||
#include "texture_dumper.h"
|
||||
@@ -26,28 +26,50 @@ void texture_dumper::dump_to_folder(const std::string& path) {
|
||||
cout << "image name is: \"" << image.name << "\"\n";
|
||||
cout << "image size is: " << image.width << 'x' << image.height << '\n';
|
||||
cout << "pixel channel count :" << image.component << '\n';
|
||||
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,
|
||||
image.image.data(), 0);
|
||||
bytes_to_write, 0);
|
||||
break;
|
||||
case texture_output_format::bmp:
|
||||
std::cout << "Image will be written to " << name << '\n';
|
||||
name = path + "/" + name + ".bmp";
|
||||
stbi_write_bmp(name.c_str(), image.width, image.height, image.component,
|
||||
image.image.data());
|
||||
bytes_to_write);
|
||||
break;
|
||||
case texture_output_format::tga:
|
||||
std::cout << "Image will be written to " << name << '\n';
|
||||
name = path + "/" + name + ".tga";
|
||||
stbi_write_tga(name.c_str(), image.width, image.height, image.component,
|
||||
image.image.data());
|
||||
bytes_to_write);
|
||||
break;
|
||||
}
|
||||
|
||||
delete[] tmp_buffer;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user