texture_from_file.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include "texture_from_file.h"
  2. #ifndef IGL_NO_OPENGL
  3. #include "texture_from_png.h"
  4. #include <igl/report_gl_error.h>
  5. #include <igl/STR.h>
  6. #include <igl/pathinfo.h>
  7. #include <igl/texture_from_tga.h>
  8. #include <YImage.hpp>
  9. #include <string>
  10. #include <algorithm>
  11. #include <iostream>
  12. IGL_INLINE bool igl::texture_from_file(const std::string filename, GLuint & id)
  13. {
  14. using namespace igl;
  15. using namespace std;
  16. // dirname, basename, extension and filename
  17. string d,b,ext,f;
  18. pathinfo(filename,d,b,ext,f);
  19. // Convert extension to lower case
  20. transform(ext.begin(), ext.end(), ext.begin(), ::tolower);
  21. if(ext == "tga")
  22. {
  23. return texture_from_tga(filename,id);
  24. }else if(ext == "png")
  25. {
  26. return texture_from_png(filename,id);
  27. }else
  28. {
  29. #ifdef __APPLE__
  30. // Convert to a temporary png file
  31. string tmp = "/var/tmp/.texture_from_file.png";
  32. #define PATH_TO_CONVERT "/opt/local/bin/convert"
  33. string command = STR(PATH_TO_CONVERT<<" \""<<filename<<"\" \""<<tmp<<"\"");
  34. try
  35. {
  36. if(system(command.c_str())==0)
  37. {
  38. return texture_from_file(tmp.c_str(),id);
  39. }else
  40. {
  41. cerr<<"texture_from_file: calling to convert ('"<<command<<"') failed..."<<endl;
  42. return false;
  43. }
  44. }catch(int e)
  45. {
  46. cerr<<"^"<<__FUNCTION__<<": Calling to convert crashed..."<<endl;
  47. return false;
  48. }
  49. #else
  50. return false;
  51. #endif
  52. }
  53. }
  54. #endif