Generate mipmap levels from RGBA source using stb_image_resize2
| 43 | |
| 44 | // Generate mipmap levels from RGBA source using stb_image_resize2 |
| 45 | static std::vector<Image> generateMipmaps(const Image& rgba, int maxLevels) |
| 46 | { |
| 47 | std::vector<Image> levels; |
| 48 | levels.push_back(rgba); |
| 49 | |
| 50 | int w = rgba.width(); |
| 51 | int h = rgba.height(); |
| 52 | |
| 53 | for (int level = 1; level < maxLevels && (w > 1 || h > 1); ++level) |
| 54 | { |
| 55 | int nw = std::max(w / 2, 1); |
| 56 | int nh = std::max(h / 2, 1); |
| 57 | |
| 58 | std::vector<uint8_t> resized(nw * nh * 4); |
| 59 | stbir_resize_uint8_linear(levels.back().data().data(), w, h, w * 4, resized.data(), nw, nh, nw * 4, STBIR_RGBA); |
| 60 | |
| 61 | levels.push_back(Image::FromRGBA(nw, nh, std::move(resized))); |
| 62 | w = nw; |
| 63 | h = nh; |
| 64 | } |
| 65 | |
| 66 | return levels; |
| 67 | } |
| 68 | |
| 69 | // Encode a single mipmap level's pixel data in the target format |
| 70 | static std::vector<uint8_t> encodeLevel(const Image& rgbaLevel, PixelFormat fmt) |