d5012791fea0192b11988ece056d3614a3328ca6
[convexer.git] / nbvtf / vtf_cmd.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdint.h>
4 #include <math.h>
5
6 #define STB_IMAGE_IMPLEMENTATION
7 #define NBVTF_SHOW_STDERR
8 #include "nbvtf.h"
9
10 // Find file path extension, returns NULL if no ext (0x00)
11 char *str_findext( char *szPath, char const delim )
12 {
13 char *c, *ptr ;
14
15 c = szPath;
16 ptr = NULL;
17
18 while( *c )
19 {
20 if( *c == delim )
21 {
22 ptr = c + 1;
23 }
24
25 c ++;
26 }
27
28 return ptr;
29 }
30
31 // gets rid of extension on string only left with folder/filename
32 void path_stripext( char *szPath )
33 {
34 char *point, *start;
35
36 // Skip folders
37 if( !(start = str_findext( szPath, '/' )) )
38 {
39 start = szPath;
40 }
41
42 if( (point = str_findext( start, '.' )) )
43 {
44 if( point > szPath )
45 {
46 *(point-1) = 0x00;
47 }
48 }
49 }
50
51 EImageFormat_t format_from_str( const char *str )
52 {
53 if( !strcmp( str, "dxt1" ) )
54 return k_EImageFormat_DXT1;
55 if( !strcmp( str, "dxt5" ) )
56 return k_EImageFormat_DXT5;
57 if( !strcmp( str, "rgb8" ) )
58 return k_EImageFormat_BGR888;
59 if( !strcmp( str, "rgba8" ) )
60 return k_EImageFormat_ABGR8888;
61
62 return k_EImageFormat_NONE;
63 }
64
65 EImageFormat_t format_in_path( const char *path )
66 {
67 char filepath[ 512 ];
68
69 strcpy( filepath, path );
70 path_stripext( filepath );
71
72 char *format_str = str_findext( filepath, '.' );
73
74 if( format_str )
75 {
76 EImageFormat_t fmt;
77 fmt = format_from_str( format_str );
78
79 if( fmt != -1 )
80 {
81 return fmt;
82 }
83 }
84
85 return k_EImageFormat_DXT1;
86 }
87
88 void auto_output( const char *path, char *dest )
89 {
90 strcpy( dest, path );
91 path_stripext( dest );
92 strcat( dest, ".vtf" );
93 }
94
95 int main( int argc, char *argv[] )
96 {
97 char dest[500];
98
99 char *path_source;
100 EImageFormat_t format = k_EImageFormat_NONE;
101
102 if( argc < 2 )
103 {
104 printf( "Usage: vmt_cmd <optional_format> input_file<.format_in_path>.png\nSupported Formats:\n\trgb8, rgba8, dxt1, dxt5\n" );
105 return 0;
106 }
107
108 if( argc == 3 )
109 {
110 path_source = argv[2];
111 format = format_from_str( argv[1] );
112 }
113
114 if( argc == 2 )
115 {
116 path_source = argv[1];
117 format = format_in_path( path_source );
118 }
119
120 if( format == -1 )
121 {
122 fprintf( stderr, "tovtf: error with format choice. Unsupported\n" );
123 return 0;
124 }
125
126 printf( "tovtf: Creating vtf with format '%s'\n", vtf_format_strings[ format ] );
127
128 auto_output( path_source, dest );
129 nbvtf_init();
130 nbvtf_convert( path_source, 0, 0, 1, format, 16, 0x00, dest );
131
132 return 0;
133 }