Major API revision
[convexer.git] / nbvtf / vtf_cmd.c
diff --git a/nbvtf/vtf_cmd.c b/nbvtf/vtf_cmd.c
new file mode 100644 (file)
index 0000000..1fc5536
--- /dev/null
@@ -0,0 +1,132 @@
+#include <stdio.h>
+#include <string.h>
+#include <stdint.h>
+#include <math.h>
+
+#define STB_IMAGE_IMPLEMENTATION
+#define NBVTF_SHOW_STDERR
+#include "nbvtf.h"
+
+// Find file path extension, returns NULL if no ext (0x00)
+char *str_findext( char *szPath, char const delim )
+{
+       char *c, *ptr ;
+
+       c = szPath;
+       ptr = NULL;
+       
+       while( *c )
+       {
+               if( *c == delim )
+               {
+                       ptr = c + 1;
+               }
+       
+               c ++;
+       }
+
+       return ptr;
+}
+
+// gets rid of extension on string only left with folder/filename
+void path_stripext( char *szPath )
+{
+       char *point, *start;
+       
+       // Skip folders
+       if( !(start = str_findext( szPath, '/' )) )
+       {
+               start = szPath;
+       }
+       
+       if( (point = str_findext( start, '.' )) )
+       {
+               if( point > szPath )
+               {
+                       *(point-1) = 0x00;
+               }
+       }
+}
+
+EImageFormat_t format_from_str( const char *str )
+{
+       if( !strcmp( str, "dxt1" ) )
+               return k_EImageFormat_DXT1;
+       if( !strcmp( str, "dxt5" ) )
+               return k_EImageFormat_DXT5;
+       if( !strcmp( str, "rgb8" ) )
+               return k_EImageFormat_BGR888;
+       if( !strcmp( str, "rgba8" ) )
+               return k_EImageFormat_ABGR8888;
+       
+       return k_EImageFormat_NONE;
+}
+
+EImageFormat_t format_in_path( const char *path )
+{
+       char filepath[ 512 ];
+       
+       strcpy( filepath, path );
+       path_stripext( filepath );
+       
+       char *format_str = str_findext( filepath, '.' );
+       
+       if( format_str )
+       {
+               EImageFormat_t fmt;
+               fmt = format_from_str( format_str );
+               
+               if( fmt != -1 )
+               {
+                       return fmt;
+               }
+       }
+       
+       return k_EImageFormat_DXT1;
+}
+
+void auto_output( const char *path, char *dest )
+{
+       strcpy( dest, path );
+       path_stripext( dest );
+       strcat( dest, ".vtf" );
+}
+
+int main( int argc, char *argv[] )
+{
+       char dest[500];
+       
+       char *path_source;
+       EImageFormat_t format = k_EImageFormat_NONE;
+
+       if( argc < 2 )
+       {
+               printf( "Usage: vmt_cmd <optional_format> input_file<.format_in_path>.png\nSupported Formats:\n\trgb8, rgba8, dxt1, dxt5\n" );
+               return 0;
+       }
+
+       if( argc == 3 )
+       {
+               path_source = argv[2];
+               format = format_from_str( argv[1] );
+       }
+       
+       if( argc == 2 )
+       {
+               path_source = argv[1];
+               format = format_in_path( path_source );
+       }
+       
+       if( format == -1 )
+       {
+               fprintf( stderr, "tovtf: error with format choice. Unsupported\n" );
+               return 0;
+       }
+       
+       printf( "tovtf: Creating vtf with format '%s'\n", vtf_format_strings[ format ] );
+       
+       auto_output( path_source, dest );
+       nbvtf_convert( path_source, 0, 0, 1, format, 0x00, dest );
+
+       return 0;
+}