pvrtc_compress.cpp 4.9 KB

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