Repository: godotengine/godot
File: editor/scene/texture/texture_editor_plugin.cpp (C++)

Code before:
static Image::Format get_texture_2d_format(const Ref<Texture2D> &p_texture) {
	const Ref<Texture2DRD> rd_texture = p_texture;
	if (rd_texture.is_valid() && RD::get_singleton() && RD::get_singleton()->texture_is_valid(rd_texture->get_texture_rd_rid())) {
		return rd_texture->get_image()->get_format();
	}

	return p_texture->get_format();
}

static int get_texture_mipmaps_count(const Ref<Texture2D> &p_texture) {
	ERR_FAIL_COND_V(p_texture.is_null(), -1);

	// We are having to download the image only to get its mipmaps count. It would be nice if we didn't have to.
	Ref<Image> image;
	Ref<AtlasTexture> at = p_texture;
	Ref<Texture2DRD> rd_texture = p_texture;

	if (at.is_valid()) {
		// The AtlasTexture tries to obtain the region from the atlas as an image,
		// which will fail if it is a compressed format.
		Ref<Texture2D> atlas = at->get_atlas();
		if (atlas.is_valid()) {
			image = atlas->get_image();
		}
	} else if (rd_texture.is_valid()) {
		if (RD::get_singleton()->texture_is_valid(rd_texture->get_texture_rd_rid())) {
			return -1;
		}
		image = p_texture->get_image();
	} else {
		image = p_texture->get_image();
	}

	if (image.is_valid()) {
		return image->get_mipmap_count();
	}
	return -1;
}

void TexturePreview::_update_metadata_label_text() {
	const Ref<Texture2D> texture = texture_display->get_texture();
	ERR_FAIL_COND(texture.is_null());

	Image::Format format;
	int mipmaps;

	Ref<Image> image = texture->get_image();
	if (image.is_valid()) {
		format = image->get_format();
		mipmaps = image->get_mipmap_count();
	} else {

Review comment (suggestion):
Also needs safeguard
```suggestion
		if (RD::get_singleton() && RD::get_singleton()->texture_is_valid(rd_texture->get_texture_rd_rid())) {
```

Code after:

static Image::Format get_texture_2d_format(const Ref<Texture2D> &p_texture) {
	const Ref<Texture2DRD> rd_texture = p_texture;
	if (rd_texture.is_valid() && RD::get_singleton() && RD::get_singleton()->texture_is_valid(rd_texture->get_texture_rd_rid())) {
		return rd_texture->get_image()->get_format();
	}

	return p_texture->get_format();
}

static int get_texture_mipmaps_count(const Ref<Texture2D> &p_texture) {
	ERR_FAIL_COND_V(p_texture.is_null(), -1);

	// We are having to download the image only to get its mipmaps count. It would be nice if we didn't have to.
	Ref<Image> image;
	Ref<AtlasTexture> at = p_texture;
	Ref<Texture2DRD> rd_texture = p_texture;

	if (at.is_valid()) {
		// The AtlasTexture tries to obtain the region from the atlas as an image,
		// which will fail if it is a compressed format.
		Ref<Texture2D> atlas = at->get_atlas();
		if (atlas.is_valid()) {
			image = atlas->get_image();
		}
	} else if (rd_texture.is_valid()) {
		if (RD::get_singleton() && RD::get_singleton()->texture_is_valid(rd_texture->get_texture_rd_rid())) {
			return -1;
		}
		image = p_texture->get_image();
	} else {
		image = p_texture->get_image();
	}

	if (image.is_valid()) {
		return image->get_mipmap_count();
	}
	return -1;
}

void TexturePreview::_update_metadata_label_text() {
	const Ref<Texture2D> texture = texture_display->get_texture();
	ERR_FAIL_COND(texture.is_null());

	Image::Format format;
	int mipmaps;

	Ref<Image> image = texture->get_image();
	if (image.is_valid()) {
		format = image->get_format();
		mipmaps = image->get_mipmap_count();