| 1 | /**************************************************************************/ |
| 2 | /* image_decompress_squish.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 | |
| 31 | #include "image_decompress_squish.h" |
| 32 | |
| 33 | #include <squish.h> |
| 34 | |
| 35 | void image_decompress_squish(Image *p_image) { |
| 36 | int w = p_image->get_width(); |
| 37 | int h = p_image->get_height(); |
| 38 | |
| 39 | Image::Format target_format = Image::FORMAT_RGBA8; |
| 40 | Vector<uint8_t> data; |
| 41 | int target_size = Image::get_image_data_size(w, h, target_format, p_image->has_mipmaps()); |
| 42 | int mm_count = p_image->get_mipmap_count(); |
| 43 | data.resize(target_size); |
| 44 | |
| 45 | const uint8_t *rb = p_image->get_data().ptr(); |
| 46 | uint8_t *wb = data.ptrw(); |
| 47 | |
| 48 | int squish_flags = Image::FORMAT_MAX; |
| 49 | if (p_image->get_format() == Image::FORMAT_DXT1) { |
| 50 | squish_flags = squish::kDxt1; |
| 51 | } else if (p_image->get_format() == Image::FORMAT_DXT3) { |
| 52 | squish_flags = squish::kDxt3; |
| 53 | } else if (p_image->get_format() == Image::FORMAT_DXT5 || p_image->get_format() == Image::FORMAT_DXT5_RA_AS_RG) { |
| 54 | squish_flags = squish::kDxt5; |
| 55 | } else if (p_image->get_format() == Image::FORMAT_RGTC_R) { |
| 56 | squish_flags = squish::kBc4; |
| 57 | } else if (p_image->get_format() == Image::FORMAT_RGTC_RG) { |
| 58 | squish_flags = squish::kBc5; |
| 59 | } else { |
| 60 | ERR_FAIL_MSG("Squish: Can't decompress unknown format: " + itos(p_image->get_format()) + "." ); |
| 61 | } |
| 62 | |
| 63 | for (int i = 0; i <= mm_count; i++) { |
| 64 | int src_ofs = 0, mipmap_size = 0, mipmap_w = 0, mipmap_h = 0; |
| 65 | p_image->get_mipmap_offset_size_and_dimensions(i, src_ofs, mipmap_size, mipmap_w, mipmap_h); |
| 66 | int dst_ofs = Image::get_image_mipmap_offset(p_image->get_width(), p_image->get_height(), target_format, i); |
| 67 | squish::DecompressImage(&wb[dst_ofs], w, h, &rb[src_ofs], squish_flags); |
| 68 | w >>= 1; |
| 69 | h >>= 1; |
| 70 | } |
| 71 | |
| 72 | p_image->set_data(p_image->get_width(), p_image->get_height(), p_image->has_mipmaps(), target_format, data); |
| 73 | |
| 74 | if (p_image->get_format() == Image::FORMAT_DXT5_RA_AS_RG) { |
| 75 | p_image->convert_ra_rgba8_to_rg(); |
| 76 | } |
| 77 | } |
| 78 | |