Skip to content

Commit 3776411

Browse files
committed
Merge pull request godotengine#110271 from BlueCube3310/image-conv-opti-incompatible
Image: Optimize manual format conversion
2 parents 40eb348 + b63ec3d commit 3776411

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

core/io/image.cpp

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -568,14 +568,13 @@ static bool _are_formats_compatible(Image::Format p_format0, Image::Format p_for
568568

569569
void Image::convert(Format p_new_format) {
570570
ERR_FAIL_INDEX_MSG(p_new_format, FORMAT_MAX, vformat("The Image format specified (%d) is out of range. See Image's Format enum.", p_new_format));
571+
ERR_FAIL_COND_MSG(Image::is_format_compressed(format) || Image::is_format_compressed(p_new_format),
572+
"Cannot convert to (or from) compressed formats. Use compress() and decompress() instead.");
571573

572574
if (data.is_empty() || p_new_format == format) {
573575
return;
574576
}
575577

576-
ERR_FAIL_COND_MSG(Image::is_format_compressed(format) || Image::is_format_compressed(p_new_format),
577-
"Cannot convert to (or from) compressed formats. Use compress() and decompress() instead.");
578-
579578
// Includes the main image.
580579
const int mipmap_count = get_mipmap_count() + 1;
581580

@@ -584,24 +583,23 @@ void Image::convert(Format p_new_format) {
584583
Image new_img(width, height, mipmaps, p_new_format);
585584

586585
for (int mip = 0; mip < mipmap_count; mip++) {
587-
Ref<Image> src_mip = get_image_from_mipmap(mip);
588-
Ref<Image> new_mip = new_img.get_image_from_mipmap(mip);
586+
int64_t src_mip_ofs, dst_mip_ofs;
587+
int w, h;
588+
_get_mipmap_offset_and_size(mip, src_mip_ofs, w, h);
589+
new_img._get_mipmap_offset_and_size(mip, dst_mip_ofs, w, h);
589590

590-
for (int y = 0; y < src_mip->height; y++) {
591-
for (int x = 0; x < src_mip->width; x++) {
592-
new_mip->set_pixel(x, y, src_mip->get_pixel(x, y));
591+
uint8_t *dst_mip_ptr = new_img.ptrw() + dst_mip_ofs;
592+
const uint8_t *src_mip_ptr = ptr() + src_mip_ofs;
593+
594+
for (int y = 0; y < h; y++) {
595+
for (int x = 0; x < w; x++) {
596+
uint32_t mip_ofs = y * w + x;
597+
new_img._set_color_at_ofs(dst_mip_ptr, mip_ofs, _get_color_at_ofs(src_mip_ptr, mip_ofs));
593598
}
594599
}
595-
596-
int64_t mip_offset = 0;
597-
int64_t mip_size = 0;
598-
new_img.get_mipmap_offset_and_size(mip, mip_offset, mip_size);
599-
600-
memcpy(new_img.data.ptrw() + mip_offset, new_mip->data.ptr(), mip_size);
601600
}
602601

603602
_copy_internals_from(new_img);
604-
605603
return;
606604
}
607605

0 commit comments

Comments
 (0)