Debian Codegraph @

example2/example2.cpp

raw
1 // File: example2.cpp - This example uses the crn_decomp.h stand-alone header file library
2 // to transcode .CRN files directly to .DDS, with no intermediate recompression step to DXTn.
3 // This tool does NOT depend on the crnlib library at all. It only needs the low-level
4 // decompression/transcoding functionality defined in inc/crn_decomp.h.
5 // This is the basic functionality a game engine would need to employ at runtime to utilize
6 // .CRN textures (excluding writing the output DDS file - instead you would provide the DXTn
7 // bits directly to OpenGL/D3D).
8 // See Copyright Notice and license at the end of inc/crnlib.h
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <string.h>
12 #include <math.h>
13 #include <algorithm>
14
15 #if !defined(_WIN32)
16 #include <libgen.h>
17 #endif
18
19 // CRN transcoder library.
20 #include "crn_decomp.h"
21 // .DDS file format definitions.
22 #include "dds_defs.h"
23
24 // A simple high-precision, platform independent timer class.
25 #include "timer.h"
26
27 #include "crn_platform.h"
28
29 using namespace crnlib;
30
31 #if defined(_WIN32)
32 #define example2_strcpy_safe(d, l, s) strcpy_s(d, l, s)
33 #else
34 void example2_strcpy_safe(char *d, size_t l, const char* s) {l = strnlen(s, l - 1); memcpy(d, s, l); d[l] = '\0';}
35 #endif
36
37 static int print_usage() {
38 printf("Description: Transcodes .CRN to .DDS files using crn_decomp.h.\n");
39 printf("Copyright (c) 2010-2016 Richard Geldreich, Jr. and Binomial LLC\n");
40 printf("Usage: example2 [source_file] [options]\n");
41 printf("\nOptions:\n");
42 printf("-out filename - Force output filename.\n");
43 return EXIT_FAILURE;
44 }
45
46 static int error(const char* pMsg, ...) {
47 va_list args;
48 va_start(args, pMsg);
49 char buf[512];
50 crnlib_vsnprintf(buf, sizeof(buf), pMsg, args);
51 va_end(args);
52 printf("%s", buf);
53 return EXIT_FAILURE;
54 }
55
56 // Loads an entire file into an allocated memory block.
57 static crn_uint8* read_file_into_buffer(const char* pFilename, crn_uint32& size) {
58 size = 0;
59
60 FILE* pFile = NULL;
61 crn_fopen(&pFile, pFilename, "rb");
62 if (!pFile)
63 return NULL;
64
65 fseek(pFile, 0, SEEK_END);
66 size = ftell(pFile);
67 fseek(pFile, 0, SEEK_SET);
68
69 crn_uint8* pSrc_file_data = static_cast<crn_uint8*>(malloc(std::max(1U, size)));
70 if ((!pSrc_file_data) || (fread(pSrc_file_data, size, 1, pFile) != 1)) {
71 fclose(pFile);
72 free(pSrc_file_data);
73 size = 0;
74 return NULL;
75 }
76
77 fclose(pFile);
78 return pSrc_file_data;
79 }
80
81 int main(int argc, char* argv[]) {
82 printf("example2 - Version v%u.%02u Built " __DATE__ ", " __TIME__ "\n", CRNLIB_VERSION / 100, CRNLIB_VERSION % 100);
83
84 if (argc < 2)
85 return print_usage();
86
87 // Parse command line options
88 const char* pSrc_filename = argv[1];
89 char out_filename[FILENAME_MAX] = {'\0'};
90
91 for (int i = 2; i < argc; i++) {
92 if (argv[i][0] == '/')
93 argv[i][0] = '-';
94
95 if (!crnlib_stricmp(argv[i], "-out")) {
96 if (++i >= argc)
97 return error("Expected output filename!");
98
99 example2_strcpy_safe(out_filename, sizeof(out_filename), argv[i]);
100 } else
101 return error("Invalid option: %s\n", argv[i]);
102 }
103
104 // Load the source file into memory.
105 printf("Loading source file: %s\n", pSrc_filename);
106 crn_uint32 src_file_size;
107 crn_uint8* pSrc_file_data = read_file_into_buffer(pSrc_filename, src_file_size);
108 if (!pSrc_file_data)
109 return error("Unable to read source file\n");
110
111 // Decompress/transcode CRN to DDS.
112 // DDS files are organized in face-major order, like this:
113 // Face0: Mip0, Mip1, Mip2, etc.
114 // Face1: Mip0, Mip1, Mip2, etc.
115 // etc.
116 // While CRN files are organized in mip-major order, like this:
117 // Mip0: Face0, Face1, Face2, Face3, Face4, Face5
118 // Mip1: Face0, Face1, Face2, Face3, Face4, Face5
119 // etc.
120 printf("Transcoding CRN to DDS\n");
121
122 crnd::crn_texture_info tex_info;
123 if (!crnd::crnd_get_texture_info(pSrc_file_data, src_file_size, &tex_info)) {
124 free(pSrc_file_data);
125 return error("crnd_get_texture_info() failed!\n");
126 }
127
128 timer tm;
129
130 tm.start();
131 crnd::crnd_unpack_context pContext = crnd::crnd_unpack_begin(pSrc_file_data, src_file_size);
132 double total_unpack_begin_time = tm.get_elapsed_ms();
133
134 if (!pContext) {
135 free(pSrc_file_data);
136 return error("crnd_unpack_begin() failed!\n");
137 }
138
139 // Now create the DDS file.
140 char dst_filename[FILENAME_MAX];
141 if (out_filename[0]) {
142 example2_strcpy_safe(dst_filename, sizeof(dst_filename), out_filename);
143 } else {
144 unsigned int stripped_length = UINT32_MAX;
145 const char* ext_begin = strrchr(pSrc_filename, '.');
146 if (ext_begin) {
147 #ifdef _WIN32
148 const char* sep = strpbrk(ext_begin, "/\\");
149 #else
150 const char* sep = strpbrk(ext_begin, "/");
151 #endif
152 if (!sep)
153 stripped_length = ext_begin - pSrc_filename;
154 }
155 crnlib_snprintf(dst_filename, sizeof(dst_filename), "%-*s.dds", stripped_length, pSrc_filename);
156 }
157
158 printf("Writing DDS file: %s\n", dst_filename);
159
160 FILE* pDDS_file = NULL;
161 crn_fopen(&pDDS_file, dst_filename, "wb");
162 if (!pDDS_file) {
163 crnd::crnd_unpack_end(pContext);
164 free(pSrc_file_data);
165 return error("Failed creating destination file!\n");
166 }
167
168 // Write the 4-byte DDS signature (not endian safe, but whatever this is a sample).
169 fwrite(&crnlib::cDDSFileSignature, sizeof(crnlib::cDDSFileSignature), 1, pDDS_file);
170
171 // Prepare the DDS header.
172 crnlib::DDSURFACEDESC2 dds_desc;
173 memset(&dds_desc, 0, sizeof(dds_desc));
174 dds_desc.dwSize = sizeof(dds_desc);
175 dds_desc.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT | ((tex_info.m_levels > 1) ? DDSD_MIPMAPCOUNT : 0);
176 dds_desc.dwWidth = tex_info.m_width;
177 dds_desc.dwHeight = tex_info.m_height;
178 dds_desc.dwMipMapCount = (tex_info.m_levels > 1) ? tex_info.m_levels : 0;
179
180 dds_desc.ddpfPixelFormat.dwSize = sizeof(crnlib::DDPIXELFORMAT);
181 dds_desc.ddpfPixelFormat.dwFlags = DDPF_FOURCC;
182 crn_format fundamental_fmt = crnd::crnd_get_fundamental_dxt_format(tex_info.m_format);
183 dds_desc.ddpfPixelFormat.dwFourCC = crnd::crnd_crn_format_to_fourcc(fundamental_fmt);
184 if (fundamental_fmt != tex_info.m_format) {
185 // It's a funky swizzled DXTn format - write its FOURCC to dwRGBBitCount.
186 dds_desc.ddpfPixelFormat.dwRGBBitCount = crnd::crnd_crn_format_to_fourcc(tex_info.m_format);
187 }
188
189 dds_desc.ddsCaps.dwCaps = DDSCAPS_TEXTURE;
190 if (tex_info.m_levels > 1) {
191 dds_desc.ddsCaps.dwCaps |= (DDSCAPS_COMPLEX | DDSCAPS_MIPMAP);
192 }
193
194 if (tex_info.m_faces == 6) {
195 dds_desc.ddsCaps.dwCaps2 = DDSCAPS2_CUBEMAP |
196 DDSCAPS2_CUBEMAP_POSITIVEX | DDSCAPS2_CUBEMAP_NEGATIVEX | DDSCAPS2_CUBEMAP_POSITIVEY |
197 DDSCAPS2_CUBEMAP_NEGATIVEY | DDSCAPS2_CUBEMAP_POSITIVEZ | DDSCAPS2_CUBEMAP_NEGATIVEZ;
198 }
199
200 // Set pitch/linearsize field (some DDS readers require this field to be non-zero).
201 int bits_per_pixel = crnd::crnd_get_crn_format_bits_per_texel(tex_info.m_format);
202 dds_desc.lPitch = (((dds_desc.dwWidth + 3) & ~3) * ((dds_desc.dwHeight + 3) & ~3) * bits_per_pixel) >> 3;
203 dds_desc.dwFlags |= DDSD_LINEARSIZE;
204
205 // Write the DDS header to the output file.
206 fwrite(&dds_desc, sizeof(dds_desc), 1, pDDS_file);
207
208 // Now transcode all face and mipmap levels into memory, one mip level at a time.
209 void* pImages[cCRNMaxFaces][cCRNMaxLevels];
210 crn_uint32 image_size_in_bytes[cCRNMaxLevels];
211 memset(pImages, 0, sizeof(pImages));
212 memset(image_size_in_bytes, 0, sizeof(image_size_in_bytes));
213
214 crn_uint32 total_unpacked_texels = 0;
215
216 double total_unpack_time = 0.0f;
217 for (crn_uint32 level_index = 0; level_index < tex_info.m_levels; level_index++) {
218 // Compute the face's width, height, number of DXT blocks per row/col, etc.
219 const crn_uint32 width = std::max(1U, tex_info.m_width >> level_index);
220 const crn_uint32 height = std::max(1U, tex_info.m_height >> level_index);
221 const crn_uint32 blocks_x = std::max(1U, (width + 3) >> 2);
222 const crn_uint32 blocks_y = std::max(1U, (height + 3) >> 2);
223 const crn_uint32 row_pitch = blocks_x * crnd::crnd_get_bytes_per_dxt_block(tex_info.m_format);
224 const crn_uint32 total_face_size = row_pitch * blocks_y;
225
226 image_size_in_bytes[level_index] = total_face_size;
227
228 for (crn_uint32 face_index = 0; face_index < tex_info.m_faces; face_index++) {
229 void* p = malloc(total_face_size);
230 if (!p) {
231 for (crn_uint32 f = 0; f < cCRNMaxFaces; f++)
232 for (crn_uint32 l = 0; l < cCRNMaxLevels; l++)
233 free(pImages[f][l]);
234 crnd::crnd_unpack_end(pContext);
235 free(pSrc_file_data);
236 return error("Out of memory!");
237 }
238
239 pImages[face_index][level_index] = p;
240 }
241
242 // Prepare the face pointer array needed by crnd_unpack_level().
243 void* pDecomp_images[cCRNMaxFaces];
244 for (crn_uint32 face_index = 0; face_index < tex_info.m_faces; face_index++)
245 pDecomp_images[face_index] = pImages[face_index][level_index];
246
247 // Now transcode the level to raw DXTn
248 tm.start();
249 if (!crnd::crnd_unpack_level(pContext, pDecomp_images, total_face_size, row_pitch, level_index)) {
250 for (crn_uint32 f = 0; f < cCRNMaxFaces; f++)
251 for (crn_uint32 l = 0; l < cCRNMaxLevels; l++)
252 free(pImages[f][l]);
253
254 crnd::crnd_unpack_end(pContext);
255 free(pSrc_file_data);
256
257 return error("Failed transcoding texture!");
258 }
259
260 total_unpack_time += tm.get_elapsed_ms();
261 total_unpacked_texels += (blocks_x * blocks_y * 16);
262 }
263
264 printf("crnd_unpack_begin time: %3.3fms\n", total_unpack_begin_time);
265 printf("Total crnd_unpack_level time: %3.3fms\n", total_unpack_time);
266 double total_time = total_unpack_begin_time + total_unpack_time;
267 printf("Total transcode time: %3.3fms\n", total_time);
268 printf("Total texels transcoded: %u\n", total_unpacked_texels);
269 printf("Overall transcode throughput: %3.3f million texels/sec\n", (total_unpacked_texels / (total_time / 1000.0f)) / 1000000.0f);
270
271 // Now write the DXTn data to the DDS file in face-major order.
272 for (crn_uint32 face_index = 0; face_index < tex_info.m_faces; face_index++)
273 for (crn_uint32 level_index = 0; level_index < tex_info.m_levels; level_index++)
274 fwrite(pImages[face_index][level_index], image_size_in_bytes[level_index], 1, pDDS_file);
275
276 for (crn_uint32 f = 0; f < cCRNMaxFaces; f++)
277 for (crn_uint32 l = 0; l < cCRNMaxLevels; l++)
278 free(pImages[f][l]);
279
280 crnd::crnd_unpack_end(pContext);
281 free(pSrc_file_data);
282
283 if (fclose(pDDS_file) == EOF) {
284 return error("Failed writing to DDS file!\n");
285 }
286
287 return EXIT_SUCCESS;
288 }