pvrtc_compress.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*************************************************************************/
  2. /* pvrtc_compress.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #include "pvrtc_compress.h"
  30. #include "editor_settings.h"
  31. #include "scene/resources/texture.h"
  32. #include "io/resource_saver.h"
  33. #include "io/resource_loader.h"
  34. #include "os/os.h"
  35. static void _compress_image(Image::CompressMode p_mode,Image *p_image) {
  36. String ttpath = EditorSettings::get_singleton()->get("PVRTC/texture_tool");
  37. String spath = EditorSettings::get_singleton()->get_settings_path();
  38. List<String> args;
  39. String src_img = spath+"/"+"_tmp_src_img.png";
  40. String dst_img = spath+"/"+"_tmp_dst_img.pvr";
  41. args.push_back("-i");
  42. args.push_back(src_img);
  43. args.push_back("-o");
  44. args.push_back(dst_img);
  45. args.push_back("-f");
  46. switch(p_mode) {
  47. case Image::COMPRESS_PVRTC2: args.push_back("PVRTC2"); break;
  48. case Image::COMPRESS_PVRTC4: args.push_back("PVRTC4"); break;
  49. case Image::COMPRESS_ETC: args.push_back("ETC"); break;
  50. default: ERR_FAIL();
  51. }
  52. if (EditorSettings::get_singleton()->get("PVRTC/fast_conversion").operator bool()) {
  53. args.push_back("-pvrtcfast");
  54. }
  55. if (p_image->get_mipmaps()>0)
  56. args.push_back("-m");
  57. Ref<ImageTexture> t = memnew( ImageTexture );
  58. t->create_from_image(*p_image,0);
  59. ResourceSaver::save(src_img,t);
  60. Error err = OS::get_singleton()->execute(ttpath,args,true);
  61. ERR_EXPLAIN("Could not execute PVRTC Tool: "+ttpath);
  62. ERR_FAIL_COND(err!=OK);
  63. t=ResourceLoader::load(dst_img,"Texture");
  64. ERR_EXPLAIN("Can't load back converted image using PVRTC Tool: "+dst_img);
  65. ERR_FAIL_COND(t.is_null());
  66. *p_image=t->get_data();
  67. }
  68. static void _compress_pvrtc2(Image *p_image) {
  69. _compress_image(Image::COMPRESS_PVRTC2,p_image);
  70. }
  71. static void _compress_pvrtc4(Image *p_image) {
  72. _compress_image(Image::COMPRESS_PVRTC4,p_image);
  73. }
  74. static void _compress_etc(Image *p_image) {
  75. _compress_image(Image::COMPRESS_ETC,p_image);
  76. }
  77. void _pvrtc_register_compressors() {
  78. Image::_image_compress_pvrtc2_func=_compress_pvrtc2;
  79. Image::_image_compress_pvrtc4_func=_compress_pvrtc4;
  80. //Image::_image_compress_etc_func=_compress_etc; //use the built in one for ETC
  81. }