texture_from_file.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. // This file is part of libigl, a simple c++ geometry processing library.
  2. //
  3. // Copyright (C) 2014 Alec Jacobson <alecjacobson@gmail.com>
  4. //
  5. // This Source Code Form is subject to the terms of the Mozilla Public License
  6. // v. 2.0. If a copy of the MPL was not distributed with this file, You can
  7. // obtain one at http://mozilla.org/MPL/2.0/.
  8. #include "texture_from_file.h"
  9. #include "texture_from_png.h"
  10. #include "../STR.h"
  11. #include "../pathinfo.h"
  12. #include "../opengl/report_gl_error.h"
  13. #include "../opengl/texture_from_tga.h"
  14. #include <string>
  15. #include <algorithm>
  16. #include <iostream>
  17. IGL_INLINE bool igl::png::texture_from_file(const std::string filename, GLuint & id)
  18. {
  19. using namespace igl::opengl;
  20. using namespace std;
  21. // dirname, basename, extension and filename
  22. string d,b,ext,f;
  23. pathinfo(filename,d,b,ext,f);
  24. // Convert extension to lower case
  25. transform(ext.begin(), ext.end(), ext.begin(), ::tolower);
  26. if(ext == "tga")
  27. {
  28. return texture_from_tga(filename,id);
  29. }else if(ext == "png")
  30. {
  31. return texture_from_png(filename,id);
  32. }else
  33. {
  34. #ifdef __APPLE__
  35. // Convert to a temporary png file
  36. string tmp = "/var/tmp/.texture_from_file.png";
  37. #define PATH_TO_CONVERT "/opt/local/bin/convert"
  38. string command = STR(PATH_TO_CONVERT<<" \""<<filename<<"\" \""<<tmp<<"\"");
  39. try
  40. {
  41. if(system(command.c_str())==0)
  42. {
  43. return texture_from_file(tmp.c_str(),id);
  44. }else
  45. {
  46. cerr<<"texture_from_file: calling to convert ('"<<command<<"') failed..."<<endl;
  47. return false;
  48. }
  49. }catch(int e)
  50. {
  51. cerr<<"^"<<__FUNCTION__<<": Calling to convert crashed..."<<endl;
  52. return false;
  53. }
  54. #else
  55. return false;
  56. #endif
  57. }
  58. }