1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
|
// This file is part of gltfpack; see gltfpack.h for version/license details
#include "gltfpack.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct BasisSettings
{
int etc1s_l;
int etc1s_q;
int uastc_l;
float uastc_q;
};
static const char* kMimeTypes[][2] = {
{"image/jpeg", ".jpg"},
{"image/jpeg", ".jpeg"},
{"image/png", ".png"},
};
static const BasisSettings kBasisSettings[10] = {
{1, 1, 0, 1.5f},
{1, 6, 0, 1.f},
{1, 20, 1, 1.0f},
{1, 50, 1, 0.75f},
{1, 90, 1, 0.5f},
{1, 128, 1, 0.4f},
{1, 160, 1, 0.34f},
{1, 192, 1, 0.29f}, // default
{1, 224, 2, 0.26f},
{1, 255, 2, 0.f},
};
void analyzeImages(cgltf_data* data, std::vector<ImageInfo>& images)
{
for (size_t i = 0; i < data->materials_count; ++i)
{
const cgltf_material& material = data->materials[i];
if (material.has_pbr_metallic_roughness)
{
const cgltf_pbr_metallic_roughness& pbr = material.pbr_metallic_roughness;
if (pbr.base_color_texture.texture && pbr.base_color_texture.texture->image)
images[pbr.base_color_texture.texture->image - data->images].srgb = true;
}
if (material.has_pbr_specular_glossiness)
{
const cgltf_pbr_specular_glossiness& pbr = material.pbr_specular_glossiness;
if (pbr.diffuse_texture.texture && pbr.diffuse_texture.texture->image)
images[pbr.diffuse_texture.texture->image - data->images].srgb = true;
}
if (material.has_clearcoat)
{
const cgltf_clearcoat& clearcoat = material.clearcoat;
if (clearcoat.clearcoat_normal_texture.texture && clearcoat.clearcoat_normal_texture.texture->image)
images[clearcoat.clearcoat_normal_texture.texture->image - data->images].normal_map = true;
}
if (material.has_specular)
{
const cgltf_specular& specular = material.specular;
if (specular.specular_texture.texture && specular.specular_texture.texture->image)
images[specular.specular_texture.texture->image - data->images].srgb = true;
}
if (material.has_sheen)
{
const cgltf_sheen& sheen = material.sheen;
if (sheen.sheen_color_texture.texture && sheen.sheen_color_texture.texture->image)
images[sheen.sheen_color_texture.texture->image - data->images].srgb = true;
}
if (material.emissive_texture.texture && material.emissive_texture.texture->image)
images[material.emissive_texture.texture->image - data->images].srgb = true;
if (material.normal_texture.texture && material.normal_texture.texture->image)
images[material.normal_texture.texture->image - data->images].normal_map = true;
}
}
const char* inferMimeType(const char* path)
{
const char* ext = strrchr(path, '.');
if (!ext)
return "";
std::string extl = ext;
for (size_t i = 0; i < extl.length(); ++i)
extl[i] = char(tolower(extl[i]));
for (size_t i = 0; i < sizeof(kMimeTypes) / sizeof(kMimeTypes[0]); ++i)
if (extl == kMimeTypes[i][1])
return kMimeTypes[i][0];
return "";
}
static const char* mimeExtension(const char* mime_type)
{
for (size_t i = 0; i < sizeof(kMimeTypes) / sizeof(kMimeTypes[0]); ++i)
if (strcmp(kMimeTypes[i][0], mime_type) == 0)
return kMimeTypes[i][1];
return ".raw";
}
#ifdef __wasi__
static int execute(const char* cmd, bool ignore_stdout, bool ignore_stderr)
{
return system(cmd);
}
static const char* readenv(const char* name)
{
return NULL;
}
#else
static int execute(const char* cmd_, bool ignore_stdout, bool ignore_stderr)
{
#ifdef _WIN32
std::string ignore = "nul";
#else
std::string ignore = "/dev/null";
#endif
std::string cmd = cmd_;
if (ignore_stdout)
(cmd += " >") += ignore;
if (ignore_stderr)
(cmd += " 2>") += ignore;
return system(cmd.c_str());
}
static const char* readenv(const char* name)
{
return getenv(name);
}
#endif
bool checkBasis(bool verbose)
{
const char* basisu_path = readenv("BASISU_PATH");
std::string cmd = basisu_path ? basisu_path : "basisu";
cmd += " -version";
int rc = execute(cmd.c_str(), /* ignore_stdout= */ true, /* ignore_stderr= */ true);
if (verbose)
printf("%s => %d\n", cmd.c_str(), rc);
return rc == 0;
}
bool encodeBasis(const std::string& data, const char* mime_type, std::string& result, bool normal_map, bool srgb, int quality, float scale, bool pow2, bool uastc, bool verbose)
{
(void)scale;
(void)pow2;
TempFile temp_input(mimeExtension(mime_type));
TempFile temp_output(".basis");
if (!writeFile(temp_input.path.c_str(), data))
return false;
const char* basisu_path = readenv("BASISU_PATH");
std::string cmd = basisu_path ? basisu_path : "basisu";
const BasisSettings& bs = kBasisSettings[quality <= 0 ? 0 : quality > 9 ? 9 : quality - 1];
cmd += " -mipmap";
if (normal_map)
{
cmd += " -normal_map";
// for optimal quality we should specify seperate_rg_to_color_alpha but this requires renderer awareness
}
else if (!srgb)
{
cmd += " -linear";
}
if (uastc)
{
char settings[128];
sprintf(settings, " -uastc_level %d -uastc_rdo_q %.2f", bs.uastc_l, bs.uastc_q);
cmd += " -uastc";
cmd += settings;
cmd += " -uastc_rdo_d 1024";
}
else
{
char settings[128];
sprintf(settings, " -comp_level %d -q %d", bs.etc1s_l, bs.etc1s_q);
cmd += settings;
}
cmd += " -file ";
cmd += temp_input.path;
cmd += " -output_file ";
cmd += temp_output.path;
int rc = execute(cmd.c_str(), /* ignore_stdout= */ true, /* ignore_stderr= */ false);
if (verbose)
printf("%s => %d\n", cmd.c_str(), rc);
return rc == 0 && readFile(temp_output.path.c_str(), result);
}
bool checkKtx(bool verbose)
{
const char* toktx_path = readenv("TOKTX_PATH");
std::string cmd = toktx_path ? toktx_path : "toktx";
cmd += " --version";
int rc = execute(cmd.c_str(), /* ignore_stdout= */ true, /* ignore_stderr= */ true);
if (verbose)
printf("%s => %d\n", cmd.c_str(), rc);
return rc == 0;
}
static int readInt16(const std::string& data, size_t offset)
{
return (unsigned char)data[offset] * 256 + (unsigned char)data[offset + 1];
}
static bool getDimensionsPng(const std::string& data, int& width, int& height)
{
if (data.size() < 8 + 8 + 13 + 4)
return false;
const char* signature = "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a";
if (data.compare(0, 8, signature) != 0)
return false;
if (data.compare(12, 4, "IHDR") != 0)
return false;
width = readInt16(data, 18);
height = readInt16(data, 22);
return true;
}
static bool getDimensionsJpeg(const std::string& data, int& width, int& height)
{
size_t offset = 0;
// note, this can stop parsing before reaching the end but we stop at SOF anyway
while (offset + 4 <= data.size())
{
if (data[offset] != '\xff')
return false;
char marker = data[offset + 1];
if (marker == '\xff')
{
offset++;
continue; // padding
}
// d0..d9 correspond to SOI, RSTn, EOI
if (marker == 0 || unsigned(marker - '\xd0') <= 9)
{
offset += 2;
continue; // no payload
}
// c0..c1 correspond to SOF0, SOF1
if (marker == '\xc0' || marker == '\xc2')
{
if (offset + 10 > data.size())
return false;
width = readInt16(data, offset + 7);
height = readInt16(data, offset + 5);
return true;
}
offset += 2 + readInt16(data, offset + 2);
}
return false;
}
static bool getDimensions(const std::string& data, const char* mime_type, int& width, int& height)
{
if (strcmp(mime_type, "image/png") == 0)
return getDimensionsPng(data, width, height);
if (strcmp(mime_type, "image/jpeg") == 0)
return getDimensionsJpeg(data, width, height);
return false;
}
static int roundPow2(int value)
{
int result = 1;
while (result < value)
result <<= 1;
// to prevent odd texture sizes from increasing the size too much, we round to nearest power of 2 above a certain size
if (value > 128 && result * 3 / 4 > value)
result >>= 1;
return result;
}
static int roundBlock(int value, bool pow2)
{
if (value == 0)
return 4;
if (pow2 && value > 4)
return roundPow2(value);
return (value + 3) & ~3;
}
bool encodeKtx(const std::string& data, const char* mime_type, std::string& result, bool normal_map, bool srgb, int quality, float scale, bool pow2, bool uastc, bool verbose)
{
int width = 0, height = 0;
if (!getDimensions(data, mime_type, width, height))
return false;
TempFile temp_input(mimeExtension(mime_type));
TempFile temp_output(".ktx2");
if (!writeFile(temp_input.path.c_str(), data))
return false;
const char* toktx_path = readenv("TOKTX_PATH");
std::string cmd = toktx_path ? toktx_path : "toktx";
const BasisSettings& bs = kBasisSettings[quality <= 0 ? 0 : quality > 9 ? 9 : quality - 1];
cmd += " --t2";
cmd += " --2d";
cmd += " --genmipmap";
cmd += " --nowarn";
int newWidth = roundBlock(int(width * scale), pow2);
int newHeight = roundBlock(int(height * scale), pow2);
if (newWidth != width || newHeight != height)
{
char wh[128];
sprintf(wh, " --resize %dx%d", newWidth, newHeight);
cmd += wh;
}
if (uastc)
{
char settings[128];
sprintf(settings, " %d --uastc_rdo_q %.2f", bs.uastc_l, bs.uastc_q);
cmd += " --uastc";
cmd += settings;
cmd += " --uastc_rdo_d 1024";
cmd += " --zcmp 9";
}
else
{
char settings[128];
sprintf(settings, " --clevel %d --qlevel %d", bs.etc1s_l, bs.etc1s_q);
cmd += " --bcmp";
cmd += settings;
// for optimal quality we should specify separate_rg_to_color_alpha but this requires renderer awareness
if (normal_map)
cmd += " --normal_map";
}
if (srgb)
cmd += " --srgb";
else
cmd += " --linear";
cmd += " -- ";
cmd += temp_output.path;
cmd += " ";
cmd += temp_input.path;
int rc = execute(cmd.c_str(), /* ignore_stdout= */ false, /* ignore_stderr= */ false);
if (verbose)
printf("%s => %d\n", cmd.c_str(), rc);
return rc == 0 && readFile(temp_output.path.c_str(), result);
}
|