| 1 |
// File: example1.cpp - Simple command line tool that uses the crnlib lib and the crn_decomp.h header file library |
| 2 |
// to compress, transcode/unpack, and inspect CRN/DDS textures. |
| 3 |
// See Copyright Notice and license at the end of inc/crnlib.h |
| 4 |
#include <stdlib.h> |
| 5 |
#include <stdio.h> |
| 6 |
#include <string.h> |
| 7 |
#include <math.h> |
| 8 |
#include <algorithm> |
| 9 |
|
| 10 |
#if !defined(_WIN32) |
| 11 |
#include <libgen.h> |
| 12 |
#endif |
| 13 |
|
| 14 |
// Public crnlib header. |
| 15 |
#include "crnlib.h" |
| 16 |
|
| 17 |
// CRN transcoder library. |
| 18 |
#include "crn_decomp.h" |
| 19 |
// .DDS file format definitions. |
| 20 |
#include "dds_defs.h" |
| 21 |
|
| 22 |
#include "crn_core.h" |
| 23 |
#include "crn_strutils.h" |
| 24 |
#include "crn_file_utils.h" |
| 25 |
|
| 26 |
// stb_image, for loading/saving image files. |
| 27 |
#ifdef _MSC_VER |
| 28 |
#pragma warning(disable : 4244) // conversion from 'int' to 'uint8', possible loss of data |
| 29 |
#pragma warning(disable : 4100) // unreferenced formal parameter |
| 30 |
#endif |
| 31 |
|
| 32 |
#define STB_IMAGE_IMPLEMENTATION |
| 33 |
#include "stb_image.h" |
| 34 |
#define STB_IMAGE_WRITE_IMPLEMENTATION |
| 35 |
#include "stb_image_write.h" |
| 36 |
|
| 37 |
#if defined(_WIN32) |
| 38 |
// windows.h is only needed here for GetSystemInfo(). |
| 39 |
#define WIN32_LEAN_AND_MEAN |
| 40 |
#define NOMINMAX |
| 41 |
#include "windows.h" |
| 42 |
#elif defined(__FreeBSD__) || defined(__APPLE__) |
| 43 |
#include <unistd.h> |
| 44 |
#include <sys/sysctl.h> |
| 45 |
#elif defined(__GNUC__) |
| 46 |
#include <sys/sysinfo.h> |
| 47 |
#endif |
| 48 |
|
| 49 |
using namespace crnlib; |
| 50 |
|
| 51 |
typedef unsigned int uint; |
| 52 |
|
| 53 |
const int cDefaultCRNQualityLevel = 128; |
| 54 |
|
| 55 |
static int print_usage() { |
| 56 |
printf("Description: Simple crnlib API example program.\n"); |
| 57 |
printf("Copyright (c) 2010-2016 Richard Geldreich, Jr. and Binomial LLC\n"); |
| 58 |
printf("Usage: example1 [mode: i/c/d] [source_file] [options]\n"); |
| 59 |
printf("\nModes:\n"); |
| 60 |
printf("c: Compress to .DDS or .CRN using the crn_compress() func. in crnlib.h\n"); |
| 61 |
printf(" The default output format is .DDS\n"); |
| 62 |
printf(" Supported source image formats:\n"); |
| 63 |
printf(" Baseline JPEG, PNG, BMP, TGA, PSD, and HDR\n"); |
| 64 |
printf("d: Transcodes a .CRN file to .DDS using the crn_decompress_crn_to_dds() func.,\n"); |
| 65 |
printf("or unpacks each face and mipmap level in a .DDS file to multiple .TGA files.\n"); |
| 66 |
printf("i: Display info about source_file.\n"); |
| 67 |
printf("\nOptions:\n"); |
| 68 |
printf("-out filename - Force output filename.\n"); |
| 69 |
printf("\nCompression mode options:\n"); |
| 70 |
printf("-crn - Generate a .CRN file instead of .DDS\n"); |
| 71 |
printf("-bitrate # - Specify desired CRN/DDS bits/texel, from [.1-8]\n"); |
| 72 |
printf(" When writing .DDS: -bitrate or -quality enable clustered DXTn compression.\n"); |
| 73 |
printf("-quality # - Specify CRN/DDS quality level factor, from [0-255]\n"); |
| 74 |
printf("-noAdaptiveBlocks - Always use 4x4 blocks instead of up to 8x8 macroblocks\n"); |
| 75 |
printf("-nonsrgb - Input is not sRGB: disables gamma filtering, perceptual metrics.\n"); |
| 76 |
printf("-nomips - Don't generate mipmaps\n"); |
| 77 |
printf("-setalphatoluma - Set alpha channel to luma before compression.\n"); |
| 78 |
printf("-converttoluma - Set RGB to luma before compression.\n"); |
| 79 |
printf("-pixelformat fmt - Output file's crn_format: DXT1, DXT1A, DXT3, DXT5_CCxY,\n"); |
| 80 |
printf(" DXT5_xGxR, DXT5_xGBR, DXT5_AGBR, DXN_XY (ATI 3DC), DXN_YX (ATI 3DC),\n"); |
| 81 |
printf(" DXT5A (ATN1N)\n"); |
| 82 |
printf(" If no output format is specified, this example uses either DXT1 or DXT5.\n"); |
| 83 |
return EXIT_FAILURE; |
| 84 |
} |
| 85 |
|
| 86 |
static int error(const char* pMsg, ...) { |
| 87 |
va_list args; |
| 88 |
va_start(args, pMsg); |
| 89 |
char buf[512]; |
| 90 |
crnlib_vsnprintf(buf, sizeof(buf), pMsg, args); |
| 91 |
va_end(args); |
| 92 |
printf("%s", buf); |
| 93 |
return EXIT_FAILURE; |
| 94 |
} |
| 95 |
|
| 96 |
// Loads an entire file into an allocated memory block. |
| 97 |
static crn_uint8* read_file_into_buffer(const char* pFilename, crn_uint32& size) { |
| 98 |
size = 0; |
| 99 |
|
| 100 |
FILE* pFile = NULL; |
| 101 |
crn_fopen(&pFile, pFilename, "rb"); |
| 102 |
if (!pFile) |
| 103 |
return NULL; |
| 104 |
|
| 105 |
fseek(pFile, 0, SEEK_END); |
| 106 |
size = ftell(pFile); |
| 107 |
fseek(pFile, 0, SEEK_SET); |
| 108 |
|
| 109 |
crn_uint8* pSrc_file_data = static_cast<crn_uint8*>(malloc(std::max(1U, size))); |
| 110 |
if ((!pSrc_file_data) || (fread(pSrc_file_data, size, 1, pFile) != 1)) { |
| 111 |
fclose(pFile); |
| 112 |
free(pSrc_file_data); |
| 113 |
size = 0; |
| 114 |
return NULL; |
| 115 |
} |
| 116 |
|
| 117 |
fclose(pFile); |
| 118 |
return pSrc_file_data; |
| 119 |
} |
| 120 |
|
| 121 |
// Cracks a CRN's file header using the helper functions in crn_decomp.h. |
| 122 |
static bool print_crn_info(const crn_uint8* pData, crn_uint32 data_size) { |
| 123 |
crnd::crn_file_info file_info; |
| 124 |
if (!crnd::crnd_validate_file(pData, data_size, &file_info)) |
| 125 |
return false; |
| 126 |
|
| 127 |
printf("crnd_validate_file:\n"); |
| 128 |
printf("File size: %u\nActualDataSize: %u\nHeaderSize: %u\nTotalPaletteSize: %u\nTablesSize: %u\nLevels: %u\n", data_size, |
| 129 |
file_info.m_actual_data_size, file_info.m_header_size, file_info.m_total_palette_size, file_info.m_tables_size, file_info.m_levels); |
| 130 |
|
| 131 |
printf("LevelCompressedSize: "); |
| 132 |
for (crn_uint32 i = 0; i < cCRNMaxLevels; i++) |
| 133 |
printf("%u ", file_info.m_level_compressed_size[i]); |
| 134 |
printf("\n"); |
| 135 |
|
| 136 |
printf("ColorEndpointPaletteSize: %u\n", file_info.m_color_endpoint_palette_entries); |
| 137 |
printf("ColorSelectorPaletteSize: %u\n", file_info.m_color_selector_palette_entries); |
| 138 |
printf("AlphaEndpointPaletteSize: %u\n", file_info.m_alpha_endpoint_palette_entries); |
| 139 |
printf("AlphaSelectorPaletteSize: %u\n", file_info.m_alpha_selector_palette_entries); |
| 140 |
|
| 141 |
printf("crnd_get_texture_info:\n"); |
| 142 |
crnd::crn_texture_info tex_info; |
| 143 |
if (!crnd::crnd_get_texture_info(pData, data_size, &tex_info)) |
| 144 |
return false; |
| 145 |
|
| 146 |
printf("Dimensions: %ux%u\nLevels: %u\nFaces: %u\nBytesPerBlock: %u\nUserData0: %u\nUserData1: %u\nCrnFormat: %s\n", |
| 147 |
tex_info.m_width, tex_info.m_height, tex_info.m_levels, tex_info.m_faces, tex_info.m_bytes_per_block, tex_info.m_userdata0, tex_info.m_userdata1, crn_get_format_string(tex_info.m_format)); |
| 148 |
|
| 149 |
return true; |
| 150 |
} |
| 151 |
|
| 152 |
// Cracks the DDS header and dump its contents. |
| 153 |
static bool print_dds_info(const void* pData, crn_uint32 data_size) { |
| 154 |
if ((data_size < 128) || (*reinterpret_cast<const crn_uint32*>(pData) != crnlib::cDDSFileSignature)) |
| 155 |
return false; |
| 156 |
|
| 157 |
const crnlib::DDSURFACEDESC2& desc = *reinterpret_cast<const crnlib::DDSURFACEDESC2*>((reinterpret_cast<const crn_uint8*>(pData) + sizeof(crn_uint32))); |
| 158 |
if (desc.dwSize != sizeof(crnlib::DDSURFACEDESC2)) |
| 159 |
return false; |
| 160 |
|
| 161 |
printf("DDS file information:\n"); |
| 162 |
printf("File size: %u\nDimensions: %ux%u\nPitch/LinearSize: %u\n", data_size, desc.dwWidth, desc.dwHeight, desc.dwLinearSize); |
| 163 |
printf("MipMapCount: %u\nAlphaBitDepth: %u\n", desc.dwMipMapCount, desc.dwAlphaBitDepth); |
| 164 |
|
| 165 |
const char* pDDSDFlagNames[] = |
| 166 |
{ |
| 167 |
"DDSD_CAPS", "DDSD_HEIGHT", "DDSD_WIDTH", "DDSD_PITCH", |
| 168 |
NULL, "DDSD_BACKBUFFERCOUNT", "DDSD_ZBUFFERBITDEPTH", "DDSD_ALPHABITDEPTH", |
| 169 |
NULL, NULL, NULL, "DDSD_LPSURFACE", |
| 170 |
"DDSD_PIXELFORMAT", "DDSD_CKDESTOVERLAY", "DDSD_CKDESTBLT", "DDSD_CKSRCOVERLAY", |
| 171 |
"DDSD_CKSRCBLT", "DDSD_MIPMAPCOUNT", "DDSD_REFRESHRATE", "DDSD_LINEARSIZE", |
| 172 |
"DDSD_TEXTURESTAGE", "DDSD_FVF", "DDSD_SRCVBHANDLE", "DDSD_DEPTH"}; |
| 173 |
|
| 174 |
printf("DDSD Flags: 0x%08X ", desc.dwFlags); |
| 175 |
for (size_t i = 0; i < sizeof(pDDSDFlagNames) / sizeof(pDDSDFlagNames[0]); i++) |
| 176 |
if ((pDDSDFlagNames[i]) && (desc.dwFlags & (1 << i))) |
| 177 |
printf("%s ", pDDSDFlagNames[i]); |
| 178 |
printf("\n\n"); |
| 179 |
|
| 180 |
printf("ddpfPixelFormat.dwFlags: 0x%08X ", desc.ddpfPixelFormat.dwFlags); |
| 181 |
if (desc.ddpfPixelFormat.dwFlags & DDPF_ALPHAPIXELS) |
| 182 |
printf("DDPF_ALPHAPIXELS "); |
| 183 |
if (desc.ddpfPixelFormat.dwFlags & DDPF_ALPHA) |
| 184 |
printf("DDPF_ALPHA "); |
| 185 |
if (desc.ddpfPixelFormat.dwFlags & DDPF_FOURCC) |
| 186 |
printf("DDPF_FOURCC "); |
| 187 |
if (desc.ddpfPixelFormat.dwFlags & DDPF_PALETTEINDEXED8) |
| 188 |
printf("DDPF_PALETTEINDEXED8 "); |
| 189 |
if (desc.ddpfPixelFormat.dwFlags & DDPF_RGB) |
| 190 |
printf("DDPF_RGB "); |
| 191 |
if (desc.ddpfPixelFormat.dwFlags & DDPF_LUMINANCE) |
| 192 |
printf("DDPF_LUMINANCE "); |
| 193 |
printf("\n"); |
| 194 |
|
| 195 |
printf("ddpfPixelFormat.dwFourCC: 0x%08X '%c' '%c' '%c' '%c'\n", |
| 196 |
desc.ddpfPixelFormat.dwFourCC, |
| 197 |
std::max(32U, desc.ddpfPixelFormat.dwFourCC & 0xFF), |
| 198 |
std::max(32U, (desc.ddpfPixelFormat.dwFourCC >> 8) & 0xFF), |
| 199 |
std::max(32U, (desc.ddpfPixelFormat.dwFourCC >> 16) & 0xFF), |
| 200 |
std::max(32U, (desc.ddpfPixelFormat.dwFourCC >> 24) & 0xFF)); |
| 201 |
|
| 202 |
printf("dwRGBBitCount: %u 0x%08X\n", |
| 203 |
desc.ddpfPixelFormat.dwRGBBitCount, desc.ddpfPixelFormat.dwRGBBitCount); |
| 204 |
|
| 205 |
printf("dwRGBBitCount as FOURCC: '%c' '%c' '%c' '%c'\n", |
| 206 |
std::max(32U, desc.ddpfPixelFormat.dwRGBBitCount & 0xFF), |
| 207 |
std::max(32U, (desc.ddpfPixelFormat.dwRGBBitCount >> 8) & 0xFF), |
| 208 |
std::max(32U, (desc.ddpfPixelFormat.dwRGBBitCount >> 16) & 0xFF), |
| 209 |
std::max(32U, (desc.ddpfPixelFormat.dwRGBBitCount >> 24) & 0xFF)); |
| 210 |
|
| 211 |
printf("dwRBitMask: 0x%08X\ndwGBitMask: 0x%08X\ndwBBitMask: 0x%08X\ndwRGBAlphaBitMask: 0x%08X\n", |
| 212 |
desc.ddpfPixelFormat.dwRBitMask, desc.ddpfPixelFormat.dwGBitMask, desc.ddpfPixelFormat.dwBBitMask, desc.ddpfPixelFormat.dwRGBAlphaBitMask); |
| 213 |
|
| 214 |
printf("\n"); |
| 215 |
printf("ddsCaps.dwCaps: 0x%08X ", desc.ddsCaps.dwCaps); |
| 216 |
if (desc.ddsCaps.dwCaps & DDSCAPS_COMPLEX) |
| 217 |
printf("DDSCAPS_COMPLEX "); |
| 218 |
if (desc.ddsCaps.dwCaps & DDSCAPS_TEXTURE) |
| 219 |
printf("DDSCAPS_TEXTURE "); |
| 220 |
if (desc.ddsCaps.dwCaps & DDSCAPS_MIPMAP) |
| 221 |
printf("DDSCAPS_MIPMAP"); |
| 222 |
printf("\n"); |
| 223 |
|
| 224 |
printf("ddsCaps.dwCaps2: 0x%08X ", desc.ddsCaps.dwCaps2); |
| 225 |
const char* pDDCAPS2FlagNames[] = |
| 226 |
{ |
| 227 |
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, |
| 228 |
NULL, "DDSCAPS2_CUBEMAP", "DDSCAPS2_CUBEMAP_POSITIVEX", "DDSCAPS2_CUBEMAP_NEGATIVEX", |
| 229 |
"DDSCAPS2_CUBEMAP_POSITIVEY", "DDSCAPS2_CUBEMAP_NEGATIVEY", "DDSCAPS2_CUBEMAP_POSITIVEZ", "DDSCAPS2_CUBEMAP_NEGATIVEZ", |
| 230 |
NULL, NULL, NULL, NULL, |
| 231 |
NULL, "DDSCAPS2_VOLUME"}; |
| 232 |
for (size_t i = 0; i < sizeof(pDDCAPS2FlagNames) / sizeof(pDDCAPS2FlagNames[0]); i++) |
| 233 |
if ((pDDCAPS2FlagNames[i]) && (desc.ddsCaps.dwCaps2 & (1 << i))) |
| 234 |
printf("%s ", pDDCAPS2FlagNames[i]); |
| 235 |
printf("\n"); |
| 236 |
|
| 237 |
printf("ddsCaps.dwCaps3: 0x%08X\nddsCaps.dwCaps4: 0x%08X\n", |
| 238 |
desc.ddsCaps.dwCaps3, desc.ddsCaps.dwCaps4); |
| 239 |
|
| 240 |
return true; |
| 241 |
} |
| 242 |
|
| 243 |
// CRN/DDS compression callback function. |
| 244 |
static crn_bool progress_callback_func(crn_uint32 phase_index, crn_uint32 total_phases, crn_uint32 subphase_index, crn_uint32 total_subphases, void* pUser_data_ptr) { |
| 245 |
int percentage_complete = (int)(.5f + (phase_index + float(subphase_index) / total_subphases) * 100.0f) / total_phases; |
| 246 |
printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\bProcessing: %u%%", std::min(100, std::max(0, percentage_complete))); |
| 247 |
return true; |
| 248 |
} |
| 249 |
|
| 250 |
int main(int argc, char* argv[]) { |
| 251 |
printf("example1 - Version v%u.%02u Built " __DATE__ ", " __TIME__ "\n", CRNLIB_VERSION / 100, CRNLIB_VERSION % 100); |
| 252 |
|
| 253 |
if (argc < 3) |
| 254 |
return print_usage(); |
| 255 |
|
| 256 |
// Parse command line options |
| 257 |
int mode = argv[1][0]; |
| 258 |
if ((mode != 'c') && (mode != 'd') && (mode != 'i')) |
| 259 |
return error("Invalid mode!\n"); |
| 260 |
|
| 261 |
const char* pSrc_filename = argv[2]; |
| 262 |
char out_filename[FILENAME_MAX] = {'\0'}; |
| 263 |
|
| 264 |
float bitrate = 0.0f; |
| 265 |
int quality_level = -1; |
| 266 |
bool srgb_colorspace = true; |
| 267 |
bool create_mipmaps = true; |
| 268 |
bool output_crn = false; |
| 269 |
crn_format fmt = cCRNFmtInvalid; |
| 270 |
bool use_adaptive_block_sizes = true; |
| 271 |
bool set_alpha_to_luma = false; |
| 272 |
bool convert_to_luma = false; |
| 273 |
bool enable_dxt1a = false; |
| 274 |
|
| 275 |
for (int i = 3; i < argc; i++) { |
| 276 |
if (argv[i][0] == '/') |
| 277 |
argv[i][0] = '-'; |
| 278 |
|
| 279 |
if (!crnlib_stricmp(argv[i], "-crn")) { |
| 280 |
output_crn = true; |
| 281 |
} else if (!crnlib_stricmp(argv[i], "-pixelformat")) { |
| 282 |
if (++i >= argc) |
| 283 |
return error("Expected pixel format!"); |
| 284 |
|
| 285 |
if (!crnlib_stricmp(argv[i], "dxt1a")) { |
| 286 |
enable_dxt1a = true; |
| 287 |
fmt = cCRNFmtDXT1; |
| 288 |
} else { |
| 289 |
uint f; |
| 290 |
for (f = 0; f < cCRNFmtTotal; f++) { |
| 291 |
if (!crnlib_stricmp(argv[i], crn_get_format_string(static_cast<crn_format>(f)))) { |
| 292 |
fmt = static_cast<crn_format>(f); |
| 293 |
break; |
| 294 |
} |
| 295 |
} |
| 296 |
if (f == cCRNFmtTotal) |
| 297 |
return error("Unrecognized pixel format: %s\n", argv[i]); |
| 298 |
} |
| 299 |
} else if (!crnlib_stricmp(argv[i], "-bitrate")) { |
| 300 |
if (++i >= argc) |
| 301 |
return error("Invalid bitrate!"); |
| 302 |
|
| 303 |
bitrate = (float)atof(argv[i]); |
| 304 |
if ((bitrate < .1f) || (bitrate > 8.0f)) |
| 305 |
return error("Invalid bitrate!"); |
| 306 |
} else if (!crnlib_stricmp(argv[i], "-quality")) { |
| 307 |
if (++i >= argc) |
| 308 |
return error("Invalid quality level!"); |
| 309 |
|
| 310 |
quality_level = atoi(argv[i]); |
| 311 |
if ((quality_level < 0) || (quality_level > cCRNMaxQualityLevel)) |
| 312 |
return error("Invalid quality level!"); |
| 313 |
} else if (!crnlib_stricmp(argv[i], "-out")) { |
| 314 |
if (++i >= argc) |
| 315 |
return error("Expected output filename!"); |
| 316 |
|
| 317 |
strcpy_safe(out_filename, sizeof(out_filename), argv[i]); |
| 318 |
} else if (!crnlib_stricmp(argv[i], "-nonsrgb")) |
| 319 |
srgb_colorspace = false; |
| 320 |
else if (!crnlib_stricmp(argv[i], "-nomips")) |
| 321 |
create_mipmaps = false; |
| 322 |
else if (!crnlib_stricmp(argv[i], "-noAdaptiveBlocks")) |
| 323 |
use_adaptive_block_sizes = false; |
| 324 |
else if (!crnlib_stricmp(argv[i], "-setalphatoluma")) |
| 325 |
set_alpha_to_luma = true; |
| 326 |
else if (!crnlib_stricmp(argv[i], "-converttoluma")) |
| 327 |
convert_to_luma = true; |
| 328 |
else |
| 329 |
return error("Invalid option: %s\n", argv[i]); |
| 330 |
} |
| 331 |
|
| 332 |
// Split the source filename into its various components. |
| 333 |
dynamic_string drive, dir, fname, ext; |
| 334 |
|
| 335 |
if (!file_utils::split_path(pSrc_filename, &drive, &dir, &fname, &ext)) |
| 336 |
return error("Invalid source filename!\n"); |
| 337 |
|
| 338 |
// Load the source file into memory. |
| 339 |
printf("Loading source file: %s\n", pSrc_filename); |
| 340 |
crn_uint32 src_file_size; |
| 341 |
crn_uint8* pSrc_file_data = read_file_into_buffer(pSrc_filename, src_file_size); |
| 342 |
if (!pSrc_file_data) |
| 343 |
return error("Unable to read source file\n"); |
| 344 |
|
| 345 |
if (mode == 'i') { |
| 346 |
// Information |
| 347 |
if (crnlib_stricmp(ext.get_ptr(), ".crn") == 0) { |
| 348 |
if (!print_crn_info(pSrc_file_data, src_file_size)) { |
| 349 |
free(pSrc_file_data); |
| 350 |
return error("Not a CRN file!\n"); |
| 351 |
} |
| 352 |
} else if (crnlib_stricmp(ext.get_ptr(), ".dds") == 0) { |
| 353 |
if (!print_dds_info(pSrc_file_data, src_file_size)) { |
| 354 |
free(pSrc_file_data); |
| 355 |
return error("Not a DDS file!\n"); |
| 356 |
} |
| 357 |
} else { |
| 358 |
// Try parsing the source file as a regular image. |
| 359 |
int x, y, actual_comps; |
| 360 |
stbi_uc* p = stbi_load_from_memory(pSrc_file_data, src_file_size, &x, &y, &actual_comps, 4); |
| 361 |
if (!p) { |
| 362 |
free(pSrc_file_data); |
| 363 |
return error("Failed reading image file!\n"); |
| 364 |
} |
| 365 |
stbi_image_free(p); |
| 366 |
|
| 367 |
printf("File size: %u\nDimensions: %ix%i\nActual Components: %i\n", src_file_size, x, y, actual_comps); |
| 368 |
} |
| 369 |
} else if (mode == 'c') { |
| 370 |
// Compression to DDS or CRN. |
| 371 |
|
| 372 |
// If the user has explicitly specified an output file, check the output file's extension to ensure we write the expected format. |
| 373 |
if (out_filename[0]) { |
| 374 |
dynamic_string out_fname, out_ext; |
| 375 |
|
| 376 |
file_utils::split_path(out_filename, NULL, NULL, &out_fname, &out_ext); |
| 377 |
|
| 378 |
if (!crnlib_stricmp(out_ext.get_ptr(), ".crn")) |
| 379 |
output_crn = true; |
| 380 |
else if (!crnlib_stricmp(out_ext.get_ptr(), ".dds")) |
| 381 |
output_crn = false; |
| 382 |
} |
| 383 |
|
| 384 |
// Load source image |
| 385 |
int width, height, actual_comps; |
| 386 |
crn_uint32* pSrc_image = (crn_uint32*)stbi_load_from_memory(pSrc_file_data, src_file_size, &width, &height, &actual_comps, 4); |
| 387 |
if (!pSrc_image) { |
| 388 |
free(pSrc_file_data); |
| 389 |
return error("Failed reading image file!\n"); |
| 390 |
} |
| 391 |
|
| 392 |
printf("Source file size: %u, Dimensions: %ux%u\nActual Components: %u\n", src_file_size, width, height, actual_comps); |
| 393 |
|
| 394 |
// Fill in compression parameters struct. |
| 395 |
bool has_alpha_channel = actual_comps > 3; |
| 396 |
|
| 397 |
if ((fmt == cCRNFmtDXT5A) && (actual_comps <= 3)) |
| 398 |
set_alpha_to_luma = true; |
| 399 |
|
| 400 |
if ((set_alpha_to_luma) || (convert_to_luma)) { |
| 401 |
for (int i = 0; i < width * height; i++) { |
| 402 |
crn_uint32 r = pSrc_image[i] & 0xFF, g = (pSrc_image[i] >> 8) & 0xFF, b = (pSrc_image[i] >> 16) & 0xFF; |
| 403 |
// Compute CCIR 601 luma. |
| 404 |
crn_uint32 y = (19595U * r + 38470U * g + 7471U * b + 32768) >> 16U; |
| 405 |
crn_uint32 a = (pSrc_image[i] >> 24) & 0xFF; |
| 406 |
if (set_alpha_to_luma) |
| 407 |
a = y; |
| 408 |
if (convert_to_luma) { |
| 409 |
r = y; |
| 410 |
g = y; |
| 411 |
b = y; |
| 412 |
} |
| 413 |
pSrc_image[i] = r | (g << 8) | (b << 16) | (a << 24); |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
crn_comp_params comp_params; |
| 418 |
comp_params.m_width = width; |
| 419 |
comp_params.m_height = height; |
| 420 |
comp_params.set_flag(cCRNCompFlagPerceptual, srgb_colorspace); |
| 421 |
comp_params.set_flag(cCRNCompFlagDXT1AForTransparency, enable_dxt1a && has_alpha_channel); |
| 422 |
comp_params.set_flag(cCRNCompFlagHierarchical, use_adaptive_block_sizes); |
| 423 |
comp_params.m_file_type = output_crn ? cCRNFileTypeCRN : cCRNFileTypeDDS; |
| 424 |
comp_params.m_format = (fmt != cCRNFmtInvalid) ? fmt : (has_alpha_channel ? cCRNFmtDXT5 : cCRNFmtDXT1); |
| 425 |
|
| 426 |
// Important note: This example only feeds a single source image to the compressor, and it internaly generates mipmaps from that source image. |
| 427 |
// If you want, there's nothing stopping you from generating the mipmaps on your own, then feeding the multiple source images |
| 428 |
// to the compressor. Just set the crn_mipmap_params::m_mode member (set below) to cCRNMipModeUseSourceMips. |
| 429 |
comp_params.m_pImages[0][0] = pSrc_image; |
| 430 |
|
| 431 |
if (bitrate > 0.0f) |
| 432 |
comp_params.m_target_bitrate = bitrate; |
| 433 |
else if (quality_level >= 0) |
| 434 |
comp_params.m_quality_level = quality_level; |
| 435 |
else if (output_crn) { |
| 436 |
// Set a default quality level for CRN, otherwise we'll get the default (highest quality) which leads to huge compressed palettes. |
| 437 |
comp_params.m_quality_level = cDefaultCRNQualityLevel; |
| 438 |
} |
| 439 |
|
| 440 |
// Determine the # of helper threads (in addition to the main thread) to use during compression. NumberOfCPU's-1 is reasonable. |
| 441 |
int number_of_processors = 1; |
| 442 |
#if defined(_WIN32) |
| 443 |
SYSTEM_INFO g_system_info; |
| 444 |
GetSystemInfo(&g_system_info); |
| 445 |
number_of_processors = std::max<int>(1, (int)g_system_info.dwNumberOfProcessors); |
| 446 |
#elif defined(__FreeBSD__) || defined(__APPLE__) |
| 447 |
number_of_processors = std::max<int>(1, sysconf(_SC_NPROCESSORS_ONLN)); |
| 448 |
#elif defined(__GNUC__) |
| 449 |
number_of_processors = std::max<int>(1, get_nprocs()); |
| 450 |
#endif |
| 451 |
comp_params.m_num_helper_threads = std::min(number_of_processors - 1, (int)cCRNMaxHelperThreads); |
| 452 |
|
| 453 |
comp_params.m_pProgress_func = progress_callback_func; |
| 454 |
|
| 455 |
// Fill in mipmap parameters struct. |
| 456 |
crn_mipmap_params mip_params; |
| 457 |
mip_params.m_gamma_filtering = srgb_colorspace; |
| 458 |
mip_params.m_mode = create_mipmaps ? cCRNMipModeGenerateMips : cCRNMipModeNoMips; |
| 459 |
|
| 460 |
crn_uint32 actual_quality_level; |
| 461 |
float actual_bitrate; |
| 462 |
crn_uint32 output_file_size; |
| 463 |
|
| 464 |
printf("Compressing to %s\n", crn_get_format_string(comp_params.m_format)); |
| 465 |
|
| 466 |
// Now compress to DDS or CRN. |
| 467 |
void* pOutput_file_data = crn_compress(comp_params, mip_params, output_file_size, &actual_quality_level, &actual_bitrate); |
| 468 |
printf("\n"); |
| 469 |
|
| 470 |
if (!pOutput_file_data) { |
| 471 |
stbi_image_free(pSrc_image); |
| 472 |
free(pSrc_file_data); |
| 473 |
return error("Compression failed!"); |
| 474 |
} |
| 475 |
|
| 476 |
printf("Compressed to %u bytes, quality level: %u, effective bitrate: %f\n", output_file_size, actual_quality_level, actual_bitrate); |
| 477 |
|
| 478 |
// Write the output file. |
| 479 |
char dst_filename[FILENAME_MAX]; |
| 480 |
crnlib_snprintf(dst_filename, sizeof(dst_filename), "%s%s%s%s", drive.get_ptr(), dir.get_ptr(), fname.get_ptr(), output_crn ? ".crn" : ".dds"); |
| 481 |
if (out_filename[0]) |
| 482 |
strcpy_safe(dst_filename, sizeof(dst_filename), out_filename); |
| 483 |
|
| 484 |
printf("Writing %s file: %s\n", output_crn ? "CRN" : "DDS", dst_filename); |
| 485 |
FILE* pFile = NULL; |
| 486 |
crn_fopen(&pFile, dst_filename, "wb"); |
| 487 |
if ((!pFile) || (fwrite(pOutput_file_data, output_file_size, 1, pFile) != 1) || (fclose(pFile) == EOF)) { |
| 488 |
free(pSrc_file_data); |
| 489 |
crn_free_block(pOutput_file_data); |
| 490 |
stbi_image_free(pSrc_image); |
| 491 |
return error("Failed writing to output file!\n"); |
| 492 |
} |
| 493 |
|
| 494 |
crn_free_block(pOutput_file_data); |
| 495 |
stbi_image_free(pSrc_image); |
| 496 |
} else if (crnlib_stricmp(ext.get_ptr(), ".crn") == 0) { |
| 497 |
// Decompress/transcode CRN to DDS. |
| 498 |
printf("Decompressing CRN to DDS\n"); |
| 499 |
|
| 500 |
// Transcode the CRN file to a DDS file in memory. |
| 501 |
crn_uint32 dds_file_size = src_file_size; |
| 502 |
void* pDDS_file_data = crn_decompress_crn_to_dds(pSrc_file_data, dds_file_size); |
| 503 |
if (!pDDS_file_data) { |
| 504 |
free(pSrc_file_data); |
| 505 |
return error("Failed decompressing CRN file!\n"); |
| 506 |
} |
| 507 |
|
| 508 |
// Now write the DDS file to disk. |
| 509 |
char dst_filename[FILENAME_MAX]; |
| 510 |
crnlib_snprintf(dst_filename, sizeof(dst_filename), "%s%s%s.dds", drive.get_ptr(), dir.get_ptr(), fname.get_ptr()); |
| 511 |
if (out_filename[0]) |
| 512 |
strcpy_safe(dst_filename, sizeof(dst_filename), out_filename); |
| 513 |
|
| 514 |
printf("Writing file: %s\n", dst_filename); |
| 515 |
FILE* pFile = NULL; |
| 516 |
crn_fopen(&pFile, dst_filename, "wb"); |
| 517 |
if ((!pFile) || (fwrite(pDDS_file_data, dds_file_size, 1, pFile) != 1) || (fclose(pFile) == EOF)) { |
| 518 |
crn_free_block(pDDS_file_data); |
| 519 |
free(pSrc_file_data); |
| 520 |
return error("Failed writing to output file!\n"); |
| 521 |
} |
| 522 |
|
| 523 |
printf("\n"); |
| 524 |
|
| 525 |
print_dds_info(pDDS_file_data, dds_file_size); |
| 526 |
|
| 527 |
crn_free_block(pDDS_file_data); |
| 528 |
} else if (crnlib_stricmp(ext.get_ptr(), ".dds") == 0) { |
| 529 |
// Unpack DDS to one or more TGA's. |
| 530 |
if (out_filename[0]) { |
| 531 |
file_utils::split_path(out_filename, &drive, &dir, &fname, &ext); |
| 532 |
} |
| 533 |
|
| 534 |
crn_texture_desc tex_desc; |
| 535 |
crn_uint32* pImages[cCRNMaxFaces * cCRNMaxLevels]; |
| 536 |
if (!crn_decompress_dds_to_images(pSrc_file_data, src_file_size, pImages, tex_desc)) { |
| 537 |
free(pSrc_file_data); |
| 538 |
return error("Failed unpacking DDS file!\n"); |
| 539 |
} |
| 540 |
|
| 541 |
printf("Decompressed texture Dimensions: %ux%u, Faces: %u, Levels: %u, FourCC: 0x%08X '%c' '%c' '%c' '%c'\n", |
| 542 |
tex_desc.m_width, tex_desc.m_height, tex_desc.m_faces, tex_desc.m_levels, tex_desc.m_fmt_fourcc, |
| 543 |
std::max(32U, tex_desc.m_fmt_fourcc & 0xFF), |
| 544 |
std::max(32U, (tex_desc.m_fmt_fourcc >> 8) & 0xFF), |
| 545 |
std::max(32U, (tex_desc.m_fmt_fourcc >> 16) & 0xFF), |
| 546 |
std::max(32U, (tex_desc.m_fmt_fourcc >> 24) & 0xFF)); |
| 547 |
|
| 548 |
for (crn_uint32 face_index = 0; face_index < tex_desc.m_faces; face_index++) { |
| 549 |
for (crn_uint32 level_index = 0; level_index < tex_desc.m_levels; level_index++) { |
| 550 |
int width = std::max(1U, tex_desc.m_width >> level_index); |
| 551 |
int height = std::max(1U, tex_desc.m_height >> level_index); |
| 552 |
|
| 553 |
char dst_filename[FILENAME_MAX]; |
| 554 |
crnlib_snprintf(dst_filename, sizeof(dst_filename), "%s%s%s_face%u_mip%u.tga", drive.get_ptr(), dir.get_ptr(), fname.get_ptr(), face_index, level_index); |
| 555 |
|
| 556 |
printf("Writing file: %s\n", dst_filename); |
| 557 |
if (!stbi_write_tga(dst_filename, width, height, 4, pImages[level_index + face_index * tex_desc.m_levels])) { |
| 558 |
crn_free_all_images(pImages, tex_desc); |
| 559 |
free(pSrc_file_data); |
| 560 |
|
| 561 |
return error("Failed writing output file!\n"); |
| 562 |
} |
| 563 |
} |
| 564 |
} |
| 565 |
|
| 566 |
crn_free_all_images(pImages, tex_desc); |
| 567 |
} else { |
| 568 |
free(pSrc_file_data); |
| 569 |
return error("Decompression mode only supports .dds or .crn files!\n"); |
| 570 |
} |
| 571 |
|
| 572 |
free(pSrc_file_data); |
| 573 |
|
| 574 |
return EXIT_SUCCESS; |
| 575 |
} |