image_compress_pvrtc.cpp 4.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /**************************************************************************/
  2. /* image_compress_pvrtc.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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 "image_compress_pvrtc.h"
  31. #include "core/image.h"
  32. #include "core/reference.h"
  33. #include <PvrTcEncoder.h>
  34. #include <RgbaBitmap.h>
  35. static void _compress_pvrtc4(Image *p_img) {
  36. Ref<Image> img = p_img->duplicate();
  37. bool make_mipmaps = false;
  38. if (!img->is_size_po2() || img->get_width() != img->get_height()) {
  39. make_mipmaps = img->has_mipmaps();
  40. img->resize_to_po2(true);
  41. // Resizing can fail for some formats
  42. if (!img->is_size_po2() || img->get_width() != img->get_height()) {
  43. ERR_FAIL_MSG("Failed to resize the image for compression.");
  44. }
  45. }
  46. img->convert(Image::FORMAT_RGBA8);
  47. if (!img->has_mipmaps() && make_mipmaps) {
  48. img->generate_mipmaps();
  49. }
  50. bool use_alpha = img->detect_alpha();
  51. Ref<Image> new_img;
  52. new_img.instance();
  53. new_img->create(img->get_width(), img->get_height(), img->has_mipmaps(), use_alpha ? Image::FORMAT_PVRTC4A : Image::FORMAT_PVRTC4);
  54. PoolVector<uint8_t> data = new_img->get_data();
  55. {
  56. PoolVector<uint8_t>::Write wr = data.write();
  57. PoolVector<uint8_t>::Read r = img->get_data().read();
  58. for (int i = 0; i <= new_img->get_mipmap_count(); i++) {
  59. int ofs, size, w, h;
  60. img->get_mipmap_offset_size_and_dimensions(i, ofs, size, w, h);
  61. Javelin::RgbaBitmap bm(w, h);
  62. void *dst = (void *)bm.GetData();
  63. memcpy(dst, &r[ofs], size);
  64. Javelin::ColorRgba<unsigned char> *dp = bm.GetData();
  65. for (int j = 0; j < size / 4; j++) {
  66. // Red and blue colors are swapped.
  67. SWAP(dp[j].r, dp[j].b);
  68. }
  69. new_img->get_mipmap_offset_size_and_dimensions(i, ofs, size, w, h);
  70. Javelin::PvrTcEncoder::EncodeRgba4Bpp(&wr[ofs], bm);
  71. }
  72. }
  73. p_img->create(new_img->get_width(), new_img->get_height(), new_img->has_mipmaps(), new_img->get_format(), data);
  74. }
  75. void _register_pvrtc_compress_func() {
  76. // FIXME: We claim to support PVRTC2 but use the same method as for PVRTC4.
  77. Image::_image_compress_pvrtc2_func = _compress_pvrtc4;
  78. Image::_image_compress_pvrtc4_func = _compress_pvrtc4;
  79. }