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