texture_from_file.cpp 1.7 KB

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