texture_loader_pkm.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*************************************************************************/
  2. /* texture_loader_pkm.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 "texture_loader_pkm.h"
  31. #include "core/os/file_access.h"
  32. #include <string.h>
  33. struct ETC1Header {
  34. char tag[6]; // "PKM 10"
  35. uint16_t format; // Format == number of mips (== zero)
  36. uint16_t texWidth; // Texture dimensions, multiple of 4 (big-endian)
  37. uint16_t texHeight;
  38. uint16_t origWidth; // Original dimensions (big-endian)
  39. uint16_t origHeight;
  40. };
  41. RES ResourceFormatPKM::load(const String &p_path, const String &p_original_path, Error *r_error) {
  42. if (r_error)
  43. *r_error = ERR_CANT_OPEN;
  44. Error err;
  45. FileAccess *f = FileAccess::open(p_path, FileAccess::READ, &err);
  46. if (!f)
  47. return RES();
  48. FileAccessRef fref(f);
  49. if (r_error)
  50. *r_error = ERR_FILE_CORRUPT;
  51. ERR_EXPLAIN("Unable to open PKM texture file: " + p_path);
  52. ERR_FAIL_COND_V(err != OK, RES());
  53. // big endian
  54. f->set_endian_swap(true);
  55. ETC1Header h;
  56. ERR_EXPLAIN("Invalid or Unsupported PKM texture file: " + p_path);
  57. f->get_buffer((uint8_t *)&h.tag, sizeof(h.tag));
  58. if (strncmp(h.tag, "PKM 10", sizeof(h.tag)))
  59. ERR_FAIL_V(RES());
  60. h.format = f->get_16();
  61. h.texWidth = f->get_16();
  62. h.texHeight = f->get_16();
  63. h.origWidth = f->get_16();
  64. h.origHeight = f->get_16();
  65. PoolVector<uint8_t> src_data;
  66. uint32_t size = h.texWidth * h.texHeight / 2;
  67. src_data.resize(size);
  68. PoolVector<uint8_t>::Write wb = src_data.write();
  69. f->get_buffer(wb.ptr(), size);
  70. wb = PoolVector<uint8_t>::Write();
  71. int mipmaps = h.format;
  72. int width = h.origWidth;
  73. int height = h.origHeight;
  74. Ref<Image> img = memnew(Image(width, height, mipmaps, Image::FORMAT_ETC, src_data));
  75. Ref<ImageTexture> texture = memnew(ImageTexture);
  76. texture->create_from_image(img);
  77. if (r_error)
  78. *r_error = OK;
  79. return texture;
  80. }
  81. void ResourceFormatPKM::get_recognized_extensions(List<String> *p_extensions) const {
  82. p_extensions->push_back("pkm");
  83. }
  84. bool ResourceFormatPKM::handles_type(const String &p_type) const {
  85. return ClassDB::is_parent_class(p_type, "Texture");
  86. }
  87. String ResourceFormatPKM::get_resource_type(const String &p_path) const {
  88. if (p_path.get_extension().to_lower() == "pkm")
  89. return "ImageTexture";
  90. return "";
  91. }