resource_format_image.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*************************************************************************/
  2. /* resource_format_image.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 "resource_format_image.h"
  30. #include "scene/resources/texture.h"
  31. #include "io/image_loader.h"
  32. #include "globals.h"
  33. #include "os/os.h"
  34. RES ResourceFormatLoaderImage::load(const String &p_path,const String& p_original_path) {
  35. if (p_path.extension()=="cube") {
  36. // open as cubemap txture
  37. CubeMap* ptr = memnew(CubeMap);
  38. Ref<CubeMap> cubemap( ptr );
  39. Error err;
  40. FileAccess *f = FileAccess::open(p_path,FileAccess::READ,&err);
  41. if (err) {
  42. ERR_FAIL_COND_V( err, RES() );
  43. }
  44. String base_path=p_path.substr( 0, p_path.find_last("/")+1 );
  45. for(int i=0;i<6;i++) {
  46. String file = f->get_line().strip_edges();
  47. Image image;
  48. Error err = ImageLoader::load_image(base_path+file,&image);
  49. if (err) {
  50. memdelete(f);
  51. ERR_FAIL_COND_V( err, RES() );
  52. }
  53. if (i==0) {
  54. //cubemap->create(image.get_width(),image.get_height(),image.get_format(),Texture::FLAGS_DEFAULT|Texture::FLAG_CUBEMAP);
  55. }
  56. static const CubeMap::Side cube_side[6]= {
  57. CubeMap::SIDE_LEFT,
  58. CubeMap::SIDE_RIGHT,
  59. CubeMap::SIDE_BOTTOM,
  60. CubeMap::SIDE_TOP,
  61. CubeMap::SIDE_FRONT,
  62. CubeMap::SIDE_BACK
  63. };
  64. cubemap->set_side(cube_side[i],image);
  65. }
  66. memdelete(f);
  67. cubemap->set_name(p_path.get_file());
  68. return cubemap;
  69. } else {
  70. // simple image
  71. ImageTexture* ptr = memnew(ImageTexture);
  72. Ref<ImageTexture> texture( ptr );
  73. uint64_t begtime;
  74. double total;
  75. Image image;
  76. if (debug_load_times)
  77. begtime=OS::get_singleton()->get_ticks_usec();
  78. Error err = ImageLoader::load_image(p_path,&image);
  79. if (!err && debug_load_times) {
  80. double total=(double)(OS::get_singleton()->get_ticks_usec()-begtime)/1000000.0;
  81. print_line("IMAGE: "+itos(image.get_width())+"x"+itos(image.get_height()));
  82. print_line(" -load: "+rtos(total));
  83. }
  84. ERR_EXPLAIN("Failed loading image: "+p_path);
  85. ERR_FAIL_COND_V(err, RES());
  86. #ifdef DEBUG_ENABLED
  87. #ifdef TOOLS_ENABLED
  88. if (max_texture_size && (image.get_width() > max_texture_size || image.get_height() > max_texture_size)) {
  89. if (bool(Globals::get_singleton()->get("debug/max_texture_size_alert"))) {
  90. OS::get_singleton()->alert("Texture is too large: '"+p_path+"', at "+itos(image.get_width())+"x"+itos(image.get_height())+". Max allowed size is: "+itos(max_texture_size)+"x"+itos(max_texture_size)+".","BAD ARTIST, NO COOKIE!");
  91. }
  92. ERR_EXPLAIN("Texture is too large: '"+p_path+"', at "+itos(image.get_width())+"x"+itos(image.get_height())+". Max allowed size is: "+itos(max_texture_size)+"x"+itos(max_texture_size)+".");
  93. ERR_FAIL_V(RES());
  94. }
  95. #endif
  96. #endif
  97. uint32_t flags=0;
  98. if (bool(GLOBAL_DEF("texture_import/filter",true)))
  99. flags|=Texture::FLAG_FILTER;
  100. if (bool(GLOBAL_DEF("texture_import/gen_mipmaps",true)))
  101. flags|=Texture::FLAG_MIPMAPS;
  102. if (bool(GLOBAL_DEF("texture_import/repeat",false)))
  103. flags|=Texture::FLAG_REPEAT;
  104. if (debug_load_times)
  105. begtime=OS::get_singleton()->get_ticks_usec();
  106. //print_line("img: "+p_path+" flags: "+itos(flags));
  107. texture->create_from_image( image,flags );
  108. texture->set_name(p_path.get_file());
  109. if (debug_load_times) {
  110. total=(double)(OS::get_singleton()->get_ticks_usec()-begtime)/1000000.0;
  111. print_line(" -make texture: "+rtos(total));
  112. }
  113. return RES( texture );
  114. }
  115. }
  116. bool ResourceFormatLoaderImage::handles_type(const String& p_type) const {
  117. return ObjectTypeDB::is_type(p_type,"Texture") || ObjectTypeDB::is_type(p_type,"CubeMap");
  118. }
  119. void ResourceFormatLoaderImage::get_recognized_extensions(List<String> *p_extensions) const {
  120. ImageLoader::get_recognized_extensions(p_extensions);
  121. p_extensions->push_back("cube");
  122. }
  123. String ResourceFormatLoaderImage::get_resource_type(const String &p_path) const {
  124. String ext=p_path.extension().to_lower();
  125. if (ext=="cube")
  126. return "CubeMap";
  127. List<String> extensions;
  128. ImageLoader::get_recognized_extensions(&extensions);
  129. for(List<String>::Element *E=extensions.front();E;E=E->next()) {
  130. if (E->get()==ext)
  131. return "ImageTexture";
  132. }
  133. return "";
  134. }
  135. ResourceFormatLoaderImage::ResourceFormatLoaderImage() {
  136. max_texture_size = GLOBAL_DEF("debug/max_texture_size",0);
  137. GLOBAL_DEF("debug/max_texture_size_alert",false);
  138. debug_load_times=GLOBAL_DEF("debug/image_load_times",false);
  139. GLOBAL_DEF("texture_import/filter",true);
  140. GLOBAL_DEF("texture_import/gen_mipmaps",true);
  141. GLOBAL_DEF("texture_import/repeat",true);
  142. }