| 296 | } |
| 297 | |
| 298 | Vector<uint8_t> save_dds_buffer(const Ref<Image> &p_img) { |
| 299 | Ref<StreamPeerBuffer> stream_buffer; |
| 300 | stream_buffer.instantiate(); |
| 301 | |
| 302 | Ref<Image> image = p_img; |
| 303 | |
| 304 | stream_buffer->put_32(DDS_MAGIC); |
| 305 | stream_buffer->put_32(DDS_HEADER_SIZE); |
| 306 | |
| 307 | uint32_t flags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT | DDSD_PITCH | DDSD_LINEARSIZE; |
| 308 | |
| 309 | if (image->has_mipmaps()) { |
| 310 | flags |= DDSD_MIPMAPCOUNT; |
| 311 | } |
| 312 | |
| 313 | stream_buffer->put_32(flags); |
| 314 | |
| 315 | uint32_t height = image->get_height(); |
| 316 | stream_buffer->put_32(height); |
| 317 | |
| 318 | uint32_t width = image->get_width(); |
| 319 | stream_buffer->put_32(width); |
| 320 | |
| 321 | DDSFormat dds_format = _image_format_to_dds_format(image->get_format()); |
| 322 | const DDSFormatInfo &info = dds_format_info[dds_format]; |
| 323 | |
| 324 | uint32_t depth = 1; // Default depth for 2D textures |
| 325 | |
| 326 | uint32_t pitch; |
| 327 | if (info.compressed) { |
| 328 | pitch = ((MAX(info.divisor, width) + info.divisor - 1) / info.divisor) * ((MAX(info.divisor, height) + info.divisor - 1) / info.divisor) * info.block_size; |
| 329 | } else { |
| 330 | pitch = width * info.block_size; |
| 331 | } |
| 332 | |
| 333 | stream_buffer->put_32(pitch); |
| 334 | stream_buffer->put_32(depth); |
| 335 | |
| 336 | uint32_t mipmaps = image->get_mipmap_count() + 1; |
| 337 | stream_buffer->put_32(mipmaps); |
| 338 | |
| 339 | uint32_t reserved = 0; |
| 340 | for (int i = 0; i < 11; i++) { |
| 341 | stream_buffer->put_32(reserved); |
| 342 | } |
| 343 | |
| 344 | stream_buffer->put_32(DDS_PIXELFORMAT_SIZE); |
| 345 | |
| 346 | uint32_t pf_flags = 0; |
| 347 | |
| 348 | DDSFormatType format_type = _dds_format_get_type(dds_format); |
| 349 | |
| 350 | if (format_type == DDFT_BITMASK) { |
| 351 | pf_flags = DDPF_RGB; |
| 352 | |
| 353 | if (image->get_format() == Image::FORMAT_LA8 || image->get_format() == Image::FORMAT_RG8 || image->get_format() == Image::FORMAT_RGBA8 || image->get_format() == Image::FORMAT_RGBA4444) { |
| 354 | pf_flags |= DDPF_ALPHAPIXELS; |
| 355 | } |
no test coverage detected