texture_from_file.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. string command = STR("/opt/local/bin/convert "<<filename<<" "<<tmp);
  33. try
  34. {
  35. if(system(command.c_str())==0)
  36. {
  37. return texture_from_file(tmp.c_str(),id);
  38. }else
  39. {
  40. cerr<<"texture_from_file: calling to convert ('"<<command<<"') failed..."<<endl;
  41. return false;
  42. }
  43. }catch(int e)
  44. {
  45. cerr<<"^"<<__FUNCTION__<<": Calling to convert crashed..."<<endl;
  46. return false;
  47. }
  48. #else
  49. return false;
  50. #endif
  51. }
  52. }
  53. #endif