pvrtc_compress.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*************************************************************************/
  2. /* pvrtc_compress.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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 "core/io/resource_loader.h"
  32. #include "core/io/resource_saver.h"
  33. #include "core/os/file_access.h"
  34. #include "core/os/os.h"
  35. #include "editor_settings.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("filesystem/import/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 tmppath = EditorSettings::get_singleton()->get_cache_dir();
  58. List<String> args;
  59. String src_img = tmppath.plus_file("_tmp_src_img.png");
  60. String dst_img = tmppath.plus_file("_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("filesystem/import/pvrtc_fast_conversion").operator bool()) {
  73. args.push_back("-pvrtcfast");
  74. }
  75. if (p_image->has_mipmaps())
  76. args.push_back("-m");
  77. Ref<ImageTexture> t = memnew(ImageTexture);
  78. t->create_from_image(Ref<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->copy_internals_from(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. void _pvrtc_register_compressors() {
  95. _base_image_compress_pvrtc2_func = Image::_image_compress_pvrtc2_func;
  96. _base_image_compress_pvrtc4_func = Image::_image_compress_pvrtc4_func;
  97. Image::_image_compress_pvrtc2_func = _compress_pvrtc2;
  98. Image::_image_compress_pvrtc4_func = _compress_pvrtc4;
  99. }