Re: [Plib-devel] Converting unsupported texture image file formats on the fly
Brought to you by:
sjbaker
From: John F. F. <joh...@cy...> - 2007-10-02 01:41:58
|
Done ... see change set 2127. - John -----Original Message----- From: Paolo Leoncini Sent: Monday, October 01, 2007 7:54 AM To: 'PLIB Developers' Subject: [Plib-devel] Converting unsupported texture image file formats on the fly Dear friends, In ssgLoadTexture.cxx the function bool ssgConvertTexture( char * fname_output, const char * fname_input ) is devoted to the subject task. As it is now it has two problems which I propose here to overcome. Line 248 SVN: if ( ulFileExists ( fname_output ) ) return true; // found *.rgb-file fname_output is the .rgb version of the fname_input file name with unsupported image ext. (e.g. .jpg). The current code says that if the .rgb version of the input file name does exist no conversion is needed - but it could be older than that. Thus a test on the two files' modification time is also needed. Line 258 SVN: 258 // ****** found original file. convert it. ****** 259 #ifdef UL_WIN32 260 char command [ 1024 ] ; 261 sprintf(command, "imconvert -verbose %s sgi:%s", fname_input, fname_output); 262 unsigned int ui = WinExec(command, SW_HIDE ); 263 if ( ui < 32 ) 264 { ulSetError(UL_WARNING, "Couldn't convert texture '%s'. Did you install ImageMagick?" 265 " You may also convert it manually to '%s' and reload the model.", 266 fname_input, fname_output); 267 return false; 268 } 269 #else 270 ulSetError(UL_WARNING, "Converting textures not yet implemented under Linux." 271 "You may convert '%s' manually to '%s' and reload the model.", 272 fname_input, fname_output); 273 //sprintf(command, "-verbose %s sgi:%s", fname_input, fname_output); 274 //execlp ( "convert", "convert", command, NULL ) ; 275 276 #endif 277 return true; 278 } Under UL_WIN32 the WinExec function spawn a separate process and allows the calling program to continue, so that the latter could proceed using the (supposedly converted) image file w/o waiting for it to be actually generated. Furthermore it is not supported elsewhere than under Win32. I propose to use the simpler and widely supported system() function, which is blocking, and to test the existance of the output file as success conformation. The modified code for fixing both problems in ssgLoadTexture.cxx is reported in the following. #include <sys/stat.h> int fileMTimeCmp( const char *fname_input, const char *fname_output ) // Compare the two input file names against their modification time // -1: fname_input newer than fname_output // 1: fname_input older than fname_output // 0: stat error { struct _stat buffer_in, buffer_out; #ifdef UL_WIN32 #define stat _stat #endif if ( stat( fname_input, &buffer_in ) == 0 && stat( fname_output, &buffer_out ) == 0 ) return buffer_in.st_mtime > buffer_out.st_mtime ? -1 : 1; else return 0; } bool ssgConvertTexture( char * fname_output, const char * fname_input ) // converts file to .rgb (Silicon Graphics) format // returns true if the file has been converted to rgb, or already exists as rgb // if it returns false, then it has already output an error message { char *extension ; strcpy( fname_output, fname_input); // copy so that a) there is enough room for .rgb and b) we don't change the buffer of fname_input extension = strrchr(fname_output, '.'); if ( extension == NULL ) { ulSetError(UL_WARNING, "There is no extension in the texture '%s'.", fname_input); return false; // no extension -> give up } extension[1] = 'r'; extension[2] = 'g'; extension[3] = 'b'; extension[4] = 0; // look for original, non-rgb - file if ( !ulFileExists ( fname_input ) ) { ulSetError(UL_WARNING, "Can't find the texture file '%s'.", fname_input); return false; // no input file => we can't convert it } if ( ulFileExists ( fname_output ) && fileMTimeCmp( fname_input, fname_output ) != -1 ) return true; // found *.rgb-file, and it's newer than fname_input, so no conversion is needed // ****** found original file. convert it. ****** char command [ 1024 ] ; char *conv_cmd = #ifdef UL_WIN32 "imconvert"; #else "convert"; #endif sprintf(command, "%s -verbose %s sgi:%s", conv_cmd, fname_input, fname_output); if ( system( command ) < 0 || !ulFileExists ( fname_output ) ) { ulSetError(UL_WARNING, "Couldn't convert texture '%s'. Did you install ImageMagick?" " You may also convert it manually to '%s' and reload the model.", fname_input, fname_output); return false; } return true; } fileMTimeCmp could, of course, become an additional ul file utility (other OS-es please check the stat compatibility on non Win32 systems, but the Win32 _stat should be POSIX-compliant). Paolo Leoncini ------------------------------------------------------------------------- This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2005. http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/ _______________________________________________ plib-devel mailing list pli...@li... https://lists.sourceforge.net/lists/listinfo/plib-devel |